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