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