]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
95a2454c22ff447cf2ec8b5e21ee29bede39abf0
[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         _baseView->RefreshView();
1785 }
1786
1787 //=========================================================================
1788
1789 void wxVtkSceneManager::centerView() {
1790         double temp[3];
1791         _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1792         _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
1793         _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1794         _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
1795 }
1796
1797 //=========================================================================
1798
1799 void wxVtkSceneManager::saveDiagram(std::string &content) {
1800         char buffer[50];
1801
1802         content += "CATEGORY:";
1803         content += GetCategory();
1804         content += "\n";
1805
1806         content += "DESCRIPTION:";
1807         content += GetDescription();
1808         content += "\n";
1809
1810         content += "AUTHOR:";
1811         content += GetAuthor();
1812         content += "\n";
1813
1814         //Print info IF COMPLEX BOX
1815         content += "COMPLEXBOX:";
1816         if (_isComplexBox) {
1817                 content += "TRUE\n";
1818                 //ups1
1819                 content += "COMPLEXBOXNAME:";
1820                 content += GetCbName();
1821                 content += "\n";
1822
1823                 content += "PACKAGENAME:";
1824                 content += GetCbPackageName();
1825                 content += "\n";
1826
1827                 /* CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1828                  * External inputs are possible for applications and complex boxes
1829                 //Print info complex input ports
1830                 std::vector<int> inputs = getComplexInputPorts();
1831                 int insize = inputs.size();
1832                 content += "COMPLEXINPUTS:";
1833                 sprintf(buffer, "%d", insize);
1834                 content += buffer;
1835                 content += "\n";
1836
1837                 for (int i = 0; i < insize; i++) {
1838                         int id = inputs[i];
1839                         GObjectController *cont = _controllers[id];
1840                         cont->getModel()->save(content);
1841                 }
1842                 */
1843
1844                 //Print info complex output ports
1845                 std::vector<int> outputs = getComplexOutputPorts();
1846                 int outsize = outputs.size();
1847                 content += "COMPLEXOUTPUTS:";
1848                 sprintf(buffer, "%d", outsize);
1849                 content += buffer;
1850                 content += "\n";
1851
1852                 for (int i = 0; i < outsize; i++) {
1853                         int id = outputs[i];
1854                         GObjectController *cont = _controllers[id];
1855                         cont->getModel()->save(content);
1856                 }
1857         } // _isComplexBox
1858         else {
1859                 content += "FALSE\n";
1860         }
1861
1862         // CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1863         // External inputs are possible for applications and complex boxes
1864         std::vector<int> inputs = getComplexInputPorts();
1865         int insize = inputs.size();
1866         content += "COMPLEXINPUTS:";
1867         sprintf(buffer, "%d", insize);
1868         content += buffer;
1869         content += "\n";
1870
1871         for (int i = 0; i < insize; i++) {
1872                 int id = inputs[i];
1873                 GObjectController *cont = _controllers[id];
1874                 cont->getModel()->save(content);
1875         }
1876
1877         //Print boxes
1878         std::vector<int> boxes = getBlackBoxes();
1879         int bsize = boxes.size();
1880         content += "BOXES:";
1881         sprintf(buffer, "%d", bsize);
1882         content += buffer;
1883         content += "\n";
1884         
1885         for (int i = 0; i < bsize; i++) {
1886                 int id = boxes[i];
1887 //              std::cout<<"id "<<id<<std::endl;//CFT
1888                 GObjectController *cont = _controllers[id];
1889 //              std::cout<<"nombre: "<<cont->getStatusText()<<std::endl;//CFT
1890 //              std::cout<<"tipo: "<<cont->getModel()->getBBTKType()<<std::endl;//CFT
1891 //              //boxesDiagram[i] = cont->getModel()->getBBTKType();//CFT
1892 //              std::cout<<"nname: "<<cont->getModel()->getBBTKName()<<std::endl;//CFT
1893 //              std::cout<<"categorie: "<<GetCategory()<<std::endl;//CFT
1894 //              std::cout<<"type: "<<cont->getModel()->getGObjectType()<<std::endl;//CFT
1895                 cont->getModel()->save(content);
1896         }
1897
1898         //Print connections
1899         std::vector<int> connections = getConnections();
1900         int csize = connections.size();
1901         content += "CONNECTIONS:";
1902         sprintf(buffer, "%d", csize);
1903         content += buffer;
1904         content += "\n";
1905
1906         for (int i = 0; i < csize; i++) {
1907                 int id = connections[i];
1908                 GObjectController *cont = _controllers[id];
1909                 cont->getModel()->save(content);
1910         }
1911
1912 }
1913
1914 //=========================================================================
1915 void wxVtkSceneManager::getCleanLine(stringstream &inputStream, std::string &line)
1916 {
1917         getline(inputStream, line,'\n');
1918         int size = line.length();
1919     if (size>0)
1920         {
1921                 if ( line[ size-1 ]==13  )
1922                 {
1923                         line.erase(size-1,1);
1924                 } // if line
1925         } // if size
1926 }
1927         
1928 //=========================================================================
1929 void wxVtkSceneManager::loadDiagram(stringstream &inputStream) 
1930 {
1931
1932         int size;
1933         std::string version = "<void>";
1934         std::string line = "";
1935         char delims[] = ":";
1936         char *result = NULL;
1937         char poort[POORT_MAX_LINE_SIZE_INTERPRETER];
1938
1939         getCleanLine(inputStream, line);
1940
1941         bool start = false;
1942         while (!inputStream.eof()) {
1943                 if (line == "" || line[0] == '#') {
1944                         line="";
1945                         getCleanLine(inputStream, line);
1946                         
1947                         if (line == "# - BBTKGEditor v 1.0 BBG BlackBox Diagram file") {
1948                                 version = line.substr(18, 3);
1949                         }
1950                         if (line == "# - BBTKGEditor v 1.1 BBG BlackBox Diagram file") {
1951                                 version = line.substr(18, 3);
1952                         }
1953                         if (line == "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file") {
1954                                 version = line.substr(18, 3);
1955                         }
1956                         if (line == "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file") {
1957                                 version = line.substr(18, 3);
1958                         }
1959                         if (line == "# - BBTKGEditor v 1.4 BBG BlackBox Diagram file") {
1960                                 version = line.substr(18, 3);
1961                         }
1962                 } else if (line == "APP_START") {
1963                         start = true;
1964                         break;
1965                 }
1966         }
1967
1968
1969         if (start) {
1970
1971                 if ((version != "1.0") && (version != "1.1")) {
1972                         getCleanLine(inputStream, line);//CATEGORY:<category of the box>
1973                         char categoryTmp[30];
1974                         strcpy(categoryTmp, line.c_str());
1975                         result = strtok(categoryTmp, delims);
1976                         result = strtok(NULL, delims);
1977                         SetCategory(result);
1978
1979                         getCleanLine(inputStream, line);//DESCRIPTION:<description of the box>
1980                         char descriptionTmp[1024];
1981                         strcpy(descriptionTmp, line.c_str());
1982                         result = strtok(descriptionTmp, delims);
1983                         result = strtok(NULL, delims);
1984                         SetDescription(result);
1985
1986                         getCleanLine(inputStream, line);//AUTHOR:<author>
1987                         char authorTmp[255];
1988                         strcpy(authorTmp, line.c_str());
1989                         result = strtok(authorTmp, delims);
1990                         result = strtok(NULL, delims);
1991                         SetAuthor(result);
1992                 }
1993
1994                 //----------
1995                 getCleanLine(inputStream, line);//COMPLEX_BOX:TRUE|FALSE
1996                 char complexTmp[30];
1997                 strcpy(complexTmp, line.c_str());
1998                 result = strtok(complexTmp, delims);
1999                 result = strtok(NULL, delims);
2000                 std::string isComplexBox(result);
2001
2002                 if (isComplexBox == "TRUE") {
2003                         _isComplexBox = true;
2004
2005                         if ((version != "1.0") && (version != "1.1")) {
2006                                 getCleanLine(inputStream, line);//COMPLEXNAME:<name of the complex box>
2007                                 char complexboxnameTmp[255];
2008                                 strcpy(complexboxnameTmp, line.c_str());
2009                                 result = strtok(complexboxnameTmp, delims);
2010                                 result = strtok(NULL, delims);
2011                                 SetCbName(result);
2012
2013                                 getCleanLine(inputStream, line);//PACKAGENAME:<name of the package of the box>
2014                                 char packagenameTmp[255];
2015                                 strcpy(packagenameTmp, line.c_str());
2016                                 result = strtok(packagenameTmp, delims);
2017                                 result = strtok(NULL, delims);
2018                                 SetCbPackageName(result);
2019                         }
2020
2021                         if(version < "1.4")
2022                         {
2023                                 //-----------------------
2024                                 //- COMPLEX INPUT PORTS
2025                                 //-----------------------
2026                                 getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2027                                 char inputs[30];
2028                                 strcpy(inputs, line.c_str());
2029                                 result = strtok(inputs, delims);
2030                                 result = strtok(NULL, delims);
2031
2032                                 int numInputs;
2033                                 std::istringstream inps(result);
2034                                 inps >> numInputs;
2035
2036                                 for (int i = 0; i < numInputs; i++) {
2037                                         //----------
2038                                         getCleanLine(inputStream, line);//COMPLEX_PORT
2039                                         getCleanLine(inputStream, line);//name
2040                                         std::string inputPortName(line);
2041
2042                                         //----------
2043                                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2044                                         char coord[80];
2045                                         strcpy(coord, line.c_str());
2046                                         result = strtok(coord, delims);//xInic
2047                                         std::string xInic(result);
2048                                         result = strtok(NULL, delims);//yInic
2049                                         std::string yInic(result);
2050                                         result = strtok(NULL, delims);//zInic
2051                                         std::string zInic(result);
2052
2053                                         double xIn, yIn, zIn;
2054                                         std::istringstream xSt(xInic);
2055                                         xSt >> xIn;
2056                                         std::istringstream ySt(yInic);
2057                                         ySt >> yIn;
2058                                         std::istringstream zSt(zInic);
2059                                         zSt >> zIn;
2060
2061                                         if (version<="1.2")
2062                                         {
2063                                                 if (zIn==900) zIn=GPOSITION_Z;
2064                                         }
2065
2066                                         getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2067
2068                                         configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2069
2070                                 } // for input complex box
2071                         }
2072
2073
2074
2075                         //-----------------------
2076                         //- COMPLEX OUTPUT PORTS
2077                         //-----------------------
2078
2079                         getCleanLine(inputStream, line);//COMPLEXOUTPUTS:num
2080                         char outputs[30];
2081                         strcpy(outputs, line.c_str());
2082                         result = strtok(outputs, delims);
2083                         result = strtok(NULL, delims);
2084
2085                         int numOutputs;
2086                         std::istringstream outps(result);
2087                         outps >> numOutputs;
2088
2089                         for (int i = 0; i < numOutputs; i++) {
2090                                 //----------
2091                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2092                                 getCleanLine(inputStream, line);//name
2093                                 std::string outputPortName(line);
2094
2095                                 //----------
2096                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2097                                 char coord[80];
2098                                 strcpy(coord, line.c_str());
2099                                 result = strtok(coord, delims);//xInic
2100                                 std::string xInic(result);
2101                                 result = strtok(NULL, delims);//yInic
2102                                 std::string yInic(result);
2103                                 result = strtok(NULL, delims);//zInic
2104                                 std::string zInic(result);
2105
2106                                 double xIn, yIn, zIn;
2107                                 std::istringstream xSt(xInic);
2108                                 xSt >> xIn;
2109                                 std::istringstream ySt(yInic);
2110                                 ySt >> yIn;
2111                                 std::istringstream zSt(zInic);
2112                                 zSt >> zIn;
2113
2114                                 if (version<="1.2")
2115                                 {
2116                                         if (zIn==900) zIn=GPOSITION_Z;
2117                                 }
2118                                 
2119                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2120
2121                                 configGComBoxInputOutputPort(false, outputPortName, xIn, yIn, zIn);
2122
2123                         } // for output complex box
2124
2125                 } // complex box
2126
2127                 // RaC2012 2nd Hackfest
2128                 if(version >= "1.4")
2129                 {
2130                         //-----------------------
2131                         //- EXTERNAL INPUT PORTS
2132                         //-----------------------
2133                         getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2134                         char inputs[30];
2135                         strcpy(inputs, line.c_str());
2136                         result = strtok(inputs, delims);
2137                         result = strtok(NULL, delims);
2138
2139                         int numInputs;
2140                         std::istringstream inps(result);
2141                         inps >> numInputs;
2142
2143                         for (int i = 0; i < numInputs; i++) {
2144                                 //----------
2145                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2146                                 getCleanLine(inputStream, line);//name
2147                                 std::string inputPortName(line);
2148
2149                                 //----------
2150                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2151                                 char coord[80];
2152                                 strcpy(coord, line.c_str());
2153                                 result = strtok(coord, delims);//xInic
2154                                 std::string xInic(result);
2155                                 result = strtok(NULL, delims);//yInic
2156                                 std::string yInic(result);
2157                                 result = strtok(NULL, delims);//zInic
2158                                 std::string zInic(result);
2159
2160                                 double xIn, yIn, zIn;
2161                                 std::istringstream xSt(xInic);
2162                                 xSt >> xIn;
2163                                 std::istringstream ySt(yInic);
2164                                 ySt >> yIn;
2165                                 std::istringstream zSt(zInic);
2166                                 zSt >> zIn;
2167
2168                                 if (version<="1.2")
2169                                 {
2170                                         if (zIn==900) zIn=GPOSITION_Z;
2171                                 }
2172                                 
2173                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2174                                 configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2175
2176                         } // for external input
2177                 }
2178
2179
2180                 //----------
2181                 getCleanLine(inputStream, line);//BOXES:num
2182                 char boxes[9];
2183                 strcpy(boxes, line.c_str());
2184                 result = strtok(boxes, delims);
2185                 result = strtok(NULL, delims);
2186
2187                 int numBoxes;
2188                 std::istringstream is(result);
2189                 is >> numBoxes;
2190
2191                 for (int i = 0; i < numBoxes; i++) 
2192                 {
2193                         //----------
2194                         getCleanLine(inputStream, line);//BOX
2195                         getCleanLine(inputStream, line);//package:type:name
2196                         char box[150];
2197                         strcpy(box, line.c_str());
2198                         result = strtok(box, delims);//package
2199                         std::string package(result);
2200                         result = strtok(NULL, delims);//type
2201                         std::string type(result);
2202                         result = strtok(NULL, delims);//name
2203                         std::string name(result);
2204
2205
2206                         getCleanLine(inputStream, line);//ISEXEC:TRUE|FALSE
2207                         char exec[15];
2208                         strcpy(exec, line.c_str());
2209                         result = strtok(exec, delims);//ISEXEC
2210                         result = strtok(NULL, delims);//TRUE|FALSE
2211                         std::string isExec(result);
2212
2213                         //----------
2214                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2215                         char coord[80];
2216                         strcpy(coord, line.c_str());
2217                         result = strtok(coord, delims);//xInic
2218                         std::string xInic(result);
2219                         result = strtok(NULL, delims);//yInic
2220                         std::string yInic(result);
2221                         result = strtok(NULL, delims);//zInic
2222                         std::string zInic(result);
2223
2224                         double xIn, yIn, zIn;
2225                         std::istringstream xSt(xInic);
2226                         xSt >> xIn;
2227                         std::istringstream ySt(yInic);
2228                         ySt >> yIn;
2229                         std::istringstream zSt(zInic);
2230                         zSt >> zIn;
2231
2232                         
2233                         if (version<="1.2")
2234                         {
2235                                 if (zIn==900) zIn=GPOSITION_Z;
2236                         }
2237                         
2238                         
2239                         //----------
2240                         getCleanLine(inputStream, line);//xEnd:yEnd:zEnd
2241                         strcpy(coord, line.c_str());
2242                         result = strtok(coord, delims);//xEnd
2243                         std::string xEnd(result);
2244                         result = strtok(NULL, delims);//yEnd
2245                         std::string yEnd(result);
2246                         result = strtok(NULL, delims);//zEnd
2247                         std::string zEnd(result);
2248
2249                         double xEn, yEn, zEn;
2250                         std::istringstream xEt(xEnd);
2251                         xEt >> xEn;
2252                         std::istringstream yEt(yEnd);
2253                         yEt >> yEn;
2254                         std::istringstream zEt(zEnd);
2255                         zEt >> zEn;
2256
2257                         if (version<="1.2")
2258                         {
2259                                 if (zEn==900) zEn=GPOSITION_Z;
2260                         }
2261                         
2262                         
2263                         bool boxExecutable = false;
2264                         if (isExec == "TRUE") 
2265                         {
2266                                 boxExecutable = true;
2267                         }
2268
2269                         int idBox = createGBlackBox(xIn, yIn, package, type);
2270                         configGBlackBox(idBox, xIn, yIn, zIn, name, boxExecutable, xEn, yEn, zEn);
2271
2272                         GObjectController *cont = _controllers[idBox];
2273                         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
2274
2275                         //----------
2276                         getCleanLine(inputStream, line);//PORT o FIN_BOX
2277                         std::string port = line.substr(0, 4);
2278                         while (port == "PORT") 
2279                         {
2280                                 getCleanLine(inputStream, line);//name:value
2281                                 if (line.length()>=POORT_MAX_LINE_SIZE_INTERPRETER-1)
2282                                 {
2283                                         line=line.substr(0, POORT_MAX_LINE_SIZE_INTERPRETER-1);
2284                                 }
2285                                 
2286                                 strcpy(poort, line.c_str());
2287                                 result = strtok(poort, delims);//name
2288                                 std::string name(result);
2289                                 
2290                                 //EED 16 oct 2012 Adding at the end character " if necesary in the value definition
2291                                 result = strtok(NULL, delims);//value
2292                                 if (( result[0]=='"' ) && ( result[ strlen(result)-1 ]!='"' )  )
2293                                 {
2294                                         std::string tmpString=" ";
2295                                         tmpString[0]=34;         // character "
2296                                         strcat(result,tmpString.c_str());
2297                                 }
2298                                 std::string value(result);
2299
2300                                 bbmod->setValueToInput(name, value);
2301
2302                                 getCleanLine(inputStream, line);//PORT o FIN_BOX
2303                                 port = line.substr(0, 4);
2304                         } // while
2305
2306                         //EED                           bbmod->notifyObservers(_idManager);
2307                 } // for boxes
2308
2309                 /// CONNECTIONS
2310                 //----------
2311                 getCleanLine(inputStream, line);//CONNECTIONS:num
2312                 char conns[30];
2313                 strcpy(conns, line.c_str());
2314                 result = strtok(conns, delims);
2315                 result = strtok(NULL, delims);
2316
2317                 int numConns;
2318                 std::istringstream isCons(result);
2319                 isCons >> numConns;
2320                 
2321                 
2322                 for (int i = 0; i < numConns; i++) 
2323                 {
2324                         //----------
2325                         getCleanLine(inputStream, line);//CONNECTION
2326                         getCleanLine(inputStream, line);//Startbox.PortName:EndBox.PortName
2327
2328                         char connec[200];
2329                         strcpy(connec, line.c_str());
2330                         result = strtok(connec, delims);
2331                         std::string nameStartBox(result);
2332                         result = strtok(NULL, delims);
2333                         std::string nameStartPort(result);
2334                         result = strtok(NULL, delims);
2335                         std::string nameEndBox(result);
2336                         result = strtok(NULL, delims);
2337                         std::string nameEndPort(result);
2338
2339                         int idCon = configGConnetion(nameStartBox, nameStartPort,nameEndBox, nameEndPort);
2340
2341                         if (version != "1.0") {
2342                                 //Readding control points of the manualContour
2343                                 GConnectorController *tempp             = (GConnectorController*) _controllers[idCon];
2344                                 GConnectorModel *conMod                 = (GConnectorModel*) tempp->getModel();
2345                                 vtkGConnectorView *conView              = (vtkGConnectorView*) tempp->getView();
2346                                 getCleanLine(inputStream, line); //NumberOfControlPoints:##
2347                                 strcpy(conns, line.c_str());
2348                                 result = strtok(conns, delims);
2349                                 result = strtok(NULL, delims);
2350
2351                                 int numberOfControlPoints;
2352                                 std::istringstream isCons(result);
2353                                 isCons >> numberOfControlPoints;
2354
2355                                 for (int ii = 0; ii < numberOfControlPoints; ii++) {
2356                                         getCleanLine(inputStream, line); //XX:YY:ZZ
2357                                         char connec[200];
2358                                         strcpy(connec, line.c_str());
2359
2360                                         double px, py, pz;
2361                                         result = strtok(connec, delims);
2362                                         std::istringstream isPointX(result);
2363                                         isPointX >> px;
2364                                         result = strtok(NULL, delims);
2365                                         std::istringstream isPointY(result);
2366                                         isPointY >> py;
2367                                         result = strtok(NULL, delims);
2368                                         std::istringstream isPointZ(result);
2369                                         isPointZ >> pz;
2370                 
2371                                         if (version<="1.2")
2372                                         {
2373                                                 if (pz==900) pz=GPOSITION_Z;
2374                                         }
2375                                         
2376                                         conMod->getManualContourModel()->InsertPoint_id(ii + 1, px, py, pz);
2377                                         conView->getManualContourView()->AddPoint();
2378                                 }
2379                         }// version !=1.0
2380                 } // for numConns
2381         } // start      
2382 }
2383 //=========================================================================
2384
2385
2386 //=========================================================================
2387 void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput,
2388                 std::string portName, double xIn, double yIn, double zIn) 
2389 {
2390         int idPort;
2391         if (inputoutput == true) {
2392                 idPort = createGComplexBoxInputPort(portName);
2393         } else {
2394                 idPort = createGComplexBoxOutputPort(portName);
2395         }
2396         GObjectController *cont = _controllers[idPort];
2397         GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2398         cbmod->setInicPoint(xIn, yIn, zIn);
2399         cbmod->notifyObservers(_idManager);
2400 }
2401 //=========================================================================
2402
2403
2404 //=========================================================================
2405 GBoxModel* wxVtkSceneManager::findGBox(std::string boxname) 
2406 {
2407         int j;
2408         int idB;
2409         GBoxModel *bMod;
2410         GBoxModel *boxModel = NULL;
2411         std::vector<int> lstB = getBlackBoxes();
2412         for (j = 0; j < (int) lstB.size(); j++) {
2413                 idB = lstB[j];
2414                 bMod = (GBoxModel*) _controllers[idB]->getModel();
2415                 if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2416                 {
2417                         boxModel = bMod;
2418                 }
2419         } // for
2420
2421
2422
2423         if (boxModel == NULL) {
2424                 std::vector<int> lstInputs = getComplexInputPorts();
2425                 for (j = 0; j < (int) lstInputs.size(); j++) {
2426                         idB = lstInputs[j];
2427                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2428                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2429                         {
2430                                 boxModel = bMod;
2431                         }
2432                 } // for
2433
2434                 if(_isComplexBox)
2435                 {
2436                 std::vector<int> lstOutputs = getComplexOutputPorts();
2437                 for (j = 0; j < (int) lstOutputs.size(); j++) {
2438                         int idB = lstOutputs[j];
2439                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2440                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) 
2441                         {
2442                                 boxModel = bMod;
2443                         }
2444                 } // for
2445                 }
2446
2447         } // inputs/outputs
2448
2449         return boxModel;
2450 }
2451
2452 //=========================================================================
2453
2454
2455 int wxVtkSceneManager::configGConnetion(std::string nameStartBox,
2456                 std::string nameStartPort, std::string nameEndBox,
2457                 std::string nameEndPort) {
2458
2459         GBoxModel *boxModel;
2460         GPortModel *startP = NULL;
2461         GPortModel *endP = NULL;
2462
2463         boxModel = findGBox(nameStartBox);
2464         if(boxModel == NULL)
2465         {
2466                 printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2467         }
2468         if (boxModel != NULL) {
2469                 startP = boxModel->getOutputPort(nameStartPort);
2470                 if(startP == NULL){
2471                         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() );
2472                 }
2473         }
2474
2475         boxModel = findGBox(nameEndBox);
2476         if(boxModel == NULL)
2477         {
2478                  printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2479         }
2480         if (boxModel != NULL) {
2481                 endP = boxModel->getInputPort(nameEndPort);
2482                 if(endP == NULL){
2483                         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());
2484                 }
2485         }
2486
2487         //ups2
2488         int idCon = createGConnector(startP);
2489
2490         _worldState = NOTHING_HAPPENS;
2491         GConnectorController *tempp = (GConnectorController*) _controllers[idCon];
2492
2493         GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2494         vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2495         tempp->endContourCreation();
2496         conMod->setEndPort(endP);
2497         conView->updateStartEndPoints();
2498         return idCon;
2499 }
2500
2501 //=========================================================================
2502
2503 bool wxVtkSceneManager::boxExist(std::string boxname) {
2504         bool ok = false;
2505         std::map<int, GObjectController*>::iterator it;
2506         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2507                 GObjectController *cont = it->second;
2508                 if (cont->getModel()->getBBTKName() == boxname) {
2509                         ok = true;
2510                 }
2511         }
2512         return ok;
2513 }
2514
2515 //=========================================================================
2516
2517 std::vector<int> wxVtkSceneManager::getBlackBoxes() {
2518         std::vector<int> vect;
2519         std::map<int, GObjectController*>::iterator it;
2520         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2521                 GObjectController *cont = it->second;
2522                 if (cont->getGObjectType() == GBLACKBOX) {
2523                         vect.push_back(cont->getId());
2524                 }
2525         }
2526         return vect;
2527 }
2528
2529 //=========================================================================
2530
2531 std::vector<int> wxVtkSceneManager::getComplexInputPorts() {
2532         std::vector<int> vect;
2533         std::map<int, GObjectController*>::iterator it;
2534         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2535                 GObjectController *cont = it->second;
2536                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT) {
2537                         vect.push_back(cont->getId());
2538                 }
2539         }
2540         return vect;
2541 }
2542
2543 //=========================================================================
2544
2545 std::vector<int> wxVtkSceneManager::getComplexOutputPorts() {
2546         std::vector<int> vect;
2547         std::map<int, GObjectController*>::iterator it;
2548         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2549                 GObjectController *cont = it->second;
2550                 if (cont->getGObjectType() == GCOMPLEXOUTPUTPORT) {
2551                         vect.push_back(cont->getId());
2552                 }
2553         }
2554         return vect;
2555 }
2556
2557 //=========================================================================
2558
2559 std::vector<int> wxVtkSceneManager::getConnections() {
2560         std::vector<int> vect;
2561         std::map<int, GObjectController*>::iterator it;
2562         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2563                 GObjectController *cont = it->second;
2564                 if (cont->getGObjectType() == GCONNECTOR) {
2565                         vect.push_back(cont->getId());
2566                 }
2567         }
2568         return vect;
2569 }
2570
2571 //=========================================================================
2572
2573 bool wxVtkSceneManager::isComplexBox() {
2574         return _isComplexBox;
2575 }
2576
2577 //=========================================================================
2578
2579 void wxVtkSceneManager::setComplexBox(bool val) {
2580         _isComplexBox = val;
2581 }
2582
2583 //=========================================================================
2584
2585 int wxVtkSceneManager::addObjectController(GObjectController* objController) {
2586         //Register the controller of the new object
2587         registerController((InteractorStyleMaracas*) objController);
2588         //Add the object to the objects list
2589         int newId = _contLastId;//_controllers.size();
2590         objController->setId(newId);
2591         _controllers[newId] = objController;
2592         //std::cout << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = " << _contLastId << std::endl;
2593         _contLastId++;
2594         return newId;
2595 }
2596
2597 //=========================================================================
2598
2599 void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
2600
2601         std::map<int, int> oldIdNewIdBoxes;
2602         std::vector<int> connections;
2603
2604         std::vector<int> newBoxesID;
2605
2606         std::map<int, GObjectController*>::iterator it;
2607         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2608             GObjectController *cont = it->second;
2609             int type = cont->getGObjectType();
2610
2611             if (type == GBLACKBOX) {
2612                 // Copy black box
2613                 double xInic, yInic, zInic;
2614                 GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2615                 copyBox->getInicPoint(xInic, yInic, zInic);
2616                 int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2617                         copyBox->getBBTKType());
2618
2619                 int idcB = copyBox->getObjectId();
2620                 oldIdNewIdBoxes[idcB] = idBox;
2621                 cont = _controllers[idBox];
2622                 GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2623                 newbox->setInicPoint(xInic, yInic, zInic);
2624                 int num = newbox->getNumInputPorts();
2625                 for (int j = 0; j < num; j++) {
2626                     newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2627                 }
2628                 newbox->move(xInic + 20, yInic + 20, zInic);
2629                 newbox->notifyObservers(_idManager);
2630                 newBoxesID.push_back(newbox->getObjectId());
2631
2632             } else if (type == GCONNECTOR) {
2633                 int idCon = cont->getId();
2634                 connections.push_back(idCon);
2635             }
2636
2637         }
2638
2639         for (int i = 0; i < (int) connections.size(); i++) {
2640             int objId = connections[i];
2641             GObjectController *cont = objectsMap[objId];
2642             GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2643
2644             GPortModel* startPort = connectModel->getStartPort();
2645             int startPortIndex = startPort->getPosInBox();
2646             GPortModel* endPort = connectModel->getEndPort();
2647             int endPortIndex = endPort->getPosInBox();
2648
2649             GBlackBoxModel* startPortParentBox =
2650                     (GBlackBoxModel*) startPort->getParentBox();
2651             GBlackBoxModel* endPortParentBox =
2652                     (GBlackBoxModel*) endPort->getParentBox();
2653
2654             int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2655             int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2656
2657             GBlackBoxModel* newStartBox =
2658                     (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2659             GBlackBoxModel* newEndBox =
2660                     (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2661
2662             GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2663             GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2664
2665             // Creates connection
2666             int idCon = createGConnector(newStartPort);
2667             GConnectorController *tempp =
2668                     (GConnectorController*) _controllers[idCon];
2669             GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2670             vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2671             tempp->endContourCreation();
2672             conMod->setEndPort(newEndPort);
2673             conView->updateStartEndPoints();
2674         }
2675
2676         /// the new selected boxes are the duplicate ones
2677
2678         UnSelectBlackBoxes();
2679         for (int i = 0; i < newBoxesID.size(); i++) {
2680             _selectedObjects.push_back(newBoxesID.at(i));
2681         }
2682
2683         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
2684             int id = _selectedObjects[i];
2685             GObjectController* cont = _controllers[id];
2686
2687             cont->getView()->setState(SELECTED);
2688             cont->getModel()->notifyObservers(_idManager);
2689
2690         }
2691     }
2692
2693
2694 //=========================================================================
2695
2696 int wxVtkSceneManager::getNumSelectedObjects() {
2697         return _selectedObjects.size();
2698 }
2699
2700 //=========================================================================
2701
2702 std::map<int, GObjectController*> wxVtkSceneManager::getSelectedObjects() {
2703         std::map<int, GObjectController*> mapSelected;
2704
2705         std::map<int, GObjectController*>::iterator it;
2706         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2707                 GObjectController *cont = it->second;
2708                 if (cont->getGObjectType() == GBLACKBOX && cont->getView()->getState()
2709                                 == SELECTED) {
2710                         mapSelected[cont->getId()] = cont;
2711                 }
2712         }
2713
2714         std::map<int, GObjectController*>::iterator it2;
2715         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
2716                 GObjectController *cont = it2->second;
2717                 if (cont->getGObjectType() == GCONNECTOR) {
2718                         GConnectorModel* cmod = (GConnectorModel*) cont->getModel();
2719                         GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2720                         GBoxModel* startPortParentBox =
2721                                         cmod->getStartPort()->getParentBox();
2722
2723                         std::map<int, GObjectController*>::iterator iterOBJ1 =
2724                                         mapSelected.find(startPortParentBox->getObjectId());
2725                         std::map<int, GObjectController*>::iterator iterOBJ2 =
2726                                         mapSelected.find(endPortParentBox->getObjectId());
2727
2728                         if (iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end()) {
2729                                 int ID = cont->getId();
2730                                 mapSelected[ID] = cont;
2731                         }
2732                 }
2733         }
2734         return mapSelected;
2735 }
2736
2737 //=========================================================================
2738
2739 void wxVtkSceneManager::addObjects(std::map<int, GObjectController*> objectsMap) {
2740
2741         std::map<int, int> oldIdNewIdBoxes;
2742         std::vector<int> connections;
2743
2744         std::map<int, GObjectController*>::iterator it;
2745         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2746                 GObjectController *cont = it->second;
2747                 int type = cont->getGObjectType();
2748
2749                 if (type == GBLACKBOX) {
2750                         // Copy black box
2751                         double xInic, yInic, zInic;
2752                         GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2753                         copyBox->getInicPoint(xInic, yInic, zInic);
2754                         int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),copyBox->getBBTKType());
2755
2756                         int idcB = copyBox->getObjectId();
2757                         oldIdNewIdBoxes[idcB] = idBox;
2758                         cont = _controllers[idBox];
2759                         GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2760                         newbox->setInicPoint(xInic, yInic, zInic);
2761                         int num = newbox->getNumInputPorts();
2762                         for (int j = 0; j < num; j++) 
2763                         {
2764                                 newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2765                         }// for j
2766                         newbox->notifyObservers(_idManager);
2767                 } else if (type == GCONNECTOR) {
2768                         int idCon = cont->getId();
2769                         connections.push_back(idCon);
2770                 }
2771
2772         }
2773
2774         for (int i = 0; i < (int) connections.size(); i++) {
2775                 int objId = connections[i];
2776                 GObjectController *cont = objectsMap[objId];
2777                 GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2778
2779                 GPortModel* startPort = connectModel->getStartPort();
2780                 int startPortIndex = startPort->getPosInBox();
2781                 GPortModel* endPort = connectModel->getEndPort();
2782                 int endPortIndex = endPort->getPosInBox();
2783
2784                 GBlackBoxModel* startPortParentBox =
2785                                 (GBlackBoxModel*) startPort->getParentBox();
2786                 GBlackBoxModel* endPortParentBox =
2787                                 (GBlackBoxModel*) endPort->getParentBox();
2788
2789                 int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2790                 int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2791
2792                 GBlackBoxModel* newStartBox =
2793                                 (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2794                 GBlackBoxModel* newEndBox =
2795                                 (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2796
2797                 GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2798                 GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2799
2800                 // Creates connection
2801                 int idCon = createGConnector(newStartPort);
2802                 GConnectorController *tempp =
2803                                 (GConnectorController*) _controllers[idCon];
2804                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2805                 vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2806                 tempp->endContourCreation();
2807                 conMod->setEndPort(newEndPort);
2808                 conView->updateStartEndPoints();
2809         }
2810
2811         std::map<int, int>::iterator itIds;
2812         for (itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds) {
2813                 int idOld = itIds->first;
2814                 int idNew = itIds->second;
2815
2816                 GBlackBoxModel* oldBox =
2817                                 (GBlackBoxModel*) objectsMap[idOld]->getModel();
2818                 GBlackBoxModel* newBox =
2819                                 (GBlackBoxModel*) _controllers[idNew]->getModel();
2820
2821                 std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2822                 std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2823                 std::vector<int> newInputConnections = newBox->getConnectedInputs();
2824                 std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2825
2826                 for (int k = 0; k < (int) oldInputConnections.size(); k++) {
2827                         bool exist = false;
2828                         //EED                           int toCreate=-1;
2829                         for (int l = 0; l < (int) newInputConnections.size() && !exist; l++) {
2830                                 if (oldInputConnections[k] == newInputConnections[l]) {
2831                                         exist = true;
2832                                 }
2833                         }
2834
2835                         if (exist == false) {
2836                                 //Create complex input
2837                                 int posInBox = oldInputConnections[k];
2838                                 GPortModel* inputPort = oldBox->getInputPort(posInBox);
2839                                 std::string inputPortName = inputPort->getBBTKName();
2840                                 int idInputPort = createGComplexBoxInputPort(inputPortName);
2841                                 GObjectController *cont = _controllers[idInputPort];
2842                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2843                                 double xIn, yIn, zIn;
2844                                 inputPort->getInicPoint(xIn, yIn, zIn);
2845                                 yIn += 20;
2846                                 cbmod->setInicPoint(xIn, yIn, zIn);
2847                                 cbmod->notifyObservers(_idManager);
2848
2849                                 GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2850
2851                                 // Creates connection
2852                                 int idCon = createGConnector(cbmod->getOutputPort(0));
2853                                 GConnectorController *tempp =
2854                                                 (GConnectorController*) _controllers[idCon];
2855                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2856                                 vtkGConnectorView *conView =
2857                                                 (vtkGConnectorView*) tempp->getView();
2858                                 tempp->endContourCreation();
2859                                 conMod->setEndPort(inputPortEnd);
2860                                 conView->updateStartEndPoints();
2861
2862                         }
2863
2864                 }
2865
2866                 for (int k = 0; k < (int) oldOutputConnections.size(); k++) {
2867                         bool exist = false;
2868                         //EED                           int toCreate=-1;
2869                         for (int l = 0; l < (int) newOutputConnections.size() && !exist; l++) {
2870                                 if (oldOutputConnections[k] == newOutputConnections[l]) {
2871                                         exist = true;
2872                                 }
2873                         }
2874
2875                         if (exist == false) {
2876                                 //Create complex output
2877                                 int posInBox = oldOutputConnections[k];
2878                                 GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2879                                 std::string outputPortName = outputPort->getBBTKName();
2880                                 int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2881                                 GObjectController *cont = _controllers[idOutputPort];
2882                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2883                                 double xIn, yIn, zIn;
2884                                 outputPort->getInicPoint(xIn, yIn, zIn);
2885                                 yIn -= 20;
2886                                 cbmod->setInicPoint(xIn, yIn, zIn);
2887                                 cbmod->notifyObservers(_idManager);
2888
2889                                 GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2890
2891                                 // Creates connection
2892                                 int idCon = createGConnector(outputPortEnd);
2893                                 GConnectorController *tempp =
2894                                                 (GConnectorController*) _controllers[idCon];
2895                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2896                                 vtkGConnectorView *conView =
2897                                                 (vtkGConnectorView*) tempp->getView();
2898                                 tempp->endContourCreation();
2899                                 conMod->setEndPort(cbmod->getInputPort(0));
2900                                 conView->updateStartEndPoints();
2901
2902                         }
2903
2904                 }
2905
2906         }
2907
2908 }
2909
2910 //=========================================================================
2911 void wxVtkSceneManager::SetCbName(std::string cbName) {
2912         _cbName = cbName;
2913         if (_cbName == "") {
2914                 _cbName = "<complex box name>";
2915         }
2916 }
2917
2918 //=========================================================================
2919 std::string wxVtkSceneManager::GetCbName() {
2920         return _cbName;
2921 }
2922
2923 //=========================================================================
2924 void wxVtkSceneManager::SetCbPackageName(std::string packagename) {
2925         _cbPackageName = packagename;
2926         if (_cbPackageName == "") {
2927                 _cbPackageName = "<package name of the complex box>";
2928         }
2929 }
2930
2931 //=========================================================================
2932 std::string wxVtkSceneManager::GetCbPackageName() {
2933         return _cbPackageName;
2934 }
2935
2936 //=========================================================================
2937 void wxVtkSceneManager::SetAuthor(std::string author) {
2938         _Author = author;
2939         if (_Author == "") {
2940                 _Author = "<author of the box>";
2941         }
2942 }
2943
2944 //=========================================================================
2945 std::string wxVtkSceneManager::GetAuthor() {
2946         return _Author;
2947 }
2948
2949 //=========================================================================
2950 void wxVtkSceneManager::SetCategory(std::string category) {
2951         _Category = category;
2952         if (_Category == "") {
2953                 _Category = "<category of the box>";
2954         }
2955 }
2956
2957 //=========================================================================
2958 std::string wxVtkSceneManager::GetCategory() {
2959         return _Category;
2960 }
2961
2962 //=========================================================================
2963 void wxVtkSceneManager::SetDescription(std::string description) {
2964         _Description = description;
2965         if (_Description == "") {
2966                 _Description = "<description of the box>";
2967         }
2968 }
2969
2970 //=========================================================================
2971 std::string wxVtkSceneManager::GetDescription() {
2972         return _Description;
2973 }
2974
2975 //=========================================================================
2976
2977 void wxVtkSceneManager::SetMessageKind(std::string kind) {
2978                 _MessageKind = kind;
2979         
2980 }
2981                                 
2982 //=========================================================================
2983
2984 std::string wxVtkSceneManager::GetMessageKind(){
2985                 return _MessageKind;
2986 }
2987
2988 //=========================================================================
2989
2990 void wxVtkSceneManager::SetMessageLevel(std::string Level){
2991                 _MessageLevel = Level;
2992 }
2993
2994 //=========================================================================
2995
2996 std::string wxVtkSceneManager::GetMessageLevel(){
2997                 return _MessageLevel;
2998 }
2999
3000 //=========================================================================
3001
3002 std::vector< std::string> wxVtkSceneManager::GetExternalInputsNames()
3003 {
3004
3005         std::vector< std::string> extInputs;
3006         std::map<int, GObjectController*>::iterator it;
3007         for (it = _controllers.begin(); it != _controllers.end(); ++it)
3008         {
3009                 GObjectController *cont = it->second;
3010                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT)
3011                 {
3012                         extInputs.push_back(cont->getModel()->getBBTKName());
3013                 }
3014         }
3015         return extInputs;
3016 }
3017
3018 //=========================================================================
3019
3020
3021
3022 void wxVtkSceneManager::printAll(int com , int sta ){
3023         
3024          int tmpState = sta ;
3025         if ( tmpState == NOTHING_HAPPENS ) {
3026             std::cout << "CONTROLER STATE---->NOTHING_HAPPENS\n" ;
3027         }
3028         if ( tmpState == HIGHLIGHTED ) {
3029             std::cout << "CONTROLER STATE---->HIGHLIGHTED\n" ;
3030         }
3031         if ( tmpState == POSSIBLE_CONNECTION ) {
3032             std::cout << "CONTROLER STATE---->POSSIBLE_CONNECTION\n" ;
3033         }
3034         if ( tmpState == SELECTED_POSSIBLE_CONNECTION ) {
3035             std::cout << "CONTROLER STATE---->SELECTED_POSSIBLE_CONNECTION\n" ;
3036         }
3037         if ( tmpState == CLICKED ) {
3038             std::cout << "CONTROLER STATE---->CLICKED\n" ;
3039         }
3040         if ( tmpState == DRAG ) {
3041             std::cout << "CONTROLER STATE---->DRAG\n" ;
3042         }
3043         if ( tmpState == SELECTED ) {
3044             std::cout << "CONTROLER STATE---->SELECTED\n" ;
3045         }
3046         if ( tmpState == CREATING_CONTOUR ) {
3047             std::cout << "CONTROLER STATE---->CREATING_CONTOUR\n" ;
3048         }   
3049          
3050          // :P 
3051
3052         int command = com ;
3053         if ( command == INIT_CREATION_CONTOUR ) {
3054             std::cout << "COMMAND ----> INIT_CREATION_CONTOUR \n" ;
3055         }
3056         if ( command == FIN_CREATION_CONTOUR ) {
3057             std::cout << "COMMAND ----> FIN_CREATION_CONTOUR \n" ;
3058         }
3059         if ( command == ADD_TO_SELECTED ) {
3060             std::cout << "COMMAND ----> ADD_TO_SELECTED \n" ;
3061         }
3062         if ( command == DRAG_OBJECTS ) {
3063             std::cout << "COMMAND ----> DRAG_OBJECTS \n" ;
3064         }
3065         if ( command == EDIT_BLACKBOX ) {
3066             std::cout << "COMMAND ----> EDIT_BLACKBOX \n" ;
3067         }
3068         if ( command == REMOVE_FROM_SELECTED ) {
3069             std::cout << "COMMAND ----> REMOVE_FROM_SELECTED \n" ;
3070         }
3071         
3072     }
3073
3074
3075 } // EO namespace bbtk
3076
3077 // EOF
3078