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