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