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