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