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