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