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