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