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