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