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