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