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