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