]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
07b43058ccffe014b0bdce97850b8266b79d2c97
[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
1543                         script += model->getBBTKName();
1544                         script += "\n";
1545                 }
1546
1547         }
1548
1549         return script;
1550 }
1551
1552 //=========================================================================
1553
1554 std::string wxVtkSceneManager::saveComplexBoxBBS() {
1555         std::cout<<"wxVtkSceneManager::saveComplexBoxBBS"<<std::endl;
1556         std::vector<std::string> packages;
1557         std::vector<int> boxes;
1558         std::vector<int> connections;
1559         std::vector<int> execBoxes;
1560
1561         std::map<int, GObjectController*>::iterator it;
1562
1563         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1564                 GObjectController *desc = it->second;
1565                 int type = desc->getGObjectType();
1566
1567                 if (type == GBLACKBOX) {
1568                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1569
1570                         std::string pkg = mod->getBBTKPackage();
1571                         bool existsPkg = false;
1572                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1573                                 if (packages[t] == pkg) {
1574                                         existsPkg = true;
1575                                 }
1576                         }
1577                         if (!existsPkg) {
1578                                 packages.push_back(pkg);
1579                         }
1580
1581                         boxes.push_back(it->first);
1582                         if (mod->isExecutable()) {
1583                                 execBoxes.push_back(it->first);
1584                         }
1585                 } else if (type == GCONNECTOR) {
1586                         connections.push_back(it->first);
1587                 }
1588         }
1589
1590         std::string script = "";
1591
1592         script += "include std\n"; // EED
1593         script += "include itkvtk\n"; // EED
1594         int i;
1595         for (i = 0; i < (int) packages.size(); i++) {
1596                 script += "include ";
1597                 script += packages[i];
1598                 script += "\n";
1599         }
1600         script += "\n";
1601
1602         // Definition of a complex box
1603         script += "define ";
1604         script += _cbName + " " + _cbPackageName;
1605         script += "\n";
1606         script += "\n";
1607
1608         script += "author \"";
1609         script += _Author;
1610         script += "\"\n";
1611
1612         script += "description \"";
1613         script += _Description;
1614         script += "\"\n";
1615         script += "\n";
1616
1617         script += "category \"";
1618         script += _Category;
1619         script += "\"\n";
1620         script += "\n";
1621
1622         // Create boxes
1623         for (i = 0; i < (int) boxes.size(); i++) {
1624                 script += "new ";
1625                 int id = boxes[i];
1626                 GObjectController *control = _controllers[id];
1627                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1628
1629                 script += model->getBBTKPackage()+":";
1630                 script += model->getBBTKType();
1631                 script += " ";
1632                 script += model->getBBTKName();
1633                 script += "\n";
1634
1635                 std::vector<GPortModel*> inputs = model->getInputPorts();
1636                 for (int j = 0; j < (int) inputs.size(); j++) {
1637                         GPortModel* inputPort = inputs[j];
1638                         if (inputPort->isValueSet()) {
1639                                 script += "  set ";
1640                                 script += model->getBBTKName();
1641                                 script += ".";
1642                                 script += inputPort->getBBTKName();
1643                                 script += " ";
1644                                 script += inputPort->getValue();
1645                                 script += "\n";
1646                         }
1647                 }
1648                 script += "\n";
1649
1650         }
1651
1652         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1653
1654         std::string complexInputs = "";
1655         std::string complexOutputs = "";
1656         script += "\n";
1657
1658         for (i = 0; i < (int) connections.size(); i++) {
1659                 int id = connections[i];
1660                 GObjectController *control = _controllers[id];
1661                 GConnectorModel *model = (GConnectorModel*) control->getModel();
1662
1663                 //Connection info
1664                 GPortModel *start = model->getStartPort();
1665                 GBoxModel *startBox = start->getParentBox();
1666
1667                 GPortModel *end = model->getEndPort();
1668                 GBoxModel *endBox = end->getParentBox();
1669
1670                 if (startBox->getGObjectType() == GCOMPLEXINPUTPORT) {
1671                         complexInputs += "input ";
1672                         complexInputs += startBox->getBBTKName();
1673
1674                         complexInputs += " ";
1675                         complexInputs += endBox->getBBTKName();
1676                         complexInputs += ".";
1677                         complexInputs += end->getBBTKName();
1678
1679                         complexInputs += " ";
1680                         complexInputs += "\" \"";
1681
1682                         complexInputs += "\n";
1683                 } else if (endBox->getGObjectType() == GCOMPLEXOUTPUTPORT) {
1684                         complexOutputs += "output ";
1685                         complexOutputs += endBox->getBBTKName();
1686
1687                         complexOutputs += " ";
1688                         complexOutputs += startBox->getBBTKName();
1689                         complexOutputs += ".";
1690                         complexOutputs += start->getBBTKName();
1691
1692                         complexOutputs += " ";
1693                         complexOutputs += "\" \"";
1694
1695                         complexOutputs += "\n";
1696                 } else {
1697                         script += "connect ";
1698                         script += startBox->getBBTKName();
1699                         script += ".";
1700                         script += start->getBBTKName();
1701
1702                         script += " ";
1703
1704                         //End Connection info
1705                         script += endBox->getBBTKName();
1706                         script += ".";
1707                         script += end->getBBTKName();
1708
1709                         script += "\n";
1710                 }
1711         }
1712
1713         script += "\n";
1714         script += "# Complex input ports\n";
1715         script += complexInputs;
1716
1717         script += "\n";
1718         script += "# Complex output ports\n";
1719         script += complexOutputs;
1720
1721         script += "\n";
1722
1723         if((_MessageKind!="-")&&(_MessageLevel!="-"))
1724         {
1725                 script += "message ";
1726                 script += _MessageKind;
1727                 script += " ";
1728                 script += _MessageLevel;
1729                 script += "\n";
1730         }
1731
1732         for (i = 0; i < (int) execBoxes.size(); i++) 
1733         {
1734                 script += "exec ";
1735                 int id = execBoxes[i];
1736                 GObjectController *control = _controllers[id];
1737                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1738
1739                 script += model->getBBTKName();
1740                 script += "\n";
1741         }
1742
1743         script += "\n";
1744         script += "endefine";
1745         script += "\n";
1746
1747         return script;
1748 }
1749
1750 //=========================================================================
1751
1752 void wxVtkSceneManager::deleteAllBoxes() {
1753         std::map<int, GObjectController*>::iterator it;
1754         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1755                 GObjectController *cont = it->second;
1756                 cont->removeFromScene();
1757                 unregisterController((InteractorStyleMaracas*) cont);
1758         }
1759         _selectedObjects.clear();
1760         _controllers.clear();
1761 }
1762
1763 //=========================================================================
1764
1765
1766 void wxVtkSceneManager::refresh() {
1767         _baseView->Refresh();
1768 }
1769 //=========================================================================
1770
1771 void wxVtkSceneManager::refreshScene() 
1772 {
1773         _baseView->RefreshView();
1774 }
1775
1776 //=========================================================================
1777
1778 void wxVtkSceneManager::centerView() 
1779 {
1780         double bb[6];
1781         _baseView->GetRenderer()->ComputeVisiblePropBounds(bb);
1782
1783         double temp[3];
1784         _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1785         _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
1786         _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1787         _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
1788
1789         _baseView->GetRenderer()->ResetCamera();
1790         _baseView->GetRenderer()->ResetCameraClippingRange();
1791
1792 /*
1793         double tt=_baseView->GetRenderer()->GetNearClippingPlaneTolerance();
1794         printf("EED wxVtkSceneManager::centerView() tolerance=%f\n",tt);
1795         double cr1,cr2;
1796         _baseView->GetRenderer()->GetActiveCamera()->GetClippingRange(cr1,cr2);
1797         printf("EED wxVtkSceneManager::centerView() cr1=%f  cr2=%f\n",cr1,cr2);
1798         _baseView->GetRenderer()->GetActiveCamera()->SetClippingRange(0.01,100000);
1799         _baseView->GetRenderer()->GetActiveCamera()->GetClippingRange(cr1,cr2);
1800         printf("EED wxVtkSceneManager::centerView() cr1=%f  cr2=%f\n",cr1,cr2);
1801 */
1802
1803         _baseView->GetRenderer()->Render();
1804 }
1805
1806 //=========================================================================
1807
1808 void wxVtkSceneManager::saveDiagram(std::string &content) 
1809 {
1810         char buffer[50];
1811
1812         content += "CATEGORY:";
1813         content += GetCategory();
1814         content += "\n";
1815
1816         content += "DESCRIPTION:";
1817         content += GetDescription();
1818         content += "\n";
1819
1820         content += "AUTHOR:";
1821         content += GetAuthor();
1822         content += "\n";
1823
1824         //Print info IF COMPLEX BOX
1825         content += "COMPLEXBOX:";
1826         if (_isComplexBox) {
1827                 content += "TRUE\n";
1828                 //ups1
1829                 content += "COMPLEXBOXNAME:";
1830                 content += GetCbName();
1831                 content += "\n";
1832
1833                 content += "PACKAGENAME:";
1834                 content += GetCbPackageName();
1835                 content += "\n";
1836
1837                 /* CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1838                  * External inputs are possible for applications and complex boxes
1839                 //Print info complex input ports
1840                 std::vector<int> inputs = getComplexInputPorts();
1841                 int insize = inputs.size();
1842                 content += "COMPLEXINPUTS:";
1843                 sprintf(buffer, "%d", insize);
1844                 content += buffer;
1845                 content += "\n";
1846
1847                 for (int i = 0; i < insize; i++) {
1848                         int id = inputs[i];
1849                         GObjectController *cont = _controllers[id];
1850                         cont->getModel()->save(content);
1851                 }
1852                 */
1853
1854                 //Print info complex output ports
1855                 std::vector<int> outputs = getComplexOutputPorts();
1856                 int outsize = outputs.size();
1857                 content += "COMPLEXOUTPUTS:";
1858                 sprintf(buffer, "%d", outsize);
1859                 content += buffer;
1860                 content += "\n";
1861
1862                 for (int i = 0; i < outsize; i++) {
1863                         int id = outputs[i];
1864                         GObjectController *cont = _controllers[id];
1865                         cont->getModel()->save(content);
1866                 }
1867         } // _isComplexBox
1868         else {
1869                 content += "FALSE\n";
1870         }
1871
1872         // CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1873         // External inputs are possible for applications and complex boxes
1874         std::vector<int> inputs = getComplexInputPorts();
1875         int insize = inputs.size();
1876         content += "COMPLEXINPUTS:";
1877         sprintf(buffer, "%d", insize);
1878         content += buffer;
1879         content += "\n";
1880
1881         for (int i = 0; i < insize; i++) {
1882                 int id = inputs[i];
1883                 GObjectController *cont = _controllers[id];
1884                 cont->getModel()->save(content);
1885         }
1886
1887         //Print boxes
1888         std::vector<int> boxes = getBlackBoxes();
1889         int bsize = boxes.size();
1890         content += "BOXES:";
1891         sprintf(buffer, "%d", bsize);
1892         content += buffer;
1893         content += "\n";
1894         
1895         for (int i = 0; i < bsize; i++) {
1896                 int id = boxes[i];
1897 //              std::cout<<"id "<<id<<std::endl;//CFT
1898                 GObjectController *cont = _controllers[id];
1899 //              std::cout<<"nombre: "<<cont->getStatusText()<<std::endl;//CFT
1900 //              std::cout<<"tipo: "<<cont->getModel()->getBBTKType()<<std::endl;//CFT
1901 //              //boxesDiagram[i] = cont->getModel()->getBBTKType();//CFT
1902 //              std::cout<<"nname: "<<cont->getModel()->getBBTKName()<<std::endl;//CFT
1903 //              std::cout<<"categorie: "<<GetCategory()<<std::endl;//CFT
1904 //              std::cout<<"type: "<<cont->getModel()->getGObjectType()<<std::endl;//CFT
1905                 cont->getModel()->save(content);
1906         }
1907
1908         //Print connections
1909         std::vector<int> connections = getConnections();
1910         int csize = connections.size();
1911         content += "CONNECTIONS:";
1912         sprintf(buffer, "%d", csize);
1913         content += buffer;
1914         content += "\n";
1915
1916         for (int i = 0; i < csize; i++) {
1917                 int id = connections[i];
1918                 GObjectController *cont = _controllers[id];
1919                 cont->getModel()->save(content);
1920         }
1921
1922 }
1923
1924 //=========================================================================
1925 void wxVtkSceneManager::getCleanLine(stringstream &inputStream, std::string &line)
1926 {
1927         getline(inputStream, line,'\n');
1928         int size = line.length();
1929     if (size>0)
1930         {
1931                 if ( line[ size-1 ]==13  )
1932                 {
1933                         line.erase(size-1,1);
1934                 } // if line
1935         } // if size
1936 }
1937         
1938 //=========================================================================
1939 void wxVtkSceneManager::loadDiagram(stringstream &inputStream) 
1940 {
1941         int size;
1942         std::string version = "<void>";
1943         std::string line = "";
1944         char delims[] = ":";
1945         char *result = NULL;
1946         char poort[POORT_MAX_LINE_SIZE_INTERPRETER];
1947
1948         getCleanLine(inputStream, line);
1949
1950         bool start = false;
1951         while (!inputStream.eof()) {
1952                 if (line == "" || line[0] == '#') {
1953                         line="";
1954                         getCleanLine(inputStream, line);
1955                         
1956                         if (line == "# - BBTKGEditor v 1.0 BBG BlackBox Diagram file") {
1957                                 version = line.substr(18, 3);
1958                         }
1959                         if (line == "# - BBTKGEditor v 1.1 BBG BlackBox Diagram file") {
1960                                 version = line.substr(18, 3);
1961                         }
1962                         if (line == "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file") {
1963                                 version = line.substr(18, 3);
1964                         }
1965                         if (line == "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file") {
1966                                 version = line.substr(18, 3);
1967                         }
1968                         if (line == "# - BBTKGEditor v 1.4 BBG BlackBox Diagram file") {
1969                                 version = line.substr(18, 3);
1970                         }
1971             if (line == "# - BBTKGEditor v 1.5 BBG BlackBox Diagram file") {
1972                 version = line.substr(18, 3);
1973             }
1974                 } else if (line == "APP_START") {
1975                         start = true;
1976                         break;
1977                 }
1978         }
1979
1980
1981         if (start)
1982     {
1983
1984                 if ((version != "1.0") && (version != "1.1")) {
1985                         getCleanLine(inputStream, line);//CATEGORY:<category of the box>
1986                         char categoryTmp[30];
1987                         strcpy(categoryTmp, line.c_str());
1988                         result = strtok(categoryTmp, delims);
1989                         result = strtok(NULL, delims);
1990                         SetCategory(result);
1991
1992                         getCleanLine(inputStream, line);//DESCRIPTION:<description of the box>
1993                         char descriptionTmp[1024];
1994                         strcpy(descriptionTmp, line.c_str());
1995                         result = strtok(descriptionTmp, delims);
1996                         result = strtok(NULL, delims);
1997                         SetDescription(result);
1998
1999                         getCleanLine(inputStream, line);//AUTHOR:<author>
2000                         char authorTmp[255];
2001                         strcpy(authorTmp, line.c_str());
2002                         result = strtok(authorTmp, delims);
2003                         result = strtok(NULL, delims);
2004                         SetAuthor(result);
2005                 }
2006
2007                 //----------
2008                 getCleanLine(inputStream, line);//COMPLEX_BOX:TRUE|FALSE
2009                 char complexTmp[30];
2010                 strcpy(complexTmp, line.c_str());
2011                 result = strtok(complexTmp, delims);
2012                 result = strtok(NULL, delims);
2013                 std::string isComplexBox(result);
2014
2015                 if (isComplexBox == "TRUE")
2016         {
2017                         _isComplexBox = true;
2018
2019                         if ((version != "1.0") && (version != "1.1"))
2020             {
2021                                 getCleanLine(inputStream, line);//COMPLEXNAME:<name of the complex box>
2022                                 char complexboxnameTmp[255];
2023                                 strcpy(complexboxnameTmp, line.c_str());
2024                                 result = strtok(complexboxnameTmp, delims);
2025                                 result = strtok(NULL, delims);
2026                                 SetCbName(result);
2027
2028                                 getCleanLine(inputStream, line);//PACKAGENAME:<name of the package of the box>
2029                                 char packagenameTmp[255];
2030                                 strcpy(packagenameTmp, line.c_str());
2031                                 result = strtok(packagenameTmp, delims);
2032                                 result = strtok(NULL, delims);
2033                                 SetCbPackageName(result);
2034                         } // if version !1.0  !1.1
2035
2036                         if(version < "1.4")
2037                         {
2038                                 //-----------------------
2039                                 //- COMPLEX INPUT PORTS
2040                                 //-----------------------
2041                                 getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2042                                 char inputs[30];
2043                                 strcpy(inputs, line.c_str());
2044                                 result = strtok(inputs, delims);
2045                                 result = strtok(NULL, delims);
2046
2047                                 int numInputs;
2048                                 std::istringstream inps(result);
2049                                 inps >> numInputs;
2050
2051                                 for (int i = 0; i < numInputs; i++)
2052                 {
2053                                         //----------
2054                                         getCleanLine(inputStream, line);//COMPLEX_PORT
2055                                         getCleanLine(inputStream, line);//name
2056                                         std::string inputPortName(line);
2057
2058                                         //----------
2059                                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2060                                         char coord[80];
2061                                         strcpy(coord, line.c_str());
2062                                         result = strtok(coord, delims);//xInic
2063                                         std::string xInic(result);
2064                                         result = strtok(NULL, delims);//yInic
2065                                         std::string yInic(result);
2066                                         result = strtok(NULL, delims);//zInic
2067                                         std::string zInic(result);
2068
2069                                         double xIn, yIn, zIn;
2070                                         std::istringstream xSt(xInic);
2071                                         xSt >> xIn;
2072                                         std::istringstream ySt(yInic);
2073                                         ySt >> yIn;
2074                                         std::istringstream zSt(zInic);
2075                                         zSt >> zIn;
2076
2077                                         if (version<="1.2")
2078                                         {
2079                                                 if (zIn==900) zIn=GPOSITION_Z;
2080                                         } // if version <=1.2
2081
2082                                         getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2083
2084                                         configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2085
2086                                 } // for input complex box
2087                         } // if version < 1.4
2088
2089
2090
2091                         //-----------------------
2092                         //- COMPLEX OUTPUT PORTS
2093                         //-----------------------
2094
2095                         getCleanLine(inputStream, line);//COMPLEXOUTPUTS:num
2096                         char outputs[30];
2097                         strcpy(outputs, line.c_str());
2098                         result = strtok(outputs, delims);
2099                         result = strtok(NULL, delims);
2100
2101                         int numOutputs;
2102                         std::istringstream outps(result);
2103                         outps >> numOutputs;
2104
2105                         for (int i = 0; i < numOutputs; i++)
2106             {
2107                                 //----------
2108                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2109                                 getCleanLine(inputStream, line);//name
2110                                 std::string outputPortName(line);
2111
2112                                 //----------
2113                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2114                                 char coord[80];
2115                                 strcpy(coord, line.c_str());
2116                                 result = strtok(coord, delims);//xInic
2117                                 std::string xInic(result);
2118                                 result = strtok(NULL, delims);//yInic
2119                                 std::string yInic(result);
2120                                 result = strtok(NULL, delims);//zInic
2121                                 std::string zInic(result);
2122
2123                                 double xIn, yIn, zIn;
2124                                 std::istringstream xSt(xInic);
2125                                 xSt >> xIn;
2126                                 std::istringstream ySt(yInic);
2127                                 ySt >> yIn;
2128                                 std::istringstream zSt(zInic);
2129                                 zSt >> zIn;
2130
2131                                 if (version<="1.2")
2132                                 {
2133                                         if (zIn==900) zIn=GPOSITION_Z;
2134                                 }
2135 //EED 2022-01-16
2136                 if (version<="1.4")
2137                 {
2138                     xIn = xIn*0.70;
2139                     yIn = yIn*0.30;
2140                 }
2141
2142                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2143
2144                                 configGComBoxInputOutputPort(false, outputPortName, xIn, yIn, zIn);
2145
2146                         } // for output complex box
2147                 } // if isComplexBox
2148
2149         
2150                 // RaC2012 2nd Hackfest
2151                 if(version >= "1.4")
2152                 {
2153                         //-----------------------
2154                         //- EXTERNAL INPUT PORTS
2155                         //-----------------------
2156                         getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2157                         char inputs[30];
2158                         strcpy(inputs, line.c_str());
2159                         result = strtok(inputs, delims);
2160                         result = strtok(NULL, delims);
2161
2162                         int numInputs;
2163                         std::istringstream inps(result);
2164                         inps >> numInputs;
2165
2166                         for (int i = 0; i < numInputs; i++)
2167             {
2168                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2169                                 getCleanLine(inputStream, line);//name
2170                                 std::string inputPortName(line);
2171                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2172                                 char coord[80];
2173                                 strcpy(coord, line.c_str());
2174                                 result = strtok(coord, delims);//xInic
2175                                 std::string xInic(result);
2176                                 result = strtok(NULL, delims);//yInic
2177                                 std::string yInic(result);
2178                                 result = strtok(NULL, delims);//zInic
2179                                 std::string zInic(result);
2180                                 double xIn, yIn, zIn;
2181                                 std::istringstream xSt(xInic);
2182                                 xSt >> xIn;
2183                                 std::istringstream ySt(yInic);
2184                                 ySt >> yIn;
2185                                 std::istringstream zSt(zInic);
2186                                 zSt >> zIn;
2187                                 if (version<="1.2")
2188                                 {
2189                                         if (zIn==900) zIn=GPOSITION_Z;
2190                                 }
2191 //EED 2022-01-16
2192                 if (version<="1.4")
2193                 {
2194                     xIn = xIn*0.70;
2195                     yIn = yIn*0.30;
2196                 }
2197                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2198                                 configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2199                         } // for external input
2200                 }   //      if(version >= "1.4")
2201
2202                 //----------
2203                 getCleanLine(inputStream, line);//BOXES:num
2204                 char boxes[9];
2205                 strcpy(boxes, line.c_str());
2206                 result = strtok(boxes, delims);
2207                 result = strtok(NULL, delims);
2208
2209                 int numBoxes;
2210                 std::istringstream is(result);
2211                 is >> numBoxes;
2212
2213                 for (int i = 0; i < numBoxes; i++) 
2214                 {
2215                         //----------
2216                         getCleanLine(inputStream, line);//BOX
2217                         getCleanLine(inputStream, line);//package:type:name
2218                         char box[150];
2219                         strcpy(box, line.c_str());
2220                         result = strtok(box, delims);//package
2221                         std::string package(result);
2222                         result = strtok(NULL, delims);//type
2223                         std::string type(result);
2224                         result = strtok(NULL, delims);//name
2225                         std::string name(result);
2226
2227
2228                         getCleanLine(inputStream, line);//ISEXEC:TRUE|FALSE
2229                         char exec[15];
2230                         strcpy(exec, line.c_str());
2231                         result = strtok(exec, delims);//ISEXEC
2232                         result = strtok(NULL, delims);//TRUE|FALSE
2233                         std::string isExec(result);
2234
2235                         //----------
2236                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2237                         char coord[80];
2238                         strcpy(coord, line.c_str());
2239                         result = strtok(coord, delims);//xInic
2240                         std::string xInic(result);
2241                         result = strtok(NULL, delims);//yInic
2242                         std::string yInic(result);
2243                         result = strtok(NULL, delims);//zInic
2244                         std::string zInic(result);
2245                         double xIn, yIn, zIn;
2246                         std::istringstream xSt(xInic);
2247                         xSt >> xIn;
2248                         std::istringstream ySt(yInic);
2249                         ySt >> yIn;
2250                         std::istringstream zSt(zInic);
2251                         zSt >> zIn;
2252
2253                         if (version<="1.2")
2254                         {
2255                                 if (zIn==900) zIn=GPOSITION_Z;
2256                         }
2257 //EED 2022-01-16
2258             if (version<="1.4")
2259             {
2260                 xIn = xIn*0.70;
2261                 yIn = yIn*0.30;
2262             }
2263                         //----------
2264                         getCleanLine(inputStream, line);//xEnd:yEnd:zEnd
2265                         strcpy(coord, line.c_str());
2266                         result = strtok(coord, delims);//xEnd
2267                         std::string xEnd(result);
2268                         result = strtok(NULL, delims);//yEnd
2269                         std::string yEnd(result);
2270                         result = strtok(NULL, delims);//zEnd
2271                         std::string zEnd(result);
2272
2273                         double xEn, yEn, zEn;
2274                         std::istringstream xEt(xEnd);
2275                         xEt >> xEn;
2276                         std::istringstream yEt(yEnd);
2277                         yEt >> yEn;
2278                         std::istringstream zEt(zEnd);
2279                         zEt >> zEn;
2280
2281                         if (version<="1.2")
2282                         {
2283                                 if (zEn==900) zEn=GPOSITION_Z;
2284                         }
2285 //EED 2022-01-16
2286             yEn = yIn-BOX_HEIGHT;
2287                         
2288                         bool boxExecutable = false;
2289                         if (isExec == "TRUE") 
2290                         {
2291                                 boxExecutable = true;
2292                         }
2293
2294                         int idBox = createGBlackBox(xIn, yIn, package, type);
2295                         configGBlackBox(idBox, xIn, yIn, zIn, name, boxExecutable, xEn, yEn, zEn);
2296
2297                         GObjectController *cont = _controllers[idBox];
2298                         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
2299
2300                         //----------
2301                         getCleanLine(inputStream, line);//PORT o FIN_BOX
2302                         std::string port = line.substr(0, 4);
2303                         while (port == "PORT") 
2304                         {
2305                                 getCleanLine(inputStream, line);//name:value
2306                                 if (line.length()>=POORT_MAX_LINE_SIZE_INTERPRETER-1)
2307                                 {
2308                                         line=line.substr(0, POORT_MAX_LINE_SIZE_INTERPRETER-1);
2309                                 }
2310                                 
2311                                 strcpy(poort, line.c_str());
2312                                 result = strtok(poort, delims);//name
2313                                 std::string name(result);
2314                                 
2315                                 //EED 16 oct 2012 Adding at the end character " if necesary in the value definition
2316                                 result = strtok(NULL, delims);//value
2317                                 if (( result[0]=='"' ) && ( result[ strlen(result)-1 ]!='"' )  )
2318                                 {
2319                                         std::string tmpString=" ";
2320                                         tmpString[0]=34;         // character "
2321                                         strcat(result,tmpString.c_str());
2322                                 }
2323                                 std::string value(result);
2324                                 bbmod->setValueToInput(name, value);
2325                                 getCleanLine(inputStream, line);//PORT o FIN_BOX
2326                                 port = line.substr(0, 4);
2327                         } // while
2328
2329                         //EED                           bbmod->notifyObservers(_idManager);
2330                 } // for boxes
2331
2332                 /// CONNECTIONS
2333                 //----------
2334                 getCleanLine(inputStream, line);//CONNECTIONS:num
2335                 char conns[30];
2336                 strcpy(conns, line.c_str());
2337                 result = strtok(conns, delims);
2338                 result = strtok(NULL, delims);
2339                 int numConns;
2340                 std::istringstream isCons(result);
2341                 isCons >> numConns;
2342                 
2343                 
2344                 for (int i = 0; i < numConns; i++) 
2345                 {
2346                         //----------
2347                         getCleanLine(inputStream, line);//CONNECTION
2348                         getCleanLine(inputStream, line);//Startbox.PortName:EndBox.PortName
2349                         char connec[200];
2350                         strcpy(connec, line.c_str());
2351                         result = strtok(connec, delims);
2352                         std::string nameStartBox(result);
2353                         result = strtok(NULL, delims);
2354                         std::string nameStartPort(result);
2355                         result = strtok(NULL, delims);
2356                         std::string nameEndBox(result);
2357                         result = strtok(NULL, delims);
2358                         std::string nameEndPort(result);
2359                         int idCon = configGConnetion(nameStartBox, nameStartPort,nameEndBox, nameEndPort);
2360                         if (version != "1.0") {
2361                                 //Readding control points of the manualContour
2362                                 GConnectorController    *tempp          = (GConnectorController*) _controllers[idCon];
2363                                 GConnectorModel         *conMod         = (GConnectorModel*) tempp->getModel();
2364                                 vtkGConnectorView       *conView        = (vtkGConnectorView*) tempp->getView();
2365                                 getCleanLine(inputStream, line); //NumberOfControlPoints:##
2366                                 strcpy(conns, line.c_str());
2367                                 result = strtok(conns, delims);
2368                                 result = strtok(NULL, delims);
2369                                 int numberOfControlPoints;
2370                                 std::istringstream isCons(result);
2371                                 isCons >> numberOfControlPoints;
2372                                 for (int ii = 0; ii < numberOfControlPoints; ii++) {
2373                                         getCleanLine(inputStream, line); //XX:YY:ZZ
2374                                         char connec[200];
2375                                         strcpy(connec, line.c_str());
2376                                         double px, py, pz;
2377                                         result = strtok(connec, delims);
2378                                         std::istringstream isPointX(result);
2379                                         isPointX >> px;
2380                                         result = strtok(NULL, delims);
2381                                         std::istringstream isPointY(result);
2382                                         isPointY >> py;
2383                                         result = strtok(NULL, delims);
2384                                         std::istringstream isPointZ(result);
2385                                         isPointZ >> pz;
2386                                         if (version<="1.2")
2387                                         {
2388                                                 if (pz==900) pz=GPOSITION_Z;
2389                                         }
2390 //EED 2022-01-16
2391                     if (version<="1.4")
2392                     {
2393                         px = px*0.70;
2394                         py = py*0.30;
2395                     }
2396                                         conMod->getManualContourModel()->InsertPoint_id(ii + 1, px, py, pz);
2397                                         conView->getManualContourView()->AddPoint();
2398                                 }
2399                         }// if version !=1.0
2400                 } // for numConns
2401         } // if start
2402 }
2403 //=========================================================================
2404
2405
2406 //=========================================================================
2407 void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput,
2408                 std::string portName, double xIn, double yIn, double zIn) 
2409 {
2410         int idPort;
2411         if (inputoutput == true) {
2412                 idPort = createGComplexBoxInputPort(portName);
2413         } else {
2414                 idPort = createGComplexBoxOutputPort(portName);
2415         }
2416         GObjectController *cont = _controllers[idPort];
2417         GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2418         cbmod->setInicPoint(xIn, yIn, zIn);
2419         cbmod->notifyObservers(_idManager);
2420 }
2421 //=========================================================================
2422
2423
2424 //=========================================================================
2425 GBoxModel* wxVtkSceneManager::findGBox(std::string boxname) 
2426 {
2427         int j;
2428         int idB;
2429         GBoxModel *bMod;
2430         GBoxModel *boxModel = NULL;
2431         std::vector<int> lstB = getBlackBoxes();
2432         for (j = 0; j < (int) lstB.size(); j++) {
2433                 idB = lstB[j];
2434                 bMod = (GBoxModel*) _controllers[idB]->getModel();
2435                 if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2436                 {
2437                         boxModel = bMod;
2438                 }
2439         } // for
2440
2441
2442
2443         if (boxModel == NULL) {
2444                 std::vector<int> lstInputs = getComplexInputPorts();
2445                 for (j = 0; j < (int) lstInputs.size(); j++) {
2446                         idB = lstInputs[j];
2447                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2448                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2449                         {
2450                                 boxModel = bMod;
2451                         }
2452                 } // for
2453
2454                 if(_isComplexBox)
2455                 {
2456                 std::vector<int> lstOutputs = getComplexOutputPorts();
2457                 for (j = 0; j < (int) lstOutputs.size(); j++) {
2458                         int idB = lstOutputs[j];
2459                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2460                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2461                         {
2462                                 boxModel = bMod;
2463                         }
2464                 } // for
2465                 }
2466
2467         } // inputs/outputs
2468
2469         return boxModel;
2470 }
2471
2472 //=========================================================================
2473
2474
2475 int wxVtkSceneManager::configGConnetion(std::string nameStartBox,
2476                 std::string nameStartPort, std::string nameEndBox,
2477                 std::string nameEndPort) {
2478
2479         GBoxModel *boxModel;
2480         GPortModel *startP = NULL;
2481         GPortModel *endP = NULL;
2482
2483         boxModel = findGBox(nameStartBox);
2484         if(boxModel == NULL)
2485         {
2486                 printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2487         }
2488         if (boxModel != NULL) {
2489                 startP = boxModel->getOutputPort(nameStartPort);
2490                 if(startP == NULL){
2491                         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() );
2492                 }
2493         }
2494
2495         boxModel = findGBox(nameEndBox);
2496         if(boxModel == NULL)
2497         {
2498                  printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2499         }
2500         if (boxModel != NULL) {
2501                 endP = boxModel->getInputPort(nameEndPort);
2502                 if(endP == NULL){
2503                         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());
2504                 }
2505         }
2506
2507         //ups2
2508         int idCon = createGConnector(startP);
2509
2510         _worldState = NOTHING_HAPPENS;
2511         GConnectorController *tempp = (GConnectorController*) _controllers[idCon];
2512
2513         GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2514         vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2515         tempp->endContourCreation();
2516         conMod->setEndPort(endP);
2517         conView->updateStartEndPoints();
2518         return idCon;
2519 }
2520
2521 //=========================================================================
2522
2523 bool wxVtkSceneManager::boxExist(std::string boxname) {
2524         bool ok = false;
2525         std::map<int, GObjectController*>::iterator it;
2526         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2527                 GObjectController *cont = it->second;
2528                 if (cont->getModel()->getBBTKName() == boxname) {
2529                         ok = true;
2530                 }
2531         }
2532         return ok;
2533 }
2534
2535 //=========================================================================
2536
2537 std::vector<int> wxVtkSceneManager::getBlackBoxes() {
2538         std::vector<int> vect;
2539         std::map<int, GObjectController*>::iterator it;
2540         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2541                 GObjectController *cont = it->second;
2542                 if (cont->getGObjectType() == GBLACKBOX) {
2543                         vect.push_back(cont->getId());
2544                 }
2545         }
2546         return vect;
2547 }
2548
2549 //=========================================================================
2550
2551 std::vector<int> wxVtkSceneManager::getComplexInputPorts() {
2552         std::vector<int> vect;
2553         std::map<int, GObjectController*>::iterator it;
2554         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2555                 GObjectController *cont = it->second;
2556                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT) {
2557                         vect.push_back(cont->getId());
2558                 }
2559         }
2560         return vect;
2561 }
2562
2563 //=========================================================================
2564
2565 std::vector<int> wxVtkSceneManager::getComplexOutputPorts() {
2566         std::vector<int> vect;
2567         std::map<int, GObjectController*>::iterator it;
2568         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2569                 GObjectController *cont = it->second;
2570                 if (cont->getGObjectType() == GCOMPLEXOUTPUTPORT) {
2571                         vect.push_back(cont->getId());
2572                 }
2573         }
2574         return vect;
2575 }
2576
2577 //=========================================================================
2578
2579 std::vector<int> wxVtkSceneManager::getConnections() {
2580         std::vector<int> vect;
2581         std::map<int, GObjectController*>::iterator it;
2582         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2583                 GObjectController *cont = it->second;
2584                 if (cont->getGObjectType() == GCONNECTOR) {
2585                         vect.push_back(cont->getId());
2586                 }
2587         }
2588         return vect;
2589 }
2590
2591 //=========================================================================
2592
2593 bool wxVtkSceneManager::isComplexBox() {
2594         return _isComplexBox;
2595 }
2596
2597 //=========================================================================
2598
2599 void wxVtkSceneManager::setComplexBox(bool val) {
2600         _isComplexBox = val;
2601 }
2602
2603 //=========================================================================
2604
2605 int wxVtkSceneManager::addObjectController(GObjectController* objController) {
2606         //Register the controller of the new object
2607         registerController((InteractorStyleMaracas*) objController);
2608         //Add the object to the objects list
2609         int newId = _contLastId;//_controllers.size();
2610         objController->setId(newId);
2611         _controllers[newId] = objController;
2612         //std::cout << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = " << _contLastId << std::endl;
2613         _contLastId++;
2614         return newId;
2615 }
2616
2617 //=========================================================================
2618
2619 void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
2620
2621         std::map<int, int> oldIdNewIdBoxes;
2622         std::vector<int> connections;
2623
2624         std::vector<int> newBoxesID;
2625
2626         std::map<int, GObjectController*>::iterator it;
2627         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2628             GObjectController *cont = it->second;
2629             int type = cont->getGObjectType();
2630
2631             if (type == GBLACKBOX) {
2632                 // Copy black box
2633                 double xInic, yInic, zInic;
2634                 GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2635                 copyBox->getInicPoint(xInic, yInic, zInic);
2636                 int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2637                         copyBox->getBBTKType());
2638
2639                 int idcB = copyBox->getObjectId();
2640                 oldIdNewIdBoxes[idcB] = idBox;
2641                 cont = _controllers[idBox];
2642                 GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2643                 newbox->setInicPoint(xInic, yInic, zInic);
2644                 int num = newbox->getNumInputPorts();
2645                 for (int j = 0; j < num; j++) {
2646                     newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2647                 }
2648                 newbox->move(xInic + 20, yInic + 20, zInic);
2649                 newbox->notifyObservers(_idManager);
2650                 newBoxesID.push_back(newbox->getObjectId());
2651
2652             } else if (type == GCONNECTOR) {
2653                 int idCon = cont->getId();
2654                 connections.push_back(idCon);
2655             }
2656
2657         }
2658
2659         for (int i = 0; i < (int) connections.size(); i++) {
2660             int objId = connections[i];
2661             GObjectController *cont = objectsMap[objId];
2662             GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2663
2664             GPortModel* startPort = connectModel->getStartPort();
2665             int startPortIndex = startPort->getPosInBox();
2666             GPortModel* endPort = connectModel->getEndPort();
2667             int endPortIndex = endPort->getPosInBox();
2668
2669             GBlackBoxModel* startPortParentBox =
2670                     (GBlackBoxModel*) startPort->getParentBox();
2671             GBlackBoxModel* endPortParentBox =
2672                     (GBlackBoxModel*) endPort->getParentBox();
2673
2674             int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2675             int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2676
2677             GBlackBoxModel* newStartBox =
2678                     (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2679             GBlackBoxModel* newEndBox =
2680                     (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2681
2682             GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2683             GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2684
2685             // Creates connection
2686             int idCon = createGConnector(newStartPort);
2687             GConnectorController *tempp =
2688                     (GConnectorController*) _controllers[idCon];
2689             GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2690             vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2691             tempp->endContourCreation();
2692             conMod->setEndPort(newEndPort);
2693             conView->updateStartEndPoints();
2694         }
2695
2696         /// the new selected boxes are the duplicate ones
2697
2698         UnSelectBlackBoxes();
2699         for (int i = 0; i < newBoxesID.size(); i++) {
2700             _selectedObjects.push_back(newBoxesID.at(i));
2701         }
2702
2703         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
2704             int id = _selectedObjects[i];
2705             GObjectController* cont = _controllers[id];
2706
2707             cont->getView()->setState(SELECTED);
2708             cont->getModel()->notifyObservers(_idManager);
2709
2710         }
2711     }
2712
2713
2714 //=========================================================================
2715
2716 int wxVtkSceneManager::getNumSelectedObjects() {
2717         return _selectedObjects.size();
2718 }
2719
2720 //=========================================================================
2721
2722 std::map<int, GObjectController*> wxVtkSceneManager::getSelectedObjects() {
2723         std::map<int, GObjectController*> mapSelected;
2724
2725         std::map<int, GObjectController*>::iterator it;
2726         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2727                 GObjectController *cont = it->second;
2728                 if (cont->getGObjectType() == GBLACKBOX && cont->getView()->getState()
2729                                 == SELECTED) {
2730                         mapSelected[cont->getId()] = cont;
2731                 }
2732         }
2733
2734         std::map<int, GObjectController*>::iterator it2;
2735         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
2736                 GObjectController *cont = it2->second;
2737                 if (cont->getGObjectType() == GCONNECTOR) {
2738                         GConnectorModel* cmod = (GConnectorModel*) cont->getModel();
2739                         GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2740                         GBoxModel* startPortParentBox =
2741                                         cmod->getStartPort()->getParentBox();
2742
2743                         std::map<int, GObjectController*>::iterator iterOBJ1 =
2744                                         mapSelected.find(startPortParentBox->getObjectId());
2745                         std::map<int, GObjectController*>::iterator iterOBJ2 =
2746                                         mapSelected.find(endPortParentBox->getObjectId());
2747
2748                         if (iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end()) {
2749                                 int ID = cont->getId();
2750                                 mapSelected[ID] = cont;
2751                         }
2752                 }
2753         }
2754         return mapSelected;
2755 }
2756
2757 //=========================================================================
2758
2759 void wxVtkSceneManager::addObjects(std::map<int, GObjectController*> objectsMap) {
2760
2761         std::map<int, int> oldIdNewIdBoxes;
2762         std::vector<int> connections;
2763
2764         std::map<int, GObjectController*>::iterator it;
2765         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2766                 GObjectController *cont = it->second;
2767                 int type = cont->getGObjectType();
2768
2769                 if (type == GBLACKBOX) {
2770                         // Copy black box
2771                         double xInic, yInic, zInic;
2772                         GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2773                         copyBox->getInicPoint(xInic, yInic, zInic);
2774                         int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),copyBox->getBBTKType());
2775
2776                         int idcB = copyBox->getObjectId();
2777                         oldIdNewIdBoxes[idcB] = idBox;
2778                         cont = _controllers[idBox];
2779                         GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2780                         newbox->setInicPoint(xInic, yInic, zInic);
2781                         int num = newbox->getNumInputPorts();
2782                         for (int j = 0; j < num; j++) 
2783                         {
2784                                 newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2785                         }// for j
2786                         newbox->notifyObservers(_idManager);
2787                 } else if (type == GCONNECTOR) {
2788                         int idCon = cont->getId();
2789                         connections.push_back(idCon);
2790                 }
2791
2792         }
2793
2794         for (int i = 0; i < (int) connections.size(); i++) {
2795                 int objId = connections[i];
2796                 GObjectController *cont = objectsMap[objId];
2797                 GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2798
2799                 GPortModel* startPort = connectModel->getStartPort();
2800                 int startPortIndex = startPort->getPosInBox();
2801                 GPortModel* endPort = connectModel->getEndPort();
2802                 int endPortIndex = endPort->getPosInBox();
2803
2804                 GBlackBoxModel* startPortParentBox =
2805                                 (GBlackBoxModel*) startPort->getParentBox();
2806                 GBlackBoxModel* endPortParentBox =
2807                                 (GBlackBoxModel*) endPort->getParentBox();
2808
2809                 int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2810                 int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2811
2812                 GBlackBoxModel* newStartBox =
2813                                 (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2814                 GBlackBoxModel* newEndBox =
2815                                 (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2816
2817                 GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2818                 GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2819
2820                 // Creates connection
2821                 int idCon = createGConnector(newStartPort);
2822                 GConnectorController *tempp =
2823                                 (GConnectorController*) _controllers[idCon];
2824                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2825                 vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2826                 tempp->endContourCreation();
2827                 conMod->setEndPort(newEndPort);
2828                 conView->updateStartEndPoints();
2829         }
2830
2831         std::map<int, int>::iterator itIds;
2832         for (itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds) {
2833                 int idOld = itIds->first;
2834                 int idNew = itIds->second;
2835
2836                 GBlackBoxModel* oldBox =
2837                                 (GBlackBoxModel*) objectsMap[idOld]->getModel();
2838                 GBlackBoxModel* newBox =
2839                                 (GBlackBoxModel*) _controllers[idNew]->getModel();
2840
2841                 std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2842                 std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2843                 std::vector<int> newInputConnections = newBox->getConnectedInputs();
2844                 std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2845
2846                 for (int k = 0; k < (int) oldInputConnections.size(); k++) {
2847                         bool exist = false;
2848                         //EED                           int toCreate=-1;
2849                         for (int l = 0; l < (int) newInputConnections.size() && !exist; l++) {
2850                                 if (oldInputConnections[k] == newInputConnections[l]) {
2851                                         exist = true;
2852                                 }
2853                         }
2854
2855                         if (exist == false) {
2856                                 //Create complex input
2857                                 int posInBox = oldInputConnections[k];
2858                                 GPortModel* inputPort = oldBox->getInputPort(posInBox);
2859                                 std::string inputPortName = inputPort->getBBTKName();
2860                                 int idInputPort = createGComplexBoxInputPort(inputPortName);
2861                                 GObjectController *cont = _controllers[idInputPort];
2862                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2863                                 double xIn, yIn, zIn;
2864                                 inputPort->getInicPoint(xIn, yIn, zIn);
2865                                 yIn += 20;
2866                                 cbmod->setInicPoint(xIn, yIn, zIn);
2867                                 cbmod->notifyObservers(_idManager);
2868
2869                                 GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2870
2871                                 // Creates connection
2872                                 int idCon = createGConnector(cbmod->getOutputPort(0));
2873                                 GConnectorController *tempp =
2874                                                 (GConnectorController*) _controllers[idCon];
2875                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2876                                 vtkGConnectorView *conView =
2877                                                 (vtkGConnectorView*) tempp->getView();
2878                                 tempp->endContourCreation();
2879                                 conMod->setEndPort(inputPortEnd);
2880                                 conView->updateStartEndPoints();
2881
2882                         }
2883
2884                 }
2885
2886                 for (int k = 0; k < (int) oldOutputConnections.size(); k++) {
2887                         bool exist = false;
2888                         //EED                           int toCreate=-1;
2889                         for (int l = 0; l < (int) newOutputConnections.size() && !exist; l++) {
2890                                 if (oldOutputConnections[k] == newOutputConnections[l]) {
2891                                         exist = true;
2892                                 }
2893                         }
2894
2895                         if (exist == false) {
2896                                 //Create complex output
2897                                 int posInBox = oldOutputConnections[k];
2898                                 GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2899                                 std::string outputPortName = outputPort->getBBTKName();
2900                                 int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2901                                 GObjectController *cont = _controllers[idOutputPort];
2902                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2903                                 double xIn, yIn, zIn;
2904                                 outputPort->getInicPoint(xIn, yIn, zIn);
2905                                 yIn -= 20;
2906                                 cbmod->setInicPoint(xIn, yIn, zIn);
2907                                 cbmod->notifyObservers(_idManager);
2908
2909                                 GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2910
2911                                 // Creates connection
2912                                 int idCon = createGConnector(outputPortEnd);
2913                                 GConnectorController *tempp =
2914                                                 (GConnectorController*) _controllers[idCon];
2915                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2916                                 vtkGConnectorView *conView =
2917                                                 (vtkGConnectorView*) tempp->getView();
2918                                 tempp->endContourCreation();
2919                                 conMod->setEndPort(cbmod->getInputPort(0));
2920                                 conView->updateStartEndPoints();
2921
2922                         }
2923
2924                 }
2925
2926         }
2927
2928 }
2929
2930 //=========================================================================
2931 void wxVtkSceneManager::SetCbName(std::string cbName) {
2932         _cbName = cbName;
2933         if (_cbName == "") {
2934                 _cbName = "<complex box name>";
2935         }
2936 }
2937
2938 //=========================================================================
2939 std::string wxVtkSceneManager::GetCbName() {
2940         return _cbName;
2941 }
2942
2943 //=========================================================================
2944 void wxVtkSceneManager::SetCbPackageName(std::string packagename) {
2945         _cbPackageName = packagename;
2946         if (_cbPackageName == "") {
2947                 _cbPackageName = "<package name of the complex box>";
2948         }
2949 }
2950
2951 //=========================================================================
2952 std::string wxVtkSceneManager::GetCbPackageName() {
2953         return _cbPackageName;
2954 }
2955
2956 //=========================================================================
2957 void wxVtkSceneManager::SetAuthor(std::string author) {
2958         _Author = author;
2959         if (_Author == "") {
2960                 _Author = "<author of the box>";
2961         }
2962 }
2963
2964 //=========================================================================
2965 std::string wxVtkSceneManager::GetAuthor() {
2966         return _Author;
2967 }
2968
2969 //=========================================================================
2970 void wxVtkSceneManager::SetCategory(std::string category) {
2971         _Category = category;
2972         if (_Category == "") {
2973                 _Category = "<category of the box>";
2974         }
2975 }
2976
2977 //=========================================================================
2978 std::string wxVtkSceneManager::GetCategory() {
2979         return _Category;
2980 }
2981
2982 //=========================================================================
2983 void wxVtkSceneManager::SetDescription(std::string description) {
2984         _Description = description;
2985         if (_Description == "") {
2986                 _Description = "<description of the box>";
2987         }
2988 }
2989
2990 //=========================================================================
2991 std::string wxVtkSceneManager::GetDescription() {
2992         return _Description;
2993 }
2994
2995 //=========================================================================
2996
2997 void wxVtkSceneManager::SetMessageKind(std::string kind) {
2998                 _MessageKind = kind;
2999         
3000 }
3001                                 
3002 //=========================================================================
3003
3004 std::string wxVtkSceneManager::GetMessageKind(){
3005                 return _MessageKind;
3006 }
3007
3008 //=========================================================================
3009
3010 void wxVtkSceneManager::SetMessageLevel(std::string Level){
3011                 _MessageLevel = Level;
3012 }
3013
3014 //=========================================================================
3015
3016 std::string wxVtkSceneManager::GetMessageLevel(){
3017                 return _MessageLevel;
3018 }
3019
3020 //=========================================================================
3021
3022 std::vector< std::string> wxVtkSceneManager::GetExternalInputsNames()
3023 {
3024
3025         std::vector< std::string> extInputs;
3026         std::map<int, GObjectController*>::iterator it;
3027         for (it = _controllers.begin(); it != _controllers.end(); ++it)
3028         {
3029                 GObjectController *cont = it->second;
3030                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT)
3031                 {
3032                         extInputs.push_back(cont->getModel()->getBBTKName());
3033                 }
3034         }
3035         return extInputs;
3036 }
3037
3038 //=========================================================================
3039
3040
3041
3042 void wxVtkSceneManager::printAll(int com , int sta ){
3043         
3044          int tmpState = sta ;
3045         if ( tmpState == NOTHING_HAPPENS ) {
3046             std::cout << "CONTROLER STATE---->NOTHING_HAPPENS\n" ;
3047         }
3048         if ( tmpState == HIGHLIGHTED ) {
3049             std::cout << "CONTROLER STATE---->HIGHLIGHTED\n" ;
3050         }
3051         if ( tmpState == POSSIBLE_CONNECTION ) {
3052             std::cout << "CONTROLER STATE---->POSSIBLE_CONNECTION\n" ;
3053         }
3054         if ( tmpState == SELECTED_POSSIBLE_CONNECTION ) {
3055             std::cout << "CONTROLER STATE---->SELECTED_POSSIBLE_CONNECTION\n" ;
3056         }
3057         if ( tmpState == CLICKED ) {
3058             std::cout << "CONTROLER STATE---->CLICKED\n" ;
3059         }
3060         if ( tmpState == DRAG ) {
3061             std::cout << "CONTROLER STATE---->DRAG\n" ;
3062         }
3063         if ( tmpState == SELECTED ) {
3064             std::cout << "CONTROLER STATE---->SELECTED\n" ;
3065         }
3066         if ( tmpState == CREATING_CONTOUR ) {
3067             std::cout << "CONTROLER STATE---->CREATING_CONTOUR\n" ;
3068         }   
3069          
3070          // :P 
3071
3072         int command = com ;
3073         if ( command == INIT_CREATION_CONTOUR ) {
3074             std::cout << "COMMAND ----> INIT_CREATION_CONTOUR \n" ;
3075         }
3076         if ( command == FIN_CREATION_CONTOUR ) {
3077             std::cout << "COMMAND ----> FIN_CREATION_CONTOUR \n" ;
3078         }
3079         if ( command == ADD_TO_SELECTED ) {
3080             std::cout << "COMMAND ----> ADD_TO_SELECTED \n" ;
3081         }
3082         if ( command == DRAG_OBJECTS ) {
3083             std::cout << "COMMAND ----> DRAG_OBJECTS \n" ;
3084         }
3085         if ( command == EDIT_BLACKBOX ) {
3086             std::cout << "COMMAND ----> EDIT_BLACKBOX \n" ;
3087         }
3088         if ( command == REMOVE_FROM_SELECTED ) {
3089             std::cout << "COMMAND ----> REMOVE_FROM_SELECTED \n" ;
3090         }
3091         
3092     }
3093
3094
3095 } // EO namespace bbtk
3096
3097 // EOF
3098