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