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