]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
#feature 1359
[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  ///JLGR 21-05-2012
906
907 bool wxVtkSceneManager::OnMiddleButtonDown() {
908
909
910         this->_vtkInteractorStyleBaseView->StartPan();
911
912         return true;
913     }
914
915 bool wxVtkSceneManager::OnMiddleButtonUp() {
916
917
918         this->_vtkInteractorStyleBaseView->EndPan();
919
920
921         return true;
922     }
923
924
925 //=========================================================================
926
927 GObjectController *wxVtkSceneManager::GetGBlackBoxControlerPointedByMouse() {
928         GObjectController *result = NULL;
929         int X, Y;
930         wxVTKRenderWindowInteractor *wxVTKiren;
931         wxVTKiren
932                         = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
933         wxVTKiren->GetEventPosition(X, Y);
934
935         std::map<int, GObjectController*>::iterator it;
936
937         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
938                 GObjectController *cont = it->second;
939                 int type = cont->getGObjectType();
940
941                 if (cont->getView()->isPointInside(X, Y) == true) {
942                         if (type == GBLACKBOX) {
943                                 result = cont;
944                         } // if type
945                 } // if isPointInside
946         } // for it
947         return result;
948 }
949
950 //=========================================================================
951 void wxVtkSceneManager::UnSelectBlackBoxes() {
952         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
953                 int id = _selectedObjects[i];
954                 GObjectController* control = _controllers[id];
955                 control->getView()->setState(NOTHING_HAPPENS);
956         }// for _selectedObjects
957         _selectedObjects.clear();
958 }
959
960 //=========================================================================
961 bool wxVtkSceneManager::OnLeftDClick() {
962         GObjectController *cont = GetGBlackBoxControlerPointedByMouse();
963         if (cont != NULL) {
964                 UnSelectBlackBoxes();
965                 _parent->editBlackBox((GBlackBoxModel*) cont->getModel());
966         }
967
968         /*EED Borrame
969
970          int X,Y;
971          wxVTKRenderWindowInteractor *wxVTKiren;
972          wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
973          wxVTKiren->GetEventPosition(X,Y);
974
975          std::map<int, GObjectController*>::iterator it;
976
977          bool clickOnObject = false;
978
979          for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
980          {
981          GObjectController *cont = it->second;
982          int type = cont->getGObjectType();
983
984          if(cont->getView()->isPointInside(X,Y))
985          {
986          if(type==GBLACKBOX)
987          {
988          for (int i=0; i< (int)_selectedObjects.size(); i++)
989          {
990          int id = _selectedObjects[i];
991          GObjectController* control = _controllers[id];
992          control->getView()->setState(NOTHING_HAPPENS);
993          }
994          _selectedObjects.clear();
995
996          GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
997          _parent->editBlackBox(bbmodel);
998          }
999          clickOnObject = true;
1000          }
1001          }
1002
1003          if(clickOnObject==false)
1004          {
1005          //_parent->editDiagramParameters(this);
1006          }
1007          */
1008         return true;
1009 }
1010
1011 //=========================================================================
1012
1013 bool wxVtkSceneManager::OnChar() {
1014         char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
1015         int ctrlkey = _vtkInteractorStyleBaseView->GetInteractor()->GetControlKey();
1016         
1017         // KeyCode 127 : Delete Key
1018         // KeyCode 8 : Backspace Key
1019         if (keyCode == 8 || keyCode == 127) {
1020                 if (_selectedObjects.size() > 0) {
1021                         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
1022                                 int id = _selectedObjects[i];
1023                                 std::map<int, GObjectController*>::iterator it;
1024                                 it = _controllers.find(id);
1025                                 if (it != _controllers.end()) {
1026                                         deleteObject(id);
1027                                 }
1028                         }
1029                         _selectedObjects.clear();
1030                 }
1031         }
1032         
1033         /// Duplicate 
1034              if(ctrlkey==5){                
1035                 if(_selectedObjects.size()){
1036                     std::cout<<"objects to copy :";
1037                     for(int i=0;i<_selectedObjects.size();i++){
1038                        std::cout<<_selectedObjects.at(i)<<" ";
1039                     }
1040                     DuplicateObjects(getSelectedObjects());   
1041                 }else{
1042                     std::cout<<"No objects to copy\n";
1043                 }
1044             }  
1045
1046         return true;
1047 }
1048
1049 //=========================================================================
1050
1051 void wxVtkSceneManager::AddControlerToBeRemove(
1052                 std::vector<int> *controllersToRemove, int id) {
1053
1054         int i;
1055         bool ok = true;
1056         for (i = 0; i < (int) controllersToRemove->size(); i++) {
1057                 if (id == (*controllersToRemove)[i]) {
1058                         ok = false;
1059                 }
1060         }
1061
1062         if (ok == true) {
1063                 controllersToRemove->push_back(id);
1064         }
1065
1066 }
1067
1068 //=========================================================================
1069
1070 void wxVtkSceneManager::deleteObject(int id) {
1071         GObjectController *control = _controllers[id];
1072         std::vector<int> controllersToRemove;
1073
1074         if (control->getGObjectType() == GBLACKBOX || control->getGObjectType()
1075                         == GCOMPLEXINPUTPORT || control->getGObjectType()
1076                         == GCOMPLEXOUTPUTPORT) {
1077                 GBoxModel *bbmod = (GBoxModel*) control->getModel();
1078                 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
1079
1080                 bool boxConnected = false;
1081
1082                 // Add box input controllers to be removed
1083                 for (int i = 0; i < (int) inputs.size(); i++) {
1084                         AddControlerToBeRemove(&controllersToRemove,
1085                                         inputs[i]->getObjectId());
1086                         if (inputs[i]->isConnected()) {
1087                                 boxConnected = true;
1088                         }
1089                 }
1090
1091                 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
1092
1093                 // Add box output controllers to be removed
1094                 for (int i = 0; i < (int) outputs.size(); i++) {
1095                         AddControlerToBeRemove(&controllersToRemove,
1096                                         outputs[i]->getObjectId());
1097                         if (outputs[i]->isConnected()) {
1098                                 boxConnected = true;
1099                         }
1100                 }
1101
1102                 // Add connection controllers to be removed
1103                 std::map<int, GObjectController*>::iterator it;
1104                 for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1105                         GObjectController *cont = it->second;
1106                         int type = cont->getGObjectType();
1107                         if (type == GCONNECTOR) {
1108                                 GConnectorModel *conMod = (GConnectorModel*) cont->getModel();
1109                                 if (conMod->getStartPort() != NULL
1110                                                 && conMod->getStartPort()->getParentBox()->getObjectId()
1111                                                                 == bbmod->getObjectId()) {
1112                                         AddControlerToBeRemove(&controllersToRemove,
1113                                                         conMod->getObjectId());
1114                                 }
1115                                 if (conMod->getEndPort() != NULL
1116                                                 && conMod->getEndPort()->getParentBox()->getObjectId()
1117                                                                 == bbmod->getObjectId()) {
1118                                         AddControlerToBeRemove(&controllersToRemove,
1119                                                         conMod->getObjectId());
1120                                 }
1121                         }
1122                 }
1123
1124                 // Add box controller to be removed
1125                 AddControlerToBeRemove(&controllersToRemove, bbmod->getObjectId());
1126         } else if (control->getGObjectType() == GCONNECTOR) {
1127                 GConnectorModel *conMod = (GConnectorModel*) control->getModel();
1128                 AddControlerToBeRemove(&controllersToRemove, conMod->getObjectId());
1129         }
1130
1131         std::map<int, GObjectController*>::iterator it;
1132
1133         for (int i = 0; i < (int) controllersToRemove.size(); i++) {
1134                 int key = controllersToRemove[i];
1135                 it = _controllers.find(key);
1136                 GObjectController *cont = _controllers[key];
1137                 if (cont != NULL) {
1138                         cont->removeFromScene();
1139                         unregisterController((InteractorStyleMaracas*) cont);
1140                         _controllers.erase(it);
1141                 }//if
1142         }// for
1143         _parent->saveTempDiagram();
1144 }
1145
1146 //=========================================================================
1147
1148 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName,
1149                 std::string boxName) {
1150         _parent->displayBlackBoxInfo(packageName, boxName);
1151 }
1152
1153 //=========================================================================
1154
1155 void wxVtkSceneManager::updateStatusBar(std::string textStatus) {
1156         _parent->updateStatusBar(textStatus);
1157 }
1158
1159 //=========================================================================
1160 std::string wxVtkSceneManager::LineNumber(bool withLineNumber, int &value) {
1161         std::string result = "";
1162         if (withLineNumber == true) {
1163                 value++;
1164                 stringstream StrStream;
1165                 StrStream << value;
1166                 std::string strValue = StrStream.str();
1167                 if (value < 10) {
1168                         result += "  ";
1169                 } else if (value < 100) {
1170                         result += " ";
1171                 }
1172
1173                 result += strValue + ": ";
1174         }
1175         return result;
1176 }
1177 //=========================================================================
1178 //DFCH
1179 bool wxVtkSceneManager::MakeBoxExecutable() {
1180         std::map<int, GObjectController*> selected = getSelectedObjects();
1181         if( selected.empty() )
1182                 return false;
1183         bool ret = false;
1184         std::map<int, GObjectController*>::iterator it;
1185         for (it = selected.begin(); it != selected.end(); ++it) {
1186                 GObjectController* lastSelected = it->second;
1187                 if (lastSelected) {
1188                         vtkGObjectView* view = (vtkGObjectView*) lastSelected->getView();
1189                         GBlackBoxModel *bbmodel =
1190                                         (GBlackBoxModel*) lastSelected->getModel();
1191                         view->setState(HIGHLIGHTED);
1192                         if (bbmodel->isExecutable()) {
1193                                 bbmodel->setExecutable(false);
1194                         } else {
1195                                 bbmodel->setExecutable(true);
1196                         }
1197                         bbmodel->notifyObservers(lastSelected->getId());
1198                         ret = true;
1199                 }
1200         }
1201         return ret;
1202 }
1203 //DFCH
1204
1205 //=========================================================================
1206
1207 std::string wxVtkSceneManager::getDiagramBBS(bool wln) {
1208         bool existsExec = false;
1209
1210         std::vector<std::string> packages;
1211         std::vector<int> boxes;
1212         std::vector<int> connections;
1213         std::vector<int> execBoxes;
1214
1215         std::map<int, GObjectController*>::iterator it;
1216
1217         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1218                 GObjectController *desc = it->second;
1219                 int type = desc->getGObjectType();
1220
1221                 if (type == GBLACKBOX) {
1222                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1223
1224                         std::string pkg = mod->getBBTKPackage();
1225                         bool existsPkg = false;
1226                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1227                                 if (packages[t] == pkg) {
1228                                         existsPkg = true;
1229                                 }
1230                         }
1231                         if (!existsPkg) {
1232                                 packages.push_back(pkg);
1233                         }
1234
1235                         boxes.push_back(it->first);
1236                         if (mod->isExecutable()) {
1237                                 execBoxes.push_back(it->first);
1238                                 existsExec = true;
1239                         }
1240                 } else if (type == GCONNECTOR) {
1241                         connections.push_back(it->first);
1242                 }
1243         }
1244
1245         int lnNmbr = 0;
1246         std::string script = "";
1247         script += LineNumber(wln, lnNmbr) + "# BBTK GEditor Script\n";
1248         script += LineNumber(wln, lnNmbr) + "# ----------------------\n";
1249         script += LineNumber(wln, lnNmbr) + "\n";
1250         if (existsExec) {
1251                 script += LineNumber(wln, lnNmbr) + "include std\n"; // EED
1252                 script += LineNumber(wln, lnNmbr) + "include itkvtk\n"; // EED
1253                 int i;
1254                 for (i = 0; i < (int) packages.size(); i++) {
1255                         script += LineNumber(wln, lnNmbr);
1256                         script += "include ";
1257                         script += packages[i];
1258                         script += "\n";
1259                 }
1260
1261                 script += LineNumber(wln, lnNmbr);
1262                 script += "\n";
1263
1264                 //ups2
1265
1266                 script += LineNumber(wln, lnNmbr);
1267                 script += "author \"";
1268                 script += _Author;
1269                 script += "\"\n";
1270
1271                 script += LineNumber(wln, lnNmbr);
1272                 script += "description \"";
1273                 script += _Description;
1274                 script += "\"\n";
1275
1276                 script += LineNumber(wln, lnNmbr);
1277                 script += "category \"";
1278                 script += _Category;
1279                 script += "\"\n";
1280
1281                 script += LineNumber(wln, lnNmbr);
1282                 script += "\n";
1283
1284                 // script+="include std\n"; // JPR
1285
1286                 for (i = 0; i < (int) boxes.size(); i++) {
1287                         script += LineNumber(wln, lnNmbr);
1288                         script += "new ";
1289                         int id = boxes[i];
1290                         GObjectController *control = _controllers[id];
1291                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1292
1293                         script += model->getBBTKType();
1294                         script += " ";
1295                         script += model->getBBTKName();
1296                         script += "\n";
1297
1298                         std::vector<GPortModel*> inputs = model->getInputPorts();
1299                         for (int j = 0; j < (int) inputs.size(); j++) {
1300                                 GPortModel* inputPort = inputs[j];
1301                                 if (inputPort->isValueSet()) {
1302                                         script += LineNumber(wln, lnNmbr);
1303                                         script += "  set ";
1304                                         script += model->getBBTKName();
1305                                         script += ".";
1306                                         script += inputPort->getBBTKName();
1307                                         script += " ";
1308                                         script += inputPort->getValue();
1309                                         script += "\n";
1310                                 }
1311                         }
1312
1313                         script += LineNumber(wln, lnNmbr);
1314                         script += "\n";
1315
1316                 }
1317
1318                 script += LineNumber(wln, lnNmbr);
1319                 script += "\n";
1320
1321                 for (i = 0; i < (int) connections.size(); i++) {
1322                         script += LineNumber(wln, lnNmbr);
1323                         script += "connect ";
1324                         int id = connections[i];
1325                         GObjectController *control = _controllers[id];
1326                         GConnectorModel *model = (GConnectorModel*) control->getModel();
1327
1328                         //Start Connection info
1329                         GPortModel *start = model->getStartPort();
1330                         script += start->getParentBox()->getBBTKName();
1331                         script += ".";
1332                         script += start->getBBTKName();
1333
1334                         script += " ";
1335
1336                         //End Connection info
1337                         GPortModel *end = model->getEndPort();
1338                         script += end->getParentBox()->getBBTKName();
1339                         script += ".";
1340                         script += end->getBBTKName();
1341
1342                         script += "\n";
1343                 }
1344
1345                 script += LineNumber(wln, lnNmbr);
1346                 script += "\n";
1347                 for (i = 0; i < (int) execBoxes.size(); i++) {
1348                         script += LineNumber(wln, lnNmbr);
1349                         script += "exec ";
1350                         int id = execBoxes[i];
1351                         GObjectController *control = _controllers[id];
1352                         GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1353
1354                         script += model->getBBTKName();
1355                         script += "\n";
1356                 }
1357
1358         }
1359
1360         return script;
1361 }
1362
1363 //=========================================================================
1364
1365 std::string wxVtkSceneManager::saveComplexBoxBBS() {
1366         std::vector<std::string> packages;
1367         std::vector<int> boxes;
1368         std::vector<int> connections;
1369         std::vector<int> execBoxes;
1370
1371         std::map<int, GObjectController*>::iterator it;
1372
1373         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1374                 GObjectController *desc = it->second;
1375                 int type = desc->getGObjectType();
1376
1377                 if (type == GBLACKBOX) {
1378                         GBlackBoxModel *mod = (GBlackBoxModel*) desc->getModel();
1379
1380                         std::string pkg = mod->getBBTKPackage();
1381                         bool existsPkg = false;
1382                         for (int t = 0; t < (int) packages.size() && existsPkg == false; t++) {
1383                                 if (packages[t] == pkg) {
1384                                         existsPkg = true;
1385                                 }
1386                         }
1387                         if (!existsPkg) {
1388                                 packages.push_back(pkg);
1389                         }
1390
1391                         boxes.push_back(it->first);
1392                         if (mod->isExecutable()) {
1393                                 execBoxes.push_back(it->first);
1394                         }
1395                 } else if (type == GCONNECTOR) {
1396                         connections.push_back(it->first);
1397                 }
1398         }
1399
1400         std::string script = "";
1401
1402         script += "include std\n"; // EED
1403         script += "include itkvtk\n"; // EED
1404         int i;
1405         for (i = 0; i < (int) packages.size(); i++) {
1406                 script += "include ";
1407                 script += packages[i];
1408                 script += "\n";
1409         }
1410         script += "\n";
1411
1412         // Definition of a complex box
1413         script += "define ";
1414         script += _cbName + " " + _cbPackageName;
1415         script += "\n";
1416         script += "\n";
1417
1418         script += "author \"";
1419         script += _Author;
1420         script += "\"\n";
1421
1422         script += "description \"";
1423         script += _Description;
1424         script += "\"\n";
1425         script += "\n";
1426
1427         script += "category \"";
1428         script += _Category;
1429         script += "\"\n";
1430         script += "\n";
1431
1432         // Create boxes
1433         for (i = 0; i < (int) boxes.size(); i++) {
1434                 script += "new ";
1435                 int id = boxes[i];
1436                 GObjectController *control = _controllers[id];
1437                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1438
1439                 script += model->getBBTKType();
1440                 script += " ";
1441                 script += model->getBBTKName();
1442                 script += "\n";
1443
1444                 std::vector<GPortModel*> inputs = model->getInputPorts();
1445                 for (int j = 0; j < (int) inputs.size(); j++) {
1446                         GPortModel* inputPort = inputs[j];
1447                         if (inputPort->isValueSet()) {
1448                                 script += "  set ";
1449                                 script += model->getBBTKName();
1450                                 script += ".";
1451                                 script += inputPort->getBBTKName();
1452                                 script += " ";
1453                                 script += inputPort->getValue();
1454                                 script += "\n";
1455                         }
1456                 }
1457                 script += "\n";
1458
1459         }
1460
1461         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1462
1463         std::string complexInputs = "";
1464         std::string complexOutputs = "";
1465         script += "\n";
1466
1467         for (i = 0; i < (int) connections.size(); i++) {
1468                 int id = connections[i];
1469                 GObjectController *control = _controllers[id];
1470                 GConnectorModel *model = (GConnectorModel*) control->getModel();
1471
1472                 //Connection info
1473                 GPortModel *start = model->getStartPort();
1474                 GBoxModel *startBox = start->getParentBox();
1475
1476                 GPortModel *end = model->getEndPort();
1477                 GBoxModel *endBox = end->getParentBox();
1478
1479                 if (startBox->getGObjectType() == GCOMPLEXINPUTPORT) {
1480                         complexInputs += "input ";
1481                         complexInputs += startBox->getBBTKName();
1482
1483                         complexInputs += " ";
1484                         complexInputs += endBox->getBBTKName();
1485                         complexInputs += ".";
1486                         complexInputs += end->getBBTKName();
1487
1488                         complexInputs += " ";
1489                         complexInputs += "\" \"";
1490
1491                         complexInputs += "\n";
1492                 } else if (endBox->getGObjectType() == GCOMPLEXOUTPUTPORT) {
1493                         complexOutputs += "output ";
1494                         complexOutputs += endBox->getBBTKName();
1495
1496                         complexOutputs += " ";
1497                         complexOutputs += startBox->getBBTKName();
1498                         complexOutputs += ".";
1499                         complexOutputs += start->getBBTKName();
1500
1501                         complexOutputs += " ";
1502                         complexOutputs += "\" \"";
1503
1504                         complexOutputs += "\n";
1505                 } else {
1506                         script += "connect ";
1507                         script += startBox->getBBTKName();
1508                         script += ".";
1509                         script += start->getBBTKName();
1510
1511                         script += " ";
1512
1513                         //End Connection info
1514                         script += endBox->getBBTKName();
1515                         script += ".";
1516                         script += end->getBBTKName();
1517
1518                         script += "\n";
1519                 }
1520         }
1521
1522         script += "\n";
1523
1524         for (i = 0; i < (int) execBoxes.size(); i++) {
1525                 script += "exec ";
1526                 int id = execBoxes[i];
1527                 GObjectController *control = _controllers[id];
1528                 GBlackBoxModel *model = (GBlackBoxModel*) control->getModel();
1529
1530                 script += model->getBBTKName();
1531                 script += "\n";
1532         }
1533
1534         script += "\n";
1535         script += "# Complex input ports\n";
1536         script += complexInputs;
1537
1538         script += "\n";
1539         script += "# Complex output ports\n";
1540         script += complexOutputs;
1541
1542         script += "\n";
1543         script += "endefine";
1544         script += "\n";
1545
1546         return script;
1547 }
1548
1549 //=========================================================================
1550
1551 void wxVtkSceneManager::deleteAllBoxes() {
1552         std::map<int, GObjectController*>::iterator it;
1553         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
1554                 GObjectController *cont = it->second;
1555                 cont->removeFromScene();
1556                 unregisterController((InteractorStyleMaracas*) cont);
1557         }
1558         _selectedObjects.clear();
1559         _controllers.clear();
1560         refreshScene();
1561 }
1562
1563 //=========================================================================
1564
1565
1566 void wxVtkSceneManager::refresh() {
1567         _baseView->Refresh();
1568 }
1569 //=========================================================================
1570
1571 void wxVtkSceneManager::refreshScene() {
1572         _baseView->RefreshView();
1573 }
1574
1575 //=========================================================================
1576
1577 void wxVtkSceneManager::centerView() {
1578         double temp[3];
1579         _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1580         _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0, 0, temp[2]);
1581         _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1582         _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0, 0, temp[2]);
1583         _baseView->RefreshView();
1584
1585 }
1586
1587 //=========================================================================
1588
1589 void wxVtkSceneManager::saveDiagram(std::string &content) {
1590         char buffer[50];
1591
1592         content += "CATEGORY:";
1593         content += GetCategory();
1594         content += "\n";
1595
1596         content += "DESCRIPTION:";
1597         content += GetDescription();
1598         content += "\n";
1599
1600         content += "AUTHOR:";
1601         content += GetAuthor();
1602         content += "\n";
1603
1604         //Print info IF COMPLEX BOX
1605         content += "COMPLEXBOX:";
1606         if (_isComplexBox) {
1607                 content += "TRUE\n";
1608                 //ups1
1609                 content += "COMPLEXBOXNAME:";
1610                 content += GetCbName();
1611                 content += "\n";
1612
1613                 content += "PACKAGENAME:";
1614                 content += GetCbPackageName();
1615                 content += "\n";
1616
1617                 //Print info complex input ports
1618                 std::vector<int> inputs = getComplexInputPorts();
1619                 int insize = inputs.size();
1620                 content += "COMPLEXINPUTS:";
1621                 sprintf(buffer, "%d", insize);
1622                 content += buffer;
1623                 content += "\n";
1624
1625                 for (int i = 0; i < insize; i++) {
1626                         int id = inputs[i];
1627                         GObjectController *cont = _controllers[id];
1628                         cont->getModel()->save(content);
1629                 }
1630
1631                 //Print info complex output ports
1632                 std::vector<int> outputs = getComplexOutputPorts();
1633                 int outsize = outputs.size();
1634                 content += "COMPLEXOUTPUTS:";
1635                 sprintf(buffer, "%d", outsize);
1636                 content += buffer;
1637                 content += "\n";
1638
1639                 for (int i = 0; i < outsize; i++) {
1640                         int id = outputs[i];
1641                         GObjectController *cont = _controllers[id];
1642                         cont->getModel()->save(content);
1643                 }
1644         } // _isComplexBox
1645         else {
1646                 content += "FALSE\n";
1647         }
1648
1649         //Print boxes
1650         std::vector<int> boxes = getBlackBoxes();
1651         int bsize = boxes.size();
1652         content += "BOXES:";
1653         sprintf(buffer, "%d", bsize);
1654         content += buffer;
1655         content += "\n";
1656
1657         for (int i = 0; i < bsize; i++) {
1658                 int id = boxes[i];
1659                 GObjectController *cont = _controllers[id];
1660                 cont->getModel()->save(content);
1661         }
1662
1663         //Print connections
1664         std::vector<int> connections = getConnections();
1665         int csize = connections.size();
1666         content += "CONNECTIONS:";
1667         sprintf(buffer, "%d", csize);
1668         content += buffer;
1669         content += "\n";
1670
1671         for (int i = 0; i < csize; i++) {
1672                 int id = connections[i];
1673                 GObjectController *cont = _controllers[id];
1674                 cont->getModel()->save(content);
1675         }
1676
1677 }
1678
1679 //=========================================================================
1680
1681 void wxVtkSceneManager::loadDiagram(stringstream &inputStream) {
1682
1683         std::string version = "<void>";
1684         std::string line = "";
1685         char delims[] = ":";
1686         char *result = NULL;
1687
1688         getline(inputStream, line);
1689
1690         bool start = false;
1691         while (!inputStream.eof()) {
1692                 if (line == "" || line[0] == '#') {
1693                         getline(inputStream, line);
1694                         if (line == "# - BBTKGEditor v 1.0 BBG BlackBox Diagram file") {
1695                                 version = line.substr(18, 3);
1696                         }
1697                         if (line == "# - BBTKGEditor v 1.1 BBG BlackBox Diagram file") {
1698                                 version = line.substr(18, 3);
1699                         }
1700                         if (line == "# - BBTKGEditor v 1.2 BBG BlackBox Diagram file") {
1701                                 version = line.substr(18, 3);
1702                         }
1703                 } else if (line == "APP_START") {
1704                         start = true;
1705                         break;
1706                 }
1707         }
1708
1709         printf("EED wxVtkSceneManager::loadDiagram  version=%s\n", version.c_str());
1710
1711         if (start) {
1712
1713                 if ((version != "1.0") && (version != "1.1")) {
1714                         getline(inputStream, line);//CATEGORY:<category of the box>
1715                         char categoryTmp[30];
1716                         strcpy(categoryTmp, line.c_str());
1717                         result = strtok(categoryTmp, delims);
1718                         result = strtok(NULL, delims);
1719                         SetCategory(result);
1720
1721                         getline(inputStream, line);//DESCRIPTION:<description of the box>
1722                         char descriptionTmp[1024];
1723                         strcpy(descriptionTmp, line.c_str());
1724                         result = strtok(descriptionTmp, delims);
1725                         result = strtok(NULL, delims);
1726                         SetDescription(result);
1727
1728                         getline(inputStream, line);//AUTHOR:<author>
1729                         char authorTmp[255];
1730                         strcpy(authorTmp, line.c_str());
1731                         result = strtok(authorTmp, delims);
1732                         result = strtok(NULL, delims);
1733                         SetAuthor(result);
1734                 }
1735
1736                 //----------
1737                 getline(inputStream, line);//COMPLEX_BOX:TRUE|FALSE
1738                 char complexTmp[30];
1739                 strcpy(complexTmp, line.c_str());
1740                 result = strtok(complexTmp, delims);
1741                 result = strtok(NULL, delims);
1742                 std::string isComplexBox(result);
1743
1744                 if (isComplexBox == "TRUE") {
1745                         _isComplexBox = true;
1746
1747                         if ((version != "1.0") && (version != "1.1")) {
1748                                 getline(inputStream, line);//COMPLEXNAME:<name of the complex box>
1749                                 char complexboxnameTmp[255];
1750                                 strcpy(complexboxnameTmp, line.c_str());
1751                                 result = strtok(complexboxnameTmp, delims);
1752                                 result = strtok(NULL, delims);
1753                                 SetCbName(result);
1754
1755                                 getline(inputStream, line);//PACKAGENAME:<name of the package of the box>
1756                                 char packagenameTmp[255];
1757                                 strcpy(packagenameTmp, line.c_str());
1758                                 result = strtok(packagenameTmp, delims);
1759                                 result = strtok(NULL, delims);
1760                                 SetCbPackageName(result);
1761                         }
1762
1763                         //-----------------------
1764                         //- COMPLEX INPUT PORTS
1765                         //-----------------------
1766                         getline(inputStream, line);//COMPLEXINPUTS:num
1767                         char inputs[30];
1768                         strcpy(inputs, line.c_str());
1769                         result = strtok(inputs, delims);
1770                         result = strtok(NULL, delims);
1771
1772                         int numInputs;
1773                         std::istringstream inps(result);
1774                         inps >> numInputs;
1775
1776                         for (int i = 0; i < numInputs; i++) {
1777                                 //----------
1778                                 getline(inputStream, line);//COMPLEX_PORT
1779                                 getline(inputStream, line);//name
1780                                 std::string inputPortName(line);
1781
1782                                 //----------
1783                                 getline(inputStream, line);//xInic:yInic:zInic
1784                                 char coord[80];
1785                                 strcpy(coord, line.c_str());
1786                                 result = strtok(coord, delims);//xInic
1787                                 std::string xInic(result);
1788                                 result = strtok(NULL, delims);//yInic
1789                                 std::string yInic(result);
1790                                 result = strtok(NULL, delims);//zInic
1791                                 std::string zInic(result);
1792
1793                                 double xIn, yIn, zIn;
1794                                 std::istringstream xSt(xInic);
1795                                 xSt >> xIn;
1796                                 std::istringstream ySt(yInic);
1797                                 ySt >> yIn;
1798                                 std::istringstream zSt(zInic);
1799                                 zSt >> zIn;
1800
1801                                 getline(inputStream, line);//FIN_COMPLEX_PORT
1802
1803                                 configGComBoxInputOutputPort(true, inputPortName, xIn, yIn, zIn);
1804
1805                         } // for input complex box
1806
1807
1808                         //-----------------------
1809                         //- COMPLEX OUTPUT PORTS
1810                         //-----------------------
1811
1812                         getline(inputStream, line);//COMPLEXOUTPUTS:num
1813                         char outputs[30];
1814                         strcpy(outputs, line.c_str());
1815                         result = strtok(outputs, delims);
1816                         result = strtok(NULL, delims);
1817
1818                         int numOutputs;
1819                         std::istringstream outps(result);
1820                         outps >> numOutputs;
1821
1822                         for (int i = 0; i < numOutputs; i++) {
1823                                 //----------
1824                                 getline(inputStream, line);//COMPLEX_PORT
1825                                 getline(inputStream, line);//name
1826                                 std::string outputPortName(line);
1827
1828                                 //----------
1829                                 getline(inputStream, line);//xInic:yInic:zInic
1830                                 char coord[80];
1831                                 strcpy(coord, line.c_str());
1832                                 result = strtok(coord, delims);//xInic
1833                                 std::string xInic(result);
1834                                 result = strtok(NULL, delims);//yInic
1835                                 std::string yInic(result);
1836                                 result = strtok(NULL, delims);//zInic
1837                                 std::string zInic(result);
1838
1839                                 double xIn, yIn, zIn;
1840                                 std::istringstream xSt(xInic);
1841                                 xSt >> xIn;
1842                                 std::istringstream ySt(yInic);
1843                                 ySt >> yIn;
1844                                 std::istringstream zSt(zInic);
1845                                 zSt >> zIn;
1846
1847                                 getline(inputStream, line);//FIN_COMPLEX_PORT
1848
1849                                 configGComBoxInputOutputPort(false, outputPortName, xIn, yIn,
1850                                                 zIn);
1851
1852                         } // for output complex box
1853
1854                 } // complex box
1855
1856                 //----------
1857                 getline(inputStream, line);//BOXES:num
1858                 char boxes[9];
1859                 strcpy(boxes, line.c_str());
1860                 result = strtok(boxes, delims);
1861                 result = strtok(NULL, delims);
1862
1863                 int numBoxes;
1864                 std::istringstream is(result);
1865                 is >> numBoxes;
1866
1867                 for (int i = 0; i < numBoxes; i++) {
1868                         //----------
1869                         getline(inputStream, line);//BOX
1870                         getline(inputStream, line);//package:type:name
1871                         char box[150];
1872                         strcpy(box, line.c_str());
1873                         result = strtok(box, delims);//package
1874                         std::string package(result);
1875                         result = strtok(NULL, delims);//type
1876                         std::string type(result);
1877                         result = strtok(NULL, delims);//name
1878                         std::string name(result);
1879
1880                         getline(inputStream, line);//ISEXEC:TRUE|FALSE
1881                         char exec[15];
1882                         strcpy(exec, line.c_str());
1883                         result = strtok(exec, delims);//ISEXEC
1884                         result = strtok(NULL, delims);//TRUE|FALSE
1885                         std::string isExec(result);
1886
1887                         //----------
1888                         getline(inputStream, line);//xInic:yInic:zInic
1889                         char coord[80];
1890                         strcpy(coord, line.c_str());
1891                         result = strtok(coord, delims);//xInic
1892                         std::string xInic(result);
1893                         result = strtok(NULL, delims);//yInic
1894                         std::string yInic(result);
1895                         result = strtok(NULL, delims);//zInic
1896                         std::string zInic(result);
1897
1898                         double xIn, yIn, zIn;
1899                         std::istringstream xSt(xInic);
1900                         xSt >> xIn;
1901                         std::istringstream ySt(yInic);
1902                         ySt >> yIn;
1903                         std::istringstream zSt(zInic);
1904                         zSt >> zIn;
1905
1906                         //----------
1907                         getline(inputStream, line);//xEnd:yEnd:zEnd
1908                         strcpy(coord, line.c_str());
1909                         result = strtok(coord, delims);//xEnd
1910                         std::string xEnd(result);
1911                         result = strtok(NULL, delims);//yEnd
1912                         std::string yEnd(result);
1913                         result = strtok(NULL, delims);//zEnd
1914                         std::string zEnd(result);
1915
1916                         double xEn, yEn, zEn;
1917                         std::istringstream xEt(xEnd);
1918                         xEt >> xEn;
1919                         std::istringstream yEt(yEnd);
1920                         yEt >> yEn;
1921                         std::istringstream zEt(zEnd);
1922                         zEt >> zEn;
1923
1924                         bool boxExecutable = false;
1925                         if (isExec == "TRUE") {
1926                                 boxExecutable = true;
1927                         }
1928
1929                         int idBox = createGBlackBox(xIn, yIn, package, type);
1930                         configGBlackBox(idBox, xIn, yIn, zIn, name, boxExecutable, xEn,
1931                                         yEn, zEn);
1932
1933                         GObjectController *cont = _controllers[idBox];
1934                         GBlackBoxModel *bbmod = (GBlackBoxModel*) cont->getModel();
1935
1936                         //----------
1937                         getline(inputStream, line);//PORT o FIN_BOX
1938                         std::string port = line.substr(0, 4);
1939                         while (port == "PORT") {
1940                                 getline(inputStream, line);//name:value
1941                                 char poort[150];
1942                                 strcpy(poort, line.c_str());
1943                                 result = strtok(poort, delims);//name
1944                                 std::string name(result);
1945                                 result = strtok(NULL, delims);//value
1946                                 std::string value(result);
1947
1948                                 bbmod->setValueToInput(name, value);
1949
1950                                 getline(inputStream, line);//PORT o FIN_BOX
1951                                 port = line.substr(0, 4);
1952                         } // while
1953
1954                         //EED                           bbmod->notifyObservers(_idManager);
1955                 } // for boxes
1956
1957                 /// CONNECTIONS
1958                 //----------
1959                 getline(inputStream, line);//CONNECTIONS:num
1960                 char conns[30];
1961                 strcpy(conns, line.c_str());
1962                 result = strtok(conns, delims);
1963                 result = strtok(NULL, delims);
1964
1965                 int numConns;
1966                 std::istringstream isCons(result);
1967                 isCons >> numConns;
1968
1969                 for (int i = 0; i < numConns; i++) {
1970                         //----------
1971                         getline(inputStream, line);//CONNECTION
1972                         getline(inputStream, line);//Startbox.PortName:EndBox.PortName
1973
1974                         char connec[200];
1975                         strcpy(connec, line.c_str());
1976                         result = strtok(connec, delims);
1977                         std::string nameStartBox(result);
1978                         result = strtok(NULL, delims);
1979                         std::string nameStartPort(result);
1980                         result = strtok(NULL, delims);
1981                         std::string nameEndBox(result);
1982                         result = strtok(NULL, delims);
1983                         std::string nameEndPort(result);
1984
1985                         int idCon = configGConnetion(nameStartBox, nameStartPort,
1986                                         nameEndBox, nameEndPort);
1987
1988                         if (version != "1.0") {
1989                                 //Readding control points of the manualContour
1990                                 //ups1
1991                                 GConnectorController *tempp =
1992                                                 (GConnectorController*) _controllers[idCon];
1993                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
1994                                 vtkGConnectorView *conView =
1995                                                 (vtkGConnectorView*) tempp->getView();
1996                                 getline(inputStream, line); //NumberOfControlPoints:##
1997                                 strcpy(conns, line.c_str());
1998                                 result = strtok(conns, delims);
1999                                 result = strtok(NULL, delims);
2000
2001                                 int numberOfControlPoints;
2002                                 std::istringstream isCons(result);
2003                                 isCons >> numberOfControlPoints;
2004
2005                                 for (int ii = 0; ii < numberOfControlPoints; ii++) {
2006                                         getline(inputStream, line); //XX:YY:ZZ
2007                                         char connec[200];
2008                                         strcpy(connec, line.c_str());
2009
2010                                         double px, py, pz;
2011                                         result = strtok(connec, delims);
2012                                         std::istringstream isPointX(result);
2013                                         isPointX >> px;
2014                                         result = strtok(NULL, delims);
2015                                         std::istringstream isPointY(result);
2016                                         isPointY >> py;
2017                                         result = strtok(NULL, delims);
2018                                         std::istringstream isPointZ(result);
2019                                         isPointZ >> pz;
2020
2021                                         conMod->getManualContourModel()->InsertPoint_id(ii + 1, px,
2022                                                         py, pz);
2023                                         conView->getManualContourView()->AddPoint();
2024                                 }
2025                         }// version !=1.0
2026
2027                 } // for numConns
2028
2029         } // start
2030
2031         refresh();
2032 }
2033 //=========================================================================
2034
2035
2036 //=========================================================================
2037 void wxVtkSceneManager::configGComBoxInputOutputPort(bool inputoutput,
2038                 std::string portName, double xIn, double yIn, double zIn) {
2039         int idPort;
2040         if (inputoutput == true) {
2041                 idPort = createGComplexBoxInputPort(portName);
2042         } else {
2043                 idPort = createGComplexBoxOutputPort(portName);
2044         }
2045         GObjectController *cont = _controllers[idPort];
2046         GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2047         cbmod->setInicPoint(xIn, yIn, zIn);
2048         cbmod->notifyObservers(_idManager);
2049 }
2050 //=========================================================================
2051
2052
2053 //=========================================================================
2054 GBoxModel* wxVtkSceneManager::findGBox(std::string boxname) {
2055         int j;
2056         int idB;
2057         GBoxModel *bMod;
2058         GBoxModel *boxModel = NULL;
2059         std::vector<int> lstB = getBlackBoxes();
2060         for (j = 0; j < (int) lstB.size(); j++) {
2061                 idB = lstB[j];
2062                 bMod = (GBoxModel*) _controllers[idB]->getModel();
2063                 if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2064                         boxModel = bMod;
2065                 }
2066         } // for
2067
2068         if ((_isComplexBox) && (boxModel == NULL)) {
2069                 std::vector<int> lstInputs = getComplexInputPorts();
2070                 for (j = 0; j < (int) lstInputs.size(); j++) {
2071                         idB = lstInputs[j];
2072                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2073                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2074                                 boxModel = bMod;
2075                         }
2076                 } // for
2077
2078                 std::vector<int> lstOutputs = getComplexOutputPorts();
2079                 for (j = 0; j < (int) lstOutputs.size(); j++) {
2080                         int idB = lstOutputs[j];
2081                         bMod = (GBoxModel*) _controllers[idB]->getModel();
2082                         if (_controllers[idB]->getModel()->getBBTKName() == boxname) {
2083                                 boxModel = bMod;
2084                         }
2085                 } // for
2086
2087         } // complex box
2088
2089         return boxModel;
2090 }
2091
2092 //=========================================================================
2093
2094
2095 int wxVtkSceneManager::configGConnetion(std::string nameStartBox,
2096                 std::string nameStartPort, std::string nameEndBox,
2097                 std::string nameEndPort) {
2098
2099         GBoxModel *boxModel;
2100         GPortModel *startP = NULL;
2101         GPortModel *endP = NULL;
2102
2103         boxModel = findGBox(nameStartBox);
2104         if (boxModel != NULL) {
2105                 startP = boxModel->getOutputPort(nameStartPort);
2106         }
2107
2108         boxModel = findGBox(nameEndBox);
2109         if (boxModel != NULL) {
2110                 endP = boxModel->getInputPort(nameEndPort);
2111         }
2112
2113         //ups2
2114         int idCon = createGConnector(startP);
2115         _worldState = NOTHING_HAPPENS;
2116         GConnectorController *tempp = (GConnectorController*) _controllers[idCon];
2117
2118         GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2119         vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2120         tempp->endContourCreation();
2121         conMod->setEndPort(endP);
2122         conView->updateStartEndPoints();
2123         return idCon;
2124 }
2125
2126 //=========================================================================
2127
2128 bool wxVtkSceneManager::boxExist(std::string boxname) {
2129         bool ok = false;
2130         std::map<int, GObjectController*>::iterator it;
2131         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2132                 GObjectController *cont = it->second;
2133                 if (cont->getModel()->getBBTKName() == boxname) {
2134                         ok = true;
2135                 }
2136         }
2137         return ok;
2138 }
2139
2140 //=========================================================================
2141
2142 std::vector<int> wxVtkSceneManager::getBlackBoxes() {
2143         std::vector<int> vect;
2144         std::map<int, GObjectController*>::iterator it;
2145         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2146                 GObjectController *cont = it->second;
2147                 if (cont->getGObjectType() == GBLACKBOX) {
2148                         vect.push_back(cont->getId());
2149                 }
2150         }
2151         return vect;
2152 }
2153
2154 //=========================================================================
2155
2156 std::vector<int> wxVtkSceneManager::getComplexInputPorts() {
2157         std::vector<int> vect;
2158         std::map<int, GObjectController*>::iterator it;
2159         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2160                 GObjectController *cont = it->second;
2161                 if (cont->getGObjectType() == GCOMPLEXINPUTPORT) {
2162                         vect.push_back(cont->getId());
2163                 }
2164         }
2165         return vect;
2166 }
2167
2168 //=========================================================================
2169
2170 std::vector<int> wxVtkSceneManager::getComplexOutputPorts() {
2171         std::vector<int> vect;
2172         std::map<int, GObjectController*>::iterator it;
2173         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2174                 GObjectController *cont = it->second;
2175                 if (cont->getGObjectType() == GCOMPLEXOUTPUTPORT) {
2176                         vect.push_back(cont->getId());
2177                 }
2178         }
2179         return vect;
2180 }
2181
2182 //=========================================================================
2183
2184 std::vector<int> wxVtkSceneManager::getConnections() {
2185         std::vector<int> vect;
2186         std::map<int, GObjectController*>::iterator it;
2187         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2188                 GObjectController *cont = it->second;
2189                 if (cont->getGObjectType() == GCONNECTOR) {
2190                         vect.push_back(cont->getId());
2191                 }
2192         }
2193         return vect;
2194 }
2195
2196 //=========================================================================
2197
2198 bool wxVtkSceneManager::isComplexBox() {
2199         return _isComplexBox;
2200 }
2201
2202 //=========================================================================
2203
2204 void wxVtkSceneManager::setComplexBox(bool val) {
2205         _isComplexBox = val;
2206 }
2207
2208 //=========================================================================
2209
2210 int wxVtkSceneManager::addObjectController(GObjectController* objController) {
2211         //Register the controller of the new object
2212         registerController((InteractorStyleMaracas*) objController);
2213         //Add the object to the objects list
2214         int newId = _contLastId;//_controllers.size();
2215         objController->setId(newId);
2216         _controllers[newId] = objController;
2217         std::cout
2218                         << "DFCH: int wxVtkSceneManager::addObjectController(GObjectController* objController) ---- _contLastId = "
2219                         << _contLastId << std::endl;
2220         _contLastId++;
2221         return newId;
2222 }
2223
2224 //=========================================================================
2225
2226 void wxVtkSceneManager::DuplicateObjects(std::map<int, GObjectController*> objectsMap) {
2227
2228         std::map<int, int> oldIdNewIdBoxes;
2229         std::vector<int> connections;
2230
2231         std::vector<int> newBoxesID;
2232
2233         std::map<int, GObjectController*>::iterator it;
2234         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2235             GObjectController *cont = it->second;
2236             int type = cont->getGObjectType();
2237
2238             if (type == GBLACKBOX) {
2239                 // Copy black box
2240                 double xInic, yInic, zInic;
2241                 GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2242                 copyBox->getInicPoint(xInic, yInic, zInic);
2243                 int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2244                         copyBox->getBBTKType());
2245
2246                 int idcB = copyBox->getObjectId();
2247                 oldIdNewIdBoxes[idcB] = idBox;
2248                 cont = _controllers[idBox];
2249                 GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2250                 newbox->setInicPoint(xInic, yInic, zInic);
2251                 int num = newbox->getNumInputPorts();
2252                 for (int j = 0; j < num; j++) {
2253                     newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2254                 }
2255                 newbox->move(xInic + 20, yInic + 20, zInic);
2256                 newbox->notifyObservers(_idManager);
2257                 newBoxesID.push_back(newbox->getObjectId());
2258
2259             } else if (type == GCONNECTOR) {
2260                 int idCon = cont->getId();
2261                 connections.push_back(idCon);
2262             }
2263
2264         }
2265
2266         for (int i = 0; i < (int) connections.size(); i++) {
2267             int objId = connections[i];
2268             GObjectController *cont = objectsMap[objId];
2269             GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2270
2271             GPortModel* startPort = connectModel->getStartPort();
2272             int startPortIndex = startPort->getPosInBox();
2273             GPortModel* endPort = connectModel->getEndPort();
2274             int endPortIndex = endPort->getPosInBox();
2275
2276             GBlackBoxModel* startPortParentBox =
2277                     (GBlackBoxModel*) startPort->getParentBox();
2278             GBlackBoxModel* endPortParentBox =
2279                     (GBlackBoxModel*) endPort->getParentBox();
2280
2281             int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2282             int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2283
2284             GBlackBoxModel* newStartBox =
2285                     (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2286             GBlackBoxModel* newEndBox =
2287                     (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2288
2289             GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2290             GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2291
2292             // Creates connection
2293             int idCon = createGConnector(newStartPort);
2294             GConnectorController *tempp =
2295                     (GConnectorController*) _controllers[idCon];
2296             GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2297             vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2298             tempp->endContourCreation();
2299             conMod->setEndPort(newEndPort);
2300             conView->updateStartEndPoints();
2301         }
2302
2303         /// the new selected boxes are the duplicate ones
2304
2305         UnSelectBlackBoxes();
2306         for (int i = 0; i < newBoxesID.size(); i++) {
2307             _selectedObjects.push_back(newBoxesID.at(i));
2308         }
2309
2310         for (int i = 0; i < (int) _selectedObjects.size(); i++) {
2311             int id = _selectedObjects[i];
2312             GObjectController* cont = _controllers[id];
2313
2314             cont->getView()->setState(SELECTED);
2315             cont->getModel()->notifyObservers(_idManager);
2316
2317         }
2318         refreshScene();
2319     }
2320
2321
2322 //=========================================================================
2323
2324 int wxVtkSceneManager::getNumSelectedObjects() {
2325         return _selectedObjects.size();
2326 }
2327
2328 //=========================================================================
2329
2330 std::map<int, GObjectController*> wxVtkSceneManager::getSelectedObjects() {
2331         std::map<int, GObjectController*> mapSelected;
2332
2333         std::map<int, GObjectController*>::iterator it;
2334         for (it = _controllers.begin(); it != _controllers.end(); ++it) {
2335                 GObjectController *cont = it->second;
2336                 if (cont->getGObjectType() == GBLACKBOX && cont->getView()->getState()
2337                                 == SELECTED) {
2338                         mapSelected[cont->getId()] = cont;
2339                 }
2340         }
2341
2342         std::map<int, GObjectController*>::iterator it2;
2343         for (it2 = _controllers.begin(); it2 != _controllers.end(); ++it2) {
2344                 GObjectController *cont = it2->second;
2345                 if (cont->getGObjectType() == GCONNECTOR) {
2346                         GConnectorModel* cmod = (GConnectorModel*) cont->getModel();
2347                         GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2348                         GBoxModel* startPortParentBox =
2349                                         cmod->getStartPort()->getParentBox();
2350
2351                         std::map<int, GObjectController*>::iterator iterOBJ1 =
2352                                         mapSelected.find(startPortParentBox->getObjectId());
2353                         std::map<int, GObjectController*>::iterator iterOBJ2 =
2354                                         mapSelected.find(endPortParentBox->getObjectId());
2355
2356                         if (iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end()) {
2357                                 int ID = cont->getId();
2358                                 mapSelected[ID] = cont;
2359                         }
2360                 }
2361         }
2362         return mapSelected;
2363 }
2364
2365 //=========================================================================
2366
2367 void wxVtkSceneManager::addObjects(std::map<int, GObjectController*> objectsMap) {
2368
2369         std::map<int, int> oldIdNewIdBoxes;
2370         std::vector<int> connections;
2371
2372         std::map<int, GObjectController*>::iterator it;
2373         for (it = objectsMap.begin(); it != objectsMap.end(); ++it) {
2374                 GObjectController *cont = it->second;
2375                 int type = cont->getGObjectType();
2376
2377                 if (type == GBLACKBOX) {
2378                         // Copy black box
2379                         double xInic, yInic, zInic;
2380                         GBlackBoxModel* copyBox = (GBlackBoxModel*) cont->getModel();
2381                         copyBox->getInicPoint(xInic, yInic, zInic);
2382                         int idBox = createGBlackBox(0, 0, copyBox->getBBTKPackage(),
2383                                         copyBox->getBBTKType());
2384
2385                         int idcB = copyBox->getObjectId();
2386                         oldIdNewIdBoxes[idcB] = idBox;
2387                         cont = _controllers[idBox];
2388                         GBlackBoxModel* newbox = (GBlackBoxModel*) cont->getModel();
2389                         newbox->setInicPoint(xInic, yInic, zInic);
2390                         int num = newbox->getNumInputPorts();
2391                         for (int j = 0; j < num; j++) {
2392                                 newbox->setValueToInputPort(j, copyBox->getValueInputPort(j));
2393                         }
2394                         newbox->notifyObservers(_idManager);
2395                 } else if (type == GCONNECTOR) {
2396                         int idCon = cont->getId();
2397                         connections.push_back(idCon);
2398                 }
2399
2400         }
2401
2402         for (int i = 0; i < (int) connections.size(); i++) {
2403                 int objId = connections[i];
2404                 GObjectController *cont = objectsMap[objId];
2405                 GConnectorModel* connectModel = (GConnectorModel*) cont->getModel();
2406
2407                 GPortModel* startPort = connectModel->getStartPort();
2408                 int startPortIndex = startPort->getPosInBox();
2409                 GPortModel* endPort = connectModel->getEndPort();
2410                 int endPortIndex = endPort->getPosInBox();
2411
2412                 GBlackBoxModel* startPortParentBox =
2413                                 (GBlackBoxModel*) startPort->getParentBox();
2414                 GBlackBoxModel* endPortParentBox =
2415                                 (GBlackBoxModel*) endPort->getParentBox();
2416
2417                 int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2418                 int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2419
2420                 GBlackBoxModel* newStartBox =
2421                                 (GBlackBoxModel*) _controllers[idNewStartBox]->getModel();
2422                 GBlackBoxModel* newEndBox =
2423                                 (GBlackBoxModel*) _controllers[idNewEndBox]->getModel();
2424
2425                 GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2426                 GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2427
2428                 // Creates connection
2429                 int idCon = createGConnector(newStartPort);
2430                 GConnectorController *tempp =
2431                                 (GConnectorController*) _controllers[idCon];
2432                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2433                 vtkGConnectorView *conView = (vtkGConnectorView*) tempp->getView();
2434                 tempp->endContourCreation();
2435                 conMod->setEndPort(newEndPort);
2436                 conView->updateStartEndPoints();
2437         }
2438
2439         std::map<int, int>::iterator itIds;
2440         for (itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds) {
2441                 int idOld = itIds->first;
2442                 int idNew = itIds->second;
2443
2444                 GBlackBoxModel* oldBox =
2445                                 (GBlackBoxModel*) objectsMap[idOld]->getModel();
2446                 GBlackBoxModel* newBox =
2447                                 (GBlackBoxModel*) _controllers[idNew]->getModel();
2448
2449                 std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2450                 std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2451                 std::vector<int> newInputConnections = newBox->getConnectedInputs();
2452                 std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2453
2454                 for (int k = 0; k < (int) oldInputConnections.size(); k++) {
2455                         bool exist = false;
2456                         //EED                           int toCreate=-1;
2457                         for (int l = 0; l < (int) newInputConnections.size() && !exist; l++) {
2458                                 if (oldInputConnections[k] == newInputConnections[l]) {
2459                                         exist = true;
2460                                 }
2461                         }
2462
2463                         if (exist == false) {
2464                                 //Create complex input
2465                                 int posInBox = oldInputConnections[k];
2466                                 GPortModel* inputPort = oldBox->getInputPort(posInBox);
2467                                 std::string inputPortName = inputPort->getBBTKName();
2468                                 int idInputPort = createGComplexBoxInputPort(inputPortName);
2469                                 GObjectController *cont = _controllers[idInputPort];
2470                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2471                                 double xIn, yIn, zIn;
2472                                 inputPort->getInicPoint(xIn, yIn, zIn);
2473                                 yIn += 20;
2474                                 cbmod->setInicPoint(xIn, yIn, zIn);
2475                                 cbmod->notifyObservers(_idManager);
2476
2477                                 GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2478
2479                                 // Creates connection
2480                                 int idCon = createGConnector(cbmod->getOutputPort(0));
2481                                 GConnectorController *tempp =
2482                                                 (GConnectorController*) _controllers[idCon];
2483                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2484                                 vtkGConnectorView *conView =
2485                                                 (vtkGConnectorView*) tempp->getView();
2486                                 tempp->endContourCreation();
2487                                 conMod->setEndPort(inputPortEnd);
2488                                 conView->updateStartEndPoints();
2489
2490                         }
2491
2492                 }
2493
2494                 for (int k = 0; k < (int) oldOutputConnections.size(); k++) {
2495                         bool exist = false;
2496                         //EED                           int toCreate=-1;
2497                         for (int l = 0; l < (int) newOutputConnections.size() && !exist; l++) {
2498                                 if (oldOutputConnections[k] == newOutputConnections[l]) {
2499                                         exist = true;
2500                                 }
2501                         }
2502
2503                         if (exist == false) {
2504                                 //Create complex output
2505                                 int posInBox = oldOutputConnections[k];
2506                                 GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2507                                 std::string outputPortName = outputPort->getBBTKName();
2508                                 int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2509                                 GObjectController *cont = _controllers[idOutputPort];
2510                                 GBoxModel *cbmod = (GBoxModel*) cont->getModel();
2511                                 double xIn, yIn, zIn;
2512                                 outputPort->getInicPoint(xIn, yIn, zIn);
2513                                 yIn -= 20;
2514                                 cbmod->setInicPoint(xIn, yIn, zIn);
2515                                 cbmod->notifyObservers(_idManager);
2516
2517                                 GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2518
2519                                 // Creates connection
2520                                 int idCon = createGConnector(outputPortEnd);
2521                                 GConnectorController *tempp =
2522                                                 (GConnectorController*) _controllers[idCon];
2523                                 GConnectorModel *conMod = (GConnectorModel*) tempp->getModel();
2524                                 vtkGConnectorView *conView =
2525                                                 (vtkGConnectorView*) tempp->getView();
2526                                 tempp->endContourCreation();
2527                                 conMod->setEndPort(cbmod->getInputPort(0));
2528                                 conView->updateStartEndPoints();
2529
2530                         }
2531
2532                 }
2533
2534         }
2535
2536 }
2537
2538 //=========================================================================
2539 void wxVtkSceneManager::SetCbName(std::string cbName) {
2540         _cbName = cbName;
2541         if (_cbName == "") {
2542                 _cbName = "<complex box name>";
2543         }
2544 }
2545
2546 //=========================================================================
2547 std::string wxVtkSceneManager::GetCbName() {
2548         return _cbName;
2549 }
2550
2551 //=========================================================================
2552 void wxVtkSceneManager::SetCbPackageName(std::string packagename) {
2553         _cbPackageName = packagename;
2554         if (_cbPackageName == "") {
2555                 _cbPackageName = "<package name of the complex box>";
2556         }
2557 }
2558
2559 //=========================================================================
2560 std::string wxVtkSceneManager::GetCbPackageName() {
2561         return _cbPackageName;
2562 }
2563
2564 //=========================================================================
2565 void wxVtkSceneManager::SetAuthor(std::string author) {
2566         _Author = author;
2567         if (_Author == "") {
2568                 _Author = "<author of the box>";
2569         }
2570 }
2571
2572 //=========================================================================
2573 std::string wxVtkSceneManager::GetAuthor() {
2574         return _Author;
2575 }
2576
2577 //=========================================================================
2578 void wxVtkSceneManager::SetCategory(std::string category) {
2579         _Category = category;
2580         if (_Category == "") {
2581                 _Category = "<category of the box>";
2582         }
2583 }
2584
2585 //=========================================================================
2586 std::string wxVtkSceneManager::GetCategory() {
2587         return _Category;
2588 }
2589
2590 //=========================================================================
2591 void wxVtkSceneManager::SetDescription(std::string description) {
2592         _Description = description;
2593         if (_Description == "") {
2594                 _Description = "<description of the box>";
2595         }
2596 }
2597
2598 //=========================================================================
2599 std::string wxVtkSceneManager::GetDescription() {
2600         return _Description;
2601 }
2602
2603 //=========================================================================
2604
2605
2606 } // EO namespace bbtk
2607
2608 // EOF
2609