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