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