]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxVtkSceneManager.cxx
26deb1419775f151635a9fbe12aef97804854c84
[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<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
1175                 // Definition of a complex box
1176                 script+="define ";
1177                 script+=cbName;
1178                 script+="\n";
1179
1180                 script+="author \" ";
1181                 script+=cbAuthor;
1182                 script+="\"\n";
1183
1184                 script+="description \" ";
1185                 script+=cbDescription;
1186                 script+="\"\n";
1187
1188                 // Create boxes
1189                 for(i = 0; i<boxes.size();i++)
1190                 {
1191                         script+="new ";
1192                         int id = boxes[i];
1193                         GObjectController *control = _controllers[id];
1194                         GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1195
1196                         script+=model->getBBTKType();
1197                         script+=" ";
1198                         script+=model->getBBTKName();
1199                         script+="\n";
1200
1201                                 std::vector<GPortModel*> inputs = model->getInputPorts();
1202                                 for(int j = 0; j<(int)inputs.size();j++)
1203                                 {
1204                                         GPortModel* inputPort = inputs[j];
1205                                         if(inputPort->isValueSet())
1206                                         {
1207                                                 script+="set ";
1208                                                 script+=model->getBBTKName();
1209                                                 script+=".";
1210                                                 script+=inputPort->getBBTKName();
1211                                                 script+=" ";
1212                                                 script+=inputPort->getValue();
1213                                                 script+="\n";
1214                                         }
1215                                 }
1216
1217                         }
1218
1219                         // Create connections in the script. If the connection is made with a complex port, it is created the input or output
1220
1221                         std::string complexInputs="";
1222                         std::string complexOutputs="";
1223
1224                         for(i = 0; i<(int)connections.size();i++)
1225                         {
1226                                 int id = connections[i];
1227                                 GObjectController *control = _controllers[id];
1228                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
1229
1230                                 //Connection info
1231                                 GPortModel *start = model->getStartPort();
1232                                 GBoxModel *startBox =start->getParentBox();
1233
1234                                 GPortModel *end = model->getEndPort();
1235                                 GBoxModel *endBox =end->getParentBox();
1236
1237                                 if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
1238                                 {
1239                                         complexInputs+="input ";
1240                                         complexInputs+=startBox->getBBTKName();
1241
1242                                         complexInputs+=" ";
1243                                         complexInputs+=endBox->getBBTKName();
1244                                         complexInputs+=".";
1245                                         complexInputs+=end->getBBTKName();
1246
1247                                         complexInputs+=" ";
1248                                         complexInputs+="\" \"";
1249
1250                                         complexInputs+="\n";
1251                                 }
1252                                 else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
1253                                 {
1254                                         complexOutputs+="output ";
1255                                         complexOutputs+=endBox->getBBTKName();
1256
1257                                         complexOutputs+=" ";
1258                                         complexOutputs+=startBox->getBBTKName();
1259                                         complexOutputs+=".";
1260                                         complexOutputs+=start->getBBTKName();
1261
1262                                         complexOutputs+=" ";
1263                                         complexOutputs+="\" \"";
1264
1265                                         complexOutputs+="\n";
1266                                 }
1267                                 else
1268                                 {
1269                                         script+="connect ";
1270                                         script+=startBox->getBBTKName();
1271                                         script+=".";
1272                                         script+=start->getBBTKName();
1273
1274                                         script+=" ";
1275
1276                                         //End Connection info
1277                                         script+=endBox->getBBTKName();
1278                                         script+=".";
1279                                         script+=end->getBBTKName();
1280
1281                                         script+="\n";
1282                                 }
1283                         }
1284
1285                         for(i = 0; i<(int)execBoxes.size();i++)
1286                         {
1287                                 script+="exec ";
1288                                 int id = execBoxes[i];
1289                                 GObjectController *control = _controllers[id];
1290                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1291
1292                                 script+=model->getBBTKName();
1293                                 script+="\n";
1294                         }
1295
1296                         script+="\n";
1297                         script+="# Complex input ports\n";
1298                         script+=complexInputs;
1299
1300                         script+="\n";
1301                         script+="# Complex output ports\n";
1302                         script+=complexOutputs;
1303
1304                         script+="\n";
1305                         script+="endefine";
1306                         script+="\n";           
1307
1308                 return script;
1309         }
1310
1311         //=========================================================================
1312
1313         void wxVtkSceneManager::deleteAllBoxes()
1314         {               
1315                 std::map<int, GObjectController*>::iterator it;
1316                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1317                 {
1318                         GObjectController *cont = it->second;           
1319                         cont->removeFromScene();
1320                         unregisterController((InteractorStyleMaracas*)cont);
1321                 }
1322                 _selectedObjects.clear();
1323                 _controllers.clear();   
1324                 refreshScene();
1325         }
1326
1327         //=========================================================================
1328
1329         void wxVtkSceneManager::refreshScene()
1330         {
1331                 _baseView->RefreshView();
1332         }
1333
1334         //=========================================================================
1335         
1336         void wxVtkSceneManager::centerView()
1337         {
1338                 double temp[3];
1339                 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1340                 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1341                 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1342                 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1343                 _baseView->RefreshView();
1344
1345         }
1346
1347         //=========================================================================
1348
1349         void wxVtkSceneManager::saveDiagram(std::string &content)
1350         {
1351                 char buffer [50];
1352
1353                 //Print info IF COMPLEX BOX
1354                 content+="COMPLEXBOX:";
1355                 if(_isComplexBox)
1356                 {
1357                         content+="TRUE\n";
1358
1359                         //Print info complex input ports
1360                         std::vector<int> inputs = getComplexInputPorts();
1361                         int insize = inputs.size();
1362                         content+="COMPLEXINPUTS:";
1363                         sprintf (buffer, "%d", insize);
1364                         content+=buffer;
1365                         content+="\n";
1366                         
1367                         for(int i = 0;i<insize;i++)
1368                         {
1369                                 int id = inputs[i];
1370                                 GObjectController *cont = _controllers[id];
1371                                 cont->getModel()->save(content);
1372                         }
1373
1374                         //Print info complex output ports
1375                         std::vector<int> outputs = getComplexOutputPorts();
1376                         int outsize = outputs.size();
1377                         content+="COMPLEXOUTPUTS:";
1378                         sprintf (buffer, "%d", outsize);
1379                         content+=buffer;
1380                         content+="\n";
1381                         
1382                         for(int i = 0;i<outsize;i++)
1383                         {
1384                                 int id = outputs[i];
1385                                 GObjectController *cont = _controllers[id];
1386                                 cont->getModel()->save(content);
1387                         }
1388                 } // _isComplexBox
1389                 else
1390                 {
1391                         content+="FALSE\n";
1392                 }
1393
1394                 //Print boxes
1395                 std::vector<int> boxes = getBlackBoxes();
1396                 int bsize = boxes.size();
1397                 content+="BOXES:";
1398                 sprintf (buffer, "%d", bsize);
1399                 content+=buffer;
1400                 content+="\n";
1401                 
1402                 for(int i = 0;i<bsize;i++)
1403                 {
1404                         int id = boxes[i];
1405                         GObjectController *cont = _controllers[id];
1406                         cont->getModel()->save(content);
1407                 }
1408
1409                 //Print connections
1410                 std::vector<int> connections = getConnections();
1411                 int csize = connections.size();
1412                 content+="CONNECTIONS:";
1413                 sprintf (buffer, "%d", csize);
1414                 content+=buffer;
1415                 content+="\n";
1416                 
1417                 for(int i = 0;i<csize;i++)
1418                 {
1419                         int id = connections[i];
1420                         GObjectController *cont = _controllers[id];
1421                         cont->getModel()->save(content);
1422                 }
1423
1424         }
1425
1426         //=========================================================================
1427
1428         void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1429         {
1430
1431                 std::string line="";    
1432                 char delims[] = ":";
1433                 char *result = NULL;
1434         getline(inputStream,line);
1435
1436                 bool start = false;
1437                 while ( !inputStream.eof() ) 
1438                 {
1439                         if(line=="" || line[0]=='#')
1440                         {
1441                                 getline(inputStream,line);
1442                         }
1443                         else if(line=="APP_START")
1444                         {
1445                                 start = true;
1446                                 break;
1447                         }
1448                 }
1449
1450                 if(start)
1451                 {
1452
1453                         //----------
1454                         getline(inputStream,line);//COMPLEX_BOX:TRUE|FALSE
1455                         char complex[30];
1456                         strcpy( complex, line.c_str() );
1457                         result = strtok( complex, delims );
1458                         result = strtok( NULL, delims );
1459                         std::string isComplexBox(result);
1460
1461                         if(isComplexBox=="TRUE")
1462                         {
1463                                 _isComplexBox=true;
1464
1465                                 //-----------------------
1466                                 //- COMPLEX INPUT PORTS
1467                                 //-----------------------
1468                                 getline(inputStream,line);//COMPLEXINPUTS:num
1469                                 char inputs[30];
1470                                 strcpy( inputs, line.c_str() );
1471                                 result = strtok( inputs, delims );
1472                                 result = strtok( NULL, delims );
1473                                                 
1474                                 int numInputs;
1475                                 std::istringstream inps(result);
1476                                 inps >> numInputs;
1477                                 
1478                                 for(int i = 0;i<numInputs;i++)
1479                                 {
1480                                         //----------
1481                                         getline(inputStream,line);//COMPLEX_PORT
1482                                         getline(inputStream,line);//name
1483                                         std::string inputPortName(line);
1484
1485                                         //----------
1486                                         getline(inputStream,line);//xInic:yInic:zInic
1487                                         char coord[80];
1488                                         strcpy( coord, line.c_str() );
1489                                         result = strtok( coord, delims );//xInic
1490                                         std::string xInic(result);
1491                                         result = strtok( NULL, delims );//yInic
1492                                         std::string yInic(result);
1493                                         result = strtok( NULL, delims );//zInic
1494                                         std::string zInic(result);
1495
1496                                         double xIn, yIn, zIn;
1497                                         std::istringstream xSt(xInic);
1498                                         xSt >> xIn;
1499                                         std::istringstream ySt(yInic);
1500                                         ySt >> yIn;
1501                                         std::istringstream zSt(zInic);
1502                                         zSt >> zIn;
1503
1504                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1505
1506                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
1507                                         GObjectController *cont = _controllers[idInputPort];
1508                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1509                                         cbmod->setInicPoint(xIn,yIn,zIn);
1510                                         cbmod->notifyObservers(_idManager);
1511                                 } // for input complex box
1512
1513
1514                                 //-----------------------
1515                                 //- COMPLEX OUTPUT PORTS
1516                                 //-----------------------
1517
1518                                 getline(inputStream,line);//COMPLEXOUTPUTS:num
1519                                 char outputs[30];
1520                                 strcpy( outputs, line.c_str() );
1521                                 result = strtok( outputs, delims );
1522                                 result = strtok( NULL, delims );
1523                                                 
1524                                 int numOutputs;
1525                                 std::istringstream outps(result);
1526                                 outps >> numOutputs;
1527                                 
1528                                 for(int i = 0;i<numOutputs;i++)
1529                                 {
1530                                         //----------
1531                                         getline(inputStream,line);//COMPLEX_PORT
1532                                         getline(inputStream,line);//name
1533                                         std::string outputPortName(line);
1534
1535                                         //----------
1536                                         getline(inputStream,line);//xInic:yInic:zInic
1537                                         char coord[80];
1538                                         strcpy( coord, line.c_str() );
1539                                         result = strtok( coord, delims );//xInic
1540                                         std::string xInic(result);
1541                                         result = strtok( NULL, delims );//yInic
1542                                         std::string yInic(result);
1543                                         result = strtok( NULL, delims );//zInic
1544                                         std::string zInic(result);
1545
1546                                         double xIn, yIn, zIn;
1547                                         std::istringstream xSt(xInic);
1548                                         xSt >> xIn;
1549                                         std::istringstream ySt(yInic);
1550                                         ySt >> yIn;
1551                                         std::istringstream zSt(zInic);
1552                                         zSt >> zIn;
1553
1554                                         getline(inputStream,line);//FIN_COMPLEX_PORT
1555
1556                                         
1557                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
1558                                         GObjectController *cont = _controllers[idOutputPort];
1559                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
1560                                         cbmod->setInicPoint(xIn,yIn,zIn);
1561                                         cbmod->notifyObservers(_idManager);
1562                                 } // for output complex box
1563
1564                         } // complex box
1565
1566                         //----------
1567                         getline(inputStream,line);//BOXES:num
1568                         char boxes[9];
1569                         strcpy( boxes, line.c_str() );
1570                         result = strtok( boxes, delims );
1571                         result = strtok( NULL, delims );
1572                                         
1573                         int numBoxes;
1574                         std::istringstream is(result);
1575                         is >> numBoxes;
1576                         
1577                         for(int i = 0;i<numBoxes;i++)
1578                         {
1579                                 //----------
1580                                 getline(inputStream,line);//BOX
1581                                 getline(inputStream,line);//package:type:name
1582                                 char box[150];
1583                                 strcpy( box, line.c_str() );
1584                                 result = strtok( box, delims );//package
1585                                 std::string package(result);
1586                                 result = strtok( NULL, delims );//type
1587                                 std::string type(result);
1588                                 result = strtok( NULL, delims );//name
1589                                 std::string name(result);
1590
1591                                 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1592                                 char exec[15];
1593                                 strcpy( exec, line.c_str() );
1594                                 result = strtok( exec, delims );//ISEXEC                                
1595                                 result = strtok( NULL, delims );//TRUE|FALSE
1596                                 std::string isExec(result);
1597
1598                                 //----------
1599                                 getline(inputStream,line);//xInic:yInic:zInic
1600                                 char coord[80];
1601                                 strcpy( coord, line.c_str() );
1602                                 result = strtok( coord, delims );//xInic
1603                                 std::string xInic(result);
1604                                 result = strtok( NULL, delims );//yInic
1605                                 std::string yInic(result);
1606                                 result = strtok( NULL, delims );//zInic
1607                                 std::string zInic(result);
1608
1609                                 double xIn, yIn, zIn;
1610                                 std::istringstream xSt(xInic);
1611                                 xSt >> xIn;
1612                                 std::istringstream ySt(yInic);
1613                                 ySt >> yIn;
1614                                 std::istringstream zSt(zInic);
1615                                 zSt >> zIn;
1616                                                 
1617                                 //----------
1618                                 getline(inputStream,line);//xEnd:yEnd:zEnd
1619                                 strcpy( coord, line.c_str() );
1620                                 result = strtok( coord, delims );//xEnd
1621                                 std::string xEnd(result);
1622                                 result = strtok( NULL, delims );//yEnd
1623                                 std::string yEnd(result);
1624                                 result = strtok( NULL, delims );//zEnd
1625                                 std::string zEnd(result);
1626
1627                                 double xEn, yEn, zEn;
1628                                 std::istringstream xEt(xEnd);
1629                                 xEt >> xEn;
1630                                 std::istringstream yEt(yEnd);
1631                                 yEt >> yEn;
1632                                 std::istringstream zEt(zEnd);
1633                                 zEt >> zEn;
1634
1635                                 bool boxExecutable=false;
1636                                 if(isExec=="TRUE")
1637                                 {
1638                                         boxExecutable= true;
1639                                 }
1640                                 
1641                                 int idBox                               = createGBlackBox(xIn,yIn,package,type);
1642                                 configGBlackBox(idBox, xIn,yIn,zIn,name, boxExecutable,xEn,yEn,zEn);
1643                                 
1644                                 GObjectController *cont = _controllers[idBox];
1645                                 GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
1646                                 
1647                                 
1648 //ups2 eed                              
1649 //                              GObjectController *cont = _controllers[idBox];
1650 //                              GBlackBoxModel *bbmod   = (GBlackBoxModel*)cont->getModel();
1651 //                              bbmod->setBBTKName(name);
1652 //                              bbmod->setInicPoint(xIn,yIn,zIn);
1653 //                              bbmod->setFinalPoint(xEn,yEn,zEn);
1654 //                              bbmod->setExecutable(boxExecutable);
1655
1656                                                         
1657                                 //----------
1658                                 getline(inputStream,line);//PORT o FIN_BOX
1659                                 std::string port=line.substr(0,4);
1660                                 while(port=="PORT")
1661                                 {
1662                                         getline(inputStream,line);//name:value
1663                                         char poort[150];
1664                                         strcpy( poort, line.c_str() );
1665                                         result = strtok( poort, delims );//name
1666                                         std::string name(result);
1667                                         result = strtok( NULL, delims );//value
1668                                         std::string value(result);
1669
1670                                         bbmod->setValueToInput(name,value);                                     
1671
1672                                         getline(inputStream,line);//PORT o FIN_BOX
1673                                         port=line.substr(0,4);
1674                                 } // while              
1675
1676 //EED                           bbmod->notifyObservers(_idManager);
1677                         } // boxes
1678
1679                         /// CONNECTIONS
1680                         //----------
1681                         getline(inputStream,line);//CONNECTIONS:num
1682                         char conns[30];
1683                         strcpy( conns, line.c_str() );
1684                         result = strtok( conns, delims );
1685                         result = strtok( NULL, delims );
1686                                         
1687                         int numConns;
1688                         std::istringstream isCons(result);
1689                         isCons >> numConns;
1690                                         
1691                         for(int i = 0;i<numConns;i++)
1692                         {
1693                                 //----------
1694                                 getline(inputStream,line);//CONNECTION
1695                                 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1696
1697                                 char connec[200];
1698                                 strcpy( connec, line.c_str() );
1699                                 result = strtok( connec, delims );
1700                                 std::string nameStartBox(result);
1701                                 result = strtok( NULL, delims );
1702                                 std::string nameStartPort(result);
1703                                 result = strtok( NULL, delims );
1704                                 std::string nameEndBox(result);
1705                                 result = strtok( NULL, delims );
1706                                 std::string nameEndPort(result);
1707
1708                                 configGConnetion(_isComplexBox, nameStartBox, nameStartPort, nameEndBox, nameEndPort);
1709 //ups 3 EED Borrame
1710 /*                              
1711                                 std::vector<int> lstB   = getBlackBoxes();                              
1712                                 GPortModel *startP              = NULL;
1713                                 GPortModel *endP                = NULL;
1714                                 int j;
1715                                 for(j = 0;j<lstB.size();j++)
1716                                 {
1717                                         int idB = lstB[j];
1718                                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1719                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1720                                         {                                               
1721                                                 startP = bbMod->getOutputPort(nameStartPort);
1722                                         }
1723                                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1724                                         {
1725                                                 endP = bbMod->getInputPort(nameEndPort);
1726                                         }
1727                                 } // for
1728
1729                                 if(_isComplexBox)
1730                                 {
1731                                         if(startP==NULL)
1732                                         {
1733                                                 std::vector<int> lstInputs = getComplexInputPorts();
1734                                                 for(j = 0;j<lstInputs.size();j++)
1735                                                 {
1736                                                         int idB = lstInputs[j];
1737                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1738                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1739                                                         {                                               
1740                                                                 startP = cbMod->getOutputPort(nameStartPort);
1741                                                         } // if 
1742                                                 } // for
1743                                         } // if 
1744
1745                                         if(endP==NULL)
1746                                         {
1747                                                 std::vector<int> lstOutputs = getComplexOutputPorts();
1748                                                 for(j = 0;j<lstOutputs.size();j++)
1749                                                 {
1750                                                         int idB = lstOutputs[j];
1751                                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1752                                                         if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1753                                                         {                                               
1754                                                                 endP = cbMod->getInputPort(nameEndPort);
1755                                                         } // if
1756                                                 } // for
1757                                         } // if endP
1758                                 } // complex box
1759                                                                 
1760                                 int idCon = createGConnector(startP);
1761                                 _worldState = NOTHING_HAPPENS;
1762                                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1763
1764                                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1765                                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1766                                 tempp->endContourCreation();
1767                                 conMod->setEndPort(endP);                                       
1768                                 conView->updateStartEndPoints();
1769  */
1770                         } // for numConns
1771                         
1772         } // start
1773
1774         }
1775
1776         //=========================================================================
1777         
1778         GBlackBoxModel* wxVtkSceneManager::findGBox(std::string boxname)
1779         {       
1780                 GBlackBoxModel *boxModel        = NULL;
1781                 GBlackBoxModel *bbMod;
1782                 std::vector<int> lstB           = getBlackBoxes();                              
1783                 int idB;
1784                 int j;
1785                 for(j = 0;j<(int)lstB.size();j++)
1786                 {
1787                         idB = lstB[j];
1788                         bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1789                         if(_controllers[idB]->getModel()->getBBTKName()==boxname)
1790                         {                                               
1791                                 boxModel = bbMod;
1792                         }
1793                 } // for
1794                 return boxModel;
1795         }
1796         
1797         //=========================================================================
1798
1799
1800         void wxVtkSceneManager::configGConnetion(bool _isComplexBox, 
1801                                                                                          std::string nameStartBox, std::string  nameStartPort, std::string  
1802                                                                                          nameEndBox, std::string  nameEndPort)
1803         {
1804                 GPortModel *startP              = findGBox(nameStartBox)->getOutputPort(nameStartPort);
1805                 GPortModel *endP                = findGBox(nameEndBox)->getInputPort(nameEndPort);
1806
1807 /*ups5 EED Borrame
1808                 std::vector<int> lstB   = getBlackBoxes();                              
1809                 int j;
1810                 for(j = 0;j<(int)lstB.size();j++)
1811                 {
1812                         int idB = lstB[j];
1813                         GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1814                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1815                         {                                               
1816                                 startP = bbMod->getOutputPort(nameStartPort);
1817                         }
1818                         else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1819                         {
1820                                 endP = bbMod->getInputPort(nameEndPort);
1821                         }
1822                 } // for
1823 */              
1824                 int j;
1825                 if(_isComplexBox)
1826                 {
1827                         if(startP==NULL)
1828                         {
1829                                 std::vector<int> lstInputs = getComplexInputPorts();
1830                                 for(j = 0;j<(int)lstInputs.size();j++)
1831                                 {
1832                                         int idB = lstInputs[j];
1833                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1834                                         if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1835                                         {                                               
1836                                                 startP = cbMod->getOutputPort(nameStartPort);
1837                                         } // if 
1838                                 } // for
1839                         } // if 
1840                         
1841                         if(endP==NULL)
1842                         {
1843                                 std::vector<int> lstOutputs = getComplexOutputPorts();
1844                                 for(j = 0;j<(int)lstOutputs.size();j++)
1845                                 {
1846                                         int idB = lstOutputs[j];
1847                                         GBoxModel *cbMod = (GBoxModel*)_controllers[idB]->getModel();
1848                                         if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1849                                         {                                               
1850                                                 endP = cbMod->getInputPort(nameEndPort);
1851                                         } // if
1852                                 } // for
1853                         } // if endP
1854                 } // complex box
1855                 
1856                 int idCon = createGConnector(startP);
1857                 _worldState = NOTHING_HAPPENS;
1858                 GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
1859                 
1860                 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1861                 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1862                 tempp->endContourCreation();
1863                 conMod->setEndPort(endP);                                       
1864                 conView->updateStartEndPoints();                
1865         }
1866
1867         //=========================================================================
1868
1869         bool wxVtkSceneManager::boxExist(std::string boxname)
1870         {
1871                 bool ok=false;
1872                 std::map<int, GObjectController*>::iterator it;
1873                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1874                 {
1875                         GObjectController *cont = it->second;           
1876                         if(cont->getModel()->getBBTKName()==boxname)
1877                         {
1878                                 ok=true;
1879                         }
1880                 }
1881                 return ok;
1882         }
1883
1884         //=========================================================================
1885
1886         std::vector<int> wxVtkSceneManager::getBlackBoxes()
1887         {
1888                 std::vector<int> vect;
1889                 std::map<int, GObjectController*>::iterator it;
1890                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1891                 {
1892                         GObjectController *cont = it->second;           
1893                         if(cont->getGObjectType()==GBLACKBOX)
1894                         {
1895                                 vect.push_back(cont->getId());
1896                         }
1897                 }
1898                 return vect;
1899         }
1900
1901         //=========================================================================
1902
1903         std::vector<int> wxVtkSceneManager::getComplexInputPorts()
1904         {
1905                 std::vector<int> vect;
1906                 std::map<int, GObjectController*>::iterator it;
1907                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1908                 {
1909                         GObjectController *cont = it->second;           
1910                         if(cont->getGObjectType()==GCOMPLEXINPUTPORT)
1911                         {
1912                                 vect.push_back(cont->getId());
1913                         }
1914                 }
1915                 return vect;
1916         }
1917
1918         //=========================================================================
1919
1920         std::vector<int> wxVtkSceneManager::getComplexOutputPorts()
1921         {
1922                 std::vector<int> vect;
1923                 std::map<int, GObjectController*>::iterator it;
1924                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1925                 {
1926                         GObjectController *cont = it->second;           
1927                         if(cont->getGObjectType()==GCOMPLEXOUTPUTPORT)
1928                         {
1929                                 vect.push_back(cont->getId());
1930                         }
1931                 }
1932                 return vect;
1933         }
1934
1935         //=========================================================================
1936
1937         std::vector<int> wxVtkSceneManager::getConnections()
1938         {
1939                 std::vector<int> vect;
1940                 std::map<int, GObjectController*>::iterator it;
1941                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1942                 {
1943                         GObjectController *cont = it->second;           
1944                         if(cont->getGObjectType()==GCONNECTOR)
1945                         {
1946                                 vect.push_back(cont->getId());
1947                         }
1948                 }
1949                 return vect;
1950         }
1951
1952         //=========================================================================
1953         
1954         bool wxVtkSceneManager::isComplexBox()
1955         {
1956                 return _isComplexBox;
1957         }
1958
1959         //=========================================================================
1960
1961         void wxVtkSceneManager::setComplexBox(bool val)
1962         {
1963                 _isComplexBox=val;
1964         }
1965  
1966         //=========================================================================
1967
1968         int wxVtkSceneManager::addObjectController(GObjectController* objController)
1969         {
1970                 //Register the controller of the new object
1971                 registerController((InteractorStyleMaracas*) objController);
1972
1973                 //Add the object to the objects list 
1974                 int newId = _contLastId;//_controllers.size();
1975                 objController->setId(newId);
1976                 _controllers[newId] = objController;
1977                 _contLastId++;
1978                 return newId;
1979         }
1980
1981         //=========================================================================
1982
1983         int wxVtkSceneManager::getNumSelectedObjects()
1984         {
1985                 return _selectedObjects.size();
1986         }
1987
1988         //=========================================================================
1989
1990         std::map<int,GObjectController*> wxVtkSceneManager::getSelectedObjects()
1991         {
1992                 std::map<int,GObjectController*> mapSelected;
1993
1994                 std::map<int, GObjectController*>::iterator it;
1995                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1996                 {
1997                         GObjectController *cont = it->second;           
1998                         if(cont->getGObjectType()==GBLACKBOX && cont->getView()->getState()==SELECTED)
1999                         {
2000                                 mapSelected[cont->getId()]=cont;
2001                         }
2002                 }
2003
2004                 std::map<int, GObjectController*>::iterator it2;
2005                 for(it2 = _controllers.begin(); it2 != _controllers.end(); ++it2)
2006                 {
2007                         GObjectController *cont = it2->second;          
2008                         if(cont->getGObjectType()==GCONNECTOR)
2009                         {
2010                                 GConnectorModel* cmod = (GConnectorModel*)cont->getModel();
2011                                 GBoxModel* endPortParentBox = cmod->getEndPort()->getParentBox();
2012                                 GBoxModel* startPortParentBox = cmod->getStartPort()->getParentBox();
2013
2014                                 std::map<int, GObjectController*>::iterator iterOBJ1 = mapSelected.find(startPortParentBox->getObjectId());
2015                                 std::map<int, GObjectController*>::iterator iterOBJ2 = mapSelected.find(endPortParentBox->getObjectId());
2016                                 
2017                                 if(iterOBJ1 != mapSelected.end() && iterOBJ2 != mapSelected.end())
2018                                 {                                       
2019                                         int ID = cont->getId();
2020                                         mapSelected[ID]=cont;
2021                                 }
2022                                 
2023                         }
2024                 }
2025                 return mapSelected;
2026         }
2027
2028         //=========================================================================
2029
2030         void wxVtkSceneManager::addObjects(std::map<int,GObjectController*> objectsMap)
2031         {
2032                 
2033                 std::map<int,int> oldIdNewIdBoxes;
2034                 std::vector<int> connections;
2035
2036                 std::map<int, GObjectController*>::iterator it;         
2037                 for(it = objectsMap.begin(); it != objectsMap.end(); ++it)
2038                 {
2039                         GObjectController *cont = it->second;
2040                         int type = cont->getGObjectType();
2041
2042                         if(type==GBLACKBOX)
2043                         {
2044                                 // Copy black box
2045                                 double xInic, yInic,zInic;
2046                                 GBlackBoxModel* copyBox = (GBlackBoxModel*)cont->getModel();
2047                                 copyBox->getInicPoint(xInic,yInic,zInic);
2048                                 int idBox = createGBlackBox(0,0,copyBox->getBBTKPackage(),copyBox->getBBTKType());
2049
2050                                 int idcB = copyBox->getObjectId();
2051                                 oldIdNewIdBoxes[idcB]=idBox;
2052                                 cont = _controllers[idBox];                                                             
2053                                 GBlackBoxModel* newbox  = (GBlackBoxModel*)cont->getModel();
2054                                 newbox->setInicPoint(xInic,yInic,zInic);                                                        
2055                                 int num = newbox->getNumInputPorts();
2056                                 for(int j=0;j<num;j++)
2057                                 {
2058                                         newbox->setValueToInputPort(j,copyBox->getValueInputPort(j));
2059                                 }
2060                                 newbox->notifyObservers(_idManager);
2061                         }
2062                         else if(type==GCONNECTOR)
2063                         {
2064                                 int idCon = cont->getId();
2065                                 connections.push_back(idCon);
2066                         }
2067                         
2068                 }
2069                 
2070                 for(int i = 0 ;i<connections.size();i++)
2071                 {
2072                         int objId = connections[i];
2073                         GObjectController *cont = objectsMap[objId];                    
2074                         GConnectorModel* connectModel = (GConnectorModel*)cont->getModel();
2075                         
2076                         GPortModel* startPort = connectModel->getStartPort();
2077                         int startPortIndex = startPort->getPosInBox();
2078                         GPortModel* endPort = connectModel->getEndPort();
2079                         int endPortIndex = endPort->getPosInBox();                      
2080
2081                         GBlackBoxModel* startPortParentBox = (GBlackBoxModel*)startPort->getParentBox();
2082                         GBlackBoxModel* endPortParentBox = (GBlackBoxModel*)endPort->getParentBox();
2083                         
2084                         int idNewStartBox = oldIdNewIdBoxes[startPortParentBox->getObjectId()];
2085                         int idNewEndBox = oldIdNewIdBoxes[endPortParentBox->getObjectId()];
2086
2087                         GBlackBoxModel* newStartBox = (GBlackBoxModel*)_controllers[idNewStartBox]->getModel();
2088                         GBlackBoxModel* newEndBox = (GBlackBoxModel*)_controllers[idNewEndBox]->getModel();
2089
2090                         GPortModel* newStartPort = newStartBox->getOutputPort(startPortIndex);
2091                         GPortModel* newEndPort = newEndBox->getInputPort(endPortIndex);
2092
2093                         // Creates connection 
2094                         int idCon = createGConnector(newStartPort);
2095                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2096                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2097                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2098                         tempp->endContourCreation();
2099                         conMod->setEndPort(newEndPort);                                 
2100                         conView->updateStartEndPoints();
2101                 }
2102
2103                 std::map<int, int>::iterator itIds;             
2104                 for(itIds = oldIdNewIdBoxes.begin(); itIds != oldIdNewIdBoxes.end(); ++itIds)
2105                 {
2106                         int idOld = itIds->first;
2107                         int idNew = itIds->second;
2108
2109                         GBlackBoxModel* oldBox = (GBlackBoxModel*)objectsMap[idOld]->getModel();
2110                         GBlackBoxModel* newBox = (GBlackBoxModel*)_controllers[idNew]->getModel();
2111
2112                         std::vector<int> oldInputConnections = oldBox->getConnectedInputs();
2113                         std::vector<int> oldOutputConnections = oldBox->getConnectedOutputs();
2114                         std::vector<int> newInputConnections = newBox->getConnectedInputs();
2115                         std::vector<int> newOutputConnections = newBox->getConnectedOutputs();
2116
2117                         for(int k = 0; k<(int)oldInputConnections.size();k++)
2118                         {
2119                                 bool exist=false;
2120 //EED                           int toCreate=-1;
2121                                 for(int l = 0; l<(int)newInputConnections.size() && !exist;l++)
2122                                 {
2123                                         if(oldInputConnections[k]==newInputConnections[l])
2124                                         {
2125                                                 exist=true;
2126                                         }
2127                                 }
2128
2129                                 if(exist==false)
2130                                 {
2131                                         //Create complex input 
2132                                         int posInBox = oldInputConnections[k];
2133                                         GPortModel* inputPort = oldBox->getInputPort(posInBox);
2134                                         std::string inputPortName = inputPort->getBBTKName();
2135                                         int idInputPort = createGComplexBoxInputPort(inputPortName);
2136                                         GObjectController *cont = _controllers[idInputPort];
2137                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2138                                         double xIn,yIn,zIn;
2139                                         inputPort->getInicPoint(xIn,yIn,zIn);
2140                                         yIn+=20;
2141                                         cbmod->setInicPoint(xIn,yIn,zIn);
2142                                         cbmod->notifyObservers(_idManager);
2143                                                                                                                 
2144                                         GPortModel* inputPortEnd = newBox->getInputPort(posInBox);
2145
2146                                         // Creates connection 
2147                                         int idCon = createGConnector(cbmod->getOutputPort(0));
2148                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2149                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2150                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2151                                         tempp->endContourCreation();
2152                                         conMod->setEndPort(inputPortEnd);                                       
2153                                         conView->updateStartEndPoints();
2154
2155                                 }
2156                                         
2157                         }
2158                         
2159
2160                         for(int k = 0; k<(int)oldOutputConnections.size();k++)
2161                         {
2162                                 bool exist=false;
2163 //EED                           int toCreate=-1;
2164                                 for(int l = 0; l<(int)newOutputConnections.size() && !exist;l++)
2165                                 {
2166                                         if(oldOutputConnections[k]==newOutputConnections[l])
2167                                         {
2168                                                 exist=true;
2169                                         }
2170                                 }
2171
2172                                 if(exist==false)
2173                                 {
2174                                         //Create complex output 
2175                                         int posInBox = oldOutputConnections[k];
2176                                         GPortModel* outputPort = oldBox->getOutputPort(posInBox);
2177                                         std::string outputPortName = outputPort->getBBTKName();
2178                                         int idOutputPort = createGComplexBoxOutputPort(outputPortName);
2179                                         GObjectController *cont = _controllers[idOutputPort];
2180                                         GBoxModel *cbmod = (GBoxModel*)cont->getModel();
2181                                         double xIn,yIn,zIn;
2182                                         outputPort->getInicPoint(xIn,yIn,zIn);
2183                                         yIn-=20;
2184                                         cbmod->setInicPoint(xIn,yIn,zIn);
2185                                         cbmod->notifyObservers(_idManager);
2186                                                                                                                 
2187                                         GPortModel* outputPortEnd = newBox->getOutputPort(posInBox);
2188
2189                                         // Creates connection 
2190                                         int idCon = createGConnector(outputPortEnd);
2191                                         GConnectorController *tempp = (GConnectorController*)_controllers[idCon]; 
2192                                         GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
2193                                         vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
2194                                         tempp->endContourCreation();
2195                                         conMod->setEndPort(cbmod->getInputPort(0));                                     
2196                                         conView->updateStartEndPoints();
2197
2198                                 }
2199                                         
2200                         }
2201
2202                 }
2203
2204
2205         }
2206
2207         //=========================================================================     
2208
2209 }  // EO namespace bbtk
2210
2211 // EOF
2212