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