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