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