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