]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
Actual Version : It is possible to define, to create and to save a complex box ....
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::wxVtkSceneManager . 
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _parent = parent;
47                 _numBoxes=0;
48                 _idManager=idManager;
49                 _baseView=baseView;
50                 _startDragging=false;
51                 _isComplexBox=false;
52
53                 if( _baseView!=NULL )
54                 {
55                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
56                         configureBaseView();
57                         _worldState=NOTHING_HAPPENS;
58                         registerController(this);
59                         
60                 }
61
62         }
63
64         //=========================================================================
65
66         void wxVtkSceneManager::disconnectDrop()
67         {
68                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
69         }
70
71         //=========================================================================
72
73         wxVtkSceneManager::~wxVtkSceneManager()
74         {
75         }
76  
77         //=========================================================================
78
79         void wxVtkSceneManager::configureBaseView()
80         {
81                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
82
83                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
84
85                 // Important to activate the 2D interaction system
86                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
87                 interactorstylebaseview->SetInteractor ( iren );
88                 iren->SetInteractorStyle(interactorstylebaseview);
89                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
90                 
91                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
93                 
94                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95                 _baseView->GetRenderer()->GradientBackgroundOff();
96                 _baseView->Refresh();
97         }
98
99         //=========================================================================
100
101         int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
102         {
103                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
104                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
105
106                 int type = GBLACKBOX;
107
108                 //Create the MVC Objects
109                 
110                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
111                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
112                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
113                 
114                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
115                 
116                 //Prepares the initial model
117                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
118
119                 double xx = x;
120                 double yy = windowHeight-y;
121                 
122                 //z value is not important yet, because it is only used a parallel projection
123                 double zz = 900;
124
125                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
126                 model->setInicPoint(xx,yy,zz);
127                 
128                 _numBoxes++;
129                 std::stringstream stream;
130                 
131                 if(_numBoxes<10)
132                 {
133                         stream << "Box0" << _numBoxes;
134                 }
135                 else
136                 {
137                         stream << "Box" << _numBoxes;
138                 }
139                 std::string arraystring = stream.str();
140
141                 model->setBBTKName(arraystring);
142                 model->setBBTKType(boxType);
143                 model->setBBTKPackage(packageName);
144                                 
145                 model->addObserver(view);
146                 model->addObserver(this);
147
148                 //Iterate and create the input ports
149                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
150                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
151
152                 int i=0;
153                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
154                 {
155                         BlackBoxInputDescriptor *desc = itInput->second;
156                         createGInputPort(GINPUTPORT,i,model,desc);
157                         i++;
158                 }
159
160                 //Iterate and create the output ports
161                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
162                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
163
164                 i=0;
165                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
166                 {
167                         BlackBoxOutputDescriptor *desc = itOutput->second;
168                         createGOutputPort(GOUTPUTPORT,i,model,desc);
169                         i++;
170                 }
171
172
173                 //Associates the view with the correspondent renderer and the  model.
174                 //(NOTE: Refresh is only made by the view)
175                 view->setModel(model);
176                 view->setBaseView(_baseView);
177                 view->initVtkObjects();
178                 
179                 //Associates the controller with the correspondent model and view
180                 controller->setModelAndView(model,view);
181
182                 //Resgiter change to the observers of the actual model
183                 model->notifyObservers(_idManager);
184                 
185                 //Register the controller of the new object
186                 registerController((InteractorStyleMaracas*) controller);
187
188                 //Add the object to the objects list 
189                 int newId = _controllers.size();
190                 controller->setId(newId);
191                 _controllers[newId] = 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                 //Register the controller of the new object
246                 registerController((InteractorStyleMaracas*) controller);
247
248                 //Add the object to the objects list 
249                 int newId = _controllers.size();
250                 controller->setId(newId);
251                 _controllers[newId] = controller;
252                 return newId;
253         }
254
255         //=========================================================================
256         
257         int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
258         {
259                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
260                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
261
262                 int type = GCOMPLEXOUTPUTPORT;
263
264                 //Create the MVC Objects
265                 
266                 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
267                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
268                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
269                                 
270                 //Prepares the initial model 
271
272                 double xx = 5;
273                 double yy = windowHeight-5;
274                 
275                 //z value is not important yet, because it is only used a parallel projection
276                 double zz = 900;
277
278                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
279                 model->setInicPoint(xx,yy,zz);
280                                 
281                 model->setBBTKName(outputName);
282                 model->setBBTKType("ComplexOutputPort");
283                 model->setComplexPortType(type);
284                                 
285                 model->addObserver(view);
286                 model->addObserver(this);
287                 
288                 //create the output port                
289                 GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
290                 model->addInputPort((GPortModel*)portController->getModel());
291                 
292                 //Associates the view with the correspondent renderer and the  model.
293                 //(NOTE: Refresh is only made by the view)
294                 view->setModel(model);
295                 view->setBaseView(_baseView);
296                 view->initVtkObjects();
297                 
298                 //Associates the controller with the correspondent model and view
299                 controller->setModelAndView(model,view);
300
301                 //Resgiter change to the observers of the actual model
302                 model->notifyObservers(_idManager);
303                 
304                 //Register the controller of the new object
305                 registerController((InteractorStyleMaracas*) controller);
306
307                 //Add the object to the objects list 
308                 int newId = _controllers.size();
309                 controller->setId(newId);
310                 _controllers[newId] = controller;
311                 return newId;
312         }
313
314         //=========================================================================
315
316         int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
317         {
318                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
319                 blackBox->addInputPort((GPortModel*)portController->getModel());
320                 return portController->getId();
321         }
322
323         //=========================================================================
324
325         int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
326         {
327                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
328                 blackBox->addOutputPort((GPortModel*)portController->getModel());
329                 return portController->getId();
330         }
331
332         //=========================================================================
333
334         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
335         {
336                 int type = GPORT;
337
338                 //Create the MVC Objects
339                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
340                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
341                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
342
343                 model->registerInBox(blackBox,portType, posInBox);
344                 
345                 model->setBBTKType(bbtkType);
346                 model->setBBTKName(bbtkName);
347
348                 model->addObserver(view);
349                 model->addObserver(this);
350
351                 //Associates the view with the correspondent renderer and the  model.
352                 //(NOTE: Refresh is only made by the view)
353                 view->setModel(model);
354                 view->setBaseView(_baseView);
355                 view->initVtkObjects();
356                 
357                 //Associates the controller with the correspondent model and view
358                 controller->setModelAndView(model,view);
359
360                 model->notifyObservers(_idManager);
361
362                 //Register the controller of the new object
363                 registerController((InteractorStyleMaracas*) controller);
364                 
365                 int newId = _controllers.size();
366                 controller->setId(newId);
367                 _controllers[newId] = controller;
368
369                 return (GPortController*)controller;
370         }
371
372         //=========================================================================
373
374         int wxVtkSceneManager::createGConnector(GPortModel* startPort)
375         {
376                 int type = GCONNECTOR;
377
378                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
379                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
380                 manualContourModel* manContourModel     = new manualContourModel();
381
382                 GConnectorController* connectorcontroller = new GConnectorController();                         
383                 GConnectorModel* connectorModel = new GConnectorModel();
384                 vtkGConnectorView* connectorView = new vtkGConnectorView();
385                 connectorModel->setGObjectType(type);
386
387                 manContourModel->SetCloseContour(false);
388                 connectorModel->setStartPort(startPort);
389
390                 manContourView->SetModel( manContourModel );
391                 manContourView->SetWxVtkBaseView( _baseView );
392                 manContourView->SetRange( 0.5 );
393                 manContourView->SetZ( 900 );
394
395                 manContourView->SetColorNormalContour(0, 0, 1);
396                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
397                 manContourView->SetColorSelectContour(1, 0.8, 0);
398                 manContourView->SetWidthLine(1);
399                 manContourView->SetShowText(false);
400
401                 manContourControl->SetModelView( manContourModel , manContourView );
402                 
403                 manContourControl->CreateNewManualContour();
404
405                 manContourView->RefreshContour();
406
407
408                 double x,y,z;
409                 connectorModel->getInicPoint(x,y,z);
410
411                 manContourControl->SetState(1);
412                 manContourModel->SetCloseContour(false);
413                 
414                 manContourModel->AddPoint(x,y,z);
415                 manContourView->AddPoint();
416
417                 manContourModel->AddPoint(x,y,z);
418                 manContourView->AddPoint();
419
420                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
421                 manContourControl->_bakIdPoint=bak;
422                 manContourView->Refresh();
423
424                 manContourControl->SetMoving( false );
425
426                 registerController((InteractorStyleMaracas*) connectorcontroller);
427
428                 connectorcontroller->setManualContourController(manContourControl);             
429                 connectorModel->setManualContourModel(manContourModel);
430                 connectorView->setManualContourView(manContourView);
431                 connectorView->setModel(connectorModel);
432                 connectorView->setBaseView(_baseView);
433                 connectorcontroller->setModelAndView(connectorModel,connectorView);
434
435                 connectorModel->addObserver(connectorView);
436                 connectorModel->addObserver(this);
437
438                 int newId = _controllers.size();
439                 connectorcontroller->setId(newId);
440                 _controllers[newId] = connectorcontroller;
441
442                 return newId;
443         }
444
445         //=========================================================================
446
447         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
448         {
449                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
450                 baseViewControlManager->AddInteractorStyleMaracas( param );
451         }
452
453         //=========================================================================
454
455         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
456         {
457                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
458                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
459         }
460
461         //=========================================================================
462
463         vtkRenderer* wxVtkSceneManager::getRenderer()
464         {
465                 return _baseView->GetRenderer();
466         }
467
468         //=========================================================================
469
470         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
471         {
472                 return _baseView->GetRenWin();
473         }
474         //=========================================================================
475
476                 
477         void wxVtkSceneManager::update(int idController,int command)
478         {
479                 if(command != NO_COMMAND)
480                 {
481                         if(command == ADD_TO_SELECTED)
482                         {
483                                 GObjectController* cont = _controllers[idController];
484
485                                 bool foundID=false;
486                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
487                                 {
488                                         int id = _selectedObjects[i];
489                                         if(id==idController)
490                                         {
491                                                 foundID = true;
492                                         }
493                                 }
494                                 if(!foundID)
495                                 {
496                                         int id = idController;
497                                         _selectedObjects.push_back(id);
498                                 }
499
500                         }
501                         else if(command == INIT_CREATION_CONTOUR)
502                         {       
503                                 _worldState = INIT_CREATION_CONTOUR;
504                                 GObjectController* cont = _controllers[idController];
505                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
506                                 
507
508                                 // The last one is the controller of the connector
509                                 for(int i=0;i<_controllers.size();i++)
510                                 {
511                                         GObjectController* cont = _controllers[i];
512                                         if(cont->getGObjectType() == GPORT )
513                                         {
514                                                 GPortModel* port = (GPortModel*)cont->getModel();
515                                                 if(port->getPortType()==GINPUTPORT)
516                                                 {
517                                                         cont->SetActive(true);
518                                                 }
519                                                 else
520                                                 {
521                                                         cont->getView()->setState(NOTHING_HAPPENS);
522                                                         cont->getModel()->notifyObservers(_idManager);
523                                                         cont->SetActive(false);
524                                                 }
525                                         }
526                                         else
527                                         {
528                                                 cont->getView()->setState(NOTHING_HAPPENS);
529                                                 cont->getModel()->notifyObservers(_idManager);
530                                                 cont->SetActive(false);
531                                         }                               
532                                 }
533
534                                 _selectedObjects.clear();
535
536                                 createGConnector(startOutputPort);
537
538                         }
539                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
540                         {                               
541                                 _worldState = NOTHING_HAPPENS;
542                                 int id = _controllers.size()-1;
543                                 GObjectController* cont = _controllers[id];                     
544                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
545
546                                 GObjectController* finPort = _controllers[idController];
547                                 if(finPort->getGObjectType() == GPORT)
548                                 {
549                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
550                                         modelContour->setEndPort(modelPort);
551                                 }                       
552
553                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
554                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
555                                 connView->Refresh();
556
557                                 for(int i=0;i<_controllers.size();i++)
558                                 {
559                                         GObjectController* cont = _controllers[i];
560                                         if(cont->getView()!=NULL)
561                                         {
562                                                 cont->getView()->setState(NOTHING_HAPPENS);
563                                                 cont->getModel()->notifyObservers(_idManager);
564                                         }
565                                         cont->SetActive(true);                                                          
566                                 }
567                         }
568                         
569                 }
570         }
571
572         //=========================================================================
573
574         bool wxVtkSceneManager::OnMouseMove()
575         {
576                 int X,Y;
577                 wxVTKRenderWindowInteractor *wxVTKiren;
578                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
579                 wxVTKiren->GetEventPosition(X,Y);
580         
581                 
582                 if(_worldState == DRAG_OBJECTS)
583                 {                       
584                         for (int i=0; i<_selectedObjects.size(); i++)
585                         {
586                                 int id = _selectedObjects[i];
587                                 GObjectController* cont = _controllers[id];
588                                 if(_startDragging)
589                                 {
590                                         cont->getView()->setStartDragging(true);
591                                 }
592                                 cont->getView()->moveObject(X,Y);
593                                 cont->getView()->setState(DRAG);
594                                 cont->getModel()->notifyObservers(_idManager);
595                         }
596
597                         std::map<int, GObjectController*>::iterator it;
598
599                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
600                         {
601                                 GObjectController *desc = it->second;
602                                 if(desc->getGObjectType()==GCONNECTOR)
603                                 {
604                                         vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
605                                         vconn->updateStartEndPoints();
606                                 }
607                         }
608                         
609                         _startDragging=false;
610
611                 }
612                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
613                 {
614                         std::map<int, GObjectController*>::iterator it;
615
616                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
617                         {
618                                 GObjectController *desc = it->second;
619                                 int type = desc->getGObjectType();
620                                 int state = desc->getView()->getState();
621                                 
622                                 if(state == HIGHLIGHTED){
623
624                                         updateStatusBar(desc->getStatusText());
625                                         if(type==GBLACKBOX)
626                                         {
627                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
628                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
629                                         }
630                                 }
631                         }
632                 }
633                 
634                 
635
636
637                 return true;
638         }
639
640         //=========================================================================
641         
642         bool wxVtkSceneManager::OnLeftButtonDown()
643         {
644                 if(_worldState==INIT_CREATION_CONTOUR)
645                 {
646                         bool isOverPort=false;
647                         std::map<int, GObjectController*>::iterator it;
648                         for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
649                         {
650                                 GObjectController *desc = it->second;
651                                 if(desc->getGObjectType()==GPORT)
652                                 {
653                                         GPortModel* portmod=(GPortModel*)desc->getModel();
654                                         vtkGObjectView* portView=desc->getView();
655                                         if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
656                                         {
657                                                 isOverPort=true;
658                                         }
659                                 }
660                         }
661                         
662                         if(isOverPort==false)
663                         {
664                                 _worldState=NOTHING_HAPPENS;
665                                 int lastId = _controllers.size()-1;
666                                 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
667                                 connector->removeFromScene();
668                                 unregisterController(connector);
669                                 _controllers.erase(lastId);                     
670
671                                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
672                                 {
673                                         GObjectController *desc = it->second;
674                                         desc->SetActive(true);
675                                         desc->getView()->setState(NOTHING_HAPPENS);
676                                         desc->getModel()->notifyObservers(_idManager);
677                                 }
678                         }
679                 }
680                 
681                 if(_selectedObjects.size()!=0)
682                 {
683                         _worldState = DRAG_OBJECTS;
684                         _startDragging = true;
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(DRAG);
691                                 cont->getModel()->notifyObservers(_idManager);
692                         }
693                 }
694                 
695                 
696                 
697                 return true;
698         }
699
700         //=========================================================================
701         
702         bool wxVtkSceneManager::OnLeftButtonUp()
703         {
704                 if(_worldState == DRAG_OBJECTS)
705                 {
706                         _worldState = NOTHING_HAPPENS;
707
708                         for (int i = 0; i < _selectedObjects.size(); i++)
709                         {
710                                 int id = _selectedObjects[i];
711                                 GObjectController* cont = _controllers[id];
712                                 cont->getView()->setState(SELECTED);
713                                 cont->getModel()->notifyObservers(_idManager);
714                         }
715                 }
716                 return true;
717         }
718
719         //=========================================================================
720
721         bool wxVtkSceneManager::OnRightButtonUp()
722         {
723                 if(_worldState==INIT_CREATION_CONTOUR)
724                 {
725                         _worldState=NOTHING_HAPPENS;
726                         int lastId = _controllers.size()-1;
727                         GConnectorController *connector = (GConnectorController*)_controllers[lastId];
728                         connector->removeFromScene();
729                         unregisterController(connector);
730                         _controllers.erase(lastId);                     
731
732                         std::map<int, GObjectController*>::iterator it;
733                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
734                         {
735                                 GObjectController *desc = it->second;
736                                 desc->SetActive(true);
737                                 desc->getView()->setState(NOTHING_HAPPENS);
738                                 desc->getModel()->notifyObservers(_idManager);
739                         }
740                 }
741
742                         
743                 for (int i = 0; i < _selectedObjects.size(); i++)
744                 {
745                         int id = _selectedObjects[i];
746                         GObjectController* cont = _controllers[id];
747                         cont->SetActive(true);
748                         cont->getView()->setState(NOTHING_HAPPENS);
749                         cont->getModel()->notifyObservers(_idManager);
750                 }
751
752                 _selectedObjects.clear();
753
754                 return true;
755         }
756
757         //=========================================================================
758
759         bool wxVtkSceneManager::OnLeftDClick()
760         {
761                 int X,Y;
762                 wxVTKRenderWindowInteractor *wxVTKiren;
763                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
764                 wxVTKiren->GetEventPosition(X,Y);
765
766                 std::map<int, GObjectController*>::iterator it;
767
768                 bool clickOnObject = false;
769
770                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
771                 {
772                         GObjectController *cont = it->second;
773                         int type = cont->getGObjectType();
774                         
775                         if(cont->getView()->isPointInside(X,Y))
776                         {
777                                 if(type==GBLACKBOX)
778                                 {
779                                         for (int i=0; i<_selectedObjects.size(); i++)
780                                         {
781                                                 int id = _selectedObjects[i];
782                                                 GObjectController* control = _controllers[id];
783                                                 control->getView()->setState(NOTHING_HAPPENS);
784                                         }
785                                         _selectedObjects.clear();
786
787                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
788                                         _parent->editBlackBox(bbmodel);
789                                 }
790                                 clickOnObject = true;                   
791                         }
792                 }
793
794                 if(clickOnObject==false)
795                 {
796                         //_parent->editDiagramParameters(this);
797                 }
798
799                 return true;
800         }
801         
802         //=========================================================================
803
804         bool wxVtkSceneManager::OnChar()
805         {       
806                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
807                 
808                 // KeyCode 127 : Delete Key
809                 // KeyCode 8 : Backspace Key
810                 if(keyCode == 8 || keyCode == 127)
811                 {
812                         if(_selectedObjects.size()>0)
813                         {
814                                 for(int i=0;i<_selectedObjects.size();i++)
815                                 {
816                                         int id = _selectedObjects[i];
817                                         deleteObject(id);
818                                 }
819                                 _selectedObjects.clear();
820                         }
821                 }
822
823                 return true;
824         }
825
826         //=========================================================================
827
828         void wxVtkSceneManager::deleteObject(int id)
829         {
830                 GObjectController *control = _controllers[id];
831                 std::vector<int> controllersToRemove;
832
833                 if(control->getGObjectType()==GBLACKBOX)
834                 {
835                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
836                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
837                         
838                         bool boxConnected = false;
839
840                         // Add box input controllers to be removed
841                         for(int i = 0;i<inputs.size();i++)
842                         {
843                                 controllersToRemove.push_back(inputs[i]->getObjectId());
844                                 if(inputs[i]->isConnected())
845                                 {
846                                         boxConnected = true;
847                                 }
848                         }
849
850                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
851
852                         // Add box output controllers to be removed
853                         for(int i = 0;i<outputs.size();i++)
854                         {
855                                 controllersToRemove.push_back(outputs[i]->getObjectId());
856                                 if(outputs[i]->isConnected())
857                                 {
858                                         boxConnected = true;
859                                 }
860                         }
861
862                         // Add connection controllers to be removed
863                         std::map<int, GObjectController*>::iterator it;
864                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
865                         {
866                                 GObjectController *cont = it->second;
867                                 int type = cont->getGObjectType();
868                                 if(type==GCONNECTOR)
869                                 {
870                                         GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
871                                         if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
872                                         {
873                                                 controllersToRemove.push_back(conMod->getObjectId());
874                                         }
875                                         if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
876                                         {
877                                                 controllersToRemove.push_back(conMod->getObjectId());
878                                         }
879                                 }
880                         }
881
882                         // Add box controller to be removed
883                         controllersToRemove.push_back(bbmod->getObjectId());
884                 }
885                 else if(control->getGObjectType()==GCONNECTOR)
886                 {                       
887                         GConnectorModel *conMod = (GConnectorModel*)control->getModel();
888                         controllersToRemove.push_back(conMod->getObjectId());
889                 }
890
891                 for(int i = 0;i<controllersToRemove.size();i++)
892                 {
893                         int id = controllersToRemove[i];
894                         GObjectController *cont = _controllers[id];                     
895                         cont->removeFromScene();
896                         unregisterController((InteractorStyleMaracas*)cont);                    
897                         _controllers.erase(id);
898                 }
899
900
901         }
902
903         //=========================================================================
904
905         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
906         {
907                 _parent->displayBlackBoxInfo(packageName,boxName);
908         }
909
910         //=========================================================================
911
912         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
913         {
914                 _parent->updateStatusBar(textStatus);
915         }
916
917         //=========================================================================
918
919         std::string wxVtkSceneManager::getDiagramBBS()
920         {
921                 bool existsExec=false;
922
923                 std::vector<std::string> packages;
924                 std::vector<int> boxes;
925                 std::vector<int> connections;
926                 std::vector<int> execBoxes;
927
928                 std::map<int, GObjectController*>::iterator it;
929
930                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
931                 {
932                         GObjectController *desc = it->second;
933                         int type = desc->getGObjectType();
934
935                         if(type==GBLACKBOX)
936                         {
937                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
938                                 
939                                 std::string pkg = mod->getBBTKPackage();
940                                 bool existsPkg = false;
941                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
942                                 {
943                                         if(packages[t]==pkg)
944                                         {
945                                                 existsPkg=true;
946                                         }
947                                 }
948                                 if(!existsPkg)
949                                 {
950                                         packages.push_back(pkg);
951                                 }
952
953
954                                 boxes.push_back(it->first);
955                                 if(mod->isExecutable())
956                                 {
957                                         execBoxes.push_back(it->first);
958                                         existsExec=true;
959                                 }
960                         }
961                         else if(type==GCONNECTOR)
962                         {
963                                 connections.push_back(it->first);
964                         }
965                 }
966
967                 std::string script = "";
968                 script+="# BBTK GEditor Script\n";
969                 script+="# ----------------------\n";
970                 if(existsExec)
971                 {
972                         int i;
973                         for(i = 0; i<packages.size();i++)
974                         {
975                                 script+="include ";
976                                 script+=packages[i];
977                                 script+="\n";
978                         }
979
980                         script+="include std\n";
981
982                         for(i = 0; i<boxes.size();i++)
983                         {
984                                 script+="new ";
985                                 int id = boxes[i];
986                                 GObjectController *control = _controllers[id];
987                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
988
989                                 script+=model->getBBTKType();
990                                 script+=" ";
991                                 script+=model->getBBTKName();
992                                 script+="\n";
993
994                                 std::vector<GPortModel*> inputs = model->getInputPorts();
995                                 for(int j = 0; j<inputs.size();j++)
996                                 {
997                                         GPortModel* inputPort = inputs[j];
998                                         if(inputPort->isValueSet())
999                                         {
1000                                                 script+="set ";
1001                                                 script+=model->getBBTKName();
1002                                                 script+=".";
1003                                                 script+=inputPort->getBBTKName();
1004                                                 script+=" ";
1005                                                 script+=inputPort->getValue();
1006                                                 script+="\n";
1007                                         }
1008                                 }
1009
1010                         }
1011
1012                         for(i = 0; i<connections.size();i++)
1013                         {
1014                                 script+="connect ";
1015                                 int id = connections[i];
1016                                 GObjectController *control = _controllers[id];
1017                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1018
1019                                 //Start Connection info
1020                                 GPortModel *start = model->getStartPort();
1021                                 script+=start->getParentBox()->getBBTKName();
1022                                 script+=".";
1023                                 script+=start->getBBTKName();
1024
1025                                 script+=" ";
1026
1027                                 //End Connection info
1028                                 GPortModel *end = model->getEndPort();
1029                                 script+=end->getParentBox()->getBBTKName();
1030                                 script+=".";
1031                                 script+=end->getBBTKName();
1032
1033                                 script+="\n";
1034                         }
1035
1036                         for(i = 0; i<execBoxes.size();i++)
1037                         {
1038                                 script+="exec ";
1039                                 int id = execBoxes[i];
1040                                 GObjectController *control = _controllers[id];
1041                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1042
1043                                 script+=model->getBBTKName();
1044                                 script+="\n";
1045                         }
1046
1047                 }
1048
1049                 return script;
1050         }
1051
1052         //=========================================================================
1053
1054         std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
1055         {
1056
1057                 std::vector<std::string> packages;
1058                 std::vector<int> boxes;
1059                 std::vector<int> connections;
1060                 std::vector<int> execBoxes;
1061
1062                 std::map<int, GObjectController*>::iterator it;
1063
1064                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1065                 {
1066                         GObjectController *desc = it->second;
1067                         int type = desc->getGObjectType();
1068
1069                         if(type==GBLACKBOX)
1070                         {
1071                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
1072                                 
1073                                 std::string pkg = mod->getBBTKPackage();
1074                                 bool existsPkg = false;
1075                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
1076                                 {
1077                                         if(packages[t]==pkg)
1078                                         {
1079                                                 existsPkg=true;
1080                                         }
1081                                 }
1082                                 if(!existsPkg)
1083                                 {
1084                                         packages.push_back(pkg);
1085                                 }
1086
1087
1088                                 boxes.push_back(it->first);
1089                                 if(mod->isExecutable())
1090                                 {
1091                                         execBoxes.push_back(it->first);
1092                                 }
1093                         }
1094                         else if(type==GCONNECTOR)
1095                         {
1096                                 connections.push_back(it->first);
1097                         }
1098                 }
1099
1100                 std::string script = "";
1101                 int i;
1102                 for(i = 0; i<packages.size();i++)
1103                 {
1104                         script+="include ";
1105                         script+=packages[i];
1106                         script+="\n";
1107                 }
1108
1109                 script+="include std\n";
1110
1111                 // Definition of a complex box
1112                 script+="define ";
1113                 script+=cbName;
1114                 script+="\n";
1115
1116                 script+="author \" ";
1117                 script+=cbAuthor;
1118                 script+="\"\n";
1119
1120                 script+="description \" ";
1121                 script+=cbDescription;
1122                 script+="\"\n";
1123
1124                 // Create boxes
1125                 for(i = 0; i<boxes.size();i++)
1126                 {
1127                         script+="new ";
1128                         int id = boxes[i];
1129                         GObjectController *control = _controllers[id];
1130                         GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1131
1132                         script+=model->getBBTKType();
1133                         script+=" ";
1134                         script+=model->getBBTKName();
1135                         script+="\n";
1136
1137                                 std::vector<GPortModel*> inputs = model->getInputPorts();
1138                                 for(int j = 0; j<inputs.size();j++)
1139                                 {
1140                                         GPortModel* inputPort = inputs[j];
1141                                         if(inputPort->isValueSet())
1142                                         {
1143                                                 script+="set ";
1144                                                 script+=model->getBBTKName();
1145                                                 script+=".";
1146                                                 script+=inputPort->getBBTKName();
1147                                                 script+=" ";
1148                                                 script+=inputPort->getValue();
1149                                                 script+="\n";
1150                                         }
1151                                 }
1152
1153                         }
1154
1155                         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1156
1157                         std::string complexInputs="";
1158                         std::string complexOutputs="";
1159
1160                         for(i = 0; i<connections.size();i++)
1161                         {
1162                                 int id = connections[i];
1163                                 GObjectController *control = _controllers[id];
1164                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1165
1166                                 //Connection info
1167                                 GPortModel *start = model->getStartPort();
1168                                 GBoxModel *startBox =start->getParentBox();
1169
1170                                 GPortModel *end = model->getEndPort();
1171                                 GBoxModel *endBox =end->getParentBox();
1172
1173                                 if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
1174                                 {
1175                                         complexInputs+="input ";
1176                                         complexInputs+=startBox->getBBTKName();
1177
1178                                         complexInputs+=" ";
1179                                         complexInputs+=endBox->getBBTKName();
1180                                         complexInputs+=".";
1181                                         complexInputs+=end->getBBTKName();
1182
1183                                         complexInputs+=" ";
1184                                         complexInputs+="\" \"";
1185
1186                                         complexInputs+="\n";
1187                                 }
1188                                 else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
1189                                 {
1190                                         complexOutputs+="output ";
1191                                         complexOutputs+=endBox->getBBTKName();
1192
1193                                         complexOutputs+=" ";
1194                                         complexOutputs+=startBox->getBBTKName();
1195                                         complexOutputs+=".";
1196                                         complexOutputs+=start->getBBTKName();
1197
1198                                         complexOutputs+=" ";
1199                                         complexOutputs+="\" \"";
1200
1201                                         complexOutputs+="\n";
1202                                 }
1203                                 else
1204                                 {
1205                                         script+="connect ";
1206                                         script+=startBox->getBBTKName();
1207                                         script+=".";
1208                                         script+=start->getBBTKName();
1209
1210                                         script+=" ";
1211
1212                                         //End Connection info
1213                                         script+=endBox->getBBTKName();
1214                                         script+=".";
1215                                         script+=end->getBBTKName();
1216
1217                                         script+="\n";
1218                                 }
1219                         }
1220
1221                         for(i = 0; i<execBoxes.size();i++)
1222                         {
1223                                 script+="exec ";
1224                                 int id = execBoxes[i];
1225                                 GObjectController *control = _controllers[id];
1226                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1227
1228                                 script+=model->getBBTKName();
1229                                 script+="\n";
1230                         }
1231
1232                         script+="\n";
1233                         script+="# Complex input ports\n";
1234                         script+=complexInputs;
1235
1236                         script+="\n";
1237                         script+="# Complex output ports\n";
1238                         script+=complexOutputs;
1239
1240                         script+="\n";
1241                         script+="endefine";
1242                         script+="\n";           
1243
1244                 return script;
1245         }
1246
1247         //=========================================================================
1248
1249         void wxVtkSceneManager::deleteAllBoxes()
1250         {               
1251                 std::map<int, GObjectController*>::iterator it;
1252                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1253                 {
1254                         GObjectController *cont = it->second;           
1255                         cont->removeFromScene();
1256                         unregisterController((InteractorStyleMaracas*)cont);
1257                 }
1258                 _selectedObjects.clear();
1259                 _controllers.clear();   
1260                 refreshScene();
1261         }
1262
1263         //=========================================================================
1264
1265         void wxVtkSceneManager::refreshScene()
1266         {
1267                 _baseView->RefreshView();
1268         }
1269
1270         //=========================================================================
1271         
1272         void wxVtkSceneManager::centerView()
1273         {
1274                 double temp[3];
1275                 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1276                 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1277                 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1278                 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1279                 _baseView->RefreshView();
1280
1281         }
1282
1283         //=========================================================================
1284
1285         void wxVtkSceneManager::saveDiagram(std::string &content)
1286         {
1287                 //Print boxes
1288                 char buffer [50];
1289                 std::vector<int> boxes = getBoxes();
1290                 int bsize = boxes.size();
1291                 content+="BOXES:";
1292                 sprintf (buffer, "%d", bsize);
1293                 content+=buffer;
1294                 content+="\n";
1295                 
1296                 for(int i = 0;i<bsize;i++)
1297                 {
1298                         int id = boxes[i];
1299                         GObjectController *cont = _controllers[id];
1300                         cont->getModel()->save(content);
1301                 }
1302
1303                 //Print connections
1304                 std::vector<int> connections = getConnections();
1305                 int csize = connections.size();
1306                 content+="CONNECTIONS:";
1307                 sprintf (buffer, "%d", csize);
1308                 content+=buffer;
1309                 content+="\n";
1310                 
1311                 for(int i = 0;i<csize;i++)
1312                 {
1313                         int id = connections[i];
1314                         GObjectController *cont = _controllers[id];
1315                         cont->getModel()->save(content);
1316                 }
1317
1318         }
1319
1320         //=========================================================================
1321
1322         void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1323         {
1324
1325                 std::string line="";    
1326                 char delims[] = ":";
1327                 char *result = NULL;
1328         getline(inputStream,line);
1329
1330                 bool start = false;
1331                 while ( !inputStream.eof() ) 
1332                 {
1333                         if(line=="" || line[0]=='#')
1334                         {
1335                                 getline(inputStream,line);
1336                         }
1337                         else if(line=="APP_START")
1338                         {
1339                                 start = true;
1340                                 break;
1341                         }
1342                 }
1343
1344                 if(start)
1345                 {
1346                         //----------
1347                         getline(inputStream,line);//BOXES:num
1348                         char boxes[9];
1349                         strcpy( boxes, line.c_str() );
1350                         result = strtok( boxes, delims );
1351                         result = strtok( NULL, delims );
1352                                         
1353                         int numBoxes;
1354                         std::istringstream is(result);
1355                         is >> numBoxes;
1356                         
1357                         for(int i = 0;i<numBoxes;i++)
1358                         {
1359                                 //----------
1360                                 getline(inputStream,line);//BOX
1361                                 getline(inputStream,line);//package:type:name
1362                                 char box[100];
1363                                 strcpy( box, line.c_str() );
1364                                 result = strtok( box, delims );//package
1365                                 std::string package(result);
1366                                 result = strtok( NULL, delims );//type
1367                                 std::string type(result);
1368                                 result = strtok( NULL, delims );//name
1369                                 std::string name(result);
1370
1371                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1372                                 char exec[15];
1373                                 strcpy( exec, line.c_str() );
1374                                 result = strtok( exec, delims );//ISEXEC                                
1375                                 result = strtok( NULL, delims );//TRUE|FALSE
1376                                 std::string isExec(result);
1377
1378                                 //----------
1379                                 getline(inputStream,line);//xInic:yInic:zInic
1380                                 char coord[80];
1381                                 strcpy( coord, line.c_str() );
1382                                 result = strtok( coord, delims );//xInic
1383                                 std::string xInic(result);
1384                                 result = strtok( NULL, delims );//yInic
1385                                 std::string yInic(result);
1386                                 result = strtok( NULL, delims );//zInic
1387                                 std::string zInic(result);
1388
1389                                 double xIn, yIn, zIn;
1390                                 std::istringstream xSt(xInic);
1391                                 xSt >> xIn;
1392                                 std::istringstream ySt(yInic);
1393                                 ySt >> yIn;
1394                                 std::istringstream zSt(zInic);
1395                                 zSt >> zIn;
1396                                                 
1397                                 //----------
1398                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1399                                 strcpy( coord, line.c_str() );
1400                                 result = strtok( coord, delims );//xEnd
1401                                 std::string xEnd(result);
1402                                 result = strtok( NULL, delims );//yEnd
1403                                 std::string yEnd(result);
1404                                 result = strtok( NULL, delims );//zEnd
1405                                 std::string zEnd(result);
1406
1407                                 double xEn, yEn, zEn;
1408                                 std::istringstream xEt(xEnd);
1409                                 xEt >> xEn;
1410                                 std::istringstream yEt(yEnd);
1411                                 yEt >> yEn;
1412                                 std::istringstream zEt(zEnd);
1413                                 zEt >> zEn;
1414
1415                                 int idBox = createGBlackBox(0,0,package,type);
1416                                 GObjectController *cont = _controllers[idBox];
1417                                 GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
1418                                 bbmod->setBBTKName(name);
1419                                 bbmod->setInicPoint(xIn,yIn,zIn);
1420                                 bbmod->setFinalPoint(xEn,yEn,zEn);
1421                                 if(isExec=="TRUE")
1422                                 {
1423                                         bbmod->setExecutable(true);
1424                                 }
1425                                 else if(isExec=="FALSE")
1426                                 {
1427                                         bbmod->setExecutable(false);
1428                                 }
1429                                                         
1430                                 //----------
1431                                 getline(inputStream,line);//PORT o FIN_BOX
1432                                 std::string port=line.substr(0,4);
1433                                 while(port=="PORT")
1434                                 {
1435                                         getline(inputStream,line);//name:value
1436                                         char poort[20];
1437                                         strcpy( poort, line.c_str() );
1438                                         result = strtok( poort, delims );//name
1439                                         std::string name(result);
1440                                         result = strtok( NULL, delims );//value
1441                                         std::string value(result);
1442
1443                                         bbmod->setValueToInput(name,value);                                     
1444
1445                                         getline(inputStream,line);//PORT o FIN_BOX
1446                                         port=line.substr(0,4);
1447                                 }               
1448
1449                                 bbmod->notifyObservers(_idManager);
1450                         }
1451
1452                         /// CONNECTIONS
1453                         //----------
1454                         getline(inputStream,line);//CONNECTIONS:num
1455                         char conns[15];
1456                         strcpy( conns, line.c_str() );
1457                         result = strtok( conns, delims );
1458                         result = strtok( NULL, delims );
1459                                         
1460                         int numConns;
1461                         std::istringstream isCons(result);
1462                         isCons >> numConns;
1463                                         
1464                         for(int i = 0;i<numConns;i++)
1465                         {
1466                                 //----------
1467                                 getline(inputStream,line);//CONNECTION
1468                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1469
1470                                 char connec[100];
1471                                 strcpy( connec, line.c_str() );
1472                                 result = strtok( connec, delims );
1473                                 std::string nameStartBox(result);
1474                                 result = strtok( NULL, delims );
1475                                 std::string nameStartPort(result);
1476                                 result = strtok( NULL, delims );
1477                                 std::string nameEndBox(result);
1478                                 result = strtok( NULL, delims );
1479                                 std::string nameEndPort(result);
1480
1481                                 std::vector<int> lstB = getBoxes();
1482                                 
1483                                 GPortModel *startP=NULL;
1484                                 GPortModel *endP=NULL;                          
1485                                 for(int j = 0;j<lstB.size();j++)
1486                                 {
1487                                         int idB = lstB[j];
1488                                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1489                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1490                                         {                                               
1491                                                 startP = bbMod->getOutputPort(nameStartPort);
1492                                         }
1493                                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1494                                         {
1495                                                 endP = bbMod->getInputPort(nameEndPort);
1496                                         }
1497                                 }
1498                                 
1499                                 int idCon = createGConnector(startP);
1500                                 _worldState = NOTHING_HAPPENS;
1501                                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1502
1503                                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1504                                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1505                                 tempp->endContourCreation();
1506                                 conMod->setEndPort(endP);                                       
1507                                 conView->updateStartEndPoints();
1508
1509                         }
1510         }
1511
1512         }
1513
1514         //=========================================================================
1515
1516         std::vector<int> wxVtkSceneManager::getBoxes()
1517         {
1518                 std::vector<int> vect;
1519                 std::map<int, GObjectController*>::iterator it;
1520                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1521                 {
1522                         GObjectController *cont = it->second;           
1523                         if(cont->getGObjectType()==GBLACKBOX)
1524                         {
1525                                 vect.push_back(cont->getId());
1526                         }
1527                 }
1528                 return vect;
1529         }
1530
1531         //=========================================================================
1532
1533         std::vector<int> wxVtkSceneManager::getConnections()
1534         {
1535                 std::vector<int> vect;
1536                 std::map<int, GObjectController*>::iterator it;
1537                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1538                 {
1539                         GObjectController *cont = it->second;           
1540                         if(cont->getGObjectType()==GCONNECTOR)
1541                         {
1542                                 vect.push_back(cont->getId());
1543                         }
1544                 }
1545                 return vect;
1546         }
1547
1548         //=========================================================================
1549         
1550         bool wxVtkSceneManager::isComplexBox()
1551         {
1552                 return _isComplexBox;
1553         }
1554
1555         //=========================================================================
1556
1557         void wxVtkSceneManager::setComplexBox(bool val)
1558         {
1559                 _isComplexBox=val;
1560         }
1561
1562         //=========================================================================
1563
1564 }  // EO namespace bbtk
1565
1566 // EOF
1567