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