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