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