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