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