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