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