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