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