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