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