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