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