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