]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
*** empty log message ***
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and
15 *  abiding by the rules of distribution of free software. You can  use,
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B
17 *  license as circulated by CEA, CNRS and INRIA at the following URL
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability.
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
30
31 /**
32 *  \file
33 *  \brief Class bbtk::wxVtkSceneManager .
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39  #include <vtkPolygon.h>
40  #include <vtkUnstructuredGrid.h>
41
42
43 namespace bbtk
44 {
45
46
47         //=========================================================================
48         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
49         {
50                 _parent                 = parent;
51                 _numBoxes               = 0;
52                 _idManager              = idManager;
53                 _baseView               = baseView;
54                 _startDragging  = false;
55                 _isComplexBox   = false;
56
57                 if( _baseView!=NULL )
58                 {
59 //EED02JUIN2010
60                         printf ("RaC-EED 21-06-2010 %p wxVtkSceneManager::wxVtkSceneManager If you comment this line, the drag and drop functionnality is not initialized.\n" , this );
61 #ifdef _APPLE_
62                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
63 #endif
64
65                         configureBaseView();
66                         _worldState=NOTHING_HAPPENS;
67                         registerController(this);
68
69                 }
70                 _idConnectionInCreation = -1;
71                 _contLastId                             = 0;
72
73         }
74
75         //=========================================================================
76
77         void wxVtkSceneManager::disconnectDrop()
78         {
79                 printf ("EED %p ~wxVtkSceneManager::disconnectDrop()\n" , this );
80 //EED02JUIN2010         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
81 //      _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget( new tmpClasswxTextDropTarget() );
82         }
83
84         //=========================================================================
85
86         wxVtkSceneManager::~wxVtkSceneManager()
87         {
88                 printf ("EED %p ~wxVtkSceneManager()\n" , this );
89                 disconnectDrop();
90         }
91
92         //=========================================================================
93
94         void wxVtkSceneManager::configureBaseView()
95         {
96                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
97
98                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
99
100                 // Important to activate the 2D interaction system
101                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
102                 interactorstylebaseview->SetInteractor ( iren );
103                 iren->SetInteractorStyle(interactorstylebaseview);
104                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
105
106                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
107                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,800,1100);
108
109                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
110                 _baseView->GetRenderer()->GradientBackgroundOn();
111                 _baseView->Refresh();
112
113                 // Actos Port_Text
114                 _textActor = vtkTextActor3D::New();
115                 _textActor->SetPosition(  -9999 , -9999 , 900 );
116                 _textActor->SetInput( "<void>" );
117                 _textActor->GetTextProperty()->SetFontSize(60);
118                 _textActor->GetTextProperty()->BoldOn();
119                 _textActor->GetTextProperty()->SetColor(PORTTEXT_NH_R,PORTTEXT_NH_G,PORTTEXT_NH_B);
120
121                 _baseView->GetRenderer()->AddActor( _textActor );
122
123
124                 // Actor  Fill_Port_Text
125
126                 //------------
127                 double                          xInic                           = 0;
128                 double                          yInic                           = 0;
129                 double                          zInic                           = 900;
130
131                 vtkPolygon *aPolygon = vtkPolygon::New();
132                 _fillObjectActor                                                = vtkActor::New();
133
134                                                         _pts                            = vtkPoints::New();
135 //ups4 EED Borrame
136
137                 double w=100,h=10,b=h/15,t=3;
138                 _pts->SetNumberOfPoints(21);
139                 _pts->InsertPoint(0, xInic+w*0.33-t/2   , yInic                 , zInic );
140                 _pts->InsertPoint(1, xInic+w*0.33               , yInic-t               , zInic );
141                 _pts->InsertPoint(2, xInic+w*0.33+t/2   , yInic                 , zInic );
142                 _pts->InsertPoint(3, xInic+w*0.33+t/2   , yInic                 , zInic );
143                 _pts->InsertPoint(4, xInic+w-b*4                , yInic+b*0             , zInic );
144
145                 _pts->InsertPoint( 5, xInic+w-b*4       , yInic+b*0             , zInic );
146                 _pts->InsertPoint( 6, xInic+w-b*2       , yInic+b*1             , zInic );
147                 _pts->InsertPoint( 7, xInic+w-b*1       , yInic+b*2             , zInic );
148                 _pts->InsertPoint( 8, xInic+w-b*0       , yInic+b*4             , zInic );
149
150                 _pts->InsertPoint( 9, xInic+w-b*0       , yInic+h-b*4   , zInic );
151                 _pts->InsertPoint(10, xInic+w-b*1       , yInic+h-b*2   , zInic );
152                 _pts->InsertPoint(11, xInic+w-b*2       , yInic+h-b*1   , zInic );
153                 _pts->InsertPoint(12, xInic+w-b*4       , yInic+h-b*0   , zInic );
154
155                 _pts->InsertPoint(13, xInic+b*4         , yInic+h-b*0   , zInic );
156                 _pts->InsertPoint(14, xInic+b*2         , yInic+h-b*1   , zInic );
157                 _pts->InsertPoint(15, xInic+b*1         , yInic+h-b*2   , zInic );
158                 _pts->InsertPoint(16, xInic+b*0         , yInic+h-b*4   , zInic );
159
160                 _pts->InsertPoint(17, xInic+b*0         , yInic+b*4             , zInic );
161                 _pts->InsertPoint(18, xInic+b*1         , yInic+b*2             , zInic );
162                 _pts->InsertPoint(19, xInic+b*2         , yInic+b*1             , zInic );
163                 _pts->InsertPoint(20, xInic+b*4         , yInic+b*0             , zInic );
164
165                 aPolygon->GetPointIds()->SetNumberOfIds(21);
166                 for (int i=0;i<21; i++)
167                 {
168                         aPolygon->GetPointIds()->SetId(i, i);
169                 }
170
171                 vtkUnstructuredGrid *aPolygonGrid = vtkUnstructuredGrid::New();
172                 aPolygonGrid->Allocate(1, 1);
173                 aPolygonGrid->InsertNextCell(aPolygon->GetCellType(), aPolygon->GetPointIds());
174                 aPolygonGrid->SetPoints(_pts);
175                 _aPolygonMapper = vtkDataSetMapper::New();
176                 _aPolygonMapper->SetInput(aPolygonGrid);
177                 _fillObjectActor->SetMapper(_aPolygonMapper);
178                 _fillObjectActor->GetProperty()->SetColor(PORTFILL_NH_R,PORTFILL_NH_G,PORTFILL_NH_B);
179                 _fillObjectActor->GetProperty()->SetOpacity(0);
180                 _aPolygonMapper->Modified();
181
182                 _baseView->GetRenderer()->AddActor( _fillObjectActor );
183         }
184
185         //=========================================================================
186
187         std::string wxVtkSceneManager::generateANewNameForABox()
188         {
189                 std::stringstream boxname;
190                 if(_numBoxes<10)
191                 {
192                         boxname << "Box0" << _numBoxes;
193                 }else{
194                         boxname << "Box" << _numBoxes;
195                 }
196                 _numBoxes++;
197                 return boxname.str();
198         }
199
200         //=========================================================================
201
202         std::string wxVtkSceneManager::findANewNameForABox()
203         {
204                 std::string boxname=generateANewNameForABox();
205
206                 while (boxExist(boxname)==true)
207                 {
208                         boxname=generateANewNameForABox();
209                 } // while
210
211                 return boxname;
212         }
213
214         //=========================================================================
215         void wxVtkSceneManager::configGBlackBox(int idBox, double xIn, double yIn, double zIn, std::string name, bool boxExecutable,double xEn,double yEn,double zEn)
216         {
217                 GObjectController *cont = _controllers[idBox];
218                 GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
219                 bbmod->setBBTKName(name);
220                 bbmod->setInicPoint(xIn,yIn,zIn);
221                 bbmod->setFinalPoint(xEn,yEn,zEn);
222                 bbmod->setExecutable(boxExecutable);
223
224                 bbmod->notifyObservers(_idManager);
225
226         }
227
228
229         //=========================================================================
230
231         int wxVtkSceneManager::createGBlackBox(int x, int y,  std::string packageName, std::string boxType)
232         {
233                 _worldState                                                             = NOTHING_HAPPENS;
234 //EED           int windowWidth                                                 = _baseView->GetRenWin()->GetSize()[0];
235                 int windowHeight                                                = _baseView->GetRenWin()->GetSize()[1];
236
237                 int type                                                                = GBLACKBOX;
238
239                 //Create the MVC Objects
240
241                 GBlackBoxModel *model                                   = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
242                 vtkGObjectView *view                                    = GObjectsMVCFactory::getInstance()->createGObjectView(type);
243                 GObjectController* controller                   = GObjectsMVCFactory::getInstance()->createGObjectController(type);
244
245                 BlackBoxDescriptor::Pointer descriptor  = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
246
247                 //Prepares the initial model
248                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height
249
250                 double xx = x;
251                 double yy = windowHeight-y;
252
253                 //z value is not important yet, because it is only used a parallel projection
254                 double zz = 900;
255                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
256                 model->setInicPoint(xx,yy,zz);
257
258                 std::stringstream stream;
259
260                 std::string newBoxName;
261                 newBoxName = findANewNameForABox();
262
263
264                 stream << newBoxName;
265
266                 std::string arraystring = stream.str();
267
268                 model->setBBTKName(arraystring);
269                 model->setBBTKType(boxType);
270                 model->setBBTKPackage(packageName);
271
272                 model->addObserver(view);
273                 model->addObserver(this);
274
275                 //Iterate and create the input ports
276                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
277                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
278
279                 int i=0;
280                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
281                 {
282                         BlackBoxInputDescriptor *desc = itInput->second;
283                         createGInputPort(GINPUTPORT,i,model,desc);
284                         i++;
285                 }
286
287                 //Iterate and create the output ports
288                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
289                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
290
291                 i=0;
292                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
293                 {
294                         BlackBoxOutputDescriptor *desc = itOutput->second;
295                         createGOutputPort(GOUTPUTPORT,i,model,desc);
296                         i++;
297                 }
298
299
300                 //Associates the view with the correspondent renderer and the  model.
301                 //(NOTE: Refresh is only made by the view)
302                 view->setModel(model);
303                 view->setBaseView(_baseView);
304                 view->initVtkObjects();
305
306                 //Associates the controller with the correspondent model and view
307                 controller->setModelAndView(model,view);
308
309                 //Resgiter change to the observers of the actual model
310                 model->notifyObservers(_idManager);
311
312                 int newId = addObjectController(controller);
313
314                 return newId;
315
316         }
317
318         //=========================================================================
319
320         int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName)
321         {
322 //EED           int windowWidth=_baseView->GetRenWin()->GetSize()[0];
323                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
324
325                 int type                                                = GCOMPLEXINPUTPORT;
326
327                 //Create the MVC Objects
328
329                 GComplexBoxPortModel *model             = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
330                 vtkGObjectView *view                    = GObjectsMVCFactory::getInstance()->createGObjectView(type);
331                 GObjectController* controller   = GObjectsMVCFactory::getInstance()->createGObjectController(type);
332
333                 //Prepares the initial model
334
335                 double xx                                               = 5;
336                 double yy                                               = windowHeight-5;
337
338                 //z value is not important yet, because it is only used a parallel projection
339                 double zz                                               = 900;
340 printf("EED  wxVtkSceneManager::createGComplexBoxInputPort 900-450\n");
341
342                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
343                 model->setInicPoint(xx,yy,zz);
344
345                 model->setBBTKName(inputName);
346                 model->setBBTKType("ComplexInputPort");
347                 model->setComplexPortType(type);
348
349                 model->addObserver(view);
350                 model->addObserver(this);
351
352                 //create the output port
353                 GPortController* portController = createGPort(GOUTPUTPORT,inputName,"ComplexInputPort",0,model);
354                 model->addOutputPort((GPortModel*)portController->getModel());
355
356                 //Associates the view with the correspondent renderer and the  model.
357                 //(NOTE: Refresh is only made by the view)
358                 view->setModel(model);
359                 view->setBaseView(_baseView);
360                 view->initVtkObjects();
361
362                 //Associates the controller with the correspondent model and view
363                 controller->setModelAndView(model,view);
364
365                 //Resgiter change to the observers of the actual model
366                 model->notifyObservers(_idManager);
367
368                 int newId = addObjectController(controller);
369                 return newId;
370         }
371
372         //=========================================================================
373
374         int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
375         {
376 //EED           int windowWidth=_baseView->GetRenWin()->GetSize()[0];
377                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
378
379                 int type                                                = GCOMPLEXOUTPUTPORT;
380
381                 //Create the MVC Objects
382
383                 GComplexBoxPortModel    *model          = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
384                 vtkGObjectView                  *view           = GObjectsMVCFactory::getInstance()->createGObjectView(type);
385                 GObjectController               *controller     = GObjectsMVCFactory::getInstance()->createGObjectController(type);
386
387                 //Prepares the initial model
388
389                 double xx                                               = 5;
390                 double yy                                               = windowHeight-5;
391
392                 //z value is not important yet, because it is only used a parallel projection
393                 double zz                                               = 900;
394
395 printf("EED  wxVtkSceneManager::createGComplexBoxOutputPort 900-450\n");
396
397                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
398                 model->setInicPoint(xx,yy,zz);
399
400                 model->setBBTKName(outputName);
401                 model->setBBTKType("ComplexOutputPort");
402                 model->setComplexPortType(type);
403
404                 model->addObserver(view);
405                 model->addObserver(this);
406
407                 //create the output port
408                 GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
409                 model->addInputPort((GPortModel*)portController->getModel());
410
411                 //Associates the view with the correspondent renderer and the  model.
412                 //(NOTE: Refresh is only made by the view)
413                 view->setModel(model);
414                 view->setBaseView(_baseView);
415                 view->initVtkObjects();
416
417                 //Associates the controller with the correspondent model and view
418                 controller->setModelAndView(model,view);
419
420                 //Resgiter change to the observers of the actual model
421                 model->notifyObservers(_idManager);
422
423                 int newId = addObjectController(controller);
424                 return newId;
425         }
426
427         //=========================================================================
428
429         int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
430         {
431                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
432                 blackBox->addInputPort((GPortModel*)portController->getModel());
433                 return portController->getId();
434         }
435
436         //=========================================================================
437
438         int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
439         {
440                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
441                 blackBox->addOutputPort((GPortModel*)portController->getModel());
442                 return portController->getId();
443         }
444
445         //=========================================================================
446
447         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
448         {
449                 int type = GPORT;
450
451                 //Create the MVC Objects
452                 GPortModel *model                               = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
453                 vtkGObjectView *view                    = GObjectsMVCFactory::getInstance()->createGObjectView(type);
454                 GObjectController* controller   = GObjectsMVCFactory::getInstance()->createGObjectController(type);
455
456                 model->registerInBox(blackBox,portType, posInBox);
457
458                 model->setBBTKType(bbtkType);
459                 model->setBBTKName(bbtkName);
460
461                 model->addObserver(view);
462                 model->addObserver(this);
463
464                 //Associates the view with the correspondent renderer and the  model.
465                 //(NOTE: Refresh is only made by the view)
466                 view->setModel(model);
467                 view->setBaseView(_baseView);
468                 view->initVtkObjects();
469
470                 //Associates the controller with the correspondent model and view
471                 controller->setModelAndView(model,view);
472
473                 model->notifyObservers(_idManager);
474
475                 addObjectController(controller);
476
477                 return (GPortController*)controller;
478         }
479
480         //=========================================================================
481
482         int wxVtkSceneManager::createGConnector(GPortModel* startPort)
483         {
484                 int type = GCONNECTOR;
485
486                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
487                 manualConnectorContourView* manContourView                      = new manualConnectorContourView();
488                 manualContourModel* manContourModel                                     = new manualContourModel();
489
490                 GConnectorController* connectorcontroller                       = new GConnectorController();
491                 GConnectorModel* connectorModel                                         = new GConnectorModel();
492                 vtkGConnectorView* connectorView                                        = new vtkGConnectorView();
493                 connectorModel->setGObjectType(type);
494
495                 manContourModel->SetCloseContour(false);
496                 connectorModel->setStartPort(startPort);
497
498                 manContourView->SetModel( manContourModel );
499                 manContourView->SetWxVtkBaseView( _baseView );
500                 manContourView->SetRange( 0.5 );
501                 manContourView->SetZ( 900 );
502
503                 manContourView->SetColorNormalContour(0, 0, 1);
504                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
505                 manContourView->SetColorSelectContour(1, 0.8, 0);
506                 manContourView->SetWidthLine(1);
507                 manContourView->SetShowText(false);
508
509                 manContourControl->SetModelView( manContourModel , manContourView );
510
511                 manContourControl->CreateNewManualContour();
512
513                 manContourView->RefreshContour();
514
515
516                 double x,y,z;
517                 connectorModel->getInicPoint(x,y,z);
518
519                 manContourControl->SetState(1);
520                 manContourModel->SetCloseContour(false);
521
522                 manContourModel->AddPoint(x,y,z);
523                 manContourView->AddPoint();
524
525                 manContourModel->AddPoint(x,y,z);
526                 manContourView->AddPoint();
527
528                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
529                 manContourControl->_bakIdPoint=bak;
530                 manContourView->Refresh();
531
532                 manContourControl->SetMoving( false );
533
534                 connectorcontroller->setModelAndView(connectorModel,connectorView);
535
536                 int newId = addObjectController(connectorcontroller);
537
538                 connectorcontroller->setManualContourController(manContourControl);
539                 connectorModel->setManualContourModel(manContourModel);
540                 connectorView->setManualContourView(manContourView);
541                 connectorView->setModel(connectorModel);
542                 connectorView->setBaseView(_baseView);
543
544
545                 connectorModel->addObserver(connectorView);
546                 connectorModel->addObserver(this);
547
548                 return newId;
549         }
550
551         //=========================================================================
552
553         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
554         {
555                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
556                 baseViewControlManager->AddInteractorStyleMaracas( param );
557         }
558
559         //=========================================================================
560
561         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
562         {
563                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
564                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
565         }
566
567         //=========================================================================
568
569         vtkRenderer* wxVtkSceneManager::getRenderer()
570         {
571                 return _baseView->GetRenderer();
572         }
573
574         //=========================================================================
575
576         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
577         {
578                 return _baseView->GetRenWin();
579         }
580         //=========================================================================
581
582
583         //=========================================================================
584     int wxVtkSceneManager::GetIndexInSelected(int idControler)
585     {
586         int index=-1;
587         for (int i=0; i<(int)_selectedObjects.size(); i++)
588         {
589             if(_selectedObjects[i]==idControler)
590             {
591                 index=i;
592                 break;
593             }
594         }
595         return index;
596     }
597         //=========================================================================
598
599         void wxVtkSceneManager::update(int idController,int command)
600         {
601 printf("EED wxVtkSceneManager::update  1 \n" );
602
603                 if(command != NO_COMMAND)
604                 {
605                         if(command == ADD_TO_SELECTED)
606                         {
607 //EEDBorrame                            GObjectController* cont = _controllers[idController];
608
609                 if (GetIndexInSelected(idController)==-1)
610                                 {
611                                         _selectedObjects.push_back(idController);
612                                 }
613
614 //EEDBorrame
615 //                              bool foundID=false;
616 //                              for (int i=0; i<(int)_selectedObjects.size() && foundID==false; i++)
617 //                              {
618 //                                      if(_selectedObjects[i]==idController)
619 //                                      {
620 //                                              foundID = true;
621 //                                      }
622 //                              }
623 //                              if(!foundID)
624 //                              {
625 //                                      _selectedObjects.push_back(idController);
626 //                              }
627
628                         }else if(command == REMOVE_FROM_SELECTED)
629             {
630                 int index=GetIndexInSelected(idController);
631                 if (index>=0)
632                 {
633                     _selectedObjects.erase( _selectedObjects.begin()+index );
634                 }
635             }
636                         else if(command == INIT_CREATION_CONTOUR)
637                         {
638                                 _worldState                 = INIT_CREATION_CONTOUR;
639                                 GObjectController* cont     = _controllers[idController];
640                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
641
642
643                                 // The last one is the controller of the connector
644                                 std::map<int, GObjectController*>::iterator it2;
645
646                                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
647                                 {
648                                         GObjectController *cont = it2->second;
649                                         if(cont->getGObjectType() == GPORT )
650                                         {
651                                                 GPortModel* port = (GPortModel*)cont->getModel();
652                                                 if(port->getPortType()==GINPUTPORT)
653                                                 {
654                                                         cont->SetActive(true);
655                                                 }
656                                                 else
657                                                 {
658                                                         cont->getView()->setState(NOTHING_HAPPENS);
659                                                         cont->getModel()->notifyObservers(_idManager);
660                                                         cont->SetActive(false);
661                                                 }
662                                         }
663                                         else
664                                         {
665                                                 cont->getView()->setState(NOTHING_HAPPENS);
666                                                 cont->getModel()->notifyObservers(_idManager);
667                                                 cont->SetActive(false);
668                                         }
669                                 }
670
671                                 _selectedObjects.clear();
672
673                                 _idConnectionInCreation=createGConnector(startOutputPort);
674
675                         }
676                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
677                         {
678                                 _worldState = NOTHING_HAPPENS;
679                                 //int id = _controllers.size()-1;
680                                 GObjectController* cont = _controllers[_idConnectionInCreation];
681                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
682
683                                 GObjectController* finPort = _controllers[idController];
684                                 if(finPort->getGObjectType() == GPORT)
685                                 {
686                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
687                                         modelContour->setEndPort(modelPort);
688                                 }
689
690                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
691                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
692                                 connView->Refresh();
693
694                                 std::map<int, GObjectController*>::iterator it2;
695
696                                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
697                                 {
698                                         GObjectController *cont = it2->second;
699                                         if(cont->getView()!=NULL)
700                                         {
701                                                 cont->getView()->setState(NOTHING_HAPPENS);
702                                                 cont->getModel()->notifyObservers(_idManager);
703                                         }
704                                         cont->SetActive(true);
705                                 }
706                         }
707
708                 }
709
710 printf("EED wxVtkSceneManager::update  2 \n" );
711
712         }
713
714         //=========================================================================
715
716         bool wxVtkSceneManager::OnMouseMove()
717         {
718                 int X,Y;
719                 bool okStatusMessage    = false;
720                 bool okPortMessage              = false;
721                 wxVTKRenderWindowInteractor *wxVTKiren;
722                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
723                 wxVTKiren->GetEventPosition(X,Y);
724
725
726                 if(_worldState == DRAG_OBJECTS)
727                 {
728                         for (int i=0; i<(int)_selectedObjects.size(); i++)
729                         {
730                                 int id = _selectedObjects[i];
731                                 GObjectController* cont = _controllers[id];
732                                 if(_startDragging)
733                                 {
734                                         cont->getView()->setStartDragging(true);
735                                 }
736                                 cont->getView()->moveObject(X,Y);
737                                 cont->getView()->setState(DRAG);
738                                 cont->getModel()->notifyObservers(_idManager);
739                         }
740
741                         std::map<int, GObjectController*>::iterator it;
742
743                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
744                         {
745                                 GObjectController *desc = it->second;
746                                 if(desc->getGObjectType()==GCONNECTOR)
747                                 {
748                                         vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
749                                         vconn->updateStartEndPoints();
750                                 }
751                         }
752
753                         _startDragging=false;
754
755                 }
756                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
757                 {
758                         std::map<int, GObjectController*>::iterator it;
759                         double px, py, pz;
760                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
761                         {
762                                 GObjectController *desc = it->second;
763                                 int type                                = desc->getGObjectType();
764                                 int state                               = desc->getView()->getState();
765                                 desc->getModel()->getCenter(px,py,pz);
766
767                                 if(state == HIGHLIGHTED){
768                                         okStatusMessage = true;
769                                         updateStatusBar(desc->getStatusText());
770                                         if(type==GBLACKBOX)
771                                         {
772                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
773                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
774                                         } // if GBLACKBOX
775                                         if(type==GPORT)
776                                         {
777                                                 // PortText
778                                                 okPortMessage   =       true;
779                                                 py=py+5;
780                                                 _textActor->SetInput( desc->getStatusText().c_str() );
781                                                 _textActor->SetScale(0.1);
782                                                 _textActor->SetPosition(  px-25 ,py+1 , pz+2 );
783
784 //ups4 EED Borrame
785                                                 // FillPortText
786                                                 px=px-33;
787                                                 py=py;
788                                                 _fillObjectActor->SetScale(1);
789                                                 _fillObjectActor->GetProperty()->SetOpacity(0.50);
790                                                 _fillObjectActor->SetPosition(px,py,1);
791                                                 _aPolygonMapper->Modified();
792                                         } // if GPORT
793
794                                 } // for controllers it
795                         }
796                 }
797                 if (okStatusMessage==false)
798                 {
799                         updateStatusBar("");
800                 }
801
802                 if (okPortMessage==false)
803                 {
804                         _textActor->SetScale(0.0001);
805                         _fillObjectActor->SetScale(0.0001);
806                 }
807
808
809                 return true;
810         }
811
812         //=========================================================================
813
814         bool wxVtkSceneManager::OnLeftButtonDown()
815         {
816                 if(_worldState==INIT_CREATION_CONTOUR)
817                 {
818                         bool isOverPort=false;
819                         std::map<int, GObjectController*>::iterator it;
820                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
821                         {
822                                 GObjectController *desc = it->second;
823                                 if(desc->getGObjectType()==GPORT)
824                                 {
825                                         GPortModel* portmod=(GPortModel*)desc->getModel();
826                                         vtkGObjectView* portView=desc->getView();
827                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
828                                         {
829                                                 isOverPort=true;
830                                         } // if
831                                 } // if
832                         } // for
833
834                         if(isOverPort==false)
835                         {
836                                 _worldState=NOTHING_HAPPENS;
837                                 //int lastId = _controllers.size()-1;
838 //EED2Oct2010                           GConnectorController *connector = (GConnectorController*)_controllers[_controllers.size()-1];
839                                 GConnectorController *connector = (GConnectorController*)_controllers[_idConnectionInCreation];
840
841                                 connector->removeFromScene();
842                                 unregisterController(connector);
843                                 _controllers.erase(_controllers.size()-1);
844
845                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
846                                 {
847                                         GObjectController *desc = it->second;
848                                         desc->SetActive(true);
849                                         desc->getView()->setState(NOTHING_HAPPENS);
850                                         desc->getModel()->notifyObservers(_idManager);
851                                 } // for
852                         } // isOverPort
853                 } // _worldState
854
855         _worldState             = DRAG_OBJECTS;
856         _startDragging  = true;
857
858
859 printf("EED wxVtkSceneManager::OnLeftButtonDown  1 \n" );
860
861
862         int ctrlkey             = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
863         GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
864
865         if (GetGBlackBoxControlerPointedByMouse()!=NULL)
866         {
867             int state=cont->getView()->getState() ;
868                         if(  (ctrlkey==0) && (state==HIGHLIGHTED) )
869             {
870                 UnSelectBlackBoxes();
871             }
872         }
873
874
875         for (int i = 0; i < (int)_selectedObjects.size(); i++)
876         {
877             int id = _selectedObjects[i];
878             GObjectController* cont = _controllers[id];
879             cont->getView()->setState(DRAG);
880             cont->getModel()->notifyObservers(_idManager);
881         } // for
882
883 printf("EED wxVtkSceneManager::OnLeftButtonDown  2 \n" );
884
885                 return true;
886         }
887
888         //=========================================================================
889
890         bool wxVtkSceneManager::OnLeftButtonUp()
891         {
892                 if(_worldState == DRAG_OBJECTS)
893                 {
894                         _worldState = NOTHING_HAPPENS;
895
896                         for (int i = 0; i < (int)_selectedObjects.size(); i++)
897                         {
898                                 int id = _selectedObjects[i];
899                                 GObjectController* cont = _controllers[id];
900                                 cont->getView()->setState(SELECTED);
901                                 cont->getModel()->notifyObservers(_idManager);
902                         }
903                 }
904                 return true;
905         }
906
907         //=========================================================================
908
909         bool wxVtkSceneManager::OnRightButtonUp()
910         {
911                 if(_worldState==INIT_CREATION_CONTOUR)
912                 {
913                         _worldState=NOTHING_HAPPENS;
914                         //int lastId = _controllers.size()-1;
915
916                         GConnectorController *connector = (GConnectorController*)_controllers[_idConnectionInCreation];
917                         connector->removeFromScene();
918                         unregisterController(connector);
919                         _controllers.erase(_idConnectionInCreation);
920
921                         std::map<int, GObjectController*>::iterator it;
922                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
923                         {
924                                 GObjectController *desc = it->second;
925                                 desc->SetActive(true);
926                                 desc->getView()->setState(NOTHING_HAPPENS);
927                                 desc->getModel()->notifyObservers(_idManager);
928                         }
929                 }
930
931
932                 for (int i = 0; i < (int)_selectedObjects.size(); i++)
933                 {
934                         int id = _selectedObjects[i];
935                         GObjectController* cont = _controllers[id];
936                         cont->SetActive(true);
937                         cont->getView()->setState(NOTHING_HAPPENS);
938                         cont->getModel()->notifyObservers(_idManager);
939                 }
940
941                 _selectedObjects.clear();
942
943                 return true;
944         }
945
946         //=========================================================================
947
948     GObjectController *wxVtkSceneManager::GetGBlackBoxControlerPointedByMouse()
949     {
950         GObjectController *result=NULL;
951                 int X,Y;
952                 wxVTKRenderWindowInteractor *wxVTKiren;
953                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
954                 wxVTKiren->GetEventPosition(X,Y);
955
956                 std::map<int, GObjectController*>::iterator it;
957
958                 for(it = _controllers.begin(); it != _controllers.end() ; ++it)
959                 {
960                         GObjectController *cont = it->second;
961                         int type = cont->getGObjectType();
962
963                         if( cont->getView()->isPointInside(X,Y)==true )
964                         {
965                                 if(type==GBLACKBOX)
966                                 {
967                                     result=cont;
968                                 } // if type
969                         } // if isPointInside
970                 } // for it
971                 return result;
972     }
973
974         //=========================================================================
975         bool wxVtkSceneManager::UnSelectBlackBoxes()
976         {
977         for (int i=0; i< (int)_selectedObjects.size(); i++)
978                 {
979             int id = _selectedObjects[i];
980                         GObjectController* control = _controllers[id];
981                         control->getView()->setState(NOTHING_HAPPENS);
982         }// for _selectedObjects
983                 _selectedObjects.clear();
984     }
985
986         //=========================================================================
987         bool wxVtkSceneManager::OnLeftDClick()
988         {
989             GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
990             if (cont!=NULL)
991         {
992             UnSelectBlackBoxes();
993             _parent->editBlackBox( (GBlackBoxModel*)cont->getModel() );
994         }
995
996 /*EED Borrame
997
998                 int X,Y;
999                 wxVTKRenderWindowInteractor *wxVTKiren;
1000                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
1001                 wxVTKiren->GetEventPosition(X,Y);
1002
1003                 std::map<int, GObjectController*>::iterator it;
1004
1005                 bool clickOnObject = false;
1006
1007                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
1008                 {
1009                         GObjectController *cont = it->second;
1010                         int type = cont->getGObjectType();
1011
1012                         if(cont->getView()->isPointInside(X,Y))
1013                         {
1014                                 if(type==GBLACKBOX)
1015                                 {
1016                                         for (int i=0; i< (int)_selectedObjects.size(); i++)
1017                                         {
1018                                                 int id = _selectedObjects[i];
1019                                                 GObjectController* control = _controllers[id];
1020                                                 control->getView()->setState(NOTHING_HAPPENS);
1021                                         }
1022                                         _selectedObjects.clear();
1023
1024                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
1025                                         _parent->editBlackBox(bbmodel);
1026                                 }
1027                                 clickOnObject = true;
1028                         }
1029                 }
1030
1031                 if(clickOnObject==false)
1032                 {
1033                         //_parent->editDiagramParameters(this);
1034                 }
1035 */
1036                 return true;
1037         }
1038
1039         //=========================================================================
1040
1041         bool wxVtkSceneManager::OnChar()
1042         {
1043                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
1044
1045                 // KeyCode 127 : Delete Key
1046                 // KeyCode 8 : Backspace Key
1047                 if(keyCode == 8 || keyCode == 127)
1048                 {
1049                         if(_selectedObjects.size()>0)
1050                         {
1051                                 for(int i=0;i<(int)_selectedObjects.size();i++)
1052                                 {
1053                                         int id = _selectedObjects[i];
1054                                         std::map<int,GObjectController*>::iterator it;
1055                     it = _controllers.find(id);
1056                                         if (it!=_controllers.end())
1057                                         {
1058                                                 deleteObject(id);
1059                                         }
1060                                 }
1061                                 _selectedObjects.clear();
1062                         }
1063                 }
1064
1065                 return true;
1066         }
1067
1068         //=========================================================================
1069
1070         void wxVtkSceneManager::AddControlerToBeRemove(std::vector<int> *controllersToRemove, int id)
1071         {
1072
1073                 int i;
1074                 bool ok=true;
1075                 for(i=0 ; i < (int)controllersToRemove->size() ; i++ )
1076                 {
1077                         if (id == (*controllersToRemove)[i] )
1078                         {
1079                                 ok=false;
1080                         }
1081                 }
1082
1083                 if (ok==true)
1084                 {
1085                    controllersToRemove->push_back( id );
1086                 }
1087
1088         }
1089
1090         //=========================================================================
1091
1092         void wxVtkSceneManager::deleteObject(int id)
1093         {
1094                 GObjectController *control = _controllers[id];
1095                 std::vector<int> controllersToRemove;
1096
1097                 if(control->getGObjectType()==GBLACKBOX || control->getGObjectType()==GCOMPLEXINPUTPORT || control->getGObjectType()==GCOMPLEXOUTPUTPORT)
1098                 {
1099                         GBoxModel *bbmod = (GBoxModel*)control->getModel();
1100                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
1101
1102                         bool boxConnected = false;
1103
1104                         // Add box input controllers to be removed
1105                         for(int i = 0;i< (int)inputs.size();i++)
1106                         {
1107                                 AddControlerToBeRemove( &controllersToRemove, inputs[i]->getObjectId() );
1108                                 if(inputs[i]->isConnected())
1109                                 {
1110                                         boxConnected = true;
1111                                 }
1112                         }
1113
1114                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
1115
1116                         // Add box output controllers to be removed
1117                         for(int i = 0;i<(int)outputs.size();i++)
1118                         {
1119                                 AddControlerToBeRemove( &controllersToRemove, outputs[i]->getObjectId() );
1120                                 if(outputs[i]->isConnected())
1121                                 {
1122                                         boxConnected = true;
1123                                 }
1124                         }
1125
1126                         // Add connection controllers to be removed
1127                         std::map<int, GObjectController*>::iterator it;
1128                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
1129                         {
1130                                 GObjectController *cont = it->second;
1131                                 int type = cont->getGObjectType();
1132                                 if(type==GCONNECTOR)
1133                                 {
1134                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
1135                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
1136                                         {
1137                                                 AddControlerToBeRemove( &controllersToRemove, conMod->getObjectId() );
1138                                         }
1139                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
1140                                         {
1141                                                 AddControlerToBeRemove( &controllersToRemove, conMod->getObjectId() );
1142                                         }
1143                                 }
1144                         }
1145
1146                         // Add box controller to be removed
1147                         AddControlerToBeRemove( &controllersToRemove, bbmod->getObjectId() );
1148                 }
1149                 else if(control->getGObjectType()==GCONNECTOR)
1150                 {
1151                         GConnectorModel *conMod = (GConnectorModel*)control->getModel();
1152                         AddControlerToBeRemove( &controllersToRemove, conMod->getObjectId() );
1153                 }
1154
1155
1156                 std::map<int,GObjectController*>::iterator it;
1157
1158                 for(int i = 0;i<(int)controllersToRemove.size();i++)
1159                 {
1160                         int key = controllersToRemove[i];
1161                         it = _controllers.find( key );
1162                         GObjectController *cont = _controllers[ key ];
1163                         if(cont!=NULL)
1164                         {
1165                                 cont->removeFromScene();
1166                                 unregisterController((InteractorStyleMaracas*)cont);
1167                                 _controllers.erase(it);
1168                         }//if
1169                 }// for
1170         }
1171
1172         //=========================================================================
1173
1174         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
1175         {
1176                 _parent->displayBlackBoxInfo(packageName,boxName);
1177         }
1178
1179         //=========================================================================
1180
1181         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
1182         {
1183                 _parent->updateStatusBar(textStatus);
1184         }
1185
1186
1187         //=========================================================================
1188     std::string wxVtkSceneManager::LineNumber(bool withLineNumber, int &value)
1189     {
1190         std::string result="";
1191         if (withLineNumber==true)
1192         {
1193             value++;
1194             stringstream StrStream;
1195             StrStream << value;
1196             std::string strValue=StrStream.str();
1197             if (value<10)
1198             {
1199                 result+="  ";
1200             }else if (value<100)
1201             {
1202                 result+=" ";
1203             }
1204
1205             result+=strValue+": ";
1206         }
1207         return result;
1208         }
1209         //=========================================================================
1210
1211         std::string wxVtkSceneManager::getDiagramBBS(bool wln)
1212         {
1213                 bool existsExec=false;
1214
1215                 std::vector<std::string> packages;
1216                 std::vector<int> boxes;
1217                 std::vector<int> connections;
1218                 std::vector<int> execBoxes;
1219
1220                 std::map<int, GObjectController*>::iterator it;
1221
1222                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1223                 {
1224                         GObjectController *desc = it->second;
1225                         int type = desc->getGObjectType();
1226
1227                         if(type==GBLACKBOX)
1228                         {
1229                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
1230
1231                                 std::string pkg = mod->getBBTKPackage();
1232                                 bool existsPkg = false;
1233                                 for(int t = 0;t< (int)packages.size() && existsPkg == false;t++)
1234                                 {
1235                                         if(packages[t]==pkg)
1236                                         {
1237                                                 existsPkg=true;
1238                                         }
1239                                 }
1240                                 if(!existsPkg)
1241                                 {
1242                                         packages.push_back(pkg);
1243                                 }
1244
1245
1246                                 boxes.push_back(it->first);
1247                                 if(mod->isExecutable())
1248                                 {
1249                                         execBoxes.push_back(it->first);
1250                                         existsExec=true;
1251                                 }
1252                         }
1253                         else if(type==GCONNECTOR)
1254                         {
1255                                 connections.push_back(it->first);
1256                         }
1257                 }
1258
1259         int lnNmbr=0;
1260                 std::string script = "";
1261                 script+=LineNumber(wln,lnNmbr)+"# BBTK GEditor Script\n";
1262                 script+=LineNumber(wln,lnNmbr)+"# ----------------------\n";
1263                 script+=LineNumber(wln,lnNmbr)+"\n";
1264                 if(existsExec)
1265                 {
1266                         script+=LineNumber(wln,lnNmbr)+"include std\n"; // EED
1267                         script+=LineNumber(wln,lnNmbr)+"include itkvtk\n"; // EED
1268                         int i;
1269                         for(i = 0; i< (int)packages.size();i++)
1270                         {
1271                                 script+=LineNumber(wln,lnNmbr);
1272                                 script+="include ";
1273                                 script+=packages[i];
1274                                 script+="\n";
1275                         }
1276
1277             script+=LineNumber(wln,lnNmbr);
1278                         script+="\n";
1279
1280                         // script+="include std\n"; // JPR
1281
1282                         for(i = 0; i < (int)boxes.size();i++)
1283                         {
1284                                 script+=LineNumber(wln,lnNmbr);
1285                                 script+="new ";
1286                                 int id = boxes[i];
1287                                 GObjectController *control = _controllers[id];
1288                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1289
1290                                 script+=model->getBBTKType();
1291                                 script+=" ";
1292                                 script+=model->getBBTKName();
1293                                 script+="\n";
1294
1295                                 std::vector<GPortModel*> inputs = model->getInputPorts();
1296                                 for(int j = 0; j < (int)inputs.size();j++)
1297                                 {
1298                                         GPortModel* inputPort = inputs[j];
1299                                         if(inputPort->isValueSet())
1300                                         {
1301                         script+=LineNumber(wln,lnNmbr);
1302                                                 script+="  set ";
1303                                                 script+=model->getBBTKName();
1304                                                 script+=".";
1305                                                 script+=inputPort->getBBTKName();
1306                                                 script+=" ";
1307                                                 script+=inputPort->getValue();
1308                                                 script+="\n";
1309                                         }
1310                                 }
1311
1312                                 script+=LineNumber(wln,lnNmbr);
1313                                 script+="\n";
1314
1315                         }
1316
1317             script+=LineNumber(wln,lnNmbr);
1318                         script+="\n";
1319
1320                         for(i = 0; i<(int)connections.size();i++)
1321                         {
1322                                 script+=LineNumber(wln,lnNmbr);
1323                                 script+="connect ";
1324                                 int id = connections[i];
1325                                 GObjectController *control = _controllers[id];
1326                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1327
1328                                 //Start Connection info
1329                                 GPortModel *start = model->getStartPort();
1330                                 script+=start->getParentBox()->getBBTKName();
1331                                 script+=".";
1332                                 script+=start->getBBTKName();
1333
1334                                 script+=" ";
1335
1336                                 //End Connection info
1337                                 GPortModel *end = model->getEndPort();
1338                                 script+=end->getParentBox()->getBBTKName();
1339                                 script+=".";
1340                                 script+=end->getBBTKName();
1341
1342                                 script+="\n";
1343                         }
1344
1345             script+=LineNumber(wln,lnNmbr);
1346                         script+="\n";
1347                         for(i = 0; i<(int)execBoxes.size();i++)
1348                         {
1349                                 script+=LineNumber(wln,lnNmbr);
1350                                 script+="exec ";
1351                                 int id = execBoxes[i];
1352                                 GObjectController *control = _controllers[id];
1353                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1354
1355                                 script+=model->getBBTKName();
1356                                 script+="\n";
1357                         }
1358
1359                 }
1360
1361                 return script;
1362         }
1363
1364         //=========================================================================
1365
1366         std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
1367         {
1368
1369                 std::vector<std::string> packages;
1370                 std::vector<int> boxes;
1371                 std::vector<int> connections;
1372                 std::vector<int> execBoxes;
1373
1374                 std::map<int, GObjectController*>::iterator it;
1375
1376                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1377                 {
1378                         GObjectController *desc = it->second;
1379                         int type = desc->getGObjectType();
1380
1381                         if(type==GBLACKBOX)
1382                         {
1383                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
1384
1385                                 std::string pkg = mod->getBBTKPackage();
1386                                 bool existsPkg = false;
1387                                 for(int t = 0;t<(int)packages.size() && existsPkg == false;t++)
1388                                 {
1389                                         if(packages[t]==pkg)
1390                                         {
1391                                                 existsPkg=true;
1392                                         }
1393                                 }
1394                                 if(!existsPkg)
1395                                 {
1396                                         packages.push_back(pkg);
1397                                 }
1398
1399
1400                                 boxes.push_back(it->first);
1401                                 if(mod->isExecutable())
1402                                 {
1403                                         execBoxes.push_back(it->first);
1404                                 }
1405                         }
1406                         else if(type==GCONNECTOR)
1407                         {
1408                                 connections.push_back(it->first);
1409                         }
1410                 }
1411
1412                 std::string script = "";
1413
1414                 script+="include std\n";    // EED
1415                 script+="include itkvtk\n"; // EED
1416                 int i;
1417                 for(i = 0; i<(int)packages.size();i++)
1418                 {
1419                         script+="include ";
1420                         script+=packages[i];
1421                         script+="\n";
1422                 }
1423                 script+="\n";
1424
1425                 // Definition of a complex box
1426                 script+="define ";
1427                 script+=cbName;
1428                 script+="\n";
1429                 script+="\n";
1430
1431                 script+="author \" ";
1432                 script+=cbAuthor;
1433                 script+="\"\n";
1434
1435                 script+="description \" ";
1436                 script+=cbDescription;
1437                 script+="\"\n";
1438                 script+="\n";
1439
1440                 // Create boxes
1441                 for(i = 0; i<(int)boxes.size();i++)
1442                 {
1443                         script+="new ";
1444                         int id = boxes[i];
1445                         GObjectController *control = _controllers[id];
1446                         GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1447
1448                         script+=model->getBBTKType();
1449                         script+=" ";
1450                         script+=model->getBBTKName();
1451                         script+="\n";
1452
1453                                 std::vector<GPortModel*> inputs = model->getInputPorts();
1454                                 for(int j = 0; j<(int)inputs.size();j++)
1455                                 {
1456                                         GPortModel* inputPort = inputs[j];
1457                                         if(inputPort->isValueSet())
1458                                         {
1459                                                 script+="  set ";
1460                                                 script+=model->getBBTKName();
1461                                                 script+=".";
1462                                                 script+=inputPort->getBBTKName();
1463                                                 script+=" ";
1464                                                 script+=inputPort->getValue();
1465                                                 script+="\n";
1466                                         }
1467                                 }
1468                                 script+="\n";
1469
1470                         }
1471
1472                         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1473
1474                         std::string complexInputs="";
1475                         std::string complexOutputs="";
1476                         script+="\n";
1477
1478                         for(i = 0; i<(int)connections.size();i++)
1479                         {
1480                                 int id = connections[i];
1481                                 GObjectController *control = _controllers[id];
1482                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1483
1484                                 //Connection info
1485                                 GPortModel *start = model->getStartPort();
1486                                 GBoxModel *startBox =start->getParentBox();
1487
1488                                 GPortModel *end = model->getEndPort();
1489                                 GBoxModel *endBox =end->getParentBox();
1490
1491                                 if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
1492                                 {
1493                                         complexInputs+="input ";
1494                                         complexInputs+=startBox->getBBTKName();
1495
1496                                         complexInputs+=" ";
1497                                         complexInputs+=endBox->getBBTKName();
1498                                         complexInputs+=".";
1499                                         complexInputs+=end->getBBTKName();
1500
1501                                         complexInputs+=" ";
1502                                         complexInputs+="\" \"";
1503
1504                                         complexInputs+="\n";
1505                                 }
1506                                 else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
1507                                 {
1508                                         complexOutputs+="output ";
1509                                         complexOutputs+=endBox->getBBTKName();
1510
1511                                         complexOutputs+=" ";
1512                                         complexOutputs+=startBox->getBBTKName();
1513                                         complexOutputs+=".";
1514                                         complexOutputs+=start->getBBTKName();
1515
1516                                         complexOutputs+=" ";
1517                                         complexOutputs+="\" \"";
1518
1519                                         complexOutputs+="\n";
1520                                 }
1521                                 else
1522                                 {
1523                                         script+="connect ";
1524                                         script+=startBox->getBBTKName();
1525                                         script+=".";
1526                                         script+=start->getBBTKName();
1527
1528                                         script+=" ";
1529
1530                                         //End Connection info
1531                                         script+=endBox->getBBTKName();
1532                                         script+=".";
1533                                         script+=end->getBBTKName();
1534
1535                                         script+="\n";
1536                                 }
1537                         }
1538
1539                     script+="\n";
1540
1541                         for(i = 0; i<(int)execBoxes.size();i++)
1542                         {
1543                                 script+="exec ";
1544                                 int id = execBoxes[i];
1545                                 GObjectController *control = _controllers[id];
1546                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1547
1548                                 script+=model->getBBTKName();
1549                                 script+="\n";
1550                         }
1551
1552                         script+="\n";
1553                         script+="# Complex input ports\n";
1554                         script+=complexInputs;
1555
1556                         script+="\n";
1557                         script+="# Complex output ports\n";
1558                         script+=complexOutputs;
1559
1560                         script+="\n";
1561                         script+="endefine";
1562                         script+="\n";
1563
1564                 return script;
1565         }
1566
1567         //=========================================================================
1568
1569         void wxVtkSceneManager::deleteAllBoxes()
1570         {
1571                 std::map<int, GObjectController*>::iterator it;
1572                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1573                 {
1574                         GObjectController *cont = it->second;
1575                         cont->removeFromScene();
1576                         unregisterController((InteractorStyleMaracas*)cont);
1577                 }
1578                 _selectedObjects.clear();
1579                 _controllers.clear();
1580                 refreshScene();
1581         }
1582
1583         //=========================================================================
1584
1585
1586         void wxVtkSceneManager::refresh()
1587         {
1588                 _baseView->Refresh();
1589         }
1590         //=========================================================================
1591
1592         void wxVtkSceneManager::refreshScene()
1593         {
1594                 _baseView->RefreshView();
1595         }
1596
1597         //=========================================================================
1598
1599         void wxVtkSceneManager::centerView()
1600         {
1601                 double temp[3];
1602                 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1603                 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1604                 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1605                 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1606                 _baseView->RefreshView();
1607
1608         }
1609
1610         //=========================================================================
1611
1612         void wxVtkSceneManager::saveDiagram(std::string &content)
1613         {
1614                 char buffer [50];
1615
1616                 //Print info IF COMPLEX BOX
1617                 content+="COMPLEXBOX:";
1618                 if(_isComplexBox)
1619                 {
1620                         content+="TRUE\n";
1621
1622                         //Print info complex input ports
1623                         std::vector<int> inputs = getComplexInputPorts();
1624                         int insize = inputs.size();
1625                         content+="COMPLEXINPUTS:";
1626                         sprintf (buffer, "%d", insize);
1627                         content+=buffer;
1628                         content+="\n";
1629
1630                         for(int i = 0;i<insize;i++)
1631                         {
1632                                 int id = inputs[i];
1633                                 GObjectController *cont = _controllers[id];
1634                                 cont->getModel()->save(content);
1635                         }
1636
1637                         //Print info complex output ports
1638                         std::vector<int> outputs = getComplexOutputPorts();
1639                         int outsize = outputs.size();
1640                         content+="COMPLEXOUTPUTS:";
1641                         sprintf (buffer, "%d", outsize);
1642                         content+=buffer;
1643                         content+="\n";
1644
1645                         for(int i = 0;i<outsize;i++)
1646                         {
1647                                 int id = outputs[i];
1648                                 GObjectController *cont = _controllers[id];
1649                                 cont->getModel()->save(content);
1650                         }
1651                 } // _isComplexBox
1652                 else
1653                 {
1654                         content+="FALSE\n";
1655                 }
1656
1657                 //Print boxes
1658                 std::vector<int> boxes = getBlackBoxes();
1659                 int bsize = boxes.size();
1660                 content+="BOXES:";
1661                 sprintf (buffer, "%d", bsize);
1662                 content+=buffer;
1663                 content+="\n";
1664
1665                 for(int i = 0;i<bsize;i++)
1666                 {
1667                         int id = boxes[i];
1668                         GObjectController *cont = _controllers[id];
1669                         cont->getModel()->save(content);
1670                 }
1671
1672                 //Print connections
1673                 std::vector<int> connections = getConnections();
1674                 int csize = connections.size();
1675                 content+="CONNECTIONS:";
1676                 sprintf (buffer, "%d", csize);
1677                 content+=buffer;
1678                 content+="\n";
1679
1680                 for(int i = 0;i<csize;i++)
1681                 {
1682                         int id = connections[i];
1683                         GObjectController *cont = _controllers[id];
1684                         cont->getModel()->save(content);
1685                 }
1686
1687         }
1688
1689         //=========================================================================
1690
1691         void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1692         {
1693
1694                 std::string line="";
1695                 char delims[] = ":";
1696                 char *result = NULL;
1697         getline(inputStream,line);
1698
1699                 bool start = false;
1700                 while ( !inputStream.eof() )
1701                 {
1702                         if(line=="" || line[0]=='#')
1703                         {
1704                                 getline(inputStream,line);
1705                         }
1706                         else if(line=="APP_START")
1707                         {
1708                                 start = true;
1709                                 break;
1710                         }
1711                 }
1712
1713                 if(start)
1714                 {
1715
1716                         //----------
1717                         getline(inputStream,line);//COMPLEX_BOX:TRUE|FALSE
1718                         char complex[30];
1719                         strcpy( complex, line.c_str() );
1720                         result = strtok( complex, delims );
1721                         result = strtok( NULL, delims );
1722                         std::string isComplexBox(result);
1723
1724                         if(isComplexBox=="TRUE")
1725                         {
1726                                 _isComplexBox=true;
1727
1728                                 //-----------------------
1729                                 //- COMPLEX INPUT PORTS
1730                                 //-----------------------
1731                                 getline(inputStream,line);//COMPLEXINPUTS:num
1732                                 char inputs[30];
1733                                 strcpy( inputs, line.c_str() );
1734                                 result = strtok( inputs, delims );
1735                                 result = strtok( NULL, delims );
1736
1737                                 int numInputs;
1738                                 std::istringstream inps(result);
1739                                 inps >> numInputs;
1740
1741                                 for(int i = 0;i<numInputs;i++)
1742                                 {
1743                                         //----------
1744                                         getline(inputStream,line);//COMPLEX_PORT
1745                                         getline(inputStream,line);//name
1746                                         std::string inputPortName(line);
1747
1748                                         //----------
1749                                         getline(inputStream,line);//xInic:yInic:zInic
1750                                         char coord[80];
1751                                         strcpy( coord, line.c_str() );
1752                                         result = strtok( coord, delims );//xInic
1753                                         std::string xInic(result);
1754                                         result = strtok( NULL, delims );//yInic
1755                                         std::string yInic(result);
1756                                         result = strtok( NULL, delims );//zInic
1757                                         std::string zInic(result);
1758
1759                                         double xIn, yIn, zIn;
1760                                         std::istringstream xSt(xInic);
1761                                         xSt >> xIn;
1762                                         std::istringstream ySt(yInic);
1763                                         ySt >> yIn;
1764                                         std::istringstream zSt(zInic);
1765                                         zSt >> zIn;
1766
1767                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1768
1769                                         configGComBoxInputOutputPort(true, inputPortName,xIn,yIn,zIn);
1770
1771
1772                                 } // for input complex box
1773
1774
1775                                 //-----------------------
1776                                 //- COMPLEX OUTPUT PORTS
1777                                 //-----------------------
1778
1779                                 getline(inputStream,line);//COMPLEXOUTPUTS:num
1780                                 char outputs[30];
1781                                 strcpy( outputs, line.c_str() );
1782                                 result = strtok( outputs, delims );
1783                                 result = strtok( NULL, delims );
1784
1785                                 int numOutputs;
1786                                 std::istringstream outps(result);
1787                                 outps >> numOutputs;
1788
1789                                 for(int i = 0;i<numOutputs;i++)
1790                                 {
1791                                         //----------
1792                                         getline(inputStream,line);//COMPLEX_PORT
1793                                         getline(inputStream,line);//name
1794                                         std::string outputPortName(line);
1795
1796                                         //----------
1797                                         getline(inputStream,line);//xInic:yInic:zInic
1798                                         char coord[80];
1799                                         strcpy( coord, line.c_str() );
1800                                         result = strtok( coord, delims );//xInic
1801                                         std::string xInic(result);
1802                                         result = strtok( NULL, delims );//yInic
1803                                         std::string yInic(result);
1804                                         result = strtok( NULL, delims );//zInic
1805                                         std::string zInic(result);
1806
1807                                         double xIn, yIn, zIn;
1808                                         std::istringstream xSt(xInic);
1809                                         xSt >> xIn;
1810                                         std::istringstream ySt(yInic);
1811                                         ySt >> yIn;
1812                                         std::istringstream zSt(zInic);
1813                                         zSt >> zIn;
1814
1815                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1816
1817                                         configGComBoxInputOutputPort(false, outputPortName,xIn,yIn,zIn);
1818
1819                                 } // for output complex box
1820
1821                         } // complex box
1822
1823                         //----------
1824                         getline(inputStream,line);//BOXES:num
1825                         char boxes[9];
1826                         strcpy( boxes, line.c_str() );
1827                         result = strtok( boxes, delims );
1828                         result = strtok( NULL, delims );
1829
1830                         int numBoxes;
1831                         std::istringstream is(result);
1832                         is >> numBoxes;
1833
1834                         for(int i = 0;i<numBoxes;i++)
1835                         {
1836                                 //----------
1837                                 getline(inputStream,line);//BOX
1838                                 getline(inputStream,line);//package:type:name
1839                                 char box[150];
1840                                 strcpy( box, line.c_str() );
1841                                 result = strtok( box, delims );//package
1842                                 std::string package(result);
1843                                 result = strtok( NULL, delims );//type
1844                                 std::string type(result);
1845                                 result = strtok( NULL, delims );//name
1846                                 std::string name(result);
1847
1848                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1849                                 char exec[15];
1850                                 strcpy( exec, line.c_str() );
1851                                 result = strtok( exec, delims );//ISEXEC
1852                                 result = strtok( NULL, delims );//TRUE|FALSE
1853                                 std::string isExec(result);
1854
1855                                 //----------
1856                                 getline(inputStream,line);//xInic:yInic:zInic
1857                                 char coord[80];
1858                                 strcpy( coord, line.c_str() );
1859                                 result = strtok( coord, delims );//xInic
1860                                 std::string xInic(result);
1861                                 result = strtok( NULL, delims );//yInic
1862                                 std::string yInic(result);
1863                                 result = strtok( NULL, delims );//zInic
1864                                 std::string zInic(result);
1865
1866                                 double xIn, yIn, zIn;
1867                                 std::istringstream xSt(xInic);
1868                                 xSt >> xIn;
1869                                 std::istringstream ySt(yInic);
1870                                 ySt >> yIn;
1871                                 std::istringstream zSt(zInic);
1872                                 zSt >> zIn;
1873
1874                                 //----------
1875                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1876                                 strcpy( coord, line.c_str() );
1877                                 result = strtok( coord, delims );//xEnd
1878                                 std::string xEnd(result);
1879                                 result = strtok( NULL, delims );//yEnd
1880                                 std::string yEnd(result);
1881                                 result = strtok( NULL, delims );//zEnd
1882                                 std::string zEnd(result);
1883
1884                                 double xEn, yEn, zEn;
1885                                 std::istringstream xEt(xEnd);
1886                                 xEt >> xEn;
1887                                 std::istringstream yEt(yEnd);
1888                                 yEt >> yEn;
1889                                 std::istringstream zEt(zEnd);
1890                                 zEt >> zEn;
1891
1892                                 bool boxExecutable=false;
1893                                 if(isExec=="TRUE")
1894                                 {
1895                                         boxExecutable= true;
1896                                 }
1897
1898                                 int idBox                               = createGBlackBox(xIn,yIn,package,type);
1899                                 configGBlackBox(idBox, xIn,yIn,zIn,name, boxExecutable,xEn,yEn,zEn);
1900
1901                                 GObjectController *cont = _controllers[idBox];
1902                                 GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
1903
1904
1905                                 //----------
1906                                 getline(inputStream,line);//PORT o FIN_BOX
1907                                 std::string port=line.substr(0,4);
1908                                 while(port=="PORT")
1909                                 {
1910                                         getline(inputStream,line);//name:value
1911                                         char poort[150];
1912                                         strcpy( poort, line.c_str() );
1913                                         result = strtok( poort, delims );//name
1914                                         std::string name(result);
1915                                         result = strtok( NULL, delims );//value
1916                                         std::string value(result);
1917
1918                                         bbmod->setValueToInput(name,value);
1919
1920                                         getline(inputStream,line);//PORT o FIN_BOX
1921                                         port=line.substr(0,4);
1922                                 } // while
1923
1924 //EED                           bbmod->notifyObservers(_idManager);
1925                         } // for boxes
1926
1927                         /// CONNECTIONS
1928                         //----------
1929                         getline(inputStream,line);//CONNECTIONS:num
1930                         char conns[30];
1931                         strcpy( conns, line.c_str() );
1932                         result = strtok( conns, delims );
1933                         result = strtok( NULL, delims );
1934
1935                         int numConns;
1936                         std::istringstream isCons(result);
1937                         isCons >> numConns;
1938
1939                         for(int i = 0;i<numConns;i++)
1940                         {
1941                                 //----------
1942                                 getline(inputStream,line);//CONNECTION
1943                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1944
1945                                 char connec[200];
1946                                 strcpy( connec, line.c_str() );
1947                                 result = strtok( connec, delims );
1948                                 std::string nameStartBox(result);
1949                                 result = strtok( NULL, delims );
1950                                 std::string nameStartPort(result);
1951                                 result = strtok( NULL, delims );
1952                                 std::string nameEndBox(result);
1953                                 result = strtok( NULL, delims );
1954                                 std::string nameEndPort(result);
1955
1956                                 configGConnetion(nameStartBox, nameStartPort, nameEndBox, nameEndPort);
1957                         } // for numConns
1958
1959         } // start
1960
1961         }
1962         //=========================================================================
1963
1964
1965
1966         //=========================================================================
1967         void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput, std::string portName, double xIn, double yIn,double zIn)
1968         {
1969                 int idPort;
1970                 if (inputoutput==true)
1971                 {
1972                         idPort = createGComplexBoxInputPort(portName);
1973                 } else  {
1974                         idPort = createGComplexBoxOutputPort(portName);
1975                 }
1976                 GObjectController *cont = _controllers[idPort];
1977                 GBoxModel *cbmod                = (GBoxModel*)cont->getModel();
1978                 cbmod->setInicPoint(xIn,yIn,zIn);
1979                 cbmod->notifyObservers(_idManager);
1980         }
1981         //=========================================================================
1982
1983
1984         //=========================================================================
1985         GBoxModel* wxVtkSceneManager::findGBox(std::string boxname)
1986         {
1987                 int j;
1988                 int idB;
1989                 GBoxModel *bMod;
1990                 GBoxModel *boxModel                     = NULL;
1991                 std::vector<int> lstB           = getBlackBoxes();
1992                 for(j = 0;j<(int)lstB.size();j++)
1993                 {
1994                         idB             = lstB[j];
1995                         bMod    = (GBoxModel*)_controllers[idB]->getModel();
1996                         if(_controllers[idB]->getModel()->getBBTKName()==boxname)
1997                         {
1998                                 boxModel = bMod;
1999                         }
2000                 } // for
2001
2002                 if((_isComplexBox) && (boxModel==NULL))
2003                 {
2004                         std::vector<int> lstInputs = getComplexInputPorts();
2005                         for(j = 0;j<(int)lstInputs.size();j++)
2006                         {
2007                                 idB             = lstInputs[j];
2008                                 bMod    = (GBoxModel*)_controllers[idB]->getModel();
2009                                 if(_controllers[idB]->getModel()->getBBTKName()==boxname)
2010                                 {
2011                                         boxModel = bMod;
2012                                 }
2013                         } // for
2014
2015                         std::vector<int> lstOutputs = getComplexOutputPorts();
2016                         for(j = 0;j<(int)lstOutputs.size();j++)
2017                         {
2018                                 int idB = lstOutputs[j];
2019                                 bMod = (GBoxModel*)_controllers[idB]->getModel();
2020                                 if(_controllers[idB]->getModel()->getBBTKName()==boxname)
2021                                 {
2022                                         boxModel = bMod;
2023                                 }
2024                         } // for
2025
2026                 } // complex box
2027
2028                 return boxModel;
2029         }
2030
2031         //=========================================================================
2032
2033
2034         void wxVtkSceneManager::configGConnetion(std::string nameStartBox, std::string  nameStartPort, std::string
2035                                                                                          nameEndBox, std::string  nameEndPort)
2036         {
2037
2038                 GBoxModel *boxModel;
2039                 GPortModel *startP      = NULL;
2040                 GPortModel *endP        = NULL;
2041
2042                 boxModel= findGBox(nameStartBox);
2043                 if (boxModel!=NULL)
2044                 {
2045                         startP          = boxModel->getOutputPort(nameStartPort);
2046                 }
2047
2048                 boxModel= findGBox(nameEndBox);
2049                 if (boxModel!=NULL)
2050                 {
2051                         endP            = boxModel->getInputPort(nameEndPort);
2052                 }
2053
2054
2055                 int idCon                                       = createGConnector(startP);
2056                 _worldState                                     = NOTHING_HAPPENS;
2057                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
2058
2059                 GConnectorModel *conMod         = (GConnectorModel*)tempp->getModel();
2060                 vtkGConnectorView *conView      = (vtkGConnectorView*)tempp->getView();
2061                 tempp->endContourCreation();
2062                 conMod->setEndPort(endP);
2063                 conView->updateStartEndPoints();
2064         }
2065
2066         //=========================================================================
2067
2068         bool wxVtkSceneManager::boxExist(std::string boxname)
2069         {
2070                 bool ok=false;
2071                 std::map<int, GObjectController*>::iterator it;
2072                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2073                 {
2074                         GObjectController *cont = it->second;
2075                         if(cont->getModel()->getBBTKName()==boxname)
2076                         {
2077                                 ok=true;
2078                         }
2079                 }
2080                 return ok;
2081         }
2082
2083         //=========================================================================
2084
2085         std::vector<int> wxVtkSceneManager::getBlackBoxes()
2086         {
2087                 std::vector<int> vect;
2088                 std::map<int, GObjectController*>::iterator it;
2089                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2090                 {
2091                         GObjectController *cont = it->second;
2092                         if(cont->getGObjectType()==GBLACKBOX)
2093                         {
2094                                 vect.push_back(cont->getId());
2095                         }
2096                 }
2097                 return vect;
2098         }
2099
2100         //=========================================================================
2101
2102         std::vector<int> wxVtkSceneManager::getComplexInputPorts()
2103         {
2104                 std::vector<int> vect;
2105                 std::map<int, GObjectController*>::iterator it;
2106                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2107                 {
2108                         GObjectController *cont = it->second;
2109                         if(cont->getGObjectType()==GCOMPLEXINPUTPORT)
2110                         {
2111                                 vect.push_back(cont->getId());
2112                         }
2113                 }
2114                 return vect;
2115         }
2116
2117         //=========================================================================
2118
2119         std::vector<int> wxVtkSceneManager::getComplexOutputPorts()
2120         {
2121                 std::vector<int> vect;
2122                 std::map<int, GObjectController*>::iterator it;
2123                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2124                 {
2125                         GObjectController *cont = it->second;
2126                         if(cont->getGObjectType()==GCOMPLEXOUTPUTPORT)
2127                         {
2128                                 vect.push_back(cont->getId());
2129                         }
2130                 }
2131                 return vect;
2132         }
2133
2134         //=========================================================================
2135
2136         std::vector<int> wxVtkSceneManager::getConnections()
2137         {
2138                 std::vector<int> vect;
2139                 std::map<int, GObjectController*>::iterator it;
2140                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2141                 {
2142                         GObjectController *cont = it->second;
2143                         if(cont->getGObjectType()==GCONNECTOR)
2144                         {
2145                                 vect.push_back(cont->getId());
2146                         }
2147                 }
2148                 return vect;
2149         }
2150
2151         //=========================================================================
2152
2153         bool wxVtkSceneManager::isComplexBox()
2154         {
2155                 return _isComplexBox;
2156         }
2157
2158         //=========================================================================
2159
2160         void wxVtkSceneManager::setComplexBox(bool val)
2161         {
2162                 _isComplexBox=val;
2163         }
2164
2165         //=========================================================================
2166
2167         int wxVtkSceneManager::addObjectController(GObjectController* objController)
2168         {
2169                 //Register the controller of the new object
2170                 registerController((InteractorStyleMaracas*) objController);
2171
2172                 //Add the object to the objects list
2173                 int newId = _contLastId;//_controllers.size();
2174                 objController->setId(newId);
2175                 _controllers[newId] = objController;
2176                 _contLastId++;
2177                 return newId;
2178         }
2179
2180         //=========================================================================
2181
2182         int wxVtkSceneManager::getNumSelectedObjects()
2183         {
2184                 return _selectedObjects.size();
2185         }
2186
2187         //=========================================================================
2188
2189         std::map<int,GObjectController*> wxVtkSceneManager::getSelectedObjects()
2190         {
2191                 std::map<int,GObjectController*> mapSelected;
2192
2193                 std::map<int, GObjectController*>::iterator it;
2194                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2195                 {
2196                         GObjectController *cont = it->second;
2197                         if(cont->getGObjectType()==GBLACKBOX && cont->getView()->getState()==SELECTED)
2198                         {
2199                                 mapSelected[cont->getId()]=cont;
2200                         }
2201                 }
2202
2203                 std::map<int, GObjectController*>::iterator it2;
2204                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
2205                 {
2206                         GObjectController *cont = it2->second;
2207                         if(cont->getGObjectType()==GCONNECTOR)
2208                         {
2209                                 GConnectorModel* cmod = (GConnectorModel*)cont->getModel();
2210                                 GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2211                                 GBoxModel* startPortParentBox = cmod->getStartPort()->getParentBox();
2212
2213                                 std::map<int, GObjectController*>::iterator iterOBJ1 = mapSelected.find(startPortParentBox->getObjectId());
2214                                 std::map<int, GObjectController*>::iterator iterOBJ2 = mapSelected.find(endPortParentBox->getObjectId());
2215
2216                                 if(iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end())
2217                                 {
2218                                         int ID = cont->getId();
2219                                         mapSelected[ID]=cont;
2220                                 }
2221                         }
2222                 }
2223                 return mapSelected;
2224         }
2225
2226         //=========================================================================
2227
2228         void wxVtkSceneManager::addObjects(std::map<int,GObjectController*> objectsMap)
2229         {
2230
2231                 std::map<int,int> oldIdNewIdBoxes;
2232                 std::vector<int> connections;
2233
2234                 std::map<int, GObjectController*>::iterator it;
2235                 for(it = objectsMap.begin(); it != objectsMap.end(); ++it)
2236                 {
2237                         GObjectController *cont = it->second;
2238                         int type = cont->getGObjectType();
2239
2240                         if(type==GBLACKBOX)
2241                         {
2242                                 // Copy black box
2243                                 double xInic, yInic,zInic;
2244                                 GBlackBoxModel* copyBox = (GBlackBoxModel*)cont->getModel();
2245                                 copyBox->getInicPoint(xInic,yInic,zInic);
2246                                 int idBox = createGBlackBox(0,0,copyBox->getBBTKPackage(),copyBox->getBBTKType());
2247
2248                                 int idcB = copyBox->getObjectId();
2249                                 oldIdNewIdBoxes[idcB]=idBox;
2250                                 cont = _controllers[idBox];
2251                                 GBlackBoxModel* newbox  = (GBlackBoxModel*)cont->getModel();
2252                                 newbox->setInicPoint(xInic,yInic,zInic);
2253                                 int num = newbox->getNumInputPorts();
2254                                 for(int j=0;j<num;j++)
2255                                 {
2256                                         newbox->setValueToInputPort(j,copyBox->getValueInputPort(j));
2257                                 }
2258                                 newbox->notifyObservers(_idManager);
2259                         }
2260                         else if(type==GCONNECTOR)
2261                         {
2262                                 int idCon = cont->getId();
2263                                 connections.push_back(idCon);
2264                         }
2265
2266                 }
2267
2268                 for(int i = 0 ;i<(int)connections.size();i++)
2269                 {
2270                         int objId = connections[i];
2271                         GObjectController *cont = objectsMap[objId];
2272                         GConnectorModel* connectModel = (GConnectorModel*)cont->getModel();
2273
2274                         GPortModel* startPort = connectModel->getStartPort();
2275                         int startPortIndex = startPort->getPosInBox();
2276                         GPortModel* endPort = connectModel->getEndPort();
2277                         int endPortIndex = endPort->getPosInBox();
2278
2279                         GBlackBoxModel* startPortParentBox = (GBlackBoxModel*)startPort->getParentBox();
2280                         GBlackBoxModel* endPortParentBox = (GBlackBoxModel*)endPort->getParentBox();
2281
2282                         int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2283                         int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2284
2285                         GBlackBoxModel* newStartBox = (GBlackBoxModel*)_controllers[idNewStartBox]->getModel();
2286                         GBlackBoxModel* newEndBox = (GBlackBoxModel*)_controllers[idNewEndBox]->getModel();
2287
2288                         GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2289                         GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2290
2291                         // Creates connection
2292                         int idCon = createGConnector(newStartPort);
2293                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
2294                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2295                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2296                         tempp->endContourCreation();
2297                         conMod->setEndPort(newEndPort);
2298                         conView->updateStartEndPoints();
2299                 }
2300
2301                 std::map<int, int>::iterator itIds;
2302                 for(itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds)
2303                 {
2304                         int idOld = itIds->first;
2305                         int idNew = itIds->second;
2306
2307                         GBlackBoxModel* oldBox = (GBlackBoxModel*)objectsMap[idOld]->getModel();
2308                         GBlackBoxModel* newBox = (GBlackBoxModel*)_controllers[idNew]->getModel();
2309
2310                         std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2311                         std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2312                         std::vector<int> newInputConnections = newBox->getConnectedInputs();
2313                         std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2314
2315                         for(int k = 0; k<(int)oldInputConnections.size();k++)
2316                         {
2317                                 bool exist=false;
2318 //EED                           int toCreate=-1;
2319                                 for(int l = 0; l<(int)newInputConnections.size() && !exist;l++)
2320                                 {
2321                                         if(oldInputConnections[k]==newInputConnections[l])
2322                                         {
2323                                                 exist=true;
2324                                         }
2325                                 }
2326
2327                                 if(exist==false)
2328                                 {
2329                                         //Create complex input
2330                                         int posInBox = oldInputConnections[k];
2331                                         GPortModel* inputPort = oldBox->getInputPort(posInBox);
2332                                         std::string inputPortName = inputPort->getBBTKName();
2333                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
2334                                         GObjectController *cont = _controllers[idInputPort];
2335                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2336                                         double xIn,yIn,zIn;
2337                                         inputPort->getInicPoint(xIn,yIn,zIn);
2338                                         yIn+=20;
2339                                         cbmod->setInicPoint(xIn,yIn,zIn);
2340                                         cbmod->notifyObservers(_idManager);
2341
2342                                         GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2343
2344                                         // Creates connection
2345                                         int idCon = createGConnector(cbmod->getOutputPort(0));
2346                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
2347                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2348                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2349                                         tempp->endContourCreation();
2350                                         conMod->setEndPort(inputPortEnd);
2351                                         conView->updateStartEndPoints();
2352
2353                                 }
2354
2355                         }
2356
2357
2358                         for(int k = 0; k<(int)oldOutputConnections.size();k++)
2359                         {
2360                                 bool exist=false;
2361 //EED                           int toCreate=-1;
2362                                 for(int l = 0; l<(int)newOutputConnections.size() && !exist;l++)
2363                                 {
2364                                         if(oldOutputConnections[k]==newOutputConnections[l])
2365                                         {
2366                                                 exist=true;
2367                                         }
2368                                 }
2369
2370                                 if(exist==false)
2371                                 {
2372                                         //Create complex output
2373                                         int posInBox = oldOutputConnections[k];
2374                                         GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2375                                         std::string outputPortName = outputPort->getBBTKName();
2376                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2377                                         GObjectController *cont = _controllers[idOutputPort];
2378                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2379                                         double xIn,yIn,zIn;
2380                                         outputPort->getInicPoint(xIn,yIn,zIn);
2381                                         yIn-=20;
2382                                         cbmod->setInicPoint(xIn,yIn,zIn);
2383                                         cbmod->notifyObservers(_idManager);
2384
2385                                         GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2386
2387                                         // Creates connection
2388                                         int idCon = createGConnector(outputPortEnd);
2389                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
2390                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2391                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2392                                         tempp->endContourCreation();
2393                                         conMod->setEndPort(cbmod->getInputPort(0));
2394                                         conView->updateStartEndPoints();
2395
2396                                 }
2397
2398                         }
2399
2400                 }
2401
2402
2403         }
2404
2405         //=========================================================================
2406
2407 }  // EO namespace bbtk
2408
2409 // EOF
2410