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