]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
Feature #1674 . RaC
[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 {
512         
513                 
514
515         int type = GCONNECTOR;
516
517         manualConnectorContourController        *manContourControl              = new manualConnectorContourController();
518         manualConnectorContourView                      *manContourView                 = new manualConnectorContourView();
519         manualContourModel                                      *manContourModel                = new manualContourModel();
520         GConnectorController                            *connectorcontroller    = new GConnectorController();
521         GConnectorModel                                         *connectorModel                 = new GConnectorModel();
522         vtkGConnectorView                                       *connectorView                  = new vtkGConnectorView();
523         connectorModel->setGObjectType(type);
524
525         manContourModel->SetCloseContour(false);
526         connectorModel->setStartPort(startPort);
527
528         manContourView->SetModel(manContourModel);
529         manContourView->SetWxVtkBaseView(_baseView);
530         manContourView->SetRange(0.5);
531         manContourView->SetZ(GPOSITION_Z);
532
533         manContourView->SetColorNormalContour(0, 0, 1);
534         manContourView->SetColorEditContour(0.5, 0.5, 0.5);
535         manContourView->SetColorSelectContour(1, 0.8, 0);
536         
537         
538
539         /**
540          * JGRR AND CM
541          * Width increased from 2 to 3, it simplifies line handling and spline control :P
542          * @param WidthLine  
543          */ 
544         manContourView->SetWidthLine( 3 ) ;
545         manContourView->SetShowText(false);
546
547         manContourControl->SetModelView(manContourModel, manContourView);
548
549         manContourControl->CreateNewManualContour();
550
551
552         double x, y, z;
553         connectorModel->getInicPoint(x, y, z);
554         
555         
556         manContourControl->SetState(1);
557         manContourModel->SetCloseContour(false);
558
559         manContourModel->AddPoint(x, y, z);
560         manContourView->AddPoint();
561
562         manContourModel->AddPoint(x, y, z);
563         manContourView->AddPoint();
564
565         int bak = manContourControl->GetNumberOfPointsManualContour() - 1;
566         manContourControl->_bakIdPoint = bak;
567         
568         manContourControl->SetMoving(false);
569
570         connectorcontroller->setModelAndView(connectorModel, connectorView);
571
572         int newId = addObjectController(connectorcontroller);
573
574         connectorcontroller->setManualContourController(manContourControl);
575         connectorModel->setManualContourModel(manContourModel);
576         connectorView->setManualContourView(manContourView);
577         connectorView->setModel(connectorModel);
578         connectorView->setBaseView(_baseView);
579
580         connectorModel->addObserver(connectorView);
581         connectorModel->addObserver(this);
582
583 //      manContourView->Refresh();
584         
585         return newId;
586 }
587
588 //=========================================================================
589
590 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param) {
591         vtkInteractorStyleBaseView
592                         * baseViewControlManager =
593                                         (vtkInteractorStyleBaseView*) _baseView->GetInteractorStyleBaseView();
594         baseViewControlManager->AddInteractorStyleMaracas(param);
595 }
596
597 //=========================================================================
598
599 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param) {
600         vtkInteractorStyleBaseView
601                         * baseViewControlManager =
602                                         (vtkInteractorStyleBaseView*) _baseView->GetInteractorStyleBaseView();
603         baseViewControlManager->RemoveInteractorStyleMaracas(param);
604 }
605
606 //=========================================================================
607
608 vtkRenderer* wxVtkSceneManager::getRenderer() {
609         return _baseView->GetRenderer();
610 }
611
612 //=========================================================================
613
614 vtkRenderWindow* wxVtkSceneManager::getRenderWindow() {
615         return _baseView->GetRenWin();
616 }
617 //=========================================================================
618
619
620 //=========================================================================
621 int wxVtkSceneManager::GetIndexInSelected(int idControler) {
622         int index = -1;
623         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
624                 if (_selectedObjects[i] == idControler) {
625                         index = i;
626                         break;
627                 }
628         }
629         return index;
630 }
631 //=========================================================================
632
633 void wxVtkSceneManager::update(int idController, int command) {
634
635         if (command != NO_COMMAND) {
636                 if (command == ADD_TO_SELECTED) {
637                         if (GetIndexInSelected(idController) == -1) {
638                                 _selectedObjects.push_back(idController);
639                         }
640                 } else if (command == REMOVE_FROM_SELECTED) {
641                         int index = GetIndexInSelected(idController);
642                         if (index >= 0) {
643                                 _selectedObjects.erase(_selectedObjects.begin() + index);
644                         }
645                 } else if (command == INIT_CREATION_CONTOUR) {
646
647                 // JGRR & CM WH
648                 typedef std::map<int , GObjectController*>::iterator TIterator ;
649                 TIterator iter ;
650                 TIterator iter_end ;
651                 // Find current item in the controller
652                 iter = _controllers.find( idController ) ;
653                 iter_end = _controllers.end( ) ;
654                 // If item exist 
655                 if ( iter != iter_end ) {
656                     GObjectController* gobjc = iter->second ;
657
658                     std::string bbtkType = gobjc->getModel( )->getBBTKType( ) ;
659                     // And if the item is a port
660                     if ( gobjc->getModel( )->getGObjectType( ) == GPORT ) {
661                         TIterator c_it = _controllers.begin( ) ;
662                         // Go through all componets 
663                         for ( c_it ; c_it != iter_end ; c_it++ ) {
664                             GObjectController* obc = c_it->second ;
665                             int obc_type = obc->getModel( )->getGObjectType( ) ;
666                             std::string obc_bbtkType = obc->getModel( )->getBBTKType( ) ;
667                             // if the component is a port and the type is the same of that selected
668                             if ( ( obc_type == GPORT ) && ( obc_bbtkType == bbtkType ) ) {
669                                 // Highliht element 
670                                 obc->getView( )->setState( POSSIBLE_CONNECTION ) ;                             
671                                 //obc->getView( )->setState( HIGHLIGHTED ) ;
672                             }
673                         }
674                     }
675                 }
676
677                 // EO JGRR & CM WH  
678                      
679                         _worldState                                     = INIT_CREATION_CONTOUR;
680                         GObjectController* cont         = _controllers[idController];
681                         GPortModel* startOutputPort = (GPortModel*) cont->getModel();
682
683                         // The last one is the controller of the connector
684                         std::map<int, GObjectController*>::iterator it2;
685
686
687                         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
688                                 GObjectController *cont = it2->second;
689                                 if (cont->getGObjectType() == GPORT) {
690                                         GPortModel* port = (GPortModel*) cont->getModel();
691                                         if (port->getPortType() == GINPUTPORT) {
692                                                 cont->SetActive(true);
693                                         } else {
694                                                 cont->getView()->setState(NOTHING_HAPPENS);
695                                                 cont->getModel()->notifyObservers(_idManager);
696                                                 cont->SetActive(false);
697                                         }
698                                 } else {
699                                         cont->getView()->setState(NOTHING_HAPPENS);
700                                         cont->getModel()->notifyObservers(_idManager);
701                                         cont->SetActive(false);
702                                 }
703                         }
704                         _selectedObjects.clear();
705                         _idConnectionInCreation = createGConnector(startOutputPort);
706                 } else if (command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR) {
707                         _worldState = NOTHING_HAPPENS;
708                         //int id = _controllers.size()-1;
709                         GObjectController       *cont                   = _controllers[_idConnectionInCreation];
710                         GConnectorModel         *modelContour   = (GConnectorModel*) cont->getModel();
711                         GObjectController       *finPort                = _controllers[idController];
712
713                         if (finPort->getGObjectType() == GPORT) {
714                                 GPortModel* modelPort = (GPortModel*) finPort->getModel();
715                                 modelContour->setEndPort(modelPort);
716
717                                 // JGRR CM                     
718                                 typedef std::map<int, GObjectController*>::iterator TIterator;
719                                 TIterator c_it = _controllers.begin();
720                                 TIterator iter_end = _controllers.end();
721                                 // Go through all componets 
722                                 for (c_it; c_it != iter_end; c_it++) {
723                                     GObjectController* obc = c_it->second;
724                                     int obc_type = obc->getModel()->getGObjectType();
725                                     int obc_state = obc->getView()->getState();
726                                     // if the component is a port and the type is the same of that selected
727                                     if ((obc_type == GPORT) && (obc_state == POSSIBLE_CONNECTION)) {
728                                         // Return element to neutral state if not selected
729                                         obc->getView()->setState(NOTHING_HAPPENS);
730                                     }
731                                     if ((obc_type == GPORT) && (obc_state == SELECTED_POSSIBLE_CONNECTION)) {
732                                         // Change element to connected state
733                                         obc->getView()->setState(HIGHLIGHTED);
734
735                                     }
736
737                                 }
738                                 // EO JGRR CM                                
739                     
740                                                                 _parent->saveTempandUpdate("create connection");
741                         }
742
743                         manualConnectorContourController        *manCont        = ((GConnectorController*) cont)->getManualContourController();
744                         manualConnectorContourView                      *connView       = (manualConnectorContourView*) manCont->GetManualViewBaseContour();
745                                      
746                         std::map<int, GObjectController*>::iterator it2;
747
748                         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
749                                 GObjectController *cont = it2->second;
750                                 if (cont->getView() != NULL) {
751                                         cont->getView()->setState(NOTHING_HAPPENS);
752                                         cont->getModel()->notifyObservers(_idManager);
753                                 }
754                                 cont->SetActive(true);
755                         }
756                         connView->Refresh();          
757                 }
758
759         }
760         
761 }
762
763 //=========================================================================
764
765 bool wxVtkSceneManager::OnMouseMove() 
766 {
767         int X, Y;
768         bool okStatusMessage    = false;
769         bool okPortMessage              = false;
770         wxVTKRenderWindowInteractor *wxVTKiren;
771         wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
772         wxVTKiren->GetEventPosition(X, Y);
773
774         if (_worldState == DRAG_OBJECTS) {
775                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
776                         int id = _selectedObjects[i];
777                         GObjectController* cont = _controllers[id];
778                         if (_startDragging) {
779                                 cont->getView()->setStartDragging(true);
780                         }
781                         cont->getView()->moveObject(X, Y);
782                         cont->getView()->setState(DRAG);
783                         cont->getModel()->notifyObservers(_idManager);
784                 }
785
786                 std::map<int, GObjectController*>::iterator it;
787
788                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
789                         GObjectController *desc = it->second;
790                         if (desc->getGObjectType() == GCONNECTOR) {
791                                 vtkGConnectorView* vconn = (vtkGConnectorView*) desc->getView();
792                                 vconn->updateStartEndPoints();
793                         }
794                 }
795
796                 _startDragging = false;
797
798         } else if (_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR) {
799                 std::map<int, GObjectController*>::iterator it;
800                 double px, py, pz;
801                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
802                         GObjectController *desc = it->second;
803                         int type        = desc->getGObjectType();
804                         int state       = desc->getView()->getState();
805                         desc->getModel()->getCenter(px, py, pz);
806
807                         // JGRR & CM
808                         if ((state == HIGHLIGHTED) || (state == SELECTED_POSSIBLE_CONNECTION )) 
809                         {
810                         // EO JGRR & CM    
811                                 okStatusMessage = true;
812                                 updateStatusBar(desc->getStatusText());
813                                 if (type == GBLACKBOX) 
814                                 {
815                                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
816                                         _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
817                                 } // if GBLACKBOX
818                                 if (type == GPORT) 
819                                 {
820                                         // PortText
821                                         okPortMessage = true;
822                                         py = py + 5;
823                                         _textActor->SetInput(desc->getStatusText().c_str());
824                                         _textActor->SetScale(0.1);
825                                         _textActor->SetPosition(px - 25, py + 1, pz + 2);
826
827                                         // FillPortText
828                                         px = px - 33;
829                                         //py = py;
830                                         _fillObjectActor->SetScale(1);
831                                         _fillObjectActor->GetProperty()->SetOpacity(0.50);
832                                         _fillObjectActor->SetPosition(px, py, 1);
833                                         _aPolygonMapper->Modified();
834                                 } // if GPORT
835
836                         } // for controllers it
837                 }
838         }
839         if (okStatusMessage == false) 
840         {
841                 updateStatusBar("");
842         }
843
844         if (okPortMessage == false) 
845         {
846                 _textActor->SetScale(0.0001);
847                 _fillObjectActor->SetScale(0.0001);
848         }
849         return true;
850 }
851
852 //=========================================================================
853
854 bool wxVtkSceneManager::OnLeftButtonDown() 
855 {
856         if (_worldState == INIT_CREATION_CONTOUR) 
857         {
858                 bool isOverPort = false;
859                 std::map<int, GObjectController*>::iterator it;
860                 for (it = _controllers.begin(); it != _controllers.end() && isOverPort== false; ++it) 
861                 {
862                         GObjectController *desc = it->second;
863                         if (desc->getGObjectType() == GPORT) 
864                         {
865                                 GPortModel *portmod = (GPortModel*) desc->getModel();
866                                 vtkGObjectView *portView = desc->getView();
867                                 if (portmod->getPortType() == GINPUTPORT && portView->getState() == HIGHLIGHTED) 
868                                 {
869                                         isOverPort = true;
870                                 } // if
871                                 // JGRR & CM                    
872                                 if ( portmod->getPortType( ) == GINPUTPORT &&  portView->getState( ) == SELECTED_POSSIBLE_CONNECTION   ) {
873                                         isOverPort = true ;
874                                         portView->setState( HIGHLIGHTED);
875                                 }                    
876                                 // EO JGRR & CM 
877
878                         } // if
879                 } // for
880
881                 if (isOverPort == false) 
882                 {
883                         CancelConnection();
884                         UnSelectBlackBoxes();
885                 } // isOverPort
886         
887
888         } else { //_worldState
889
890                 _worldState             = DRAG_OBJECTS;
891                 _startDragging  = true;
892
893                 int shiftkey    = _vtkInteractorStyleBaseView->GetInteractor()->GetShiftKey();
894                 int ctrlkey             = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
895                 ctrlkey                 = ctrlkey + shiftkey;
896
897                 GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
898
899                 if (cont != NULL) {
900                         int state = cont->getView()->getState();
901                         if ((ctrlkey == 0) && (state == HIGHLIGHTED)) {
902                                 UnSelectBlackBoxes();
903                         }
904                 } else {
905                         UnSelectBlackBoxes();  // EED 12/07/2012
906                 }
907                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
908                         int id = _selectedObjects[i];
909                         GObjectController *cont = _controllers[id];
910                         cont->getView()->setState(DRAG);
911                         cont->getModel()->notifyObservers(_idManager);
912                 } // for
913         } // if _selectedObjects.size
914
915
916         return true;
917 }
918
919 //=========================================================================
920
921 bool wxVtkSceneManager::OnLeftButtonUp() 
922 {
923         if (_worldState == DRAG_OBJECTS) {
924                 _worldState = NOTHING_HAPPENS;
925
926                 for (int i = 0; i < (int) _selectedObjects.size(); i++) {
927                         int id = _selectedObjects[i];
928                         GObjectController* cont = _controllers[id];
929                         cont->getView()->setState(SELECTED);
930                         cont->getModel()->notifyObservers(_idManager);
931                 }
932         }
933         return true;
934 }
935
936 //=========================================================================
937 void wxVtkSceneManager::CancelConnection() 
938 {
939         if (_worldState == INIT_CREATION_CONTOUR) 
940         {
941                 _worldState = NOTHING_HAPPENS;
942                 //int lastId = _controllers.size()-1;
943
944                 GConnectorController *connector =
945                                 (GConnectorController*) _controllers[_idConnectionInCreation];
946                 connector->removeFromScene();
947                 unregisterController(connector);
948                 _controllers.erase(_idConnectionInCreation);
949
950                 std::map<int, GObjectController*>::iterator it;
951                 for (it = _controllers.begin(); it != _controllers.end(); ++it) 
952                 {
953                         GObjectController *desc = it->second;
954                         desc->SetActive(true);
955                         desc->getView()->setState(NOTHING_HAPPENS);
956                         desc->getModel()->notifyObservers(_idManager);
957                 } // for
958         }// if
959 }
960
961 //=========================================================================
962
963 bool wxVtkSceneManager::OnRightButtonUp() 
964 {
965         if (_worldState == INIT_CREATION_CONTOUR) 
966         {
967                 CancelConnection();
968         }
969         UnSelectBlackBoxes();
970         return true;
971 }
972
973  ///JLGR 21-05-2012
974
975 bool wxVtkSceneManager::OnMiddleButtonDown() {
976
977
978         this->_vtkInteractorStyleBaseView->StartPan();
979
980         return true;
981     }
982
983 bool wxVtkSceneManager::OnMiddleButtonUp() {
984
985
986         this->_vtkInteractorStyleBaseView->EndPan();
987
988
989         return true;
990     }
991
992
993 //=========================================================================
994
995 GObjectController *wxVtkSceneManager::GetGBlackBoxControlerPointedByMouse() {
996         GObjectController *result = NULL;
997         int X, Y;
998         wxVTKRenderWindowInteractor *wxVTKiren;
999         wxVTKiren
1000                         = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
1001         wxVTKiren->GetEventPosition(X, Y);
1002
1003         std::map<int, GObjectController*>::iterator it;
1004
1005         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1006                 GObjectController *cont = it->second;
1007                 int type = cont->getGObjectType();
1008
1009                 if (cont->getView()->isPointInside(X, Y) == true) {
1010                         if (type == GBLACKBOX) {
1011                                 result = cont;
1012                         } // if type
1013                 } // if isPointInside
1014         } // for it
1015         return result;
1016 }
1017
1018 //=========================================================================
1019 void wxVtkSceneManager::UnSelectBlackBoxes() {
1020         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
1021                 int id = _selectedObjects[i];
1022                 GObjectController* control = _controllers[id];
1023                 control->getView()->setState(NOTHING_HAPPENS);
1024                 control->getModel()->notifyObservers(_idManager);
1025         }// for _selectedObjects
1026         _selectedObjects.clear();
1027 }
1028
1029 //=========================================================================
1030 bool wxVtkSceneManager::OnLeftDClick() {
1031         GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
1032         if (cont != NULL) {
1033                 UnSelectBlackBoxes();
1034                 _parent->editBlackBox((GBlackBoxModel*) cont->getModel());
1035         }
1036
1037         /*EED Borrame
1038
1039          int X,Y;
1040          wxVTKRenderWindowInteractor *wxVTKiren;
1041          wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
1042          wxVTKiren->GetEventPosition(X,Y);
1043
1044          std::map<int, GObjectController*>::iterator it;
1045
1046          bool clickOnObject = false;
1047
1048          for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
1049          {
1050          GObjectController *cont = it->second;
1051          int type = cont->getGObjectType();
1052
1053          if(cont->getView()->isPointInside(X,Y))
1054          {
1055          if(type==GBLACKBOX)
1056          {
1057          for (int i=0; i< (int)_selectedObjects.size(); i++)
1058          {
1059          int id = _selectedObjects[i];
1060          GObjectController* control = _controllers[id];
1061          control->getView()->setState(NOTHING_HAPPENS);
1062          }
1063          _selectedObjects.clear();
1064
1065          GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
1066          _parent->editBlackBox(bbmodel);
1067          }
1068          clickOnObject = true;
1069          }
1070          }
1071
1072          if(clickOnObject==false)
1073          {
1074          //_parent->editDiagramParameters(this);
1075          }
1076          */
1077         return true;
1078 }
1079
1080 //=========================================================================
1081
1082 bool wxVtkSceneManager::OnChar() 
1083 {
1084         bool ok=false;
1085         bool oktmp;
1086         char keyCode    = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
1087         int ctrlkey             = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
1088         
1089         // KeyCode 127 : Delete Key
1090         // KeyCode 8 : Backspace Key
1091
1092
1093         if (keyCode == 8 || keyCode == 127 || keyCode == 27) 
1094         {
1095                 if (_worldState == INIT_CREATION_CONTOUR) 
1096                 {
1097                         CancelConnection();
1098                         UnSelectBlackBoxes();
1099                 }
1100         }
1101
1102
1103         if (keyCode == 8 || keyCode == 127) 
1104         {
1105
1106                         for (int i = 0; i < (int) _selectedObjects.size(); i++) 
1107                         {
1108                                 int id = _selectedObjects[i];
1109                                 std::map<int, GObjectController*>::iterator it;
1110                                 it = _controllers.find(id);
1111                                 if (it != _controllers.end()) 
1112                                 {
1113                                         oktmp   =       deleteObject(id);
1114                                         ok              =       ok||oktmp;
1115                                 } // if
1116                         }// for selected objects                
1117                         if (ok) _selectedObjects.clear();
1118                         _parent->saveTempandUpdate("delete object");
1119         }// if keyCode
1120         
1121         /// Duplicate 
1122 #ifdef MACOSX
1123         if((keyCode==0) && (ctrlkey==1)) {                
1124 #else
1125         ctrlkey+=keyCode;
1126         if(ctrlkey==5){                
1127 #endif
1128                 if(_selectedObjects.size()){
1129                         std::cout<<"objects to copy :";
1130                         for(int i=0;i<_selectedObjects.size();i++)
1131                         {
1132                                 std::cout<<_selectedObjects.at(i)<<" ";
1133                         }
1134                         DuplicateObjects(getSelectedObjects());   
1135                 }else{
1136                         std::cout<<"No objects to copy\n";
1137                 } // _selectedObjects.size
1138         } // ctrlkey 5  
1139
1140         return true;
1141 }
1142
1143 //=========================================================================
1144
1145 void wxVtkSceneManager::AddControlerToBeRemove(
1146                 std::vector<int> *controllersToRemove, int id) {
1147
1148         int i;
1149         bool ok = true;
1150         for (i = 0; i < (int) controllersToRemove->size(); i++) {
1151                 if (id == (*controllersToRemove)[i]) {
1152                         ok = false;
1153                 }
1154         }
1155
1156         if (ok == true) {
1157                 controllersToRemove->push_back(id);
1158         }
1159
1160 }
1161
1162 //=========================================================================
1163
1164 bool wxVtkSceneManager::deleteObject(int id) 
1165 {
1166         bool result=true;
1167         GObjectController *control = _controllers[id];
1168         std::vector<int> controllersToRemove;
1169         if (control->getGObjectType() == GBLACKBOX || control->getGObjectType()
1170                         == GCOMPLEXINPUTPORT || control->getGObjectType()
1171                         == GCOMPLEXOUTPUTPORT) {
1172                 GBoxModel *bbmod = (GBoxModel*) control->getModel();
1173                 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
1174
1175                 bool boxConnected = false;
1176
1177                 // Add box input controllers to be removed
1178                 for (int i = 0; i < (int) inputs.size(); i++) {
1179                         AddControlerToBeRemove(&controllersToRemove, inputs[i]->getObjectId());
1180                         if (inputs[i]->isConnected()) {
1181                                 boxConnected = true;
1182                         }
1183                 }
1184
1185                 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
1186
1187                 // Add box output controllers to be removed
1188                 for (int i = 0; i < (int) outputs.size(); i++) {
1189                         AddControlerToBeRemove(&controllersToRemove, outputs[i]->getObjectId());
1190                         if (outputs[i]->isConnected()) {
1191                                 boxConnected = true;
1192                         }
1193                 }
1194
1195                 // Add connection controllers to be removed
1196                 std::map<int, GObjectController*>::iterator it;
1197                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1198                         GObjectController *cont = it->second;
1199                         int type = cont->getGObjectType();
1200                         if (type == GCONNECTOR) {
1201                                 GConnectorModel *conMod = (GConnectorModel*) cont->getModel();
1202                                 if (conMod->getStartPort() != NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId()) 
1203                                 {
1204                                         AddControlerToBeRemove(&controllersToRemove,conMod->getObjectId());
1205                                 }
1206                                 if (conMod->getEndPort() != NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId()) 
1207                                 {
1208                                         AddControlerToBeRemove(&controllersToRemove,conMod->getObjectId());
1209                                 }
1210                         }
1211                 }
1212
1213                 // Add box controller to be removed
1214                 AddControlerToBeRemove(&controllersToRemove, bbmod->getObjectId());
1215         } else if (control->getGObjectType() == GCONNECTOR) {
1216                 GConnectorController *gconnectorcontroler = (GConnectorController*)control;
1217                 if (gconnectorcontroler->getManualContourController()->IsEditable()==false)
1218                 {
1219                         GConnectorModel *conMod = (GConnectorModel*) control->getModel();
1220                         AddControlerToBeRemove(&controllersToRemove, conMod->getObjectId());
1221                 } else{
1222                         gconnectorcontroler->getManualContourController()->OnChar();
1223                         result = false;
1224                 }
1225         }
1226
1227         std::map<int, GObjectController*>::iterator it;
1228
1229         for (int i = 0; i < (int) controllersToRemove.size(); i++) {
1230                 int key = controllersToRemove[i];
1231                 it = _controllers.find(key);
1232                 GObjectController *cont = _controllers[key];
1233                 if (cont != NULL) {
1234                         cont->removeFromScene();
1235                         unregisterController((InteractorStyleMaracas*) cont);
1236                         _controllers.erase(it);
1237                 }//if
1238         }// for
1239         
1240         
1241         return result;
1242 }
1243
1244 //=========================================================================
1245
1246 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName,
1247                 std::string boxName) {
1248         _parent->displayBlackBoxInfo(packageName, boxName);
1249 }
1250
1251 //=========================================================================
1252
1253 void wxVtkSceneManager::updateStatusBar(std::string textStatus) {
1254         _parent->updateStatusBar(textStatus);
1255 }
1256
1257 //=========================================================================
1258 std::string wxVtkSceneManager::LineNumber(bool withLineNumber, int &value) {
1259         std::string result = "";
1260         if (withLineNumber == true) {
1261                 value++;
1262                 stringstream StrStream;
1263                 StrStream << value;
1264                 std::string strValue = StrStream.str();
1265                 if (value < 10) {
1266                         result += "  ";
1267                 } else if (value < 100) {
1268                         result += " ";
1269                 }
1270
1271                 result += strValue + ": ";
1272         }
1273         return result;
1274 }
1275 //=========================================================================
1276 //DFCH
1277 bool wxVtkSceneManager::MakeBoxExecutable() {
1278         std::map<int, GObjectController*> selected = getSelectedObjects();
1279         if( selected.empty() )
1280                 return false;
1281         bool ret = false;
1282         std::map<int, GObjectController*>::iterator it;
1283         for (it = selected.begin(); it != selected.end(); ++it) {
1284                 GObjectController* lastSelected = it->second;
1285                 if (lastSelected) {
1286                         vtkGObjectView* view = (vtkGObjectView*) lastSelected->getView();
1287                         GBlackBoxModel *bbmodel =
1288                                         (GBlackBoxModel*) lastSelected->getModel();
1289                         view->setState(HIGHLIGHTED);
1290                         if (bbmodel->isExecutable()) {
1291                                 bbmodel->setExecutable(false);
1292                         } else {
1293                                 bbmodel->setExecutable(true);
1294                         }
1295                         bbmodel->notifyObservers(lastSelected->getId());
1296                         ret = true;
1297                 }
1298         }
1299         return ret;
1300 }
1301 //DFCH
1302
1303 //=========================================================================
1304
1305 std::string wxVtkSceneManager::getDiagramBBS(bool wln) {
1306         bool existsExec = false;
1307
1308         std::vector<std::string> packages;
1309         std::vector<int> boxes;
1310         std::vector<int> connections;
1311         std::vector<int> execBoxes;
1312
1313         std::map<int, GObjectController*>::iterator it;
1314
1315         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1316                 GObjectController *desc = it->second;
1317                 int type = desc->getGObjectType();
1318
1319                 if (type == GBLACKBOX) {
1320                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1321
1322                         std::string pkg = mod->getBBTKPackage();
1323                         bool existsPkg = false;
1324                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1325                                 if (packages[t] == pkg) {
1326                                         existsPkg = true;
1327                                 }
1328                         }
1329                         if (!existsPkg) {
1330                                 packages.push_back(pkg);
1331                         }
1332
1333                         boxes.push_back(it->first);
1334                         if (mod->isExecutable()) {
1335                                 execBoxes.push_back(it->first);
1336                                 existsExec = true;
1337                         }
1338                 } else if (type == GCONNECTOR) {
1339                         connections.push_back(it->first);
1340                 }
1341         }
1342
1343         int lnNmbr = 0;
1344         std::string script = "";
1345         script += LineNumber(wln, lnNmbr) + "# BBTK GEditor Script\n";
1346         script += LineNumber(wln, lnNmbr) + "# ----------------------\n";
1347         script += LineNumber(wln, lnNmbr) + "\n";
1348         if (existsExec) {
1349                 script += LineNumber(wln, lnNmbr) + "include std\n"; // EED
1350                 script += LineNumber(wln, lnNmbr) + "include itkvtk\n"; // EED
1351                 int i;
1352                 for (i = 0; i < (int) packages.size(); i++) {
1353                         script += LineNumber(wln, lnNmbr);
1354                         script += "include ";
1355                         script += packages[i];
1356                         script += "\n";
1357                 }
1358
1359                 script += LineNumber(wln, lnNmbr);
1360                 script += "\n";
1361
1362                 //ups2
1363
1364                 script += LineNumber(wln, lnNmbr);
1365                 script += "author \"";
1366                 script += _Author;
1367                 script += "\"\n";
1368
1369                 script += LineNumber(wln, lnNmbr);
1370                 script += "description \"";
1371                 script += _Description;
1372                 script += "\"\n";
1373
1374                 script += LineNumber(wln, lnNmbr);
1375                 script += "category \"";
1376                 script += _Category;
1377                 script += "\"\n";
1378
1379                 script += LineNumber(wln, lnNmbr);
1380                 script += "\n";
1381
1382                 // script+="include std\n"; // JPR
1383
1384                 for (i = 0; i < (int) boxes.size(); i++) {
1385                         script += LineNumber(wln, lnNmbr);
1386                         script += "new ";
1387                         int id = boxes[i];
1388                         GObjectController *control = _controllers[id];
1389                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1390
1391                         script += model->getBBTKType();
1392                         script += " ";
1393                         script += model->getBBTKName();
1394                         script += "\n";
1395
1396                         std::vector<GPortModel*> inputs = model->getInputPorts();
1397                         for (int j = 0; j < (int) inputs.size(); j++) {
1398                                 GPortModel* inputPort = inputs[j];
1399                                 if (inputPort->isValueSet()) {
1400                                         script += LineNumber(wln, lnNmbr);
1401                                         script += "  set ";
1402                                         script += model->getBBTKName();
1403                                         script += ".";
1404                                         script += inputPort->getBBTKName();
1405                                         script += " ";
1406                                         script += inputPort->getValue();
1407                                         script += "\n";
1408                                 }
1409                         }
1410
1411                         script += LineNumber(wln, lnNmbr);
1412                         script += "\n";
1413
1414                 }
1415
1416                 script += LineNumber(wln, lnNmbr);
1417                 script += "\n";
1418
1419
1420                 std::string complexInputs="";
1421
1422                 for (i = 0; i < (int) connections.size(); i++) {
1423                         //script += LineNumber(wln, lnNmbr);
1424                         //script += "connect ";
1425                         int id = connections[i];
1426                         GObjectController *control = _controllers[id];
1427                         GConnectorModel *model = (GConnectorModel*) control->getModel();
1428
1429
1430                         //Connection info
1431                         GPortModel *start = model->getStartPort();
1432                         GBoxModel *startBox = start->getParentBox();
1433
1434                         GPortModel *end = model->getEndPort();
1435                         GBoxModel *endBox = end->getParentBox();
1436
1437                         if (startBox->getGObjectType() == GCOMPLEXINPUTPORT)
1438                         {
1439                                         complexInputs += "input ";
1440                                         complexInputs += startBox->getBBTKName();
1441
1442                                         complexInputs += " ";
1443                                         complexInputs += endBox->getBBTKName();
1444                                         complexInputs += ".";
1445                                         complexInputs += end->getBBTKName();
1446
1447                                         complexInputs += " ";
1448                                         complexInputs += "\" \"";
1449
1450                                         complexInputs += "\n";
1451                         }
1452                         else
1453                         {
1454                                         script += "connect ";
1455                                         script += startBox->getBBTKName();
1456                                         script += ".";
1457                                         script += start->getBBTKName();
1458
1459                                         script += " ";
1460
1461                                         //End Connection info
1462                                         script += endBox->getBBTKName();
1463                                         script += ".";
1464                                         script += end->getBBTKName();
1465
1466                                         script += "\n";
1467                         }
1468
1469                         script += "\n";
1470                 }
1471
1472                 script += LineNumber(wln, lnNmbr);
1473                 script += "\n";
1474
1475                 script += "\n";
1476                 script += "# Complex input ports\n";
1477                 script += complexInputs;
1478
1479                 for (i = 0; i < (int) execBoxes.size(); i++) {
1480                         script += LineNumber(wln, lnNmbr);
1481                         script += "exec ";
1482                         int id = execBoxes[i];
1483                         GObjectController *control = _controllers[id];
1484                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1485
1486                         script += model->getBBTKName();
1487                         script += "\n";
1488                 }
1489
1490         }
1491
1492         return script;
1493 }
1494
1495 //=========================================================================
1496
1497 std::string wxVtkSceneManager::saveComplexBoxBBS() {
1498         std::vector<std::string> packages;
1499         std::vector<int> boxes;
1500         std::vector<int> connections;
1501         std::vector<int> execBoxes;
1502
1503         std::map<int, GObjectController*>::iterator it;
1504
1505         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1506                 GObjectController *desc = it->second;
1507                 int type = desc->getGObjectType();
1508
1509                 if (type == GBLACKBOX) {
1510                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1511
1512                         std::string pkg = mod->getBBTKPackage();
1513                         bool existsPkg = false;
1514                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1515                                 if (packages[t] == pkg) {
1516                                         existsPkg = true;
1517                                 }
1518                         }
1519                         if (!existsPkg) {
1520                                 packages.push_back(pkg);
1521                         }
1522
1523                         boxes.push_back(it->first);
1524                         if (mod->isExecutable()) {
1525                                 execBoxes.push_back(it->first);
1526                         }
1527                 } else if (type == GCONNECTOR) {
1528                         connections.push_back(it->first);
1529                 }
1530         }
1531
1532         std::string script = "";
1533
1534         script += "include std\n"; // EED
1535         script += "include itkvtk\n"; // EED
1536         int i;
1537         for (i = 0; i < (int) packages.size(); i++) {
1538                 script += "include ";
1539                 script += packages[i];
1540                 script += "\n";
1541         }
1542         script += "\n";
1543
1544         // Definition of a complex box
1545         script += "define ";
1546         script += _cbName + " " + _cbPackageName;
1547         script += "\n";
1548         script += "\n";
1549
1550         script += "author \"";
1551         script += _Author;
1552         script += "\"\n";
1553
1554         script += "description \"";
1555         script += _Description;
1556         script += "\"\n";
1557         script += "\n";
1558
1559         script += "category \"";
1560         script += _Category;
1561         script += "\"\n";
1562         script += "\n";
1563
1564         // Create boxes
1565         for (i = 0; i < (int) boxes.size(); i++) {
1566                 script += "new ";
1567                 int id = boxes[i];
1568                 GObjectController *control = _controllers[id];
1569                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1570
1571                 script += model->getBBTKType();
1572                 script += " ";
1573                 script += model->getBBTKName();
1574                 script += "\n";
1575
1576                 std::vector<GPortModel*> inputs = model->getInputPorts();
1577                 for (int j = 0; j < (int) inputs.size(); j++) {
1578                         GPortModel* inputPort = inputs[j];
1579                         if (inputPort->isValueSet()) {
1580                                 script += "  set ";
1581                                 script += model->getBBTKName();
1582                                 script += ".";
1583                                 script += inputPort->getBBTKName();
1584                                 script += " ";
1585                                 script += inputPort->getValue();
1586                                 script += "\n";
1587                         }
1588                 }
1589                 script += "\n";
1590
1591         }
1592
1593         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1594
1595         std::string complexInputs = "";
1596         std::string complexOutputs = "";
1597         script += "\n";
1598
1599         for (i = 0; i < (int) connections.size(); i++) {
1600                 int id = connections[i];
1601                 GObjectController *control = _controllers[id];
1602                 GConnectorModel *model = (GConnectorModel*) control->getModel();
1603
1604                 //Connection info
1605                 GPortModel *start = model->getStartPort();
1606                 GBoxModel *startBox = start->getParentBox();
1607
1608                 GPortModel *end = model->getEndPort();
1609                 GBoxModel *endBox = end->getParentBox();
1610
1611                 if (startBox->getGObjectType() == GCOMPLEXINPUTPORT) {
1612                         complexInputs += "input ";
1613                         complexInputs += startBox->getBBTKName();
1614
1615                         complexInputs += " ";
1616                         complexInputs += endBox->getBBTKName();
1617                         complexInputs += ".";
1618                         complexInputs += end->getBBTKName();
1619
1620                         complexInputs += " ";
1621                         complexInputs += "\" \"";
1622
1623                         complexInputs += "\n";
1624                 } else if (endBox->getGObjectType() == GCOMPLEXOUTPUTPORT) {
1625                         complexOutputs += "output ";
1626                         complexOutputs += endBox->getBBTKName();
1627
1628                         complexOutputs += " ";
1629                         complexOutputs += startBox->getBBTKName();
1630                         complexOutputs += ".";
1631                         complexOutputs += start->getBBTKName();
1632
1633                         complexOutputs += " ";
1634                         complexOutputs += "\" \"";
1635
1636                         complexOutputs += "\n";
1637                 } else {
1638                         script += "connect ";
1639                         script += startBox->getBBTKName();
1640                         script += ".";
1641                         script += start->getBBTKName();
1642
1643                         script += " ";
1644
1645                         //End Connection info
1646                         script += endBox->getBBTKName();
1647                         script += ".";
1648                         script += end->getBBTKName();
1649
1650                         script += "\n";
1651                 }
1652         }
1653
1654         script += "\n";
1655         script += "# Complex input ports\n";
1656         script += complexInputs;
1657
1658         script += "\n";
1659         script += "# Complex output ports\n";
1660         script += complexOutputs;
1661
1662         script += "\n";
1663
1664         for (i = 0; i < (int) execBoxes.size(); i++) {
1665                 script += "exec ";
1666                 int id = execBoxes[i];
1667                 GObjectController *control = _controllers[id];
1668                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1669
1670                 script += model->getBBTKName();
1671                 script += "\n";
1672         }
1673
1674         script += "\n";
1675         script += "endefine";
1676         script += "\n";
1677
1678         return script;
1679 }
1680
1681 //=========================================================================
1682
1683 void wxVtkSceneManager::deleteAllBoxes() {
1684         std::map<int, GObjectController*>::iterator it;
1685         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1686                 GObjectController *cont = it->second;
1687                 cont->removeFromScene();
1688                 unregisterController((InteractorStyleMaracas*) cont);
1689         }
1690         _selectedObjects.clear();
1691         _controllers.clear();
1692 }
1693
1694 //=========================================================================
1695
1696
1697 void wxVtkSceneManager::refresh() {
1698         _baseView->Refresh();
1699 }
1700 //=========================================================================
1701
1702 void wxVtkSceneManager::refreshScene() {
1703         _baseView->RefreshView();
1704 }
1705
1706 //=========================================================================
1707
1708 void wxVtkSceneManager::centerView() {
1709         double temp[3];
1710         _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1711         _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
1712         _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1713         _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
1714 }
1715
1716 //=========================================================================
1717
1718 void wxVtkSceneManager::saveDiagram(std::string &content) {
1719         char buffer[50];
1720
1721         content += "CATEGORY:";
1722         content += GetCategory();
1723         content += "\n";
1724
1725         content += "DESCRIPTION:";
1726         content += GetDescription();
1727         content += "\n";
1728
1729         content += "AUTHOR:";
1730         content += GetAuthor();
1731         content += "\n";
1732
1733         //Print info IF COMPLEX BOX
1734         content += "COMPLEXBOX:";
1735         if (_isComplexBox) {
1736                 content += "TRUE\n";
1737                 //ups1
1738                 content += "COMPLEXBOXNAME:";
1739                 content += GetCbName();
1740                 content += "\n";
1741
1742                 content += "PACKAGENAME:";
1743                 content += GetCbPackageName();
1744                 content += "\n";
1745
1746                 /* CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1747                  * External inputs are possible for applications and complex boxes
1748                 //Print info complex input ports
1749                 std::vector<int> inputs = getComplexInputPorts();
1750                 int insize = inputs.size();
1751                 content += "COMPLEXINPUTS:";
1752                 sprintf(buffer, "%d", insize);
1753                 content += buffer;
1754                 content += "\n";
1755
1756                 for (int i = 0; i < insize; i++) {
1757                         int id = inputs[i];
1758                         GObjectController *cont = _controllers[id];
1759                         cont->getModel()->save(content);
1760                 }
1761                 */
1762
1763                 //Print info complex output ports
1764                 std::vector<int> outputs = getComplexOutputPorts();
1765                 int outsize = outputs.size();
1766                 content += "COMPLEXOUTPUTS:";
1767                 sprintf(buffer, "%d", outsize);
1768                 content += buffer;
1769                 content += "\n";
1770
1771                 for (int i = 0; i < outsize; i++) {
1772                         int id = outputs[i];
1773                         GObjectController *cont = _controllers[id];
1774                         cont->getModel()->save(content);
1775                 }
1776         } // _isComplexBox
1777         else {
1778                 content += "FALSE\n";
1779         }
1780
1781         // CHANGE TO THE VERSION 1.4 RaC2012 2nd HackFest
1782         // External inputs are possible for applications and complex boxes
1783         std::vector<int> inputs = getComplexInputPorts();
1784         int insize = inputs.size();
1785         content += "COMPLEXINPUTS:";
1786         sprintf(buffer, "%d", insize);
1787         content += buffer;
1788         content += "\n";
1789
1790         for (int i = 0; i < insize; i++) {
1791                 int id = inputs[i];
1792                 GObjectController *cont = _controllers[id];
1793                 cont->getModel()->save(content);
1794         }
1795
1796         //Print boxes
1797         std::vector<int> boxes = getBlackBoxes();
1798         int bsize = boxes.size();
1799         content += "BOXES:";
1800         sprintf(buffer, "%d", bsize);
1801         content += buffer;
1802         content += "\n";
1803
1804         for (int i = 0; i < bsize; i++) {
1805                 int id = boxes[i];
1806                 GObjectController *cont = _controllers[id];
1807                 cont->getModel()->save(content);
1808         }
1809
1810         //Print connections
1811         std::vector<int> connections = getConnections();
1812         int csize = connections.size();
1813         content += "CONNECTIONS:";
1814         sprintf(buffer, "%d", csize);
1815         content += buffer;
1816         content += "\n";
1817
1818         for (int i = 0; i < csize; i++) {
1819                 int id = connections[i];
1820                 GObjectController *cont = _controllers[id];
1821                 cont->getModel()->save(content);
1822         }
1823
1824 }
1825
1826 //=========================================================================
1827 void wxVtkSceneManager::getCleanLine(stringstream &inputStream, std::string &line)
1828 {
1829         getline(inputStream, line,'\n');
1830         int size = line.length();
1831     if (size>0)
1832         {
1833                 if ( line[ size-1 ]==13  )
1834                 {
1835                         line.erase(size-1,1);
1836                 } // if line
1837         } // if size
1838 }
1839         
1840 //=========================================================================
1841 void wxVtkSceneManager::loadDiagram(stringstream &inputStream) 
1842 {
1843         int size;
1844         std::string version = "<void>";
1845         std::string line = "";
1846         char delims[] = ":";
1847         char *result = NULL;
1848
1849         getCleanLine(inputStream, line);
1850
1851         bool start = false;
1852         while (!inputStream.eof()) {
1853                 if (line == "" || line[0] == '#') {
1854                         line="";
1855                         getCleanLine(inputStream, line);
1856                         
1857                         if (line == "# - BBTKGEditor v 1.0 BBG BlackBox Diagram file") {
1858                                 version = line.substr(18, 3);
1859                         }
1860                         if (line == "# - BBTKGEditor v 1.1 BBG BlackBox Diagram file") {
1861                                 version = line.substr(18, 3);
1862                         }
1863                         if (line == "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file") {
1864                                 version = line.substr(18, 3);
1865                         }
1866                         if (line == "# - BBTKGEditor v 1.3 BBG BlackBox Diagram file") {
1867                                 version = line.substr(18, 3);
1868                         }
1869                         if (line == "# - BBTKGEditor v 1.4 BBG BlackBox Diagram file") {
1870                                 version = line.substr(18, 3);
1871                         }
1872                 } else if (line == "APP_START") {
1873                         start = true;
1874                         break;
1875                 }
1876         }
1877
1878
1879         if (start) {
1880
1881                 if ((version != "1.0") && (version != "1.1")) {
1882                         getCleanLine(inputStream, line);//CATEGORY:<category of the box>
1883                         char categoryTmp[30];
1884                         strcpy(categoryTmp, line.c_str());
1885                         result = strtok(categoryTmp, delims);
1886                         result = strtok(NULL, delims);
1887                         SetCategory(result);
1888
1889                         getCleanLine(inputStream, line);//DESCRIPTION:<description of the box>
1890                         char descriptionTmp[1024];
1891                         strcpy(descriptionTmp, line.c_str());
1892                         result = strtok(descriptionTmp, delims);
1893                         result = strtok(NULL, delims);
1894                         SetDescription(result);
1895
1896                         getCleanLine(inputStream, line);//AUTHOR:<author>
1897                         char authorTmp[255];
1898                         strcpy(authorTmp, line.c_str());
1899                         result = strtok(authorTmp, delims);
1900                         result = strtok(NULL, delims);
1901                         SetAuthor(result);
1902                 }
1903
1904                 //----------
1905                 getCleanLine(inputStream, line);//COMPLEX_BOX:TRUE|FALSE
1906                 char complexTmp[30];
1907                 strcpy(complexTmp, line.c_str());
1908                 result = strtok(complexTmp, delims);
1909                 result = strtok(NULL, delims);
1910                 std::string isComplexBox(result);
1911
1912                 if (isComplexBox == "TRUE") {
1913                         _isComplexBox = true;
1914
1915                         if ((version != "1.0") && (version != "1.1")) {
1916                                 getCleanLine(inputStream, line);//COMPLEXNAME:<name of the complex box>
1917                                 char complexboxnameTmp[255];
1918                                 strcpy(complexboxnameTmp, line.c_str());
1919                                 result = strtok(complexboxnameTmp, delims);
1920                                 result = strtok(NULL, delims);
1921                                 SetCbName(result);
1922
1923                                 getCleanLine(inputStream, line);//PACKAGENAME:<name of the package of the box>
1924                                 char packagenameTmp[255];
1925                                 strcpy(packagenameTmp, line.c_str());
1926                                 result = strtok(packagenameTmp, delims);
1927                                 result = strtok(NULL, delims);
1928                                 SetCbPackageName(result);
1929                         }
1930
1931                         if(version < "1.4")
1932                         {
1933                                 //-----------------------
1934                                 //- COMPLEX INPUT PORTS
1935                                 //-----------------------
1936                                 getCleanLine(inputStream, line);//COMPLEXINPUTS:num
1937                                 char inputs[30];
1938                                 strcpy(inputs, line.c_str());
1939                                 result = strtok(inputs, delims);
1940                                 result = strtok(NULL, delims);
1941
1942                                 int numInputs;
1943                                 std::istringstream inps(result);
1944                                 inps >> numInputs;
1945
1946                                 for (int i = 0; i < numInputs; i++) {
1947                                         //----------
1948                                         getCleanLine(inputStream, line);//COMPLEX_PORT
1949                                         getCleanLine(inputStream, line);//name
1950                                         std::string inputPortName(line);
1951
1952                                         //----------
1953                                         getCleanLine(inputStream, line);//xInic:yInic:zInic
1954                                         char coord[80];
1955                                         strcpy(coord, line.c_str());
1956                                         result = strtok(coord, delims);//xInic
1957                                         std::string xInic(result);
1958                                         result = strtok(NULL, delims);//yInic
1959                                         std::string yInic(result);
1960                                         result = strtok(NULL, delims);//zInic
1961                                         std::string zInic(result);
1962
1963                                         double xIn, yIn, zIn;
1964                                         std::istringstream xSt(xInic);
1965                                         xSt >> xIn;
1966                                         std::istringstream ySt(yInic);
1967                                         ySt >> yIn;
1968                                         std::istringstream zSt(zInic);
1969                                         zSt >> zIn;
1970
1971                                         if (version<="1.2")
1972                                         {
1973                                                 if (zIn==900) zIn=GPOSITION_Z;
1974                                         }
1975
1976                                         getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
1977
1978                                         configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
1979
1980                                 } // for input complex box
1981                         }
1982
1983
1984                         //-----------------------
1985                         //- COMPLEX OUTPUT PORTS
1986                         //-----------------------
1987
1988                         getCleanLine(inputStream, line);//COMPLEXOUTPUTS:num
1989                         char outputs[30];
1990                         strcpy(outputs, line.c_str());
1991                         result = strtok(outputs, delims);
1992                         result = strtok(NULL, delims);
1993
1994                         int numOutputs;
1995                         std::istringstream outps(result);
1996                         outps >> numOutputs;
1997
1998                         for (int i = 0; i < numOutputs; i++) {
1999                                 //----------
2000                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2001                                 getCleanLine(inputStream, line);//name
2002                                 std::string outputPortName(line);
2003
2004                                 //----------
2005                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2006                                 char coord[80];
2007                                 strcpy(coord, line.c_str());
2008                                 result = strtok(coord, delims);//xInic
2009                                 std::string xInic(result);
2010                                 result = strtok(NULL, delims);//yInic
2011                                 std::string yInic(result);
2012                                 result = strtok(NULL, delims);//zInic
2013                                 std::string zInic(result);
2014
2015                                 double xIn, yIn, zIn;
2016                                 std::istringstream xSt(xInic);
2017                                 xSt >> xIn;
2018                                 std::istringstream ySt(yInic);
2019                                 ySt >> yIn;
2020                                 std::istringstream zSt(zInic);
2021                                 zSt >> zIn;
2022
2023                                 if (version<="1.2")
2024                                 {
2025                                         if (zIn==900) zIn=GPOSITION_Z;
2026                                 }
2027                                 
2028                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2029
2030                                 configGComBoxInputOutputPort(false, outputPortName, xIn, yIn, zIn);
2031
2032                         } // for output complex box
2033
2034                 } // complex box
2035
2036                 // RaC2012 2nd Hackfest
2037                 if(version >= "1.4")
2038                 {
2039                         //-----------------------
2040                         //- EXTERNAL INPUT PORTS
2041                         //-----------------------
2042                         getCleanLine(inputStream, line);//COMPLEXINPUTS:num
2043                         char inputs[30];
2044                         strcpy(inputs, line.c_str());
2045                         result = strtok(inputs, delims);
2046                         result = strtok(NULL, delims);
2047
2048                         int numInputs;
2049                         std::istringstream inps(result);
2050                         inps >> numInputs;
2051
2052                         for (int i = 0; i < numInputs; i++) {
2053                                 //----------
2054                                 getCleanLine(inputStream, line);//COMPLEX_PORT
2055                                 getCleanLine(inputStream, line);//name
2056                                 std::string inputPortName(line);
2057
2058                                 //----------
2059                                 getCleanLine(inputStream, line);//xInic:yInic:zInic
2060                                 char coord[80];
2061                                 strcpy(coord, line.c_str());
2062                                 result = strtok(coord, delims);//xInic
2063                                 std::string xInic(result);
2064                                 result = strtok(NULL, delims);//yInic
2065                                 std::string yInic(result);
2066                                 result = strtok(NULL, delims);//zInic
2067                                 std::string zInic(result);
2068
2069                                 double xIn, yIn, zIn;
2070                                 std::istringstream xSt(xInic);
2071                                 xSt >> xIn;
2072                                 std::istringstream ySt(yInic);
2073                                 ySt >> yIn;
2074                                 std::istringstream zSt(zInic);
2075                                 zSt >> zIn;
2076
2077                                 if (version<="1.2")
2078                                 {
2079                                         if (zIn==900) zIn=GPOSITION_Z;
2080                                 }
2081                                 
2082                                 getCleanLine(inputStream, line);//FIN_COMPLEX_PORT
2083                                 configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
2084
2085                         } // for external input
2086                 }
2087
2088
2089                 //----------
2090                 getCleanLine(inputStream, line);//BOXES:num
2091                 char boxes[9];
2092                 strcpy(boxes, line.c_str());
2093                 result = strtok(boxes, delims);
2094                 result = strtok(NULL, delims);
2095
2096                 int numBoxes;
2097                 std::istringstream is(result);
2098                 is >> numBoxes;
2099
2100                 for (int i = 0; i < numBoxes; i++) {
2101                         //----------
2102                         getCleanLine(inputStream, line);//BOX
2103                         getCleanLine(inputStream, line);//package:type:name
2104                         char box[150];
2105                         strcpy(box, line.c_str());
2106                         result = strtok(box, delims);//package
2107                         std::string package(result);
2108                         result = strtok(NULL, delims);//type
2109                         std::string type(result);
2110                         result = strtok(NULL, delims);//name
2111                         std::string name(result);
2112
2113                         getCleanLine(inputStream, line);//ISEXEC:TRUE|FALSE
2114                         char exec[15];
2115                         strcpy(exec, line.c_str());
2116                         result = strtok(exec, delims);//ISEXEC
2117                         result = strtok(NULL, delims);//TRUE|FALSE
2118                         std::string isExec(result);
2119
2120                         //----------
2121                         getCleanLine(inputStream, line);//xInic:yInic:zInic
2122                         char coord[80];
2123                         strcpy(coord, line.c_str());
2124                         result = strtok(coord, delims);//xInic
2125                         std::string xInic(result);
2126                         result = strtok(NULL, delims);//yInic
2127                         std::string yInic(result);
2128                         result = strtok(NULL, delims);//zInic
2129                         std::string zInic(result);
2130
2131                         double xIn, yIn, zIn;
2132                         std::istringstream xSt(xInic);
2133                         xSt >> xIn;
2134                         std::istringstream ySt(yInic);
2135                         ySt >> yIn;
2136                         std::istringstream zSt(zInic);
2137                         zSt >> zIn;
2138
2139                         
2140                         if (version<="1.2")
2141                         {
2142                                 if (zIn==900) zIn=GPOSITION_Z;
2143                         }
2144                         
2145                         
2146                         //----------
2147                         getCleanLine(inputStream, line);//xEnd:yEnd:zEnd
2148                         strcpy(coord, line.c_str());
2149                         result = strtok(coord, delims);//xEnd
2150                         std::string xEnd(result);
2151                         result = strtok(NULL, delims);//yEnd
2152                         std::string yEnd(result);
2153                         result = strtok(NULL, delims);//zEnd
2154                         std::string zEnd(result);
2155
2156                         double xEn, yEn, zEn;
2157                         std::istringstream xEt(xEnd);
2158                         xEt >> xEn;
2159                         std::istringstream yEt(yEnd);
2160                         yEt >> yEn;
2161                         std::istringstream zEt(zEnd);
2162                         zEt >> zEn;
2163
2164                         if (version<="1.2")
2165                         {
2166                                 if (zEn==900) zEn=GPOSITION_Z;
2167                         }
2168                         
2169                         
2170                         bool boxExecutable = false;
2171                         if (isExec == "TRUE") {
2172                                 boxExecutable = true;
2173                         }
2174
2175                         int idBox = createGBlackBox(xIn, yIn, package, type);
2176                         configGBlackBox(idBox, xIn, yIn, zIn, name, boxExecutable, xEn, yEn, zEn);
2177
2178                         GObjectController *cont = _controllers[idBox];
2179                         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
2180
2181                         //----------
2182                         getCleanLine(inputStream, line);//PORT o FIN_BOX
2183                         std::string port = line.substr(0, 4);
2184                         while (port == "PORT") {
2185                                 getCleanLine(inputStream, line);//name:value
2186                                 char poort[150];
2187                                 strcpy(poort, line.c_str());
2188                                 result = strtok(poort, delims);//name
2189                                 std::string name(result);
2190                                 result = strtok(NULL, delims);//value
2191                                 std::string value(result);
2192
2193                                 bbmod->setValueToInput(name, value);
2194
2195                                 getCleanLine(inputStream, line);//PORT o FIN_BOX
2196                                 port = line.substr(0, 4);
2197                         } // while
2198
2199                         //EED                           bbmod->notifyObservers(_idManager);
2200                 } // for boxes
2201
2202                 /// CONNECTIONS
2203                 //----------
2204                 getCleanLine(inputStream, line);//CONNECTIONS:num
2205                 char conns[30];
2206                 strcpy(conns, line.c_str());
2207                 result = strtok(conns, delims);
2208                 result = strtok(NULL, delims);
2209
2210                 int numConns;
2211                 std::istringstream isCons(result);
2212                 isCons >> numConns;
2213                 
2214                 
2215                 for (int i = 0; i < numConns; i++) {
2216                         //----------
2217                         getCleanLine(inputStream, line);//CONNECTION
2218                         getCleanLine(inputStream, line);//Startbox.PortName:EndBox.PortName
2219
2220                         char connec[200];
2221                         strcpy(connec, line.c_str());
2222                         result = strtok(connec, delims);
2223                         std::string nameStartBox(result);
2224                         result = strtok(NULL, delims);
2225                         std::string nameStartPort(result);
2226                         result = strtok(NULL, delims);
2227                         std::string nameEndBox(result);
2228                         result = strtok(NULL, delims);
2229                         std::string nameEndPort(result);
2230
2231                         int idCon = configGConnetion(nameStartBox, nameStartPort,nameEndBox, nameEndPort);
2232
2233                         if (version != "1.0") {
2234                                 //Readding control points of the manualContour
2235                                 GConnectorController *tempp             = (GConnectorController*) _controllers[idCon];
2236                                 GConnectorModel *conMod                 = (GConnectorModel*) tempp->getModel();
2237                                 vtkGConnectorView *conView              = (vtkGConnectorView*) tempp->getView();
2238                                 getCleanLine(inputStream, line); //NumberOfControlPoints:##
2239                                 strcpy(conns, line.c_str());
2240                                 result = strtok(conns, delims);
2241                                 result = strtok(NULL, delims);
2242
2243                                 int numberOfControlPoints;
2244                                 std::istringstream isCons(result);
2245                                 isCons >> numberOfControlPoints;
2246
2247                                 for (int ii = 0; ii < numberOfControlPoints; ii++) {
2248                                         getCleanLine(inputStream, line); //XX:YY:ZZ
2249                                         char connec[200];
2250                                         strcpy(connec, line.c_str());
2251
2252                                         double px, py, pz;
2253                                         result = strtok(connec, delims);
2254                                         std::istringstream isPointX(result);
2255                                         isPointX >> px;
2256                                         result = strtok(NULL, delims);
2257                                         std::istringstream isPointY(result);
2258                                         isPointY >> py;
2259                                         result = strtok(NULL, delims);
2260                                         std::istringstream isPointZ(result);
2261                                         isPointZ >> pz;
2262                 
2263                                         if (version<="1.2")
2264                                         {
2265                                                 if (pz==900) pz=GPOSITION_Z;
2266                                         }
2267                                         
2268                                         conMod->getManualContourModel()->InsertPoint_id(ii + 1, px, py, pz);
2269                                         conView->getManualContourView()->AddPoint();
2270                                 }
2271                         }// version !=1.0
2272
2273                 } // for numConns
2274
2275         } // start
2276
2277 }
2278 //=========================================================================
2279
2280
2281 //=========================================================================
2282 void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput,
2283                 std::string portName, double xIn, double yIn, double zIn) {
2284         int idPort;
2285         if (inputoutput == true) {
2286                 idPort = createGComplexBoxInputPort(portName);
2287         } else {
2288                 idPort = createGComplexBoxOutputPort(portName);
2289         }
2290         GObjectController *cont = _controllers[idPort];
2291         GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2292         cbmod->setInicPoint(xIn, yIn, zIn);
2293         cbmod->notifyObservers(_idManager);
2294 }
2295 //=========================================================================
2296
2297
2298 //=========================================================================
2299 GBoxModel* wxVtkSceneManager::findGBox(std::string boxname) {
2300         int j;
2301         int idB;
2302         GBoxModel *bMod;
2303         GBoxModel *boxModel = NULL;
2304         std::vector<int> lstB = getBlackBoxes();
2305         for (j = 0; j < (int) lstB.size(); j++) {
2306                 idB = lstB[j];
2307                 bMod = (GBoxModel*) _controllers[idB]->getModel();
2308                 if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2309                         boxModel = bMod;
2310                 }
2311         } // for
2312
2313
2314
2315         if (boxModel == NULL) {
2316                 std::vector<int> lstInputs = getComplexInputPorts();
2317                 for (j = 0; j < (int) lstInputs.size(); j++) {
2318                         idB = lstInputs[j];
2319                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2320                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2321                                 boxModel = bMod;
2322                         }
2323                 } // for
2324
2325                 if(_isComplexBox)
2326                 {
2327                 std::vector<int> lstOutputs = getComplexOutputPorts();
2328                 for (j = 0; j < (int) lstOutputs.size(); j++) {
2329                         int idB = lstOutputs[j];
2330                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2331                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2332                                 boxModel = bMod;
2333                         }
2334                 } // for
2335                 }
2336
2337         } // inputs/outputs
2338
2339         return boxModel;
2340 }
2341
2342 //=========================================================================
2343
2344
2345 int wxVtkSceneManager::configGConnetion(std::string nameStartBox,
2346                 std::string nameStartPort, std::string nameEndBox,
2347                 std::string nameEndPort) {
2348
2349         GBoxModel *boxModel;
2350         GPortModel *startP = NULL;
2351         GPortModel *endP = NULL;
2352
2353         boxModel = findGBox(nameStartBox);
2354         if(boxModel == NULL)
2355         {
2356                 printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2357         }
2358         if (boxModel != NULL) {
2359                 startP = boxModel->getOutputPort(nameStartPort);
2360                 if(startP == NULL){
2361                         printf("SCP: ERROR in wxVtkSceneManager::configGConnetion start port %s is null. Port name is probably wrong.\n", nameStartPort.c_str());
2362                 }
2363         }
2364
2365         boxModel = findGBox(nameEndBox);
2366         if(boxModel == NULL)
2367         {
2368                  printf("RaC: ERROR in wxVtkSceneManager::configGConnetion A box to connect start port %s is null. Box name is probably wrong.\n", nameStartPort.c_str());
2369         }
2370         if (boxModel != NULL) {
2371                 endP = boxModel->getInputPort(nameEndPort);
2372                 if(endP == NULL){
2373                         printf("SCP: ERROR in wxVtkSceneManager::configGConnetion end port %s is null. Port name is probably wrong\n", nameStartPort.c_str(), nameEndPort.c_str());
2374                 }
2375         }
2376
2377         //ups2
2378         int idCon = createGConnector(startP);
2379
2380         _worldState = NOTHING_HAPPENS;
2381         GConnectorController *tempp = (GConnectorController*) _controllers[idCon];
2382
2383         GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2384         vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2385         tempp->endContourCreation();
2386         conMod->setEndPort(endP);
2387         conView->updateStartEndPoints();
2388         return idCon;
2389 }
2390
2391 //=========================================================================
2392
2393 bool wxVtkSceneManager::boxExist(std::string boxname) {
2394         bool ok = false;
2395         std::map<int, GObjectController*>::iterator it;
2396         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2397                 GObjectController *cont = it->second;
2398                 if (cont->getModel()->getBBTKName() == boxname) {
2399                         ok = true;
2400                 }
2401         }
2402         return ok;
2403 }
2404
2405 //=========================================================================
2406
2407 std::vector<int> wxVtkSceneManager::getBlackBoxes() {
2408         std::vector<int> vect;
2409         std::map<int, GObjectController*>::iterator it;
2410         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2411                 GObjectController *cont = it->second;
2412                 if (cont->getGObjectType() == GBLACKBOX) {
2413                         vect.push_back(cont->getId());
2414                 }
2415         }
2416         return vect;
2417 }
2418
2419 //=========================================================================
2420
2421 std::vector<int> wxVtkSceneManager::getComplexInputPorts() {
2422         std::vector<int> vect;
2423         std::map<int, GObjectController*>::iterator it;
2424         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2425                 GObjectController *cont = it->second;
2426                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT) {
2427                         vect.push_back(cont->getId());
2428                 }
2429         }
2430         return vect;
2431 }
2432
2433 //=========================================================================
2434
2435 std::vector<int> wxVtkSceneManager::getComplexOutputPorts() {
2436         std::vector<int> vect;
2437         std::map<int, GObjectController*>::iterator it;
2438         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2439                 GObjectController *cont = it->second;
2440                 if (cont->getGObjectType() == GCOMPLEXOUTPUTPORT) {
2441                         vect.push_back(cont->getId());
2442                 }
2443         }
2444         return vect;
2445 }
2446
2447 //=========================================================================
2448
2449 std::vector<int> wxVtkSceneManager::getConnections() {
2450         std::vector<int> vect;
2451         std::map<int, GObjectController*>::iterator it;
2452         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2453                 GObjectController *cont = it->second;
2454                 if (cont->getGObjectType() == GCONNECTOR) {
2455                         vect.push_back(cont->getId());
2456                 }
2457         }
2458         return vect;
2459 }
2460
2461 //=========================================================================
2462
2463 bool wxVtkSceneManager::isComplexBox() {
2464         return _isComplexBox;
2465 }
2466
2467 //=========================================================================
2468
2469 void wxVtkSceneManager::setComplexBox(bool val) {
2470         _isComplexBox = val;
2471 }
2472
2473 //=========================================================================
2474
2475 int wxVtkSceneManager::addObjectController(GObjectController* objController) {
2476         //Register the controller of the new object
2477         registerController((InteractorStyleMaracas*) objController);
2478         //Add the object to the objects list
2479         int newId = _contLastId;//_controllers.size();
2480         objController->setId(newId);
2481         _controllers[newId] = objController;
2482         //std::cout << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = " << _contLastId << std::endl;
2483         _contLastId++;
2484         return newId;
2485 }
2486
2487 //=========================================================================
2488
2489 void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
2490
2491         std::map<int, int> oldIdNewIdBoxes;
2492         std::vector<int> connections;
2493
2494         std::vector<int> newBoxesID;
2495
2496         std::map<int, GObjectController*>::iterator it;
2497         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2498             GObjectController *cont = it->second;
2499             int type = cont->getGObjectType();
2500
2501             if (type == GBLACKBOX) {
2502                 // Copy black box
2503                 double xInic, yInic, zInic;
2504                 GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2505                 copyBox->getInicPoint(xInic, yInic, zInic);
2506                 int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2507                         copyBox->getBBTKType());
2508
2509                 int idcB = copyBox->getObjectId();
2510                 oldIdNewIdBoxes[idcB] = idBox;
2511                 cont = _controllers[idBox];
2512                 GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2513                 newbox->setInicPoint(xInic, yInic, zInic);
2514                 int num = newbox->getNumInputPorts();
2515                 for (int j = 0; j < num; j++) {
2516                     newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2517                 }
2518                 newbox->move(xInic + 20, yInic + 20, zInic);
2519                 newbox->notifyObservers(_idManager);
2520                 newBoxesID.push_back(newbox->getObjectId());
2521
2522             } else if (type == GCONNECTOR) {
2523                 int idCon = cont->getId();
2524                 connections.push_back(idCon);
2525             }
2526
2527         }
2528
2529         for (int i = 0; i < (int) connections.size(); i++) {
2530             int objId = connections[i];
2531             GObjectController *cont = objectsMap[objId];
2532             GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2533
2534             GPortModel* startPort = connectModel->getStartPort();
2535             int startPortIndex = startPort->getPosInBox();
2536             GPortModel* endPort = connectModel->getEndPort();
2537             int endPortIndex = endPort->getPosInBox();
2538
2539             GBlackBoxModel* startPortParentBox =
2540                     (GBlackBoxModel*) startPort->getParentBox();
2541             GBlackBoxModel* endPortParentBox =
2542                     (GBlackBoxModel*) endPort->getParentBox();
2543
2544             int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2545             int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2546
2547             GBlackBoxModel* newStartBox =
2548                     (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2549             GBlackBoxModel* newEndBox =
2550                     (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2551
2552             GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2553             GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2554
2555             // Creates connection
2556             int idCon = createGConnector(newStartPort);
2557             GConnectorController *tempp =
2558                     (GConnectorController*) _controllers[idCon];
2559             GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2560             vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2561             tempp->endContourCreation();
2562             conMod->setEndPort(newEndPort);
2563             conView->updateStartEndPoints();
2564         }
2565
2566         /// the new selected boxes are the duplicate ones
2567
2568         UnSelectBlackBoxes();
2569         for (int i = 0; i < newBoxesID.size(); i++) {
2570             _selectedObjects.push_back(newBoxesID.at(i));
2571         }
2572
2573         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
2574             int id = _selectedObjects[i];
2575             GObjectController* cont = _controllers[id];
2576
2577             cont->getView()->setState(SELECTED);
2578             cont->getModel()->notifyObservers(_idManager);
2579
2580         }
2581     }
2582
2583
2584 //=========================================================================
2585
2586 int wxVtkSceneManager::getNumSelectedObjects() {
2587         return _selectedObjects.size();
2588 }
2589
2590 //=========================================================================
2591
2592 std::map<int, GObjectController*> wxVtkSceneManager::getSelectedObjects() {
2593         std::map<int, GObjectController*> mapSelected;
2594
2595         std::map<int, GObjectController*>::iterator it;
2596         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2597                 GObjectController *cont = it->second;
2598                 if (cont->getGObjectType() == GBLACKBOX && cont->getView()->getState()
2599                                 == SELECTED) {
2600                         mapSelected[cont->getId()] = cont;
2601                 }
2602         }
2603
2604         std::map<int, GObjectController*>::iterator it2;
2605         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
2606                 GObjectController *cont = it2->second;
2607                 if (cont->getGObjectType() == GCONNECTOR) {
2608                         GConnectorModel* cmod = (GConnectorModel*) cont->getModel();
2609                         GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2610                         GBoxModel* startPortParentBox =
2611                                         cmod->getStartPort()->getParentBox();
2612
2613                         std::map<int, GObjectController*>::iterator iterOBJ1 =
2614                                         mapSelected.find(startPortParentBox->getObjectId());
2615                         std::map<int, GObjectController*>::iterator iterOBJ2 =
2616                                         mapSelected.find(endPortParentBox->getObjectId());
2617
2618                         if (iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end()) {
2619                                 int ID = cont->getId();
2620                                 mapSelected[ID] = cont;
2621                         }
2622                 }
2623         }
2624         return mapSelected;
2625 }
2626
2627 //=========================================================================
2628
2629 void wxVtkSceneManager::addObjects(std::map<int, GObjectController*> objectsMap) {
2630
2631         std::map<int, int> oldIdNewIdBoxes;
2632         std::vector<int> connections;
2633
2634         std::map<int, GObjectController*>::iterator it;
2635         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2636                 GObjectController *cont = it->second;
2637                 int type = cont->getGObjectType();
2638
2639                 if (type == GBLACKBOX) {
2640                         // Copy black box
2641                         double xInic, yInic, zInic;
2642                         GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2643                         copyBox->getInicPoint(xInic, yInic, zInic);
2644                         int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2645                                         copyBox->getBBTKType());
2646
2647                         int idcB = copyBox->getObjectId();
2648                         oldIdNewIdBoxes[idcB] = idBox;
2649                         cont = _controllers[idBox];
2650                         GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2651                         newbox->setInicPoint(xInic, yInic, zInic);
2652                         int num = newbox->getNumInputPorts();
2653                         for (int j = 0; j < num; j++) {
2654                                 newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2655                         }
2656                         newbox->notifyObservers(_idManager);
2657                 } else if (type == GCONNECTOR) {
2658                         int idCon = cont->getId();
2659                         connections.push_back(idCon);
2660                 }
2661
2662         }
2663
2664         for (int i = 0; i < (int) connections.size(); i++) {
2665                 int objId = connections[i];
2666                 GObjectController *cont = objectsMap[objId];
2667                 GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2668
2669                 GPortModel* startPort = connectModel->getStartPort();
2670                 int startPortIndex = startPort->getPosInBox();
2671                 GPortModel* endPort = connectModel->getEndPort();
2672                 int endPortIndex = endPort->getPosInBox();
2673
2674                 GBlackBoxModel* startPortParentBox =
2675                                 (GBlackBoxModel*) startPort->getParentBox();
2676                 GBlackBoxModel* endPortParentBox =
2677                                 (GBlackBoxModel*) endPort->getParentBox();
2678
2679                 int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2680                 int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2681
2682                 GBlackBoxModel* newStartBox =
2683                                 (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2684                 GBlackBoxModel* newEndBox =
2685                                 (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2686
2687                 GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2688                 GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2689
2690                 // Creates connection
2691                 int idCon = createGConnector(newStartPort);
2692                 GConnectorController *tempp =
2693                                 (GConnectorController*) _controllers[idCon];
2694                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2695                 vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2696                 tempp->endContourCreation();
2697                 conMod->setEndPort(newEndPort);
2698                 conView->updateStartEndPoints();
2699         }
2700
2701         std::map<int, int>::iterator itIds;
2702         for (itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds) {
2703                 int idOld = itIds->first;
2704                 int idNew = itIds->second;
2705
2706                 GBlackBoxModel* oldBox =
2707                                 (GBlackBoxModel*) objectsMap[idOld]->getModel();
2708                 GBlackBoxModel* newBox =
2709                                 (GBlackBoxModel*) _controllers[idNew]->getModel();
2710
2711                 std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2712                 std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2713                 std::vector<int> newInputConnections = newBox->getConnectedInputs();
2714                 std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2715
2716                 for (int k = 0; k < (int) oldInputConnections.size(); k++) {
2717                         bool exist = false;
2718                         //EED                           int toCreate=-1;
2719                         for (int l = 0; l < (int) newInputConnections.size() && !exist; l++) {
2720                                 if (oldInputConnections[k] == newInputConnections[l]) {
2721                                         exist = true;
2722                                 }
2723                         }
2724
2725                         if (exist == false) {
2726                                 //Create complex input
2727                                 int posInBox = oldInputConnections[k];
2728                                 GPortModel* inputPort = oldBox->getInputPort(posInBox);
2729                                 std::string inputPortName = inputPort->getBBTKName();
2730                                 int idInputPort = createGComplexBoxInputPort(inputPortName);
2731                                 GObjectController *cont = _controllers[idInputPort];
2732                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2733                                 double xIn, yIn, zIn;
2734                                 inputPort->getInicPoint(xIn, yIn, zIn);
2735                                 yIn += 20;
2736                                 cbmod->setInicPoint(xIn, yIn, zIn);
2737                                 cbmod->notifyObservers(_idManager);
2738
2739                                 GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2740
2741                                 // Creates connection
2742                                 int idCon = createGConnector(cbmod->getOutputPort(0));
2743                                 GConnectorController *tempp =
2744                                                 (GConnectorController*) _controllers[idCon];
2745                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2746                                 vtkGConnectorView *conView =
2747                                                 (vtkGConnectorView*) tempp->getView();
2748                                 tempp->endContourCreation();
2749                                 conMod->setEndPort(inputPortEnd);
2750                                 conView->updateStartEndPoints();
2751
2752                         }
2753
2754                 }
2755
2756                 for (int k = 0; k < (int) oldOutputConnections.size(); k++) {
2757                         bool exist = false;
2758                         //EED                           int toCreate=-1;
2759                         for (int l = 0; l < (int) newOutputConnections.size() && !exist; l++) {
2760                                 if (oldOutputConnections[k] == newOutputConnections[l]) {
2761                                         exist = true;
2762                                 }
2763                         }
2764
2765                         if (exist == false) {
2766                                 //Create complex output
2767                                 int posInBox = oldOutputConnections[k];
2768                                 GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2769                                 std::string outputPortName = outputPort->getBBTKName();
2770                                 int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2771                                 GObjectController *cont = _controllers[idOutputPort];
2772                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2773                                 double xIn, yIn, zIn;
2774                                 outputPort->getInicPoint(xIn, yIn, zIn);
2775                                 yIn -= 20;
2776                                 cbmod->setInicPoint(xIn, yIn, zIn);
2777                                 cbmod->notifyObservers(_idManager);
2778
2779                                 GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2780
2781                                 // Creates connection
2782                                 int idCon = createGConnector(outputPortEnd);
2783                                 GConnectorController *tempp =
2784                                                 (GConnectorController*) _controllers[idCon];
2785                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2786                                 vtkGConnectorView *conView =
2787                                                 (vtkGConnectorView*) tempp->getView();
2788                                 tempp->endContourCreation();
2789                                 conMod->setEndPort(cbmod->getInputPort(0));
2790                                 conView->updateStartEndPoints();
2791
2792                         }
2793
2794                 }
2795
2796         }
2797
2798 }
2799
2800 //=========================================================================
2801 void wxVtkSceneManager::SetCbName(std::string cbName) {
2802         _cbName = cbName;
2803         if (_cbName == "") {
2804                 _cbName = "<complex box name>";
2805         }
2806 }
2807
2808 //=========================================================================
2809 std::string wxVtkSceneManager::GetCbName() {
2810         return _cbName;
2811 }
2812
2813 //=========================================================================
2814 void wxVtkSceneManager::SetCbPackageName(std::string packagename) {
2815         _cbPackageName = packagename;
2816         if (_cbPackageName == "") {
2817                 _cbPackageName = "<package name of the complex box>";
2818         }
2819 }
2820
2821 //=========================================================================
2822 std::string wxVtkSceneManager::GetCbPackageName() {
2823         return _cbPackageName;
2824 }
2825
2826 //=========================================================================
2827 void wxVtkSceneManager::SetAuthor(std::string author) {
2828         _Author = author;
2829         if (_Author == "") {
2830                 _Author = "<author of the box>";
2831         }
2832 }
2833
2834 //=========================================================================
2835 std::string wxVtkSceneManager::GetAuthor() {
2836         return _Author;
2837 }
2838
2839 //=========================================================================
2840 void wxVtkSceneManager::SetCategory(std::string category) {
2841         _Category = category;
2842         if (_Category == "") {
2843                 _Category = "<category of the box>";
2844         }
2845 }
2846
2847 //=========================================================================
2848 std::string wxVtkSceneManager::GetCategory() {
2849         return _Category;
2850 }
2851
2852 //=========================================================================
2853 void wxVtkSceneManager::SetDescription(std::string description) {
2854         _Description = description;
2855         if (_Description == "") {
2856                 _Description = "<description of the box>";
2857         }
2858 }
2859
2860 //=========================================================================
2861 std::string wxVtkSceneManager::GetDescription() {
2862         return _Description;
2863 }
2864
2865 //=========================================================================
2866
2867 std::vector< std::string> wxVtkSceneManager::GetExternalInputsNames()
2868 {
2869
2870         std::vector< std::string> extInputs;
2871         std::map<int, GObjectController*>::iterator it;
2872         for (it = _controllers.begin(); it != _controllers.end(); ++it)
2873         {
2874                 GObjectController *cont = it->second;
2875                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT)
2876                 {
2877                         extInputs.push_back(cont->getModel()->getBBTKName());
2878                 }
2879         }
2880         return extInputs;
2881 }
2882
2883 //=========================================================================
2884
2885
2886
2887 void wxVtkSceneManager::printAll(int com , int sta ){
2888         
2889          int tmpState = sta ;
2890         if ( tmpState == NOTHING_HAPPENS ) {
2891             std::cout << "CONTROLER STATE---->NOTHING_HAPPENS\n" ;
2892         }
2893         if ( tmpState == HIGHLIGHTED ) {
2894             std::cout << "CONTROLER STATE---->HIGHLIGHTED\n" ;
2895         }
2896         if ( tmpState == POSSIBLE_CONNECTION ) {
2897             std::cout << "CONTROLER STATE---->POSSIBLE_CONNECTION\n" ;
2898         }
2899         if ( tmpState == SELECTED_POSSIBLE_CONNECTION ) {
2900             std::cout << "CONTROLER STATE---->SELECTED_POSSIBLE_CONNECTION\n" ;
2901         }
2902         if ( tmpState == CLICKED ) {
2903             std::cout << "CONTROLER STATE---->CLICKED\n" ;
2904         }
2905         if ( tmpState == DRAG ) {
2906             std::cout << "CONTROLER STATE---->DRAG\n" ;
2907         }
2908         if ( tmpState == SELECTED ) {
2909             std::cout << "CONTROLER STATE---->SELECTED\n" ;
2910         }
2911         if ( tmpState == CREATING_CONTOUR ) {
2912             std::cout << "CONTROLER STATE---->CREATING_CONTOUR\n" ;
2913         }   
2914          
2915          // :P 
2916
2917         int command = com ;
2918         if ( command == INIT_CREATION_CONTOUR ) {
2919             std::cout << "COMMAND ----> INIT_CREATION_CONTOUR \n" ;
2920         }
2921         if ( command == FIN_CREATION_CONTOUR ) {
2922             std::cout << "COMMAND ----> FIN_CREATION_CONTOUR \n" ;
2923         }
2924         if ( command == ADD_TO_SELECTED ) {
2925             std::cout << "COMMAND ----> ADD_TO_SELECTED \n" ;
2926         }
2927         if ( command == DRAG_OBJECTS ) {
2928             std::cout << "COMMAND ----> DRAG_OBJECTS \n" ;
2929         }
2930         if ( command == EDIT_BLACKBOX ) {
2931             std::cout << "COMMAND ----> EDIT_BLACKBOX \n" ;
2932         }
2933         if ( command == REMOVE_FROM_SELECTED ) {
2934             std::cout << "COMMAND ----> REMOVE_FROM_SELECTED \n" ;
2935         }
2936         
2937     }
2938
2939
2940 } // EO namespace bbtk
2941
2942 // EOF
2943