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