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