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