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