]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
f5313e7e7c7823f69ae07044082cda8831d188b3
[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                                         configGComBoxInputOutputPort(true, inputPortName,xIn,yIn,zIn);
1520
1521 //ups 3 EED Borrame
1522 //                                      int idInputPort = createGComplexBoxInputPort(inputPortName);
1523 //                                      GObjectController *cont = _controllers[idInputPort];
1524 //                                      GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1525 //                                      cbmod->setInicPoint(xIn,yIn,zIn);
1526 //                                      cbmod->notifyObservers(_idManager);
1527
1528                                         
1529                                 } // for input complex box
1530
1531
1532                                 //-----------------------
1533                                 //- COMPLEX OUTPUT PORTS
1534                                 //-----------------------
1535
1536                                 getline(inputStream,line);//COMPLEXOUTPUTS:num
1537                                 char outputs[30];
1538                                 strcpy( outputs, line.c_str() );
1539                                 result = strtok( outputs, delims );
1540                                 result = strtok( NULL, delims );
1541                                                 
1542                                 int numOutputs;
1543                                 std::istringstream outps(result);
1544                                 outps >> numOutputs;
1545                                 
1546                                 for(int i = 0;i<numOutputs;i++)
1547                                 {
1548                                         //----------
1549                                         getline(inputStream,line);//COMPLEX_PORT
1550                                         getline(inputStream,line);//name
1551                                         std::string outputPortName(line);
1552
1553                                         //----------
1554                                         getline(inputStream,line);//xInic:yInic:zInic
1555                                         char coord[80];
1556                                         strcpy( coord, line.c_str() );
1557                                         result = strtok( coord, delims );//xInic
1558                                         std::string xInic(result);
1559                                         result = strtok( NULL, delims );//yInic
1560                                         std::string yInic(result);
1561                                         result = strtok( NULL, delims );//zInic
1562                                         std::string zInic(result);
1563
1564                                         double xIn, yIn, zIn;
1565                                         std::istringstream xSt(xInic);
1566                                         xSt >> xIn;
1567                                         std::istringstream ySt(yInic);
1568                                         ySt >> yIn;
1569                                         std::istringstream zSt(zInic);
1570                                         zSt >> zIn;
1571
1572                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1573
1574                                         configGComBoxInputOutputPort(false, outputPortName,xIn,yIn,zIn);
1575                                         
1576 //ups 3 EED Borrame
1577 //                                      int idOutputPort = createGComplexBoxOutputPort(outputPortName);
1578 //                                      GObjectController *cont = _controllers[idOutputPort];
1579 //                                      GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1580 //                                      cbmod->setInicPoint(xIn,yIn,zIn);
1581 //                                      cbmod->notifyObservers(_idManager);
1582                                         
1583                                 } // for output complex box
1584
1585                         } // complex box
1586
1587                         //----------
1588                         getline(inputStream,line);//BOXES:num
1589                         char boxes[9];
1590                         strcpy( boxes, line.c_str() );
1591                         result = strtok( boxes, delims );
1592                         result = strtok( NULL, delims );
1593                                         
1594                         int numBoxes;
1595                         std::istringstream is(result);
1596                         is >> numBoxes;
1597                         
1598                         for(int i = 0;i<numBoxes;i++)
1599                         {
1600                                 //----------
1601                                 getline(inputStream,line);//BOX
1602                                 getline(inputStream,line);//package:type:name
1603                                 char box[150];
1604                                 strcpy( box, line.c_str() );
1605                                 result = strtok( box, delims );//package
1606                                 std::string package(result);
1607                                 result = strtok( NULL, delims );//type
1608                                 std::string type(result);
1609                                 result = strtok( NULL, delims );//name
1610                                 std::string name(result);
1611
1612                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1613                                 char exec[15];
1614                                 strcpy( exec, line.c_str() );
1615                                 result = strtok( exec, delims );//ISEXEC                                
1616                                 result = strtok( NULL, delims );//TRUE|FALSE
1617                                 std::string isExec(result);
1618
1619                                 //----------
1620                                 getline(inputStream,line);//xInic:yInic:zInic
1621                                 char coord[80];
1622                                 strcpy( coord, line.c_str() );
1623                                 result = strtok( coord, delims );//xInic
1624                                 std::string xInic(result);
1625                                 result = strtok( NULL, delims );//yInic
1626                                 std::string yInic(result);
1627                                 result = strtok( NULL, delims );//zInic
1628                                 std::string zInic(result);
1629
1630                                 double xIn, yIn, zIn;
1631                                 std::istringstream xSt(xInic);
1632                                 xSt >> xIn;
1633                                 std::istringstream ySt(yInic);
1634                                 ySt >> yIn;
1635                                 std::istringstream zSt(zInic);
1636                                 zSt >> zIn;
1637                                                 
1638                                 //----------
1639                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1640                                 strcpy( coord, line.c_str() );
1641                                 result = strtok( coord, delims );//xEnd
1642                                 std::string xEnd(result);
1643                                 result = strtok( NULL, delims );//yEnd
1644                                 std::string yEnd(result);
1645                                 result = strtok( NULL, delims );//zEnd
1646                                 std::string zEnd(result);
1647
1648                                 double xEn, yEn, zEn;
1649                                 std::istringstream xEt(xEnd);
1650                                 xEt >> xEn;
1651                                 std::istringstream yEt(yEnd);
1652                                 yEt >> yEn;
1653                                 std::istringstream zEt(zEnd);
1654                                 zEt >> zEn;
1655
1656                                 bool boxExecutable=false;
1657                                 if(isExec=="TRUE")
1658                                 {
1659                                         boxExecutable= true;
1660                                 }
1661                                 
1662                                 int idBox                               = createGBlackBox(xIn,yIn,package,type);
1663                                 configGBlackBox(idBox, xIn,yIn,zIn,name, boxExecutable,xEn,yEn,zEn);
1664                                 
1665                                 GObjectController *cont = _controllers[idBox];
1666                                 GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
1667                                 
1668                                 
1669 //ups2 eed                              
1670 //                              GObjectController *cont = _controllers[idBox];
1671 //                              GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
1672 //                              bbmod->setBBTKName(name);
1673 //                              bbmod->setInicPoint(xIn,yIn,zIn);
1674 //                              bbmod->setFinalPoint(xEn,yEn,zEn);
1675 //                              bbmod->setExecutable(boxExecutable);
1676
1677                                                         
1678                                 //----------
1679                                 getline(inputStream,line);//PORT o FIN_BOX
1680                                 std::string port=line.substr(0,4);
1681                                 while(port=="PORT")
1682                                 {
1683                                         getline(inputStream,line);//name:value
1684                                         char poort[150];
1685                                         strcpy( poort, line.c_str() );
1686                                         result = strtok( poort, delims );//name
1687                                         std::string name(result);
1688                                         result = strtok( NULL, delims );//value
1689                                         std::string value(result);
1690
1691                                         bbmod->setValueToInput(name,value);                                     
1692
1693                                         getline(inputStream,line);//PORT o FIN_BOX
1694                                         port=line.substr(0,4);
1695                                 } // while              
1696
1697 //EED                           bbmod->notifyObservers(_idManager);
1698                         } // for boxes
1699
1700                         /// CONNECTIONS
1701                         //----------
1702                         getline(inputStream,line);//CONNECTIONS:num
1703                         char conns[30];
1704                         strcpy( conns, line.c_str() );
1705                         result = strtok( conns, delims );
1706                         result = strtok( NULL, delims );
1707                                         
1708                         int numConns;
1709                         std::istringstream isCons(result);
1710                         isCons >> numConns;
1711                                         
1712                         for(int i = 0;i<numConns;i++)
1713                         {
1714                                 //----------
1715                                 getline(inputStream,line);//CONNECTION
1716                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1717
1718                                 char connec[200];
1719                                 strcpy( connec, line.c_str() );
1720                                 result = strtok( connec, delims );
1721                                 std::string nameStartBox(result);
1722                                 result = strtok( NULL, delims );
1723                                 std::string nameStartPort(result);
1724                                 result = strtok( NULL, delims );
1725                                 std::string nameEndBox(result);
1726                                 result = strtok( NULL, delims );
1727                                 std::string nameEndPort(result);
1728
1729                                 configGConnetion(nameStartBox, nameStartPort, nameEndBox, nameEndPort);
1730 //ups 3 EED Borrame
1731 /*                              
1732                                 std::vector<int> lstB   = getBlackBoxes();                              
1733                                 GPortModel *startP              = NULL;
1734                                 GPortModel *endP                = NULL;
1735                                 int j;
1736                                 for(j = 0;j<lstB.size();j++)
1737                                 {
1738                                         int idB = lstB[j];
1739                                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1740                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1741                                         {                                               
1742                                                 startP = bbMod->getOutputPort(nameStartPort);
1743                                         }
1744                                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1745                                         {
1746                                                 endP = bbMod->getInputPort(nameEndPort);
1747                                         }
1748                                 } // for
1749
1750                                 if(_isComplexBox)
1751                                 {
1752                                         if(startP==NULL)
1753                                         {
1754                                                 std::vector<int> lstInputs = getComplexInputPorts();
1755                                                 for(j = 0;j<lstInputs.size();j++)
1756                                                 {
1757                                                         int idB = lstInputs[j];
1758                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1759                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1760                                                         {                                               
1761                                                                 startP = cbMod->getOutputPort(nameStartPort);
1762                                                         } // if 
1763                                                 } // for
1764                                         } // if 
1765
1766                                         if(endP==NULL)
1767                                         {
1768                                                 std::vector<int> lstOutputs = getComplexOutputPorts();
1769                                                 for(j = 0;j<lstOutputs.size();j++)
1770                                                 {
1771                                                         int idB = lstOutputs[j];
1772                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1773                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1774                                                         {                                               
1775                                                                 endP = cbMod->getInputPort(nameEndPort);
1776                                                         } // if
1777                                                 } // for
1778                                         } // if endP
1779                                 } // complex box
1780                                                                 
1781                                 int idCon = createGConnector(startP);
1782                                 _worldState = NOTHING_HAPPENS;
1783                                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1784
1785                                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1786                                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1787                                 tempp->endContourCreation();
1788                                 conMod->setEndPort(endP);                                       
1789                                 conView->updateStartEndPoints();
1790  */
1791                         } // for numConns
1792                         
1793         } // start
1794
1795         }
1796         //=========================================================================
1797
1798         
1799         
1800         //=========================================================================     
1801         void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput, std::string portName, double xIn, double yIn,double zIn)
1802         {
1803                 int idPort;
1804                 if (inputoutput==true)
1805                 {
1806                         idPort = createGComplexBoxInputPort(portName);
1807                 } else  {
1808                         idPort = createGComplexBoxOutputPort(portName);
1809                 }
1810                 GObjectController *cont = _controllers[idPort];
1811                 GBoxModel *cbmod                = (GBoxModel*)cont->getModel();
1812                 cbmod->setInicPoint(xIn,yIn,zIn);
1813                 cbmod->notifyObservers(_idManager);                             
1814         }
1815         //=========================================================================
1816         
1817         
1818         //=========================================================================
1819         GBoxModel* wxVtkSceneManager::findGBox(std::string boxname)
1820         {       
1821                 int j;
1822                 int idB;
1823                 GBoxModel *bMod;
1824                 GBoxModel *boxModel                     = NULL;
1825                 std::vector<int> lstB           = getBlackBoxes();                              
1826                 for(j = 0;j<(int)lstB.size();j++)
1827                 {
1828                         idB             = lstB[j];
1829                         bMod    = (GBoxModel*)_controllers[idB]->getModel();
1830                         if(_controllers[idB]->getModel()->getBBTKName()==boxname)
1831                         {
1832                                 boxModel = bMod;
1833                         }
1834                 } // for
1835                 
1836                 if((_isComplexBox) && (boxModel==NULL))
1837                 {
1838                         std::vector<int> lstInputs = getComplexInputPorts();
1839                         for(j = 0;j<(int)lstInputs.size();j++)
1840                         {
1841                                 idB             = lstInputs[j];
1842                                 bMod    = (GBoxModel*)_controllers[idB]->getModel();
1843                                 if(_controllers[idB]->getModel()->getBBTKName()==boxname)
1844                                 {
1845                                         boxModel = bMod;
1846                                 }  
1847                         } // for
1848                         
1849                         std::vector<int> lstOutputs = getComplexOutputPorts();
1850                         for(j = 0;j<(int)lstOutputs.size();j++)
1851                         {
1852                                 int idB = lstOutputs[j];
1853                                 bMod = (GBoxModel*)_controllers[idB]->getModel();
1854                                 if(_controllers[idB]->getModel()->getBBTKName()==boxname)
1855                                 {                                               
1856                                         boxModel = bMod;
1857                                 } 
1858                         } // for
1859                         
1860                 } // complex box
1861
1862                 return boxModel;
1863         }
1864         
1865         //=========================================================================
1866
1867
1868         void wxVtkSceneManager::configGConnetion(std::string nameStartBox, std::string  nameStartPort, std::string  
1869                                                                                          nameEndBox, std::string  nameEndPort)
1870         {
1871                 printf("EED wxVtkSceneManager::configGConnetion 1 %s %s    %s %s\n", nameStartBox.c_str(), nameStartPort.c_str(), nameEndBox.c_str(), nameEndPort.c_str() );
1872                 
1873                 GBoxModel *boxModel;
1874                 GPortModel *startP      = NULL;
1875                 GPortModel *endP        = NULL;
1876                 
1877                 boxModel= findGBox(nameStartBox);
1878                 if (boxModel!=NULL)
1879                 {
1880                         startP          = boxModel->getOutputPort(nameStartPort);
1881                 }
1882
1883                 boxModel= findGBox(nameEndBox);
1884                 if (boxModel!=NULL)
1885                 {
1886                         endP            = boxModel->getInputPort(nameEndPort);
1887                 }
1888
1889                 
1890                 printf("EED wxVtkSceneManager::configGConnetion 2 %p %p \n", startP, endP );
1891
1892 /*ups5 EED Borrame
1893                 std::vector<int> lstB   = getBlackBoxes();                              
1894                 int j;
1895                 for(j = 0;j<(int)lstB.size();j++)
1896                 {
1897                         int idB = lstB[j];
1898                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1899                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1900                         {                                               
1901                                 startP = bbMod->getOutputPort(nameStartPort);
1902                         }
1903                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1904                         {
1905                                 endP = bbMod->getInputPort(nameEndPort);
1906                         }
1907                 } // for
1908  
1909                 if(_isComplexBox)
1910                 {
1911                         if(startP==NULL)
1912                         {
1913                                 std::vector<int> lstInputs = getComplexInputPorts();
1914                                 for(j = 0;j<(int)lstInputs.size();j++)
1915                                 {
1916                                         int idB                         = lstInputs[j];
1917                                         GBoxModel *cbMod        = (GBoxModel*)_controllers[idB]->getModel();
1918                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1919                                         {                                               
1920                                                 startP = cbMod->getOutputPort(nameStartPort);
1921                                         } // if 
1922                                 } // for
1923                         } // if 
1924                         
1925                         if(endP==NULL)
1926                         {
1927                                 std::vector<int> lstOutputs = getComplexOutputPorts();
1928                                 for(j = 0;j<(int)lstOutputs.size();j++)
1929                                 {
1930                                         int idB = lstOutputs[j];
1931                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1932                                         if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1933                                         {                                               
1934                                                 endP = cbMod->getInputPort(nameEndPort);
1935                                         } // if
1936                                 } // for
1937                         } // if endP
1938                 } // complex box
1939  */             
1940                 
1941                 int idCon                                       = createGConnector(startP);
1942                 _worldState                                     = NOTHING_HAPPENS;
1943                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1944                 
1945                 GConnectorModel *conMod         = (GConnectorModel*)tempp->getModel();
1946                 vtkGConnectorView *conView      = (vtkGConnectorView*)tempp->getView();
1947                 tempp->endContourCreation();
1948                 conMod->setEndPort(endP);                                       
1949                 conView->updateStartEndPoints();                
1950         }
1951
1952         //=========================================================================
1953
1954         bool wxVtkSceneManager::boxExist(std::string boxname)
1955         {
1956                 bool ok=false;
1957                 std::map<int, GObjectController*>::iterator it;
1958                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1959                 {
1960                         GObjectController *cont = it->second;           
1961                         if(cont->getModel()->getBBTKName()==boxname)
1962                         {
1963                                 ok=true;
1964                         }
1965                 }
1966                 return ok;
1967         }
1968
1969         //=========================================================================
1970
1971         std::vector<int> wxVtkSceneManager::getBlackBoxes()
1972         {
1973                 std::vector<int> vect;
1974                 std::map<int, GObjectController*>::iterator it;
1975                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1976                 {
1977                         GObjectController *cont = it->second;           
1978                         if(cont->getGObjectType()==GBLACKBOX)
1979                         {
1980                                 vect.push_back(cont->getId());
1981                         }
1982                 }
1983                 return vect;
1984         }
1985
1986         //=========================================================================
1987
1988         std::vector<int> wxVtkSceneManager::getComplexInputPorts()
1989         {
1990                 std::vector<int> vect;
1991                 std::map<int, GObjectController*>::iterator it;
1992                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1993                 {
1994                         GObjectController *cont = it->second;           
1995                         if(cont->getGObjectType()==GCOMPLEXINPUTPORT)
1996                         {
1997                                 vect.push_back(cont->getId());
1998                         }
1999                 }
2000                 return vect;
2001         }
2002
2003         //=========================================================================
2004
2005         std::vector<int> wxVtkSceneManager::getComplexOutputPorts()
2006         {
2007                 std::vector<int> vect;
2008                 std::map<int, GObjectController*>::iterator it;
2009                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2010                 {
2011                         GObjectController *cont = it->second;           
2012                         if(cont->getGObjectType()==GCOMPLEXOUTPUTPORT)
2013                         {
2014                                 vect.push_back(cont->getId());
2015                         }
2016                 }
2017                 return vect;
2018         }
2019
2020         //=========================================================================
2021
2022         std::vector<int> wxVtkSceneManager::getConnections()
2023         {
2024                 std::vector<int> vect;
2025                 std::map<int, GObjectController*>::iterator it;
2026                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2027                 {
2028                         GObjectController *cont = it->second;           
2029                         if(cont->getGObjectType()==GCONNECTOR)
2030                         {
2031                                 vect.push_back(cont->getId());
2032                         }
2033                 }
2034                 return vect;
2035         }
2036
2037         //=========================================================================
2038         
2039         bool wxVtkSceneManager::isComplexBox()
2040         {
2041                 return _isComplexBox;
2042         }
2043
2044         //=========================================================================
2045
2046         void wxVtkSceneManager::setComplexBox(bool val)
2047         {
2048                 _isComplexBox=val;
2049         }
2050  
2051         //=========================================================================
2052
2053         int wxVtkSceneManager::addObjectController(GObjectController* objController)
2054         {
2055                 //Register the controller of the new object
2056                 registerController((InteractorStyleMaracas*) objController);
2057
2058                 //Add the object to the objects list 
2059                 int newId = _contLastId;//_controllers.size();
2060                 objController->setId(newId);
2061                 _controllers[newId] = objController;
2062                 _contLastId++;
2063                 return newId;
2064         }
2065
2066         //=========================================================================
2067
2068         int wxVtkSceneManager::getNumSelectedObjects()
2069         {
2070                 return _selectedObjects.size();
2071         }
2072
2073         //=========================================================================
2074
2075         std::map<int,GObjectController*> wxVtkSceneManager::getSelectedObjects()
2076         {
2077                 std::map<int,GObjectController*> mapSelected;
2078
2079                 std::map<int, GObjectController*>::iterator it;
2080                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
2081                 {
2082                         GObjectController *cont = it->second;           
2083                         if(cont->getGObjectType()==GBLACKBOX && cont->getView()->getState()==SELECTED)
2084                         {
2085                                 mapSelected[cont->getId()]=cont;
2086                         }
2087                 }
2088
2089                 std::map<int, GObjectController*>::iterator it2;
2090                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
2091                 {
2092                         GObjectController *cont = it2->second;          
2093                         if(cont->getGObjectType()==GCONNECTOR)
2094                         {
2095                                 GConnectorModel* cmod = (GConnectorModel*)cont->getModel();
2096                                 GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2097                                 GBoxModel* startPortParentBox = cmod->getStartPort()->getParentBox();
2098
2099                                 std::map<int, GObjectController*>::iterator iterOBJ1 = mapSelected.find(startPortParentBox->getObjectId());
2100                                 std::map<int, GObjectController*>::iterator iterOBJ2 = mapSelected.find(endPortParentBox->getObjectId());
2101                                 
2102                                 if(iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end())
2103                                 {                                       
2104                                         int ID = cont->getId();
2105                                         mapSelected[ID]=cont;
2106                                 }
2107                                 
2108                         }
2109                 }
2110                 return mapSelected;
2111         }
2112
2113         //=========================================================================
2114
2115         void wxVtkSceneManager::addObjects(std::map<int,GObjectController*> objectsMap)
2116         {
2117                 
2118                 std::map<int,int> oldIdNewIdBoxes;
2119                 std::vector<int> connections;
2120
2121                 std::map<int, GObjectController*>::iterator it;         
2122                 for(it = objectsMap.begin(); it != objectsMap.end(); ++it)
2123                 {
2124                         GObjectController *cont = it->second;
2125                         int type = cont->getGObjectType();
2126
2127                         if(type==GBLACKBOX)
2128                         {
2129                                 // Copy black box
2130                                 double xInic, yInic,zInic;
2131                                 GBlackBoxModel* copyBox = (GBlackBoxModel*)cont->getModel();
2132                                 copyBox->getInicPoint(xInic,yInic,zInic);
2133                                 int idBox = createGBlackBox(0,0,copyBox->getBBTKPackage(),copyBox->getBBTKType());
2134
2135                                 int idcB = copyBox->getObjectId();
2136                                 oldIdNewIdBoxes[idcB]=idBox;
2137                                 cont = _controllers[idBox];                                                             
2138                                 GBlackBoxModel* newbox  = (GBlackBoxModel*)cont->getModel();
2139                                 newbox->setInicPoint(xInic,yInic,zInic);                                                        
2140                                 int num = newbox->getNumInputPorts();
2141                                 for(int j=0;j<num;j++)
2142                                 {
2143                                         newbox->setValueToInputPort(j,copyBox->getValueInputPort(j));
2144                                 }
2145                                 newbox->notifyObservers(_idManager);
2146                         }
2147                         else if(type==GCONNECTOR)
2148                         {
2149                                 int idCon = cont->getId();
2150                                 connections.push_back(idCon);
2151                         }
2152                         
2153                 }
2154                 
2155                 for(int i = 0 ;i<connections.size();i++)
2156                 {
2157                         int objId = connections[i];
2158                         GObjectController *cont = objectsMap[objId];                    
2159                         GConnectorModel* connectModel = (GConnectorModel*)cont->getModel();
2160                         
2161                         GPortModel* startPort = connectModel->getStartPort();
2162                         int startPortIndex = startPort->getPosInBox();
2163                         GPortModel* endPort = connectModel->getEndPort();
2164                         int endPortIndex = endPort->getPosInBox();                      
2165
2166                         GBlackBoxModel* startPortParentBox = (GBlackBoxModel*)startPort->getParentBox();
2167                         GBlackBoxModel* endPortParentBox = (GBlackBoxModel*)endPort->getParentBox();
2168                         
2169                         int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2170                         int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2171
2172                         GBlackBoxModel* newStartBox = (GBlackBoxModel*)_controllers[idNewStartBox]->getModel();
2173                         GBlackBoxModel* newEndBox = (GBlackBoxModel*)_controllers[idNewEndBox]->getModel();
2174
2175                         GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2176                         GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2177
2178                         // Creates connection 
2179                         int idCon = createGConnector(newStartPort);
2180                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2181                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2182                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2183                         tempp->endContourCreation();
2184                         conMod->setEndPort(newEndPort);                                 
2185                         conView->updateStartEndPoints();
2186                 }
2187
2188                 std::map<int, int>::iterator itIds;             
2189                 for(itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds)
2190                 {
2191                         int idOld = itIds->first;
2192                         int idNew = itIds->second;
2193
2194                         GBlackBoxModel* oldBox = (GBlackBoxModel*)objectsMap[idOld]->getModel();
2195                         GBlackBoxModel* newBox = (GBlackBoxModel*)_controllers[idNew]->getModel();
2196
2197                         std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2198                         std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2199                         std::vector<int> newInputConnections = newBox->getConnectedInputs();
2200                         std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2201
2202                         for(int k = 0; k<(int)oldInputConnections.size();k++)
2203                         {
2204                                 bool exist=false;
2205 //EED                           int toCreate=-1;
2206                                 for(int l = 0; l<(int)newInputConnections.size() && !exist;l++)
2207                                 {
2208                                         if(oldInputConnections[k]==newInputConnections[l])
2209                                         {
2210                                                 exist=true;
2211                                         }
2212                                 }
2213
2214                                 if(exist==false)
2215                                 {
2216                                         //Create complex input 
2217                                         int posInBox = oldInputConnections[k];
2218                                         GPortModel* inputPort = oldBox->getInputPort(posInBox);
2219                                         std::string inputPortName = inputPort->getBBTKName();
2220                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
2221                                         GObjectController *cont = _controllers[idInputPort];
2222                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2223                                         double xIn,yIn,zIn;
2224                                         inputPort->getInicPoint(xIn,yIn,zIn);
2225                                         yIn+=20;
2226                                         cbmod->setInicPoint(xIn,yIn,zIn);
2227                                         cbmod->notifyObservers(_idManager);
2228                                                                                                                 
2229                                         GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2230
2231                                         // Creates connection 
2232                                         int idCon = createGConnector(cbmod->getOutputPort(0));
2233                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2234                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2235                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2236                                         tempp->endContourCreation();
2237                                         conMod->setEndPort(inputPortEnd);                                       
2238                                         conView->updateStartEndPoints();
2239
2240                                 }
2241                                         
2242                         }
2243                         
2244
2245                         for(int k = 0; k<(int)oldOutputConnections.size();k++)
2246                         {
2247                                 bool exist=false;
2248 //EED                           int toCreate=-1;
2249                                 for(int l = 0; l<(int)newOutputConnections.size() && !exist;l++)
2250                                 {
2251                                         if(oldOutputConnections[k]==newOutputConnections[l])
2252                                         {
2253                                                 exist=true;
2254                                         }
2255                                 }
2256
2257                                 if(exist==false)
2258                                 {
2259                                         //Create complex output 
2260                                         int posInBox = oldOutputConnections[k];
2261                                         GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2262                                         std::string outputPortName = outputPort->getBBTKName();
2263                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2264                                         GObjectController *cont = _controllers[idOutputPort];
2265                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2266                                         double xIn,yIn,zIn;
2267                                         outputPort->getInicPoint(xIn,yIn,zIn);
2268                                         yIn-=20;
2269                                         cbmod->setInicPoint(xIn,yIn,zIn);
2270                                         cbmod->notifyObservers(_idManager);
2271                                                                                                                 
2272                                         GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2273
2274                                         // Creates connection 
2275                                         int idCon = createGConnector(outputPortEnd);
2276                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2277                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2278                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2279                                         tempp->endContourCreation();
2280                                         conMod->setEndPort(cbmod->getInputPort(0));                                     
2281                                         conView->updateStartEndPoints();
2282
2283                                 }
2284                                         
2285                         }
2286
2287                 }
2288
2289
2290         }
2291
2292         //=========================================================================     
2293
2294 }  // EO namespace bbtk
2295
2296 // EOF
2297