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