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