]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
387924595fad242bf42b9a9a4ff340acb434dbd8
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxVtkSceneManager.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------  
24 */
25
26 /*=========================================================================
27  Program:   bbtk
28  Module:    $RCSfile$
29  Language:  C++
30  Date:      $Date$
31  Version:   $Revision$
32  =========================================================================*/
33
34 /* ---------------------------------------------------------------------
35
36  * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
37  * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
38  *
39  *  This software is governed by the CeCILL-B license under French law and
40  *  abiding by the rules of distribution of free software. You can  use,
41  *  modify and/ or redistribute the software under the terms of the CeCILL-B
42  *  license as circulated by CEA, CNRS and INRIA at the following URL
43  *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
44  *  or in the file LICENSE.txt.
45  *
46  *  As a counterpart to the access to the source code and  rights to copy,
47  *  modify and redistribute granted by the license, users are provided only
48  *  with a limited warranty  and the software's author,  the holder of the
49  *  economic rights,  and the successive licensors  have only  limited
50  *  liability.
51  *
52  *  The fact that you are presently reading this means that you have had
53  *  knowledge of the CeCILL-B license and that you accept its terms.
54  * ------------------------------------------------------------------------ */
55
56 /**
57  *  \file
58  *  \brief Class bbtk::wxVtkSceneManager .
59  */
60
61 #include "wxVtkSceneManager.h"
62
63 #include <vtkPolygon.h>
64 #include <vtkUnstructuredGrid.h>
65
66 namespace bbtk {
67
68 #define POORT_MAX_LINE_SIZE_INTERPRETER 1500
69
70 //=========================================================================
71         
72 // EED 15 oct 2012  wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView, int idManager,Factory::Pointer bbtkfactory) 
73         
74 wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView, int idManager,Factory::Pointer bbtkfactory) 
75 {
76         _cbName                 = "ComplexBoxName";
77         _cbPackageName  = "PackageName";
78         _Author                 = "Author ??";
79         _Category               = "<VOID>";
80         _Description    = "Description ??";
81
82         _parent                 = parent;
83         _numBoxes               = 0;
84         _idManager              = idManager;
85         _baseView               = baseView;
86         _bbtkfactory    = bbtkfactory;
87         
88         _startDragging  = false;
89         _isComplexBox   = false;
90
91         if (_baseView != NULL) {
92                 //EED02JUIN2010
93                 printf(
94                                 "RaC-EED 21-06-2010 %p wxVtkSceneManager::wxVtkSceneManager If you comment this line, the drag and drop functionnality is not initialized.\n",
95                                 this);
96 //#ifdef __APPLE__
97                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
98 //#endif
99
100                 configureBaseView();
101                 _worldState = NOTHING_HAPPENS;
102                 registerController(this);
103
104         }
105         _idConnectionInCreation = -1;
106         _contLastId                             = 0;
107
108 }
109
110 //=========================================================================
111
112 void wxVtkSceneManager::disconnectDrop() 
113 {
114         printf("EED %p ~wxVtkSceneManager::disconnectDrop()\n", this);
115         //EED02JUIN2010         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
116         //      _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget( new tmpClasswxTextDropTarget() );
117 }
118
119 //=========================================================================
120
121 wxVtkSceneManager::~wxVtkSceneManager() 
122 {
123         //FCY memory leaks
124         printf("EED %p ~wxVtkSceneManager() START\n", this);
125         disconnectDrop();
126         delete _baseView;
127         printf("EED %p ~wxVtkSceneManager() END\n", this);
128 }
129
130 //=========================================================================
131
132 void wxVtkSceneManager::configureBaseView() 
133 {
134         vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
135
136         _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
137
138         // Important to activate the 2D interaction system
139         wxVTKRenderWindowInteractor *iren =     _baseView->GetWxVTKRenderWindowInteractor();
140         interactorstylebaseview->SetInteractor(iren);
141         iren->SetInteractorStyle(interactorstylebaseview);
142         interactorstylebaseview->SetwxVtkBaseView(_baseView);
143
144         _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
145         _baseView->GetRenderer()->ResetCamera(-100, 100, -100, 100, -1000, 1000);
146         _baseView->GetRenderer()->GetActiveCamera()->Zoom(10);
147
148
149         _baseView->GetRenderer()->SetBackground(0.9, 0.9, 0.9);
150         _baseView->GetRenderer()->GradientBackgroundOn();
151
152         // Actos Port_Text
153         _textActor = vtkTextActor3D::New();
154         _textActor->SetPosition(-9999, -9999, GPOSITION_Z);
155         _textActor->SetInput("<void>");
156         _textActor->GetTextProperty()->SetFontSize(60);
157         _textActor->GetTextProperty()->BoldOn();
158         _textActor->GetTextProperty()->SetColor(PORTTEXT_NH_R, PORTTEXT_NH_G,PORTTEXT_NH_B);
159
160         _baseView->GetRenderer()->AddActor(_textActor);
161
162         // Actor  Fill_Port_Text
163
164         //------------
165         double xInic = 0;
166         double yInic = 0;
167         double zInic = GPOSITION_Z;
168
169         vtkPolygon *aPolygon = vtkPolygon::New();
170         _fillObjectActor = vtkActor::New();
171
172         _pts = vtkPoints::New();
173
174         double w = 100, h = 10, b = h / 15, t = 3;
175         _pts->SetNumberOfPoints(21);
176         _pts->InsertPoint(0, xInic + w * 0.33 - t / 2, yInic, zInic);
177         _pts->InsertPoint(1, xInic + w * 0.33, yInic - t, zInic);
178         _pts->InsertPoint(2, xInic + w * 0.33 + t / 2, yInic, zInic);
179         _pts->InsertPoint(3, xInic + w * 0.33 + t / 2, yInic, zInic);
180         _pts->InsertPoint(4, xInic + w - b * 4, yInic + b * 0, zInic);
181
182         _pts->InsertPoint(5, xInic + w - b * 4, yInic + b * 0, zInic);
183         _pts->InsertPoint(6, xInic + w - b * 2, yInic + b * 1, zInic);
184         _pts->InsertPoint(7, xInic + w - b * 1, yInic + b * 2, zInic);
185         _pts->InsertPoint(8, xInic + w - b * 0, yInic + b * 4, zInic);
186
187         _pts->InsertPoint(9, xInic + w - b * 0, yInic + h - b * 4, zInic);
188         _pts->InsertPoint(10, xInic + w - b * 1, yInic + h - b * 2, zInic);
189         _pts->InsertPoint(11, xInic + w - b * 2, yInic + h - b * 1, zInic);
190         _pts->InsertPoint(12, xInic + w - b * 4, yInic + h - b * 0, zInic);
191
192         _pts->InsertPoint(13, xInic + b * 4, yInic + h - b * 0, zInic);
193         _pts->InsertPoint(14, xInic + b * 2, yInic + h - b * 1, zInic);
194         _pts->InsertPoint(15, xInic + b * 1, yInic + h - b * 2, zInic);
195         _pts->InsertPoint(16, xInic + b * 0, yInic + h - b * 4, zInic);
196
197         _pts->InsertPoint(17, xInic + b * 0, yInic + b * 4, zInic);
198         _pts->InsertPoint(18, xInic + b * 1, yInic + b * 2, zInic);
199         _pts->InsertPoint(19, xInic + b * 2, yInic + b * 1, zInic);
200         _pts->InsertPoint(20, xInic + b * 4, yInic + b * 0, zInic);
201
202         aPolygon->GetPointIds()->SetNumberOfIds(21);
203         for (int i = 0; i < 21; i++) {
204                 aPolygon->GetPointIds()->SetId(i, i);
205         }
206
207         vtkUnstructuredGrid *aPolygonGrid = vtkUnstructuredGrid::New();
208         aPolygonGrid->Allocate(1, 1);
209         aPolygonGrid->InsertNextCell(aPolygon->GetCellType(),
210                         aPolygon->GetPointIds());
211         aPolygonGrid->SetPoints(_pts);
212         _aPolygonMapper = vtkDataSetMapper::New();
213         _aPolygonMapper->SetInput(aPolygonGrid);
214         _fillObjectActor->SetMapper(_aPolygonMapper);
215         _fillObjectActor->GetProperty()->SetColor(PORTFILL_NH_R, PORTFILL_NH_G,
216                         PORTFILL_NH_B);
217         _fillObjectActor->GetProperty()->SetOpacity(0);
218         _aPolygonMapper->Modified();
219
220         _baseView->GetRenderer()->AddActor(_fillObjectActor);
221 }
222
223 //=========================================================================
224
225 std::string wxVtkSceneManager::generateANewNameForABox() {
226         std::stringstream boxname;
227         if (_numBoxes < 10) {
228                 boxname << "Box0" << _numBoxes;
229         } else {
230                 boxname << "Box" << _numBoxes;
231         }
232         _numBoxes++;
233         return boxname.str();
234 }
235
236 //=========================================================================
237
238 std::string wxVtkSceneManager::findANewNameForABox() {
239         std::string boxname = generateANewNameForABox();
240
241         while (boxExist(boxname) == true) {
242                 boxname = generateANewNameForABox();
243         } // while
244
245         return boxname;
246 }
247
248 //=========================================================================
249 void wxVtkSceneManager::configGBlackBox(int idBox, double xIn, double yIn,
250                 double zIn, std::string name, bool boxExecutable, double xEn,
251                 double yEn, double zEn) {
252         GObjectController *cont = _controllers[idBox];
253         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
254         bbmod->setBBTKName(name);
255         bbmod->setInicPoint(xIn, yIn, zIn);
256         bbmod->setFinalPoint(xEn, yEn, zEn);
257         bbmod->setExecutable(boxExecutable);
258
259         bbmod->notifyObservers(_idManager);
260
261 }
262
263 //=========================================================================
264
265 int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName,std::string boxType) 
266 {
267         _worldState                                             = NOTHING_HAPPENS;
268         int windowHeight                                = _baseView->GetRenWin()->GetSize()[1];
269         int type                                                = GBLACKBOX;
270         //Create the MVC Objects
271         GBlackBoxModel *model                   =       (GBlackBoxModel*) GObjectsMVCFactory::getInstance()->createGObjectModel(type);
272         vtkGObjectView *view                    =       GObjectsMVCFactory::getInstance()->createGObjectView(type);
273         GObjectController *controller   =       GObjectsMVCFactory::getInstance()->createGObjectController(type);
274
275 //EED 15 oct 2012       BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
276         
277         Package::Pointer k;
278         std::map< std::string, BlackBoxDescriptor::Pointer> mapDesc;
279         try{
280                 k               = _bbtkfactory->GetPackage(packageName);
281                 mapDesc = k->GetDescriptorMap();
282         }catch(Exception e){
283                 printf("EED. ERROR!: Exception in wxVtkSceneManager::createGBlackBox for package %s, NOT EXIST\n", packageName.c_str());
284         }
285
286         BlackBoxDescriptor::Pointer descriptor;
287         try{
288                 descriptor = mapDesc[boxType];
289         }catch(Exception e){
290                 printf("EED. ERROR!: Exception in wxVtkSceneManager::createGBlackBox for box %s of package %s, NOT EXIST\n", boxType.c_str(),packageName.c_str());
291         }
292         
293         if (descriptor == NULL)
294         {
295                         printf("SCP: ERROR in wxVtkSceneManager::createGBlackBox, box %s probably does not exist.\n",  boxType.c_str());
296         }
297         
298         //Prepares the initial model
299         //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height
300
301         double xx = x;
302         double yy = windowHeight - y;
303
304         //z value is not important yet, because it is only used a parallel projection
305         double zz = GPOSITION_Z;
306         _baseView->TransCoordScreenToWorld(xx, yy, zz);
307         model->setInicPoint(xx, yy, zz);
308
309         std::stringstream stream;
310
311         std::string newBoxName;
312         newBoxName = findANewNameForABox();
313
314         stream << newBoxName;
315
316         std::string arraystring = stream.str();
317
318         model->setBBTKName(arraystring);
319         model->setBBTKType(boxType);
320         model->setBBTKPackage(packageName);
321
322         model->addObserver(view);
323         model->addObserver(this);
324
325         //Iterate and create the input ports
326         std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap =
327                         descriptor->GetInputDescriptorMap();
328         std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
329
330         int i = 0;
331         for (itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput) {
332                 BlackBoxInputDescriptor *desc = itInput->second;
333                 createGInputPort(GINPUTPORT, i, model, desc);
334                 i++;
335         }
336
337         //Iterate and create the output ports
338         std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap =
339                         descriptor->GetOutputDescriptorMap();
340         std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
341
342         i = 0;
343         for (itOutput = descriptorOutMap.begin(); itOutput
344                         != descriptorOutMap.end(); ++itOutput) {
345                 BlackBoxOutputDescriptor *desc = itOutput->second;
346                 createGOutputPort(GOUTPUTPORT, i, model, desc);
347                 i++;
348         }
349
350         //Associates the view with the correspondent renderer and the  model.
351         //(NOTE: Refresh is only made by the view)
352         view->setModel(model);
353         view->setBaseView(_baseView);
354         view->initVtkObjects();
355
356         //Associates the controller with the correspondent model and view
357         controller->setModelAndView(model, view);
358
359         //Resgiter change to the observers of the actual model
360         model->notifyObservers(_idManager);
361
362         int newId = addObjectController(controller);
363
364         return newId;
365
366 }
367
368 //=========================================================================
369
370 int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName) {
371         //EED           int windowWidth=_baseView->GetRenWin()->GetSize()[0];
372         int windowHeight = _baseView->GetRenWin()->GetSize()[1];
373
374         int type = GCOMPLEXINPUTPORT;
375
376         //Create the MVC Objects
377
378         GComplexBoxPortModel    *model          = (GComplexBoxPortModel*) GObjectsMVCFactory::getInstance()->createGObjectModel(type);
379         vtkGObjectView                  *view           = GObjectsMVCFactory::getInstance()->createGObjectView(type);
380         GObjectController               *controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
381
382         //Prepares the initial model
383
384         double xx = 5;
385         double yy = windowHeight - 5;
386
387         //z value is not important yet, because it is only used a parallel projection
388         double zz = GPOSITION_Z;
389
390         _baseView->TransCoordScreenToWorld(xx, yy, zz);
391         model->setInicPoint(xx, yy, zz);
392
393         model->setBBTKName(inputName);
394         model->setBBTKType("ComplexInputPort");
395         model->setComplexPortType(type);
396
397         model->addObserver(view);
398         model->addObserver(this);
399
400         //create the output port
401         GPortController* portController = createGPort(GOUTPUTPORT, inputName,
402                         "ComplexOutputPortType","ComplexOutputPortDescriopton", 0, model);
403         model->addOutputPort((GPortModel*) portController->getModel());
404
405         //Associates the view with the correspondent renderer and the  model.
406         //(NOTE: Refresh is only made by the view)
407         view->setModel(model);
408         view->setBaseView(_baseView);
409         view->initVtkObjects();
410
411         //Associates the controller with the correspondent model and view
412         controller->setModelAndView(model, view);
413
414         //Resgiter change to the observers of the actual model
415         model->notifyObservers(_idManager);
416
417         int newId = addObjectController(controller);
418         return newId;
419 }
420
421 //=========================================================================
422
423 int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName) {
424         //EED           int windowWidth=_baseView->GetRenWin()->GetSize()[0];
425         int windowHeight = _baseView->GetRenWin()->GetSize()[1];
426
427         int type = GCOMPLEXOUTPUTPORT;
428
429         //Create the MVC Objects
430
431         GComplexBoxPortModel    *model          = (GComplexBoxPortModel*) GObjectsMVCFactory::getInstance()->createGObjectModel(type);
432         vtkGObjectView                  *view           = GObjectsMVCFactory::getInstance()->createGObjectView(type);
433         GObjectController               *controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
434
435         //Prepares the initial model
436
437         double xx = 5;
438         double yy = windowHeight - 5;
439
440         //z value is not important yet, because it is only used a parallel projection
441         double zz = GPOSITION_Z;
442
443         _baseView->TransCoordScreenToWorld(xx, yy, zz);
444         model->setInicPoint(xx, yy, zz);
445
446         model->setBBTKName(outputName);
447         model->setBBTKType("ComplexOutputPort");
448         model->setComplexPortType(type);
449
450         model->addObserver(view);
451         model->addObserver(this);
452
453         //create the output port
454         GPortController* portController = createGPort(GINPUTPORT, outputName,
455                         "ComplexInputPort","ComplexInputPortDescription", 0, model);
456         model->addInputPort((GPortModel*) portController->getModel());
457
458         //Associates the view with the correspondent renderer and the  model.
459         //(NOTE: Refresh is only made by the view)
460         view->setModel(model);
461         view->setBaseView(_baseView);
462         view->initVtkObjects();
463
464         //Associates the controller with the correspondent model and view
465         controller->setModelAndView(model, view);
466
467         //Resgiter change to the observers of the actual model
468         model->notifyObservers(_idManager);
469
470         int newId = addObjectController(controller);
471         return newId;
472 }
473
474 //=========================================================================
475
476 int wxVtkSceneManager::createGInputPort(int portType, int posinBox, GBoxModel *blackBox, BlackBoxInputDescriptor *desc) 
477 {
478         GPortController* portController = createGPort(portType, desc->GetName(),
479                         desc->GetTypeName(), desc->GetDescription(), posinBox, blackBox);
480         blackBox->addInputPort((GPortModel*) portController->getModel());
481         return portController->getId();
482 }
483
484 //=========================================================================
485
486 int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc) 
487         {
488         GPortController* portController = createGPort(portType, desc->GetName(),
489                         desc->GetTypeName(),desc->GetDescription(), posinBox, blackBox);
490         blackBox->addOutputPort((GPortModel*) portController->getModel());
491         return portController->getId();
492 }
493
494 //=========================================================================
495
496 GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, std::string bbtkDescription, int posInBox,GBoxModel *blackBox) 
497 {
498         int type = GPORT;
499
500         //Create the MVC Objects
501         GPortModel
502                         *model =
503                                         (GPortModel*) GObjectsMVCFactory::getInstance()->createGObjectModel(
504                                                         type);
505         vtkGObjectView *view =
506                         GObjectsMVCFactory::getInstance()->createGObjectView(type);
507         GObjectController* controller =
508                         GObjectsMVCFactory::getInstance()->createGObjectController(type);
509
510         model->registerInBox(blackBox, portType, posInBox);
511
512         model->setBBTKType(bbtkType);
513         model->setBBTKName(bbtkName);
514         model->setBBTKDescription(bbtkDescription);
515
516         model->addObserver(view);
517         model->addObserver(this);
518
519         //Associates the view with the correspondent renderer and the  model.
520         //(NOTE: Refresh is only made by the view)
521         view->setModel(model);
522         view->setBaseView(_baseView);
523         view->initVtkObjects();
524
525         //Associates the controller with the correspondent model and view
526         controller->setModelAndView(model, view);
527
528         model->notifyObservers(_idManager);
529
530         addObjectController(controller);
531
532         return (GPortController*) controller;
533 }
534
535 //=========================================================================
536
537 int wxVtkSceneManager::createGConnector(GPortModel* startPort) 
538 {
539         int type = GCONNECTOR;
540
541         manualConnectorContourController        *manContourControl              = new manualConnectorContourController();
542         manualConnectorContourView                      *manContourView                 = new manualConnectorContourView();
543         manualContourModel                                      *manContourModel                = new manualContourModel();
544         GConnectorController                            *connectorcontroller    = new GConnectorController();
545         GConnectorModel                                         *connectorModel                 = new GConnectorModel();
546         vtkGConnectorView                                       *connectorView                  = new vtkGConnectorView();
547         connectorModel->setGObjectType(type);
548
549         manContourModel->SetCloseContour(false);
550         connectorModel->setStartPort(startPort);
551
552         manContourView->SetModel(manContourModel);
553         manContourView->SetWxVtkBaseView(_baseView);
554         manContourView->SetRange(0.5);
555         manContourView->SetZ(GPOSITION_Z);
556
557         manContourView->SetColorNormalContour(0, 0, 1);
558         manContourView->SetColorEditContour(0.5, 0.5, 0.5);
559         manContourView->SetColorSelectContour(1, 0.8, 0);
560         
561         
562
563         /**
564          * JGRR AND CM
565          * Width increased from 2 to 3, it simplifies line handling and spline control :P
566          * @param WidthLine  
567          */ 
568         manContourView->SetWidthLine( 3 ) ;
569         manContourView->SetShowText(false);
570
571         manContourControl->SetModelView(manContourModel, manContourView);
572
573         manContourControl->CreateNewManualContour();
574
575
576         double x, y, z;
577         connectorModel->getInicPoint(x, y, z);
578         
579         
580         manContourControl->SetState(1);
581         manContourModel->SetCloseContour(false);
582
583         manContourModel->AddPoint(x, y, z);
584         manContourView->AddPoint();
585
586         manContourModel->AddPoint(x, y, z);
587         manContourView->AddPoint();
588
589         int bak = manContourControl->GetNumberOfPointsManualContour() - 1;
590         manContourControl->_bakIdPoint = bak;
591         
592         manContourControl->SetMoving(false);
593
594         connectorcontroller->setModelAndView(connectorModel, connectorView);
595
596         int newId = addObjectController(connectorcontroller);
597
598         connectorcontroller->setManualContourController(manContourControl);
599         connectorModel->setManualContourModel(manContourModel);
600         connectorView->setManualContourView(manContourView);
601         connectorView->setModel(connectorModel);
602         connectorView->setBaseView(_baseView);
603
604         connectorModel->addObserver(connectorView);
605         connectorModel->addObserver(this);
606
607 //      manContourView->Refresh();
608         
609         return newId;
610 }
611
612 //=========================================================================
613
614 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param) 
615 {
616         vtkInteractorStyleBaseView * baseViewControlManager = (vtkInteractorStyleBaseView*) _baseView->GetInteractorStyleBaseView();
617         baseViewControlManager->AddInteractorStyleMaracas(param);
618 }
619
620 //=========================================================================
621
622 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param) 
623 {
624         vtkInteractorStyleBaseView *baseViewControlManager = (vtkInteractorStyleBaseView*) _baseView->GetInteractorStyleBaseView();
625         baseViewControlManager->RemoveInteractorStyleMaracas(param);
626 }
627
628 //=========================================================================
629
630 vtkRenderer* wxVtkSceneManager::getRenderer() 
631 {
632         return _baseView->GetRenderer();
633 }
634
635 //=========================================================================
636
637 vtkRenderWindow* wxVtkSceneManager::getRenderWindow() 
638 {
639         return _baseView->GetRenWin();
640 }
641 //=========================================================================
642
643
644 //=========================================================================
645 int wxVtkSceneManager::GetIndexInSelected(int idControler) 
646 {
647         int index = -1;
648         for (int i = 0; i < (int) _selectedObjects.size(); i++) 
649         {
650                 if (_selectedObjects[i] == idControler) 
651                 {
652                         index = i;
653                         break;
654                 }
655         }
656         return index;
657 }
658 //=========================================================================
659
660 void wxVtkSceneManager::update(int idController, int command) {
661
662         if (command != NO_COMMAND) 
663         {
664                 if (command == ADD_TO_SELECTED) 
665                 {
666                         if (GetIndexInSelected(idController) == -1) 
667                         {
668                                 _selectedObjects.push_back(idController);
669                         }
670                 } else if (command == REMOVE_FROM_SELECTED) {
671                         int index = GetIndexInSelected(idController);
672                         if (index >= 0) 
673                         {
674                                 _selectedObjects.erase(_selectedObjects.begin() + index);
675                         }
676                 } else if (command == INIT_CREATION_CONTOUR) {
677                 // JGRR & CM WH
678                 typedef std::map<int , GObjectController*>::iterator TIterator ;
679                 TIterator iter ;
680                 TIterator iter_end ;
681                 // Find current item in the controller
682                 iter = _controllers.find( idController ) ;
683                 iter_end = _controllers.end( ) ;
684                 // If item exist 
685                 if ( iter != iter_end ) {
686                     GObjectController* gobjc = iter->second ;
687
688                     std::string bbtkType = gobjc->getModel( )->getBBTKType( ) ;
689                     // And if the item is a port
690                     if ( gobjc->getModel( )->getGObjectType( ) == GPORT ) {
691                         TIterator c_it = _controllers.begin( ) ;
692                         // Go through all componets 
693                         for ( c_it ; c_it != iter_end ; c_it++ ) {
694                             GObjectController* obc = c_it->second ;
695                             int obc_type = obc->getModel( )->getGObjectType( ) ;
696                             std::string obc_bbtkType = obc->getModel( )->getBBTKType( ) ;
697                             // if the component is a port and the type is the same of that selected
698                             if ( ( obc_type == GPORT ) && ( obc_bbtkType == bbtkType ) ) {
699                                 // Highliht element 
700                                 obc->getView( )->setState( POSSIBLE_CONNECTION ) ;                             
701                                 //obc->getView( )->setState( HIGHLIGHTED ) ;
702                             }
703                         }
704                     }
705                 }
706
707                 // EO JGRR & CM WH  
708                      
709                         _worldState                                     = INIT_CREATION_CONTOUR;
710                         GObjectController* cont         = _controllers[idController];
711                         GPortModel* startOutputPort = (GPortModel*) cont->getModel();
712
713                         // The last one is the controller of the connector
714                         std::map<int, GObjectController*>::iterator it2;
715
716                         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) 
717                         {
718                                 GObjectController *cont = it2->second;
719                                 if (cont->getGObjectType() == GPORT) 
720                                 {
721                                         GPortModel* port = (GPortModel*) cont->getModel();
722                                         if (port->getPortType() == GINPUTPORT) 
723                                         {
724                                                 cont->SetActive(true);
725                                         } else {
726                                                 cont->getView()->setState(NOTHING_HAPPENS);
727                                                 cont->getModel()->notifyObservers(_idManager);
728                                                 cont->SetActive(false);
729                                         }
730                                 } else {
731                                         cont->getView()->setState(NOTHING_HAPPENS);
732                                         cont->getModel()->notifyObservers(_idManager);
733                                         cont->SetActive(false);
734                                 }
735                         }
736                         _selectedObjects.clear();
737                         _idConnectionInCreation = createGConnector(startOutputPort);
738                 } else if (command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR) {
739                         _worldState = NOTHING_HAPPENS;
740                         //int id = _controllers.size()-1;
741                         GObjectController       *cont                   = _controllers[_idConnectionInCreation];
742                         GConnectorModel         *modelContour   = (GConnectorModel*) cont->getModel();
743                         GObjectController       *finPort                = _controllers[idController];
744
745                         if (finPort->getGObjectType() == GPORT) 
746                         {
747                                 GPortModel* modelPort = (GPortModel*) finPort->getModel();
748                                 modelContour->setEndPort(modelPort);
749
750                                 // JGRR CM                     
751                                 typedef std::map<int, GObjectController*>::iterator TIterator;
752                                 TIterator c_it = _controllers.begin();
753                                 TIterator iter_end = _controllers.end();
754                                 // Go through all componets 
755                                 for (c_it; c_it != iter_end; c_it++) {
756                                     GObjectController* obc = c_it->second;
757                                     int obc_type = obc->getModel()->getGObjectType();
758                                     int obc_state = obc->getView()->getState();
759                                     // if the component is a port and the type is the same of that selected
760                                     if ((obc_type == GPORT) && (obc_state == POSSIBLE_CONNECTION)) {
761                                         // Return element to neutral state if not selected
762                                         obc->getView()->setState(NOTHING_HAPPENS);
763                                     }
764                                     if ((obc_type == GPORT) && (obc_state == SELECTED_POSSIBLE_CONNECTION)) {
765                                         // Change element to connected state
766                                         obc->getView()->setState(HIGHLIGHTED);
767
768                                     }
769
770                                 }
771                                 // EO JGRR CM                                
772                     
773                                                                 _parent->saveTempandUpdate("create connection");
774                         }
775
776                         manualConnectorContourController        *manCont        = ((GConnectorController*) cont)->getManualContourController();
777                         manualConnectorContourView                      *connView       = (manualConnectorContourView*) manCont->GetManualViewBaseContour();
778                                      
779                         std::map<int, GObjectController*>::iterator it2;
780
781                         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) 
782                         {
783                                 GObjectController *cont = it2->second;
784                                 if (cont->getView() != NULL) {
785                                         cont->getView()->setState(NOTHING_HAPPENS);
786                                         cont->getModel()->notifyObservers(_idManager);
787                                 }
788                                 cont->SetActive(true);
789                         }
790                         connView->Refresh();          
791                 }
792
793         }
794         
795 }
796
797 //=========================================================================
798
799 bool wxVtkSceneManager::OnMouseMove() 
800 {
801         int X, Y;
802         bool okStatusMessage    = false;
803         bool okPortMessage              = false;
804         wxVTKRenderWindowInteractor *wxVTKiren;
805         wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
806         wxVTKiren->GetEventPosition(X, Y);
807
808         if (_worldState == DRAG_OBJECTS) {
809                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
810                         int id = _selectedObjects[i];
811                         GObjectController* cont = _controllers[id];
812                         if (_startDragging) {
813                                 cont->getView()->setStartDragging(true);
814                         }
815                         cont->getView()->moveObject(X, Y);
816                         cont->getView()->setState(DRAG);
817                         cont->getModel()->notifyObservers(_idManager);
818                 }
819
820                 std::map<int, GObjectController*>::iterator it;
821
822                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
823                         GObjectController *desc = it->second;
824                         if (desc->getGObjectType() == GCONNECTOR) {
825                                 vtkGConnectorView* vconn = (vtkGConnectorView*) desc->getView();
826                                 vconn->updateStartEndPoints();
827                         }
828                 }
829
830                 _startDragging = false;
831
832         } else if (_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR) {
833                 std::map<int, GObjectController*>::iterator it;
834                 double px, py, pz;
835                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
836                         GObjectController *desc = it->second;
837                         int type        = desc->getGObjectType();
838                         int state       = desc->getView()->getState();
839                         desc->getModel()->getCenter(px, py, pz);
840
841                         // JGRR & CM
842                         if ((state == HIGHLIGHTED) || (state == SELECTED_POSSIBLE_CONNECTION )) 
843                         {
844                         // EO JGRR & CM    
845                                 okStatusMessage = true;
846                                 updateStatusBar(desc->getStatusText());
847                                 if (type == GBLACKBOX) 
848                                 {
849                                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
850                                         _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
851                                 } // if GBLACKBOX
852                                 if (type == GPORT) 
853                                 {
854                                         // PortText
855                                         okPortMessage = true;
856                                         py = py + 5;
857                                         _textActor->SetInput(desc->getStatusText().c_str());
858                                         _textActor->SetScale(0.1);
859                                         _textActor->SetPosition(px - 25, py + 1, pz + 2);
860
861                                         // FillPortText
862                                         px = px - 33;
863                                         //py = py;
864                                         _fillObjectActor->SetScale(1);
865                                         _fillObjectActor->GetProperty()->SetOpacity(0.50);
866                                         _fillObjectActor->SetPosition(px, py, 1);
867                                         _aPolygonMapper->Modified();
868                                 } // if GPORT
869
870                         } // for controllers it
871                 }
872         }
873         if (okStatusMessage == false) 
874         {
875                 updateStatusBar("");
876         }
877
878         if (okPortMessage == false) 
879         {
880                 _textActor->SetScale(0.0001);
881                 _fillObjectActor->SetScale(0.0001);
882         }
883         return true;
884 }
885
886 //=========================================================================
887
888 bool wxVtkSceneManager::OnLeftButtonDown() 
889 {
890         if (_worldState == INIT_CREATION_CONTOUR) 
891         {
892                 bool isOverPort = false;
893                 std::map<int, GObjectController*>::iterator it;
894                 for (it = _controllers.begin(); it != _controllers.end() && isOverPort== false; ++it) 
895                 {
896                         GObjectController *desc = it->second;
897                         if (desc->getGObjectType() == GPORT) 
898                         {
899                                 GPortModel *portmod = (GPortModel*) desc->getModel();
900                                 vtkGObjectView *portView = desc->getView();
901                                 if (portmod->getPortType() == GINPUTPORT && portView->getState() == HIGHLIGHTED) 
902                                 {
903                                         isOverPort = true;
904                                 } // if
905                                 // JGRR & CM                    
906                                 if ( portmod->getPortType( ) == GINPUTPORT &&  portView->getState( ) == SELECTED_POSSIBLE_CONNECTION   ) {
907                                         isOverPort = true ;
908                                         portView->setState( HIGHLIGHTED);
909                                 }                    
910                                 // EO JGRR & CM 
911
912                         } // if
913                 } // for
914
915                 if (isOverPort == false) 
916                 {
917                         CancelConnection();
918                         UnSelectBlackBoxes();
919                 } // isOverPort
920         
921
922         } else { //_worldState
923
924                 _worldState             = DRAG_OBJECTS;
925                 _startDragging  = true;
926
927                 int shiftkey    = _vtkInteractorStyleBaseView->GetInteractor()->GetShiftKey();
928                 int ctrlkey             = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
929                 ctrlkey                 = ctrlkey + shiftkey;
930
931                 GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
932
933                 if (cont != NULL) {
934                         int state = cont->getView()->getState();
935                         if ((ctrlkey == 0) && (state == HIGHLIGHTED)) {
936                                 UnSelectBlackBoxes();
937                         }
938                 } else {
939                         UnSelectBlackBoxes();  // EED 12/07/2012
940                 }
941                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
942                         int id = _selectedObjects[i];
943                         GObjectController *cont = _controllers[id];
944                         cont->getView()->setState(DRAG);
945                         cont->getModel()->notifyObservers(_idManager);
946                 } // for
947         } // if _selectedObjects.size
948
949
950         return true;
951 }
952
953 //=========================================================================
954
955 bool wxVtkSceneManager::OnLeftButtonUp() 
956 {
957         if (_worldState == DRAG_OBJECTS) {
958                 _worldState = NOTHING_HAPPENS;
959
960                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
961                         int id = _selectedObjects[i];
962                         GObjectController* cont = _controllers[id];
963                         cont->getView()->setState(SELECTED);
964                         cont->getModel()->notifyObservers(_idManager);
965                 }
966         }
967         return true;
968 }
969
970 //=========================================================================
971 void wxVtkSceneManager::CancelConnection() 
972 {
973         if (_worldState == INIT_CREATION_CONTOUR) 
974         {
975                 _worldState = NOTHING_HAPPENS;
976                 //int lastId = _controllers.size()-1;
977
978                 GConnectorController *connector =
979                                 (GConnectorController*) _controllers[_idConnectionInCreation];
980                 connector->removeFromScene();
981                 unregisterController(connector);
982                 _controllers.erase(_idConnectionInCreation);
983
984                 std::map<int, GObjectController*>::iterator it;
985                 for (it = _controllers.begin(); it != _controllers.end(); ++it) 
986                 {
987                         GObjectController *desc = it->second;
988                         desc->SetActive(true);
989                         desc->getView()->setState(NOTHING_HAPPENS);
990                         desc->getModel()->notifyObservers(_idManager);
991                 } // for
992         }// if
993 }
994
995 //=========================================================================
996
997 bool wxVtkSceneManager::OnRightButtonUp() 
998 {
999         if (_worldState == INIT_CREATION_CONTOUR) 
1000         {
1001                 CancelConnection();
1002         }
1003         UnSelectBlackBoxes();
1004         return true;
1005 }
1006
1007  ///JLGR 21-05-2012
1008
1009 bool wxVtkSceneManager::OnMiddleButtonDown() {
1010
1011
1012         this->_vtkInteractorStyleBaseView->StartPan();
1013
1014         return true;
1015     }
1016
1017 bool wxVtkSceneManager::OnMiddleButtonUp() {
1018
1019
1020         this->_vtkInteractorStyleBaseView->EndPan();
1021
1022
1023         return true;
1024     }
1025
1026
1027 //=========================================================================
1028
1029 GObjectController *wxVtkSceneManager::GetGBlackBoxControlerPointedByMouse() {
1030         GObjectController *result = NULL;
1031         int X, Y;
1032         wxVTKRenderWindowInteractor *wxVTKiren;
1033         wxVTKiren
1034                         = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
1035         wxVTKiren->GetEventPosition(X, Y);
1036
1037         std::map<int, GObjectController*>::iterator it;
1038
1039         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1040                 GObjectController *cont = it->second;
1041                 int type = cont->getGObjectType();
1042
1043                 if (cont->getView()->isPointInside(X, Y) == true) {
1044                         if (type == GBLACKBOX) {
1045                                 result = cont;
1046                         } // if type
1047                 } // if isPointInside
1048         } // for it
1049         return result;
1050 }
1051
1052 //=========================================================================
1053 void wxVtkSceneManager::UnSelectBlackBoxes() {
1054         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
1055                 int id = _selectedObjects[i];
1056                 GObjectController* control = _controllers[id];
1057                 control->getView()->setState(NOTHING_HAPPENS);
1058                 control->getModel()->notifyObservers(_idManager);
1059         }// for _selectedObjects
1060         _selectedObjects.clear();
1061 }
1062
1063 //=========================================================================
1064 bool wxVtkSceneManager::OnLeftDClick() {
1065         GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
1066         if (cont != NULL) {
1067                 UnSelectBlackBoxes();
1068                 _parent->editBlackBox((GBlackBoxModel*) cont->getModel());
1069         }
1070
1071         /*EED Borrame
1072
1073          int X,Y;
1074          wxVTKRenderWindowInteractor *wxVTKiren;
1075          wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
1076          wxVTKiren->GetEventPosition(X,Y);
1077
1078          std::map<int, GObjectController*>::iterator it;
1079
1080          bool clickOnObject = false;
1081
1082          for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
1083          {
1084          GObjectController *cont = it->second;
1085          int type = cont->getGObjectType();
1086
1087          if(cont->getView()->isPointInside(X,Y))
1088          {
1089          if(type==GBLACKBOX)
1090          {
1091          for (int i=0; i< (int)_selectedObjects.size(); i++)
1092          {
1093          int id = _selectedObjects[i];
1094          GObjectController* control = _controllers[id];
1095          control->getView()->setState(NOTHING_HAPPENS);
1096          }
1097          _selectedObjects.clear();
1098
1099          GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
1100          _parent->editBlackBox(bbmodel);
1101          }
1102          clickOnObject = true;
1103          }
1104          }
1105
1106          if(clickOnObject==false)
1107          {
1108          //_parent->editDiagramParameters(this);
1109          }
1110          */
1111         return true;
1112 }
1113
1114 //=========================================================================
1115
1116 bool wxVtkSceneManager::OnChar() 
1117 {
1118         bool ok=false;
1119         bool oktmp;
1120         char keyCode    = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
1121         int ctrlkey             = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
1122         
1123         // KeyCode 127 : Delete Key
1124         // KeyCode 8 : Backspace Key
1125
1126
1127         if (keyCode == 8 || keyCode == 127 || keyCode == 27) 
1128         {
1129                 if (_worldState == INIT_CREATION_CONTOUR) 
1130                 {
1131                         CancelConnection();
1132                         UnSelectBlackBoxes();
1133                 }
1134         }
1135
1136
1137         if (keyCode == 8 || keyCode == 127) 
1138         {
1139
1140                         for (int i = 0; i < (int) _selectedObjects.size(); i++) 
1141                         {
1142                                 int id = _selectedObjects[i];
1143                                 std::map<int, GObjectController*>::iterator it;
1144                                 it = _controllers.find(id);
1145                                 if (it != _controllers.end()) 
1146                                 {
1147                                         oktmp   =       deleteObject(id);
1148                                         ok              =       ok||oktmp;
1149                                 } // if
1150                         }// for selected objects                
1151                         if (ok) _selectedObjects.clear();
1152                         _parent->saveTempandUpdate("delete object");
1153         }// if keyCode
1154         
1155         /// Duplicate 
1156 #ifdef MACOSX
1157         if((keyCode==0) && (ctrlkey==1)) {                
1158 #else
1159         ctrlkey+=keyCode;
1160         if(ctrlkey==5){                
1161 #endif
1162                 if(_selectedObjects.size()){
1163                         std::cout<<"objects to copy :";
1164                         for(int i=0;i<_selectedObjects.size();i++)
1165                         {
1166                                 std::cout<<_selectedObjects.at(i)<<" ";
1167                         }
1168                         DuplicateObjects(getSelectedObjects());   
1169                 }else{
1170                         std::cout<<"No objects to copy\n";
1171                 } // _selectedObjects.size
1172         } // ctrlkey 5  
1173
1174         return true;
1175 }
1176
1177 //=========================================================================
1178
1179 void wxVtkSceneManager::AddControlerToBeRemove(
1180                 std::vector<int> *controllersToRemove, int id) {
1181
1182         int i;
1183         bool ok = true;
1184         for (i = 0; i < (int) controllersToRemove->size(); i++) {
1185                 if (id == (*controllersToRemove)[i]) {
1186                         ok = false;
1187                 }
1188         }
1189
1190         if (ok == true) {
1191                 controllersToRemove->push_back(id);
1192         }
1193
1194 }
1195
1196 //=========================================================================
1197
1198 bool wxVtkSceneManager::deleteObject(int id) 
1199 {
1200         bool result=true;
1201         GObjectController *control = _controllers[id];
1202         std::vector<int> controllersToRemove;
1203         if (control->getGObjectType() == GBLACKBOX || control->getGObjectType()
1204                         == GCOMPLEXINPUTPORT || control->getGObjectType()
1205                         == GCOMPLEXOUTPUTPORT) {
1206                 GBoxModel *bbmod = (GBoxModel*) control->getModel();
1207                 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
1208
1209                 bool boxConnected = false;
1210
1211                 // Add box input controllers to be removed
1212                 for (int i = 0; i < (int) inputs.size(); i++) {
1213                         AddControlerToBeRemove(&controllersToRemove, inputs[i]->getObjectId());
1214                         if (inputs[i]->isConnected()) {
1215                                 boxConnected = true;
1216                         }
1217                 }
1218
1219                 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
1220
1221                 // Add box output controllers to be removed
1222                 for (int i = 0; i < (int) outputs.size(); i++) {
1223                         AddControlerToBeRemove(&controllersToRemove, outputs[i]->getObjectId());
1224                         if (outputs[i]->isConnected()) {
1225                                 boxConnected = true;
1226                         }
1227                 }
1228
1229                 // Add connection controllers to be removed
1230                 std::map<int, GObjectController*>::iterator it;
1231                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1232                         GObjectController *cont = it->second;
1233                         int type = cont->getGObjectType();
1234                         if (type == GCONNECTOR) {
1235                                 GConnectorModel *conMod = (GConnectorModel*) cont->getModel();
1236                                 if (conMod->getStartPort() != NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId()) 
1237                                 {
1238                                         AddControlerToBeRemove(&controllersToRemove,conMod->getObjectId());
1239                                 }
1240                                 if (conMod->getEndPort() != NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId()) 
1241                                 {
1242                                         AddControlerToBeRemove(&controllersToRemove,conMod->getObjectId());
1243                                 }
1244                         }
1245                 }
1246
1247                 // Add box controller to be removed
1248                 AddControlerToBeRemove(&controllersToRemove, bbmod->getObjectId());
1249         } else if (control->getGObjectType() == GCONNECTOR) {
1250                 GConnectorController *gconnectorcontroler = (GConnectorController*)control;
1251                 if (gconnectorcontroler->getManualContourController()->IsEditable()==false)
1252                 {
1253                         GConnectorModel *conMod = (GConnectorModel*) control->getModel();
1254                         AddControlerToBeRemove(&controllersToRemove, conMod->getObjectId());
1255                 } else{
1256                         gconnectorcontroler->getManualContourController()->OnChar();
1257                         result = false;
1258                 }
1259         }
1260
1261         std::map<int, GObjectController*>::iterator it;
1262
1263         for (int i = 0; i < (int) controllersToRemove.size(); i++) {
1264                 int key = controllersToRemove[i];
1265                 it = _controllers.find(key);
1266                 GObjectController *cont = _controllers[key];
1267                 if (cont != NULL) {
1268                         cont->removeFromScene();
1269                         unregisterController((InteractorStyleMaracas*) cont);
1270                         _controllers.erase(it);
1271                 }//if
1272         }// for
1273         
1274         
1275         return result;
1276 }
1277
1278 //=========================================================================
1279
1280 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName,
1281                 std::string boxName) {
1282         _parent->displayBlackBoxInfo(packageName, boxName);
1283 }
1284
1285 //=========================================================================
1286
1287 void wxVtkSceneManager::updateStatusBar(std::string textStatus) {
1288         _parent->updateStatusBar(textStatus);
1289 }
1290
1291 //=========================================================================
1292 std::string wxVtkSceneManager::LineNumber(bool withLineNumber, int &value) {
1293         std::string result = "";
1294         if (withLineNumber == true) {
1295                 value++;
1296                 stringstream StrStream;
1297                 StrStream << value;
1298                 std::string strValue = StrStream.str();
1299                 if (value < 10) {
1300                         result += "  ";
1301                 } else if (value < 100) {
1302                         result += " ";
1303                 }
1304
1305                 result += strValue + ": ";
1306         }
1307         return result;
1308 }
1309 //=========================================================================
1310 //DFCH
1311 bool wxVtkSceneManager::MakeBoxExecutable() {
1312         std::map<int, GObjectController*> selected = getSelectedObjects();
1313         if( selected.empty() )
1314                 return false;
1315         bool ret = false;
1316         std::map<int, GObjectController*>::iterator it;
1317         for (it = selected.begin(); it != selected.end(); ++it) {
1318                 GObjectController* lastSelected = it->second;
1319                 if (lastSelected) {
1320                         vtkGObjectView* view = (vtkGObjectView*) lastSelected->getView();
1321                         GBlackBoxModel *bbmodel =
1322                                         (GBlackBoxModel*) lastSelected->getModel();
1323                         view->setState(HIGHLIGHTED);
1324                         if (bbmodel->isExecutable()) {
1325                                 bbmodel->setExecutable(false);
1326                         } else {
1327                                 bbmodel->setExecutable(true);
1328                         }
1329                         bbmodel->notifyObservers(lastSelected->getId());
1330                         ret = true;
1331                 }
1332         }
1333         return ret;
1334 }
1335 //DFCH
1336
1337 //=========================================================================
1338
1339 std::string wxVtkSceneManager::getDiagramBBS(bool wln) {
1340         bool existsExec = false;
1341
1342         std::vector<std::string> packages;
1343         std::vector<int> boxes;
1344         std::vector<int> connections;
1345         std::vector<int> execBoxes;
1346
1347         std::map<int, GObjectController*>::iterator it;
1348
1349         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1350                 GObjectController *desc = it->second;
1351                 int type = desc->getGObjectType();
1352
1353                 if (type == GBLACKBOX) {
1354                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1355
1356                         std::string pkg = mod->getBBTKPackage();
1357                         bool existsPkg = false;
1358                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1359                                 if (packages[t] == pkg) {
1360                                         existsPkg = true;
1361                                 }
1362                         }
1363                         if (!existsPkg) {
1364                                 packages.push_back(pkg);
1365                         }
1366
1367                         boxes.push_back(it->first);
1368                         if (mod->isExecutable()) {
1369                                 execBoxes.push_back(it->first);
1370                                 existsExec = true;
1371                         }
1372                 } else if (type == GCONNECTOR) {
1373                         connections.push_back(it->first);
1374                 }
1375         }
1376
1377         int lnNmbr = 0;
1378         std::string script = "";
1379         script += LineNumber(wln, lnNmbr) + "# BBTK GEditor Script\n";
1380         script += LineNumber(wln, lnNmbr) + "# ----------------------\n";
1381         script += LineNumber(wln, lnNmbr) + "\n";
1382         if (existsExec) {
1383                 script += LineNumber(wln, lnNmbr) + "include std\n"; // EED
1384                 script += LineNumber(wln, lnNmbr) + "include itkvtk\n"; // EED
1385                 int i;
1386                 for (i = 0; i < (int) packages.size(); i++) {
1387                         script += LineNumber(wln, lnNmbr);
1388                         script += "include ";
1389                         script += packages[i];
1390                         script += "\n";
1391                 }
1392
1393                 script += LineNumber(wln, lnNmbr);
1394                 script += "\n";
1395
1396                 //ups2
1397
1398                 script += LineNumber(wln, lnNmbr);
1399                 script += "author \"";
1400                 script += _Author;
1401                 script += "\"\n";
1402
1403                 script += LineNumber(wln, lnNmbr);
1404                 script += "description \"";
1405                 script += _Description;
1406                 script += "\"\n";
1407
1408                 script += LineNumber(wln, lnNmbr);
1409                 script += "category \"";
1410                 script += _Category;
1411                 script += "\"\n";
1412
1413                 script += LineNumber(wln, lnNmbr);
1414                 script += "\n";
1415
1416                 // script+="include std\n"; // JPR
1417
1418                 for (i = 0; i < (int) boxes.size(); i++) {
1419                         script += LineNumber(wln, lnNmbr);
1420                         script += "new ";
1421                         int id = boxes[i];
1422                         GObjectController *control = _controllers[id];
1423                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1424
1425                         script += model->getBBTKType();
1426                         script += " ";
1427                         script += model->getBBTKName();
1428                         script += "\n";
1429
1430                         std::vector<GPortModel*> inputs = model->getInputPorts();
1431                         for (int j = 0; j < (int) inputs.size(); j++) {
1432                                 GPortModel* inputPort = inputs[j];
1433                                 if (inputPort->isValueSet()) {
1434                                         script += LineNumber(wln, lnNmbr);
1435                                         script += "  set ";
1436                                         script += model->getBBTKName();
1437                                         script += ".";
1438                                         script += inputPort->getBBTKName();
1439                                         script += " ";
1440                                         script += inputPort->getValue();
1441                                         script += "\n";
1442                                 }
1443                         }
1444
1445                         script += LineNumber(wln, lnNmbr);
1446                         script += "\n";
1447
1448                 }
1449
1450                 script += LineNumber(wln, lnNmbr);
1451                 script += "\n";
1452
1453
1454                 std::string complexInputs="";
1455
1456                 for (i = 0; i < (int) connections.size(); i++) {
1457                         //script += LineNumber(wln, lnNmbr);
1458                         //script += "connect ";
1459                         int id = connections[i];
1460                         GObjectController *control = _controllers[id];
1461                         GConnectorModel *model = (GConnectorModel*) control->getModel();
1462
1463
1464                         //Connection info
1465                         GPortModel *start = model->getStartPort();
1466                         GBoxModel *startBox = start->getParentBox();
1467
1468                         GPortModel *end = model->getEndPort();
1469                         GBoxModel *endBox = end->getParentBox();
1470
1471                         if (startBox->getGObjectType() == GCOMPLEXINPUTPORT)
1472                         {
1473                                         complexInputs += "input ";
1474                                         complexInputs += startBox->getBBTKName();
1475
1476                                         complexInputs += " ";
1477                                         complexInputs += endBox->getBBTKName();
1478                                         complexInputs += ".";
1479                                         complexInputs += end->getBBTKName();
1480
1481                                         complexInputs += " ";
1482                                         complexInputs += "\" \"";
1483
1484                                         complexInputs += "\n";
1485                         }
1486                         else
1487                         {
1488                                         script += "connect ";
1489                                         script += startBox->getBBTKName();
1490                                         script += ".";
1491                                         script += start->getBBTKName();
1492
1493                                         script += " ";
1494
1495                                         //End Connection info
1496                                         script += endBox->getBBTKName();
1497                                         script += ".";
1498                                         script += end->getBBTKName();
1499
1500                                         script += "\n";
1501                         }
1502
1503                         script += "\n";
1504                 }
1505
1506                 script += LineNumber(wln, lnNmbr);
1507                 script += "\n";
1508
1509                 script += "\n";
1510                 script += "# Complex input ports\n";
1511                 script += complexInputs;
1512
1513                 for (i = 0; i < (int) execBoxes.size(); i++) {
1514                         script += LineNumber(wln, lnNmbr);
1515                         script += "exec ";
1516                         int id = execBoxes[i];
1517                         GObjectController *control = _controllers[id];
1518                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1519
1520                         script += model->getBBTKName();
1521                         script += "\n";
1522                 }
1523
1524         }
1525
1526         return script;
1527 }
1528
1529 //=========================================================================
1530
1531 std::string wxVtkSceneManager::saveComplexBoxBBS() {
1532         std::vector<std::string> packages;
1533         std::vector<int> boxes;
1534         std::vector<int> connections;
1535         std::vector<int> execBoxes;
1536
1537         std::map<int, GObjectController*>::iterator it;
1538
1539         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1540                 GObjectController *desc = it->second;
1541                 int type = desc->getGObjectType();
1542
1543                 if (type == GBLACKBOX) {
1544                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1545
1546                         std::string pkg = mod->getBBTKPackage();
1547                         bool existsPkg = false;
1548                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1549                                 if (packages[t] == pkg) {
1550                                         existsPkg = true;
1551                                 }
1552                         }
1553                         if (!existsPkg) {
1554                                 packages.push_back(pkg);
1555                         }
1556
1557                         boxes.push_back(it->first);
1558                         if (mod->isExecutable()) {
1559                                 execBoxes.push_back(it->first);
1560                         }
1561                 } else if (type == GCONNECTOR) {
1562                         connections.push_back(it->first);
1563                 }
1564         }
1565
1566         std::string script = "";
1567
1568         script += "include std\n"; // EED
1569         script += "include itkvtk\n"; // EED
1570         int i;
1571         for (i = 0; i < (int) packages.size(); i++) {
1572                 script += "include ";
1573                 script += packages[i];
1574                 script += "\n";
1575         }
1576         script += "\n";
1577
1578         // Definition of a complex box
1579         script += "define ";
1580         script += _cbName + " " + _cbPackageName;
1581         script += "\n";
1582         script += "\n";
1583
1584         script += "author \"";
1585         script += _Author;
1586         script += "\"\n";
1587
1588         script += "description \"";
1589         script += _Description;
1590         script += "\"\n";
1591         script += "\n";
1592
1593         script += "category \"";
1594         script += _Category;
1595         script += "\"\n";
1596         script += "\n";
1597
1598         // Create boxes
1599         for (i = 0; i < (int) boxes.size(); i++) {
1600                 script += "new ";
1601                 int id = boxes[i];
1602                 GObjectController *control = _controllers[id];
1603                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1604
1605                 script += model->getBBTKType();
1606                 script += " ";
1607                 script += model->getBBTKName();
1608                 script += "\n";
1609
1610                 std::vector<GPortModel*> inputs = model->getInputPorts();
1611                 for (int j = 0; j < (int) inputs.size(); j++) {
1612                         GPortModel* inputPort = inputs[j];
1613                         if (inputPort->isValueSet()) {
1614                                 script += "  set ";
1615                                 script += model->getBBTKName();
1616                                 script += ".";
1617                                 script += inputPort->getBBTKName();
1618                                 script += " ";
1619                                 script += inputPort->getValue();
1620                                 script += "\n";
1621                         }
1622                 }
1623                 script += "\n";
1624
1625         }
1626
1627         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1628
1629         std::string complexInputs = "";
1630         std::string complexOutputs = "";
1631         script += "\n";
1632
1633         for (i = 0; i < (int) connections.size(); i++) {
1634                 int id = connections[i];
1635                 GObjectController *control = _controllers[id];
1636                 GConnectorModel *model = (GConnectorModel*) control->getModel();
1637
1638                 //Connection info
1639                 GPortModel *start = model->getStartPort();
1640                 GBoxModel *startBox = start->getParentBox();
1641
1642                 GPortModel *end = model->getEndPort();
1643                 GBoxModel *endBox = end->getParentBox();
1644
1645                 if (startBox->getGObjectType() == GCOMPLEXINPUTPORT) {
1646                         complexInputs += "input ";
1647                         complexInputs += startBox->getBBTKName();
1648
1649                         complexInputs += " ";
1650                         complexInputs += endBox->getBBTKName();
1651                         complexInputs += ".";
1652                         complexInputs += end->getBBTKName();
1653
1654                         complexInputs += " ";
1655                         complexInputs += "\" \"";
1656
1657                         complexInputs += "\n";
1658                 } else if (endBox->getGObjectType() == GCOMPLEXOUTPUTPORT) {
1659                         complexOutputs += "output ";
1660                         complexOutputs += endBox->getBBTKName();
1661
1662                         complexOutputs += " ";
1663                         complexOutputs += startBox->getBBTKName();
1664                         complexOutputs += ".";
1665                         complexOutputs += start->getBBTKName();
1666
1667                         complexOutputs += " ";
1668                         complexOutputs += "\" \"";
1669
1670                         complexOutputs += "\n";
1671                 } else {
1672                         script += "connect ";
1673                         script += startBox->getBBTKName();
1674                         script += ".";
1675                         script += start->getBBTKName();
1676
1677                         script += " ";
1678
1679                         //End Connection info
1680                         script += endBox->getBBTKName();
1681                         script += ".";
1682                         script += end->getBBTKName();
1683
1684                         script += "\n";
1685                 }
1686         }
1687
1688         script += "\n";
1689         script += "# Complex input ports\n";
1690         script += complexInputs;
1691
1692         script += "\n";
1693         script += "# Complex output ports\n";
1694         script += complexOutputs;
1695
1696         script += "\n";
1697
1698         for (i = 0; i < (int) execBoxes.size(); i++) {
1699                 script += "exec ";
1700                 int id = execBoxes[i];
1701                 GObjectController *control = _controllers[id];
1702                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1703
1704                 script += model->getBBTKName();
1705                 script += "\n";
1706         }
1707
1708         script += "\n";
1709         script += "endefine";
1710         script += "\n";
1711
1712         return script;
1713 }
1714
1715 //=========================================================================
1716
1717 void wxVtkSceneManager::deleteAllBoxes() {
1718         std::map<int, GObjectController*>::iterator it;
1719         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1720                 GObjectController *cont = it->second;
1721                 cont->removeFromScene();
1722                 unregisterController((InteractorStyleMaracas*) cont);
1723         }
1724         _selectedObjects.clear();
1725         _controllers.clear();
1726 }
1727
1728 //=========================================================================
1729
1730
1731 void wxVtkSceneManager::refresh() {
1732         _baseView->Refresh();
1733 }
1734 //=========================================================================
1735
1736 void wxVtkSceneManager::refreshScene() {
1737         _baseView->RefreshView();
1738 }
1739
1740 //=========================================================================
1741
1742 void wxVtkSceneManager::centerView() {
1743         double temp[3];
1744         _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1745         _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
1746         _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1747         _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
1748 }
1749
1750 //=========================================================================
1751
1752 void wxVtkSceneManager::saveDiagram(std::string &content) {
1753         char buffer[50];
1754
1755         content += "CATEGORY:";
1756         content += GetCategory();
1757         content += "\n";
1758
1759         content += "DESCRIPTION:";
1760         content += GetDescription();
1761         content += "\n";
1762
1763         content += "AUTHOR:";
1764         content += GetAuthor();
1765         content += "\n";
1766
1767         //Print info IF COMPLEX BOX
1768         content += "COMPLEXBOX:";
1769         if (_isComplexBox) {
1770                 content += "TRUE\n";
1771                 //ups1
1772                 content += "COMPLEXBOXNAME:";
1773                 content += GetCbName();
1774                 content += "\n";
1775
1776                 content += "PACKAGENAME:";
1777                 content += GetCbPackageName();
1778                 content += "\n";
1779
1780                 /* CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1781                  * External inputs are possible for applications and complex boxes
1782                 //Print info complex input ports
1783                 std::vector<int> inputs = getComplexInputPorts();
1784                 int insize = inputs.size();
1785                 content += "COMPLEXINPUTS:";
1786                 sprintf(buffer, "%d", insize);
1787                 content += buffer;
1788                 content += "\n";
1789
1790                 for (int i = 0; i < insize; i++) {
1791                         int id = inputs[i];
1792                         GObjectController *cont = _controllers[id];
1793                         cont->getModel()->save(content);
1794                 }
1795                 */
1796
1797                 //Print info complex output ports
1798                 std::vector<int> outputs = getComplexOutputPorts();
1799                 int outsize = outputs.size();
1800                 content += "COMPLEXOUTPUTS:";
1801                 sprintf(buffer, "%d", outsize);
1802                 content += buffer;
1803                 content += "\n";
1804
1805                 for (int i = 0; i < outsize; i++) {
1806                         int id = outputs[i];
1807                         GObjectController *cont = _controllers[id];
1808                         cont->getModel()->save(content);
1809                 }
1810         } // _isComplexBox
1811         else {
1812                 content += "FALSE\n";
1813         }
1814
1815         // CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1816         // External inputs are possible for applications and complex boxes
1817         std::vector<int> inputs = getComplexInputPorts();
1818         int insize = inputs.size();
1819         content += "COMPLEXINPUTS:";
1820         sprintf(buffer, "%d", insize);
1821         content += buffer;
1822         content += "\n";
1823
1824         for (int i = 0; i < insize; i++) {
1825                 int id = inputs[i];
1826                 GObjectController *cont = _controllers[id];
1827                 cont->getModel()->save(content);
1828         }
1829
1830         //Print boxes
1831         std::vector<int> boxes = getBlackBoxes();
1832         int bsize = boxes.size();
1833         content += "BOXES:";
1834         sprintf(buffer, "%d", bsize);
1835         content += buffer;
1836         content += "\n";
1837
1838         for (int i = 0; i < bsize; i++) {
1839                 int id = boxes[i];
1840                 GObjectController *cont = _controllers[id];
1841                 cont->getModel()->save(content);
1842         }
1843
1844         //Print connections
1845         std::vector<int> connections = getConnections();
1846         int csize = connections.size();
1847         content += "CONNECTIONS:";
1848         sprintf(buffer, "%d", csize);
1849         content += buffer;
1850         content += "\n";
1851
1852         for (int i = 0; i < csize; i++) {
1853                 int id = connections[i];
1854                 GObjectController *cont = _controllers[id];
1855                 cont->getModel()->save(content);
1856         }
1857
1858 }
1859
1860 //=========================================================================
1861 void wxVtkSceneManager::getCleanLine(stringstream &inputStream, std::string &line)
1862 {
1863         getline(inputStream, line,'\n');
1864         int size = line.length();
1865     if (size>0)
1866         {
1867                 if ( line[ size-1 ]==13  )
1868                 {
1869                         line.erase(size-1,1);
1870                 } // if line
1871         } // if size
1872 }
1873         
1874 //=========================================================================
1875 void wxVtkSceneManager::loadDiagram(stringstream &inputStream) 
1876 {
1877         int size;
1878         std::string version = "<void>";
1879         std::string line = "";
1880         char delims[] = ":";
1881         char *result = NULL;
1882         char poort[POORT_MAX_LINE_SIZE_INTERPRETER];
1883
1884         getCleanLine(inputStream, line);
1885
1886         bool start = false;
1887         while (!inputStream.eof()) {
1888                 if (line == "" || line[0] == '#') {
1889                         line="";
1890                         getCleanLine(inputStream, line);
1891                         
1892                         if (line == "# - BBTKGEditor v 1.0 BBG BlackBox Diagram file") {
1893                                 version = line.substr(18, 3);
1894                         }
1895                         if (line == "# - BBTKGEditor v 1.1 BBG BlackBox Diagram file") {
1896                                 version = line.substr(18, 3);
1897                         }
1898                         if (line == "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file") {
1899                                 version = line.substr(18, 3);
1900                         }
1901                         if (line == "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file") {
1902                                 version = line.substr(18, 3);
1903                         }
1904                         if (line == "# - BBTKGEditor v 1.4 BBG BlackBox Diagram file") {
1905                                 version = line.substr(18, 3);
1906                         }
1907                 } else if (line == "APP_START") {
1908                         start = true;
1909                         break;
1910                 }
1911         }
1912
1913
1914         if (start) {
1915
1916                 if ((version != "1.0") && (version != "1.1")) {
1917                         getCleanLine(inputStream, line);//CATEGORY:<category of the box>
1918                         char categoryTmp[30];
1919                         strcpy(categoryTmp, line.c_str());
1920                         result = strtok(categoryTmp, delims);
1921                         result = strtok(NULL, delims);
1922                         SetCategory(result);
1923
1924                         getCleanLine(inputStream, line);//DESCRIPTION:<description of the box>
1925                         char descriptionTmp[1024];
1926                         strcpy(descriptionTmp, line.c_str());
1927                         result = strtok(descriptionTmp, delims);
1928                         result = strtok(NULL, delims);
1929                         SetDescription(result);
1930
1931                         getCleanLine(inputStream, line);//AUTHOR:<author>
1932                         char authorTmp[255];
1933                         strcpy(authorTmp, line.c_str());
1934                         result = strtok(authorTmp, delims);
1935                         result = strtok(NULL, delims);
1936                         SetAuthor(result);
1937                 }
1938
1939                 //----------
1940                 getCleanLine(inputStream, line);//COMPLEX_BOX:TRUE|FALSE
1941                 char complexTmp[30];
1942                 strcpy(complexTmp, line.c_str());
1943                 result = strtok(complexTmp, delims);
1944                 result = strtok(NULL, delims);
1945                 std::string isComplexBox(result);
1946
1947                 if (isComplexBox == "TRUE") {
1948                         _isComplexBox = true;
1949
1950                         if ((version != "1.0") && (version != "1.1")) {
1951                                 getCleanLine(inputStream, line);//COMPLEXNAME:<name of the complex box>
1952                                 char complexboxnameTmp[255];
1953                                 strcpy(complexboxnameTmp, line.c_str());
1954                                 result = strtok(complexboxnameTmp, delims);
1955                                 result = strtok(NULL, delims);
1956                                 SetCbName(result);
1957
1958                                 getCleanLine(inputStream, line);//PACKAGENAME:<name of the package of the box>
1959                                 char packagenameTmp[255];
1960                                 strcpy(packagenameTmp, line.c_str());
1961                                 result = strtok(packagenameTmp, delims);
1962                                 result = strtok(NULL, delims);
1963                                 SetCbPackageName(result);
1964                         }
1965
1966                         if(version < "1.4")
1967                         {
1968                                 //-----------------------
1969                                 //- COMPLEX INPUT PORTS
1970                                 //-----------------------
1971                                 getCleanLine(inputStream, line);//COMPLEXINPUTS:num
1972                                 char inputs[30];
1973                                 strcpy(inputs, line.c_str());
1974                                 result = strtok(inputs, delims);
1975                                 result = strtok(NULL, delims);
1976
1977                                 int numInputs;
1978                                 std::istringstream inps(result);
1979                                 inps >> numInputs;
1980
1981                                 for (int i = 0; i < numInputs; i++) {
1982                                         //----------
1983                                         getCleanLine(inputStream, line);//COMPLEX_PORT
1984                                         getCleanLine(inputStream, line);//name
1985                                         std::string inputPortName(line);
1986
1987                                         //----------
1988                                         getCleanLine(inputStream, line);//xInic:yInic:zInic
1989                                         char coord[80];
1990                                         strcpy(coord, line.c_str());
1991                                         result = strtok(coord, delims);//xInic
1992                                         std::string xInic(result);
1993                                         result = strtok(NULL, delims);//yInic
1994                                         std::string yInic(result);
1995                                         result = strtok(NULL, delims);//zInic
1996                                         std::string zInic(result);
1997
1998                                         double xIn, yIn, zIn;
1999                                         std::istringstream xSt(xInic);
2000                                         xSt >> xIn;
2001                                         std::istringstream ySt(yInic);
2002                                         ySt >> yIn;
2003                                         std::istringstream zSt(zInic);
2004                                         zSt >> zIn;
2005
2006                                         if (version<="1.2")
2007                                         {
2008                                                 if (zIn==900) zIn=GPOSITION_Z;
2009                                         }
2010
2011                                         getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2012
2013                                         configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2014
2015                                 } // for input complex box
2016                         }
2017
2018
2019                         //-----------------------
2020                         //- COMPLEX OUTPUT PORTS
2021                         //-----------------------
2022
2023                         getCleanLine(inputStream, line);//COMPLEXOUTPUTS:num
2024                         char outputs[30];
2025                         strcpy(outputs, line.c_str());
2026                         result = strtok(outputs, delims);
2027                         result = strtok(NULL, delims);
2028
2029                         int numOutputs;
2030                         std::istringstream outps(result);
2031                         outps >> numOutputs;
2032
2033                         for (int i = 0; i < numOutputs; i++) {
2034                                 //----------
2035                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2036                                 getCleanLine(inputStream, line);//name
2037                                 std::string outputPortName(line);
2038
2039                                 //----------
2040                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2041                                 char coord[80];
2042                                 strcpy(coord, line.c_str());
2043                                 result = strtok(coord, delims);//xInic
2044                                 std::string xInic(result);
2045                                 result = strtok(NULL, delims);//yInic
2046                                 std::string yInic(result);
2047                                 result = strtok(NULL, delims);//zInic
2048                                 std::string zInic(result);
2049
2050                                 double xIn, yIn, zIn;
2051                                 std::istringstream xSt(xInic);
2052                                 xSt >> xIn;
2053                                 std::istringstream ySt(yInic);
2054                                 ySt >> yIn;
2055                                 std::istringstream zSt(zInic);
2056                                 zSt >> zIn;
2057
2058                                 if (version<="1.2")
2059                                 {
2060                                         if (zIn==900) zIn=GPOSITION_Z;
2061                                 }
2062                                 
2063                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2064
2065                                 configGComBoxInputOutputPort(false, outputPortName, xIn, yIn, zIn);
2066
2067                         } // for output complex box
2068
2069                 } // complex box
2070
2071                 // RaC2012 2nd Hackfest
2072                 if(version >= "1.4")
2073                 {
2074                         //-----------------------
2075                         //- EXTERNAL INPUT PORTS
2076                         //-----------------------
2077                         getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2078                         char inputs[30];
2079                         strcpy(inputs, line.c_str());
2080                         result = strtok(inputs, delims);
2081                         result = strtok(NULL, delims);
2082
2083                         int numInputs;
2084                         std::istringstream inps(result);
2085                         inps >> numInputs;
2086
2087                         for (int i = 0; i < numInputs; i++) {
2088                                 //----------
2089                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2090                                 getCleanLine(inputStream, line);//name
2091                                 std::string inputPortName(line);
2092
2093                                 //----------
2094                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2095                                 char coord[80];
2096                                 strcpy(coord, line.c_str());
2097                                 result = strtok(coord, delims);//xInic
2098                                 std::string xInic(result);
2099                                 result = strtok(NULL, delims);//yInic
2100                                 std::string yInic(result);
2101                                 result = strtok(NULL, delims);//zInic
2102                                 std::string zInic(result);
2103
2104                                 double xIn, yIn, zIn;
2105                                 std::istringstream xSt(xInic);
2106                                 xSt >> xIn;
2107                                 std::istringstream ySt(yInic);
2108                                 ySt >> yIn;
2109                                 std::istringstream zSt(zInic);
2110                                 zSt >> zIn;
2111
2112                                 if (version<="1.2")
2113                                 {
2114                                         if (zIn==900) zIn=GPOSITION_Z;
2115                                 }
2116                                 
2117                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2118                                 configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2119
2120                         } // for external input
2121                 }
2122
2123
2124                 //----------
2125                 getCleanLine(inputStream, line);//BOXES:num
2126                 char boxes[9];
2127                 strcpy(boxes, line.c_str());
2128                 result = strtok(boxes, delims);
2129                 result = strtok(NULL, delims);
2130
2131                 int numBoxes;
2132                 std::istringstream is(result);
2133                 is >> numBoxes;
2134
2135                 for (int i = 0; i < numBoxes; i++) {
2136                         //----------
2137                         getCleanLine(inputStream, line);//BOX
2138                         getCleanLine(inputStream, line);//package:type:name
2139                         char box[150];
2140                         strcpy(box, line.c_str());
2141                         result = strtok(box, delims);//package
2142                         std::string package(result);
2143                         result = strtok(NULL, delims);//type
2144                         std::string type(result);
2145                         result = strtok(NULL, delims);//name
2146                         std::string name(result);
2147
2148                         getCleanLine(inputStream, line);//ISEXEC:TRUE|FALSE
2149                         char exec[15];
2150                         strcpy(exec, line.c_str());
2151                         result = strtok(exec, delims);//ISEXEC
2152                         result = strtok(NULL, delims);//TRUE|FALSE
2153                         std::string isExec(result);
2154
2155                         //----------
2156                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2157                         char coord[80];
2158                         strcpy(coord, line.c_str());
2159                         result = strtok(coord, delims);//xInic
2160                         std::string xInic(result);
2161                         result = strtok(NULL, delims);//yInic
2162                         std::string yInic(result);
2163                         result = strtok(NULL, delims);//zInic
2164                         std::string zInic(result);
2165
2166                         double xIn, yIn, zIn;
2167                         std::istringstream xSt(xInic);
2168                         xSt >> xIn;
2169                         std::istringstream ySt(yInic);
2170                         ySt >> yIn;
2171                         std::istringstream zSt(zInic);
2172                         zSt >> zIn;
2173
2174                         
2175                         if (version<="1.2")
2176                         {
2177                                 if (zIn==900) zIn=GPOSITION_Z;
2178                         }
2179                         
2180                         
2181                         //----------
2182                         getCleanLine(inputStream, line);//xEnd:yEnd:zEnd
2183                         strcpy(coord, line.c_str());
2184                         result = strtok(coord, delims);//xEnd
2185                         std::string xEnd(result);
2186                         result = strtok(NULL, delims);//yEnd
2187                         std::string yEnd(result);
2188                         result = strtok(NULL, delims);//zEnd
2189                         std::string zEnd(result);
2190
2191                         double xEn, yEn, zEn;
2192                         std::istringstream xEt(xEnd);
2193                         xEt >> xEn;
2194                         std::istringstream yEt(yEnd);
2195                         yEt >> yEn;
2196                         std::istringstream zEt(zEnd);
2197                         zEt >> zEn;
2198
2199                         if (version<="1.2")
2200                         {
2201                                 if (zEn==900) zEn=GPOSITION_Z;
2202                         }
2203                         
2204                         
2205                         bool boxExecutable = false;
2206                         if (isExec == "TRUE") {
2207                                 boxExecutable = true;
2208                         }
2209
2210                         int idBox = createGBlackBox(xIn, yIn, package, type);
2211                         configGBlackBox(idBox, xIn, yIn, zIn, name, boxExecutable, xEn, yEn, zEn);
2212
2213                         GObjectController *cont = _controllers[idBox];
2214                         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
2215
2216                         //----------
2217                         getCleanLine(inputStream, line);//PORT o FIN_BOX
2218                         std::string port = line.substr(0, 4);
2219                         while (port == "PORT") 
2220                         {
2221                                 getCleanLine(inputStream, line);//name:value
2222                                 if (line.length()>=POORT_MAX_LINE_SIZE_INTERPRETER-1)
2223                                 {
2224                                         line=line.substr(0, POORT_MAX_LINE_SIZE_INTERPRETER-1);
2225                                 }
2226                                 
2227                                 strcpy(poort, line.c_str());
2228                                 result = strtok(poort, delims);//name
2229                                 std::string name(result);
2230                                 
2231                                 //EED 16 oct 2012 Adding at the end character " if necesary in the value definition
2232                                 result = strtok(NULL, delims);//value
2233                                 if (( result[0]=='"' ) && ( result[ strlen(result)-1 ]!='"' )  )
2234                                 {
2235                                         std::string tmpString=" ";
2236                                         tmpString[0]=34;         // character "
2237                                         strcat(result,tmpString.c_str());
2238                                 }
2239                                 std::string value(result);
2240
2241                                 bbmod->setValueToInput(name, value);
2242
2243                                 getCleanLine(inputStream, line);//PORT o FIN_BOX
2244                                 port = line.substr(0, 4);
2245                         } // while
2246
2247                         //EED                           bbmod->notifyObservers(_idManager);
2248                 } // for boxes
2249
2250                 /// CONNECTIONS
2251                 //----------
2252                 getCleanLine(inputStream, line);//CONNECTIONS:num
2253                 char conns[30];
2254                 strcpy(conns, line.c_str());
2255                 result = strtok(conns, delims);
2256                 result = strtok(NULL, delims);
2257
2258                 int numConns;
2259                 std::istringstream isCons(result);
2260                 isCons >> numConns;
2261                 
2262                 
2263                 for (int i = 0; i < numConns; i++) {
2264                         //----------
2265                         getCleanLine(inputStream, line);//CONNECTION
2266                         getCleanLine(inputStream, line);//Startbox.PortName:EndBox.PortName
2267
2268                         char connec[200];
2269                         strcpy(connec, line.c_str());
2270                         result = strtok(connec, delims);
2271                         std::string nameStartBox(result);
2272                         result = strtok(NULL, delims);
2273                         std::string nameStartPort(result);
2274                         result = strtok(NULL, delims);
2275                         std::string nameEndBox(result);
2276                         result = strtok(NULL, delims);
2277                         std::string nameEndPort(result);
2278
2279                         int idCon = configGConnetion(nameStartBox, nameStartPort,nameEndBox, nameEndPort);
2280
2281                         if (version != "1.0") {
2282                                 //Readding control points of the manualContour
2283                                 GConnectorController *tempp             = (GConnectorController*) _controllers[idCon];
2284                                 GConnectorModel *conMod                 = (GConnectorModel*) tempp->getModel();
2285                                 vtkGConnectorView *conView              = (vtkGConnectorView*) tempp->getView();
2286                                 getCleanLine(inputStream, line); //NumberOfControlPoints:##
2287                                 strcpy(conns, line.c_str());
2288                                 result = strtok(conns, delims);
2289                                 result = strtok(NULL, delims);
2290
2291                                 int numberOfControlPoints;
2292                                 std::istringstream isCons(result);
2293                                 isCons >> numberOfControlPoints;
2294
2295                                 for (int ii = 0; ii < numberOfControlPoints; ii++) {
2296                                         getCleanLine(inputStream, line); //XX:YY:ZZ
2297                                         char connec[200];
2298                                         strcpy(connec, line.c_str());
2299
2300                                         double px, py, pz;
2301                                         result = strtok(connec, delims);
2302                                         std::istringstream isPointX(result);
2303                                         isPointX >> px;
2304                                         result = strtok(NULL, delims);
2305                                         std::istringstream isPointY(result);
2306                                         isPointY >> py;
2307                                         result = strtok(NULL, delims);
2308                                         std::istringstream isPointZ(result);
2309                                         isPointZ >> pz;
2310                 
2311                                         if (version<="1.2")
2312                                         {
2313                                                 if (pz==900) pz=GPOSITION_Z;
2314                                         }
2315                                         
2316                                         conMod->getManualContourModel()->InsertPoint_id(ii + 1, px, py, pz);
2317                                         conView->getManualContourView()->AddPoint();
2318                                 }
2319                         }// version !=1.0
2320                 } // for numConns
2321         } // start      
2322 }
2323 //=========================================================================
2324
2325
2326 //=========================================================================
2327 void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput,
2328                 std::string portName, double xIn, double yIn, double zIn) 
2329 {
2330         int idPort;
2331         if (inputoutput == true) {
2332                 idPort = createGComplexBoxInputPort(portName);
2333         } else {
2334                 idPort = createGComplexBoxOutputPort(portName);
2335         }
2336         GObjectController *cont = _controllers[idPort];
2337         GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2338         cbmod->setInicPoint(xIn, yIn, zIn);
2339         cbmod->notifyObservers(_idManager);
2340 }
2341 //=========================================================================
2342
2343
2344 //=========================================================================
2345 GBoxModel* wxVtkSceneManager::findGBox(std::string boxname) 
2346 {
2347         int j;
2348         int idB;
2349         GBoxModel *bMod;
2350         GBoxModel *boxModel = NULL;
2351         std::vector<int> lstB = getBlackBoxes();
2352         for (j = 0; j < (int) lstB.size(); j++) {
2353                 idB = lstB[j];
2354                 bMod = (GBoxModel*) _controllers[idB]->getModel();
2355                 if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2356                         boxModel = bMod;
2357                 }
2358         } // for
2359
2360
2361
2362         if (boxModel == NULL) {
2363                 std::vector<int> lstInputs = getComplexInputPorts();
2364                 for (j = 0; j < (int) lstInputs.size(); j++) {
2365                         idB = lstInputs[j];
2366                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2367                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2368                                 boxModel = bMod;
2369                         }
2370                 } // for
2371
2372                 if(_isComplexBox)
2373                 {
2374                 std::vector<int> lstOutputs = getComplexOutputPorts();
2375                 for (j = 0; j < (int) lstOutputs.size(); j++) {
2376                         int idB = lstOutputs[j];
2377                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2378                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2379                                 boxModel = bMod;
2380                         }
2381                 } // for
2382                 }
2383
2384         } // inputs/outputs
2385
2386         return boxModel;
2387 }
2388
2389 //=========================================================================
2390
2391
2392 int wxVtkSceneManager::configGConnetion(std::string nameStartBox,
2393                 std::string nameStartPort, std::string nameEndBox,
2394                 std::string nameEndPort) {
2395
2396         GBoxModel *boxModel;
2397         GPortModel *startP = NULL;
2398         GPortModel *endP = NULL;
2399
2400         boxModel = findGBox(nameStartBox);
2401         if(boxModel == NULL)
2402         {
2403                 printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2404         }
2405         if (boxModel != NULL) {
2406                 startP = boxModel->getOutputPort(nameStartPort);
2407                 if(startP == NULL){
2408                         printf("SCP: ERROR in wxVtkSceneManager::configGConnetion start port %s is null. Port name is probably wrong.\n", nameStartPort.c_str());
2409                 }
2410         }
2411
2412         boxModel = findGBox(nameEndBox);
2413         if(boxModel == NULL)
2414         {
2415                  printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2416         }
2417         if (boxModel != NULL) {
2418                 endP = boxModel->getInputPort(nameEndPort);
2419                 if(endP == NULL){
2420                         printf("SCP: ERROR in wxVtkSceneManager::configGConnetion end port %s is null. Port name is probably wrong\n", nameStartPort.c_str(), nameEndPort.c_str());
2421                 }
2422         }
2423
2424         //ups2
2425         int idCon = createGConnector(startP);
2426
2427         _worldState = NOTHING_HAPPENS;
2428         GConnectorController *tempp = (GConnectorController*) _controllers[idCon];
2429
2430         GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2431         vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2432         tempp->endContourCreation();
2433         conMod->setEndPort(endP);
2434         conView->updateStartEndPoints();
2435         return idCon;
2436 }
2437
2438 //=========================================================================
2439
2440 bool wxVtkSceneManager::boxExist(std::string boxname) {
2441         bool ok = false;
2442         std::map<int, GObjectController*>::iterator it;
2443         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2444                 GObjectController *cont = it->second;
2445                 if (cont->getModel()->getBBTKName() == boxname) {
2446                         ok = true;
2447                 }
2448         }
2449         return ok;
2450 }
2451
2452 //=========================================================================
2453
2454 std::vector<int> wxVtkSceneManager::getBlackBoxes() {
2455         std::vector<int> vect;
2456         std::map<int, GObjectController*>::iterator it;
2457         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2458                 GObjectController *cont = it->second;
2459                 if (cont->getGObjectType() == GBLACKBOX) {
2460                         vect.push_back(cont->getId());
2461                 }
2462         }
2463         return vect;
2464 }
2465
2466 //=========================================================================
2467
2468 std::vector<int> wxVtkSceneManager::getComplexInputPorts() {
2469         std::vector<int> vect;
2470         std::map<int, GObjectController*>::iterator it;
2471         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2472                 GObjectController *cont = it->second;
2473                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT) {
2474                         vect.push_back(cont->getId());
2475                 }
2476         }
2477         return vect;
2478 }
2479
2480 //=========================================================================
2481
2482 std::vector<int> wxVtkSceneManager::getComplexOutputPorts() {
2483         std::vector<int> vect;
2484         std::map<int, GObjectController*>::iterator it;
2485         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2486                 GObjectController *cont = it->second;
2487                 if (cont->getGObjectType() == GCOMPLEXOUTPUTPORT) {
2488                         vect.push_back(cont->getId());
2489                 }
2490         }
2491         return vect;
2492 }
2493
2494 //=========================================================================
2495
2496 std::vector<int> wxVtkSceneManager::getConnections() {
2497         std::vector<int> vect;
2498         std::map<int, GObjectController*>::iterator it;
2499         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2500                 GObjectController *cont = it->second;
2501                 if (cont->getGObjectType() == GCONNECTOR) {
2502                         vect.push_back(cont->getId());
2503                 }
2504         }
2505         return vect;
2506 }
2507
2508 //=========================================================================
2509
2510 bool wxVtkSceneManager::isComplexBox() {
2511         return _isComplexBox;
2512 }
2513
2514 //=========================================================================
2515
2516 void wxVtkSceneManager::setComplexBox(bool val) {
2517         _isComplexBox = val;
2518 }
2519
2520 //=========================================================================
2521
2522 int wxVtkSceneManager::addObjectController(GObjectController* objController) {
2523         //Register the controller of the new object
2524         registerController((InteractorStyleMaracas*) objController);
2525         //Add the object to the objects list
2526         int newId = _contLastId;//_controllers.size();
2527         objController->setId(newId);
2528         _controllers[newId] = objController;
2529         //std::cout << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = " << _contLastId << std::endl;
2530         _contLastId++;
2531         return newId;
2532 }
2533
2534 //=========================================================================
2535
2536 void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
2537
2538         std::map<int, int> oldIdNewIdBoxes;
2539         std::vector<int> connections;
2540
2541         std::vector<int> newBoxesID;
2542
2543         std::map<int, GObjectController*>::iterator it;
2544         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2545             GObjectController *cont = it->second;
2546             int type = cont->getGObjectType();
2547
2548             if (type == GBLACKBOX) {
2549                 // Copy black box
2550                 double xInic, yInic, zInic;
2551                 GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2552                 copyBox->getInicPoint(xInic, yInic, zInic);
2553                 int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2554                         copyBox->getBBTKType());
2555
2556                 int idcB = copyBox->getObjectId();
2557                 oldIdNewIdBoxes[idcB] = idBox;
2558                 cont = _controllers[idBox];
2559                 GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2560                 newbox->setInicPoint(xInic, yInic, zInic);
2561                 int num = newbox->getNumInputPorts();
2562                 for (int j = 0; j < num; j++) {
2563                     newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2564                 }
2565                 newbox->move(xInic + 20, yInic + 20, zInic);
2566                 newbox->notifyObservers(_idManager);
2567                 newBoxesID.push_back(newbox->getObjectId());
2568
2569             } else if (type == GCONNECTOR) {
2570                 int idCon = cont->getId();
2571                 connections.push_back(idCon);
2572             }
2573
2574         }
2575
2576         for (int i = 0; i < (int) connections.size(); i++) {
2577             int objId = connections[i];
2578             GObjectController *cont = objectsMap[objId];
2579             GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2580
2581             GPortModel* startPort = connectModel->getStartPort();
2582             int startPortIndex = startPort->getPosInBox();
2583             GPortModel* endPort = connectModel->getEndPort();
2584             int endPortIndex = endPort->getPosInBox();
2585
2586             GBlackBoxModel* startPortParentBox =
2587                     (GBlackBoxModel*) startPort->getParentBox();
2588             GBlackBoxModel* endPortParentBox =
2589                     (GBlackBoxModel*) endPort->getParentBox();
2590
2591             int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2592             int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2593
2594             GBlackBoxModel* newStartBox =
2595                     (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2596             GBlackBoxModel* newEndBox =
2597                     (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2598
2599             GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2600             GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2601
2602             // Creates connection
2603             int idCon = createGConnector(newStartPort);
2604             GConnectorController *tempp =
2605                     (GConnectorController*) _controllers[idCon];
2606             GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2607             vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2608             tempp->endContourCreation();
2609             conMod->setEndPort(newEndPort);
2610             conView->updateStartEndPoints();
2611         }
2612
2613         /// the new selected boxes are the duplicate ones
2614
2615         UnSelectBlackBoxes();
2616         for (int i = 0; i < newBoxesID.size(); i++) {
2617             _selectedObjects.push_back(newBoxesID.at(i));
2618         }
2619
2620         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
2621             int id = _selectedObjects[i];
2622             GObjectController* cont = _controllers[id];
2623
2624             cont->getView()->setState(SELECTED);
2625             cont->getModel()->notifyObservers(_idManager);
2626
2627         }
2628     }
2629
2630
2631 //=========================================================================
2632
2633 int wxVtkSceneManager::getNumSelectedObjects() {
2634         return _selectedObjects.size();
2635 }
2636
2637 //=========================================================================
2638
2639 std::map<int, GObjectController*> wxVtkSceneManager::getSelectedObjects() {
2640         std::map<int, GObjectController*> mapSelected;
2641
2642         std::map<int, GObjectController*>::iterator it;
2643         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2644                 GObjectController *cont = it->second;
2645                 if (cont->getGObjectType() == GBLACKBOX && cont->getView()->getState()
2646                                 == SELECTED) {
2647                         mapSelected[cont->getId()] = cont;
2648                 }
2649         }
2650
2651         std::map<int, GObjectController*>::iterator it2;
2652         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
2653                 GObjectController *cont = it2->second;
2654                 if (cont->getGObjectType() == GCONNECTOR) {
2655                         GConnectorModel* cmod = (GConnectorModel*) cont->getModel();
2656                         GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2657                         GBoxModel* startPortParentBox =
2658                                         cmod->getStartPort()->getParentBox();
2659
2660                         std::map<int, GObjectController*>::iterator iterOBJ1 =
2661                                         mapSelected.find(startPortParentBox->getObjectId());
2662                         std::map<int, GObjectController*>::iterator iterOBJ2 =
2663                                         mapSelected.find(endPortParentBox->getObjectId());
2664
2665                         if (iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end()) {
2666                                 int ID = cont->getId();
2667                                 mapSelected[ID] = cont;
2668                         }
2669                 }
2670         }
2671         return mapSelected;
2672 }
2673
2674 //=========================================================================
2675
2676 void wxVtkSceneManager::addObjects(std::map<int, GObjectController*> objectsMap) {
2677
2678         std::map<int, int> oldIdNewIdBoxes;
2679         std::vector<int> connections;
2680
2681         std::map<int, GObjectController*>::iterator it;
2682         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2683                 GObjectController *cont = it->second;
2684                 int type = cont->getGObjectType();
2685
2686                 if (type == GBLACKBOX) {
2687                         // Copy black box
2688                         double xInic, yInic, zInic;
2689                         GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2690                         copyBox->getInicPoint(xInic, yInic, zInic);
2691                         int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),copyBox->getBBTKType());
2692
2693                         int idcB = copyBox->getObjectId();
2694                         oldIdNewIdBoxes[idcB] = idBox;
2695                         cont = _controllers[idBox];
2696                         GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2697                         newbox->setInicPoint(xInic, yInic, zInic);
2698                         int num = newbox->getNumInputPorts();
2699                         for (int j = 0; j < num; j++) 
2700                         {
2701                                 newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2702                         }// for j
2703                         newbox->notifyObservers(_idManager);
2704                 } else if (type == GCONNECTOR) {
2705                         int idCon = cont->getId();
2706                         connections.push_back(idCon);
2707                 }
2708
2709         }
2710
2711         for (int i = 0; i < (int) connections.size(); i++) {
2712                 int objId = connections[i];
2713                 GObjectController *cont = objectsMap[objId];
2714                 GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2715
2716                 GPortModel* startPort = connectModel->getStartPort();
2717                 int startPortIndex = startPort->getPosInBox();
2718                 GPortModel* endPort = connectModel->getEndPort();
2719                 int endPortIndex = endPort->getPosInBox();
2720
2721                 GBlackBoxModel* startPortParentBox =
2722                                 (GBlackBoxModel*) startPort->getParentBox();
2723                 GBlackBoxModel* endPortParentBox =
2724                                 (GBlackBoxModel*) endPort->getParentBox();
2725
2726                 int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2727                 int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2728
2729                 GBlackBoxModel* newStartBox =
2730                                 (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2731                 GBlackBoxModel* newEndBox =
2732                                 (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2733
2734                 GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2735                 GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2736
2737                 // Creates connection
2738                 int idCon = createGConnector(newStartPort);
2739                 GConnectorController *tempp =
2740                                 (GConnectorController*) _controllers[idCon];
2741                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2742                 vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2743                 tempp->endContourCreation();
2744                 conMod->setEndPort(newEndPort);
2745                 conView->updateStartEndPoints();
2746         }
2747
2748         std::map<int, int>::iterator itIds;
2749         for (itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds) {
2750                 int idOld = itIds->first;
2751                 int idNew = itIds->second;
2752
2753                 GBlackBoxModel* oldBox =
2754                                 (GBlackBoxModel*) objectsMap[idOld]->getModel();
2755                 GBlackBoxModel* newBox =
2756                                 (GBlackBoxModel*) _controllers[idNew]->getModel();
2757
2758                 std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2759                 std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2760                 std::vector<int> newInputConnections = newBox->getConnectedInputs();
2761                 std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2762
2763                 for (int k = 0; k < (int) oldInputConnections.size(); k++) {
2764                         bool exist = false;
2765                         //EED                           int toCreate=-1;
2766                         for (int l = 0; l < (int) newInputConnections.size() && !exist; l++) {
2767                                 if (oldInputConnections[k] == newInputConnections[l]) {
2768                                         exist = true;
2769                                 }
2770                         }
2771
2772                         if (exist == false) {
2773                                 //Create complex input
2774                                 int posInBox = oldInputConnections[k];
2775                                 GPortModel* inputPort = oldBox->getInputPort(posInBox);
2776                                 std::string inputPortName = inputPort->getBBTKName();
2777                                 int idInputPort = createGComplexBoxInputPort(inputPortName);
2778                                 GObjectController *cont = _controllers[idInputPort];
2779                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2780                                 double xIn, yIn, zIn;
2781                                 inputPort->getInicPoint(xIn, yIn, zIn);
2782                                 yIn += 20;
2783                                 cbmod->setInicPoint(xIn, yIn, zIn);
2784                                 cbmod->notifyObservers(_idManager);
2785
2786                                 GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2787
2788                                 // Creates connection
2789                                 int idCon = createGConnector(cbmod->getOutputPort(0));
2790                                 GConnectorController *tempp =
2791                                                 (GConnectorController*) _controllers[idCon];
2792                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2793                                 vtkGConnectorView *conView =
2794                                                 (vtkGConnectorView*) tempp->getView();
2795                                 tempp->endContourCreation();
2796                                 conMod->setEndPort(inputPortEnd);
2797                                 conView->updateStartEndPoints();
2798
2799                         }
2800
2801                 }
2802
2803                 for (int k = 0; k < (int) oldOutputConnections.size(); k++) {
2804                         bool exist = false;
2805                         //EED                           int toCreate=-1;
2806                         for (int l = 0; l < (int) newOutputConnections.size() && !exist; l++) {
2807                                 if (oldOutputConnections[k] == newOutputConnections[l]) {
2808                                         exist = true;
2809                                 }
2810                         }
2811
2812                         if (exist == false) {
2813                                 //Create complex output
2814                                 int posInBox = oldOutputConnections[k];
2815                                 GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2816                                 std::string outputPortName = outputPort->getBBTKName();
2817                                 int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2818                                 GObjectController *cont = _controllers[idOutputPort];
2819                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2820                                 double xIn, yIn, zIn;
2821                                 outputPort->getInicPoint(xIn, yIn, zIn);
2822                                 yIn -= 20;
2823                                 cbmod->setInicPoint(xIn, yIn, zIn);
2824                                 cbmod->notifyObservers(_idManager);
2825
2826                                 GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2827
2828                                 // Creates connection
2829                                 int idCon = createGConnector(outputPortEnd);
2830                                 GConnectorController *tempp =
2831                                                 (GConnectorController*) _controllers[idCon];
2832                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2833                                 vtkGConnectorView *conView =
2834                                                 (vtkGConnectorView*) tempp->getView();
2835                                 tempp->endContourCreation();
2836                                 conMod->setEndPort(cbmod->getInputPort(0));
2837                                 conView->updateStartEndPoints();
2838
2839                         }
2840
2841                 }
2842
2843         }
2844
2845 }
2846
2847 //=========================================================================
2848 void wxVtkSceneManager::SetCbName(std::string cbName) {
2849         _cbName = cbName;
2850         if (_cbName == "") {
2851                 _cbName = "<complex box name>";
2852         }
2853 }
2854
2855 //=========================================================================
2856 std::string wxVtkSceneManager::GetCbName() {
2857         return _cbName;
2858 }
2859
2860 //=========================================================================
2861 void wxVtkSceneManager::SetCbPackageName(std::string packagename) {
2862         _cbPackageName = packagename;
2863         if (_cbPackageName == "") {
2864                 _cbPackageName = "<package name of the complex box>";
2865         }
2866 }
2867
2868 //=========================================================================
2869 std::string wxVtkSceneManager::GetCbPackageName() {
2870         return _cbPackageName;
2871 }
2872
2873 //=========================================================================
2874 void wxVtkSceneManager::SetAuthor(std::string author) {
2875         _Author = author;
2876         if (_Author == "") {
2877                 _Author = "<author of the box>";
2878         }
2879 }
2880
2881 //=========================================================================
2882 std::string wxVtkSceneManager::GetAuthor() {
2883         return _Author;
2884 }
2885
2886 //=========================================================================
2887 void wxVtkSceneManager::SetCategory(std::string category) {
2888         _Category = category;
2889         if (_Category == "") {
2890                 _Category = "<category of the box>";
2891         }
2892 }
2893
2894 //=========================================================================
2895 std::string wxVtkSceneManager::GetCategory() {
2896         return _Category;
2897 }
2898
2899 //=========================================================================
2900 void wxVtkSceneManager::SetDescription(std::string description) {
2901         _Description = description;
2902         if (_Description == "") {
2903                 _Description = "<description of the box>";
2904         }
2905 }
2906
2907 //=========================================================================
2908 std::string wxVtkSceneManager::GetDescription() {
2909         return _Description;
2910 }
2911
2912 //=========================================================================
2913
2914 std::vector< std::string> wxVtkSceneManager::GetExternalInputsNames()
2915 {
2916
2917         std::vector< std::string> extInputs;
2918         std::map<int, GObjectController*>::iterator it;
2919         for (it = _controllers.begin(); it != _controllers.end(); ++it)
2920         {
2921                 GObjectController *cont = it->second;
2922                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT)
2923                 {
2924                         extInputs.push_back(cont->getModel()->getBBTKName());
2925                 }
2926         }
2927         return extInputs;
2928 }
2929
2930 //=========================================================================
2931
2932
2933
2934 void wxVtkSceneManager::printAll(int com , int sta ){
2935         
2936          int tmpState = sta ;
2937         if ( tmpState == NOTHING_HAPPENS ) {
2938             std::cout << "CONTROLER STATE---->NOTHING_HAPPENS\n" ;
2939         }
2940         if ( tmpState == HIGHLIGHTED ) {
2941             std::cout << "CONTROLER STATE---->HIGHLIGHTED\n" ;
2942         }
2943         if ( tmpState == POSSIBLE_CONNECTION ) {
2944             std::cout << "CONTROLER STATE---->POSSIBLE_CONNECTION\n" ;
2945         }
2946         if ( tmpState == SELECTED_POSSIBLE_CONNECTION ) {
2947             std::cout << "CONTROLER STATE---->SELECTED_POSSIBLE_CONNECTION\n" ;
2948         }
2949         if ( tmpState == CLICKED ) {
2950             std::cout << "CONTROLER STATE---->CLICKED\n" ;
2951         }
2952         if ( tmpState == DRAG ) {
2953             std::cout << "CONTROLER STATE---->DRAG\n" ;
2954         }
2955         if ( tmpState == SELECTED ) {
2956             std::cout << "CONTROLER STATE---->SELECTED\n" ;
2957         }
2958         if ( tmpState == CREATING_CONTOUR ) {
2959             std::cout << "CONTROLER STATE---->CREATING_CONTOUR\n" ;
2960         }   
2961          
2962          // :P 
2963
2964         int command = com ;
2965         if ( command == INIT_CREATION_CONTOUR ) {
2966             std::cout << "COMMAND ----> INIT_CREATION_CONTOUR \n" ;
2967         }
2968         if ( command == FIN_CREATION_CONTOUR ) {
2969             std::cout << "COMMAND ----> FIN_CREATION_CONTOUR \n" ;
2970         }
2971         if ( command == ADD_TO_SELECTED ) {
2972             std::cout << "COMMAND ----> ADD_TO_SELECTED \n" ;
2973         }
2974         if ( command == DRAG_OBJECTS ) {
2975             std::cout << "COMMAND ----> DRAG_OBJECTS \n" ;
2976         }
2977         if ( command == EDIT_BLACKBOX ) {
2978             std::cout << "COMMAND ----> EDIT_BLACKBOX \n" ;
2979         }
2980         if ( command == REMOVE_FROM_SELECTED ) {
2981             std::cout << "COMMAND ----> REMOVE_FROM_SELECTED \n" ;
2982         }
2983         
2984     }
2985
2986
2987 } // EO namespace bbtk
2988
2989 // EOF
2990