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