]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
7419d39cdb0bd93c334ab3c3297d0242e703ffc2
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / wxVtkSceneManager.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::wxVtkSceneManager . 
34 */
35
36
37 #include "wxVtkSceneManager.h"
38
39 namespace bbtk
40 {
41
42
43         //=========================================================================
44         wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
45         {
46                 _parent = parent;
47                 _numBoxes=0;
48                 _idManager=idManager;
49                 _baseView=baseView;
50                 _idLastController=0;
51                 _startDragging=false;
52
53                 if( _baseView!=NULL )
54                 {
55                         _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);                      
56                         configureBaseView();
57                         _worldState=NOTHING_HAPPENS;
58                         registerController(this);
59                         
60                 }
61
62         }
63
64         //=========================================================================
65
66         void wxVtkSceneManager::disconnectDrop()
67         {
68                 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
69         }
70
71         //=========================================================================
72
73         wxVtkSceneManager::~wxVtkSceneManager()
74         {
75         }
76
77         //=========================================================================
78
79         void wxVtkSceneManager::configureBaseView()
80         {
81                 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
82
83                 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
84
85                 // Important to activate the 2D interaction system
86                 wxVTKRenderWindowInteractor *iren = _baseView->GetWxVTKRenderWindowInteractor();
87                 interactorstylebaseview->SetInteractor ( iren );
88                 iren->SetInteractorStyle(interactorstylebaseview);
89                 interactorstylebaseview->SetwxVtkBaseView(_baseView);
90                 
91                 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92                 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
93                 
94                 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95                 _baseView->GetRenderer()->GradientBackgroundOff();
96                 _baseView->Refresh();
97         }
98
99         //=========================================================================
100
101         void wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
102         {
103
104                 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
105                 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
106
107                 int type = GBLACKBOX;
108
109                 //Create the MVC Objects
110                 
111                 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
112                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
113                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
114                 
115                 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
116                 
117                 //Prepares the initial model
118                 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height  
119
120                 double xx = x;
121                 double yy = windowHeight-y;
122                 
123                 //z value is not important yet, because it is only used a parallel projection
124                 double zz = 900;
125
126                 _baseView->TransCoordScreenToWorld(xx,yy,zz);
127                 model->setInicPoint(xx,yy,zz);
128                 
129                 _numBoxes++;
130                 std::stringstream stream;
131                 
132                 if(_numBoxes<10)
133                 {
134                         stream << "Box0" << _numBoxes;
135                 }
136                 else
137                 {
138                         stream << "Box" << _numBoxes;
139                 }
140                 std::string arraystring = stream.str();
141
142                 model->setBBTKName(arraystring);
143                 model->setBBTKType(boxType);
144                 model->setBBTKPackage(packageName);
145                                 
146                 model->addObserver(view);
147                 model->addObserver(this);
148
149                 //Iterate and create the input ports
150                 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
151                 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
152
153                 int i=0;
154                 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
155                 {
156                         BlackBoxInputDescriptor *desc = itInput->second;
157                         createGInputPort(GINPUTPORT,i,model,desc);
158                         i++;
159                 }
160
161                 //Iterate and create the output ports
162                 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
163                 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
164
165                 i=0;
166                 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
167                 {
168                         BlackBoxOutputDescriptor *desc = itOutput->second;
169                         createGOutputPort(GOUTPUTPORT,i,model,desc);
170                         i++;
171                 }
172
173
174                 //Associates the view with the correspondent renderer and the  model.
175                 //(NOTE: Refresh is only made by the view)
176                 view->setModel(model);
177                 view->setBaseView(_baseView);
178                 view->initVtkObjects();
179                 
180                 //Associates the controller with the correspondent model and view
181                 controller->setModelAndView(model,view);
182
183                 //Resgiter change to the observers of the actual model
184                 model->notifyObservers(_idManager);
185                 
186                 //Register the controller of the new object
187                 registerController((InteractorStyleMaracas*) controller);
188
189                 //Add the object to the objects list 
190                 int newId = _controllers.size();
191                 controller->setId(newId);
192                 _controllers[newId] = controller;
193
194         }
195
196         //=========================================================================
197
198         void wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxInputDescriptor *desc)
199         {
200                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
201                 blackBox->addInputPort((GPortModel*)portController->getModel());
202         }
203
204         //=========================================================================
205
206         void wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBlackBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
207         {
208                 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
209                 blackBox->addOutputPort((GPortModel*)portController->getModel());
210         }
211
212         //=========================================================================
213
214         GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBlackBoxModel *blackBox)
215         {
216                 int type = GPORT;
217
218                 //Create the MVC Objects
219                 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
220                 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
221                 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
222
223                 model->registerInBox(blackBox,portType, posInBox);
224                 blackBox->addOutputPort(model);
225                 
226                 model->setBBTKType(bbtkType);
227                 model->setBBTKName(bbtkName);
228
229                 model->addObserver(view);
230                 model->addObserver(this);
231
232                 //Associates the view with the correspondent renderer and the  model.
233                 //(NOTE: Refresh is only made by the view)
234                 view->setModel(model);
235                 view->setBaseView(_baseView);
236                 view->initVtkObjects();
237                 
238                 //Associates the controller with the correspondent model and view
239                 controller->setModelAndView(model,view);
240
241                 model->notifyObservers(_idManager);
242
243                 //Register the controller of the new object
244                 registerController((InteractorStyleMaracas*) controller);
245                 
246                 int newId = _controllers.size();
247                 controller->setId(newId);
248                 _controllers[newId] = controller;
249
250                 return (GPortController*)controller;
251         }
252
253         //=========================================================================
254
255         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
256         {
257                 int type = GCONNECTOR;
258
259                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
260                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
261                 manualContourModel* manContourModel     = new manualContourModel();
262
263                 GConnectorController* connectorcontroller = new GConnectorController();                         
264                 GConnectorModel* connectorModel = new GConnectorModel();
265                 GConnectorView* connectorView = new GConnectorView();
266                 connectorModel->setGObjectType(type);
267
268                 manContourModel->SetCloseContour(false);
269                 connectorModel->setStartPort(startPort);
270
271                 manContourView->SetModel( manContourModel );
272                 manContourView->SetWxVtkBaseView( _baseView );
273                 manContourView->SetRange( 0.5 );
274                 manContourView->SetZ( 900 );
275
276                 manContourView->SetColorNormalContour(0, 0, 1);
277                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
278                 manContourView->SetColorSelectContour(1, 0.8, 0);
279                 manContourView->SetWidthLine(1);
280
281                 manContourControl->SetModelView( manContourModel , manContourView );
282                 
283                 manContourControl->CreateNewManualContour();
284
285                 manContourView->RefreshContour();
286
287
288                 double x,y,z;
289                 connectorModel->getInicPoint(x,y,z);
290
291                 manContourControl->SetState(1);
292                 manContourModel->SetCloseContour(false);
293                 
294                 manContourModel->AddPoint(x,y,z);
295                 manContourView->AddPoint();
296
297                 manContourModel->AddPoint(x,y,z);
298                 manContourView->AddPoint();
299
300                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
301                 manContourControl->_bakIdPoint=bak;
302                 manContourView->Refresh();
303
304                 manContourControl->SetMoving( false );
305
306                 registerController((InteractorStyleMaracas*) connectorcontroller);
307
308                 connectorcontroller->setManualContourController(manContourControl);             
309                 connectorModel->setManualContourModel(manContourModel);
310                 connectorView->setManualContourView(manContourView);
311                 connectorView->setModel(connectorModel);
312                 connectorcontroller->setModelAndView(connectorModel,connectorView);
313
314                 int newId = _controllers.size();
315                 connectorcontroller->setId(newId);
316                 _controllers[newId] = connectorcontroller;
317
318         }
319
320         //=========================================================================
321
322         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
323         {
324                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
325                 baseViewControlManager->AddInteractorStyleMaracas( param );
326         }
327
328         //=========================================================================
329
330         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
331         {
332                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
333                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
334         }
335
336         //=========================================================================
337
338         vtkRenderer* wxVtkSceneManager::getRenderer()
339         {
340                 return _baseView->GetRenderer();
341         }
342
343         //=========================================================================
344
345         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
346         {
347                 return _baseView->GetRenWin();
348         }
349         //=========================================================================
350
351                 
352         void wxVtkSceneManager::update(int idController,int command)
353         {
354                 if(command != NO_COMMAND)
355                 {
356                         if(command == ADD_TO_SELECTED)
357                         {
358                                 GObjectController* cont = _controllers[idController];
359
360                                 bool foundID=false;
361                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
362                                 {
363                                         int id = _selectedObjects[i];
364                                         if(id==idController)
365                                         {
366                                                 foundID = true;
367                                         }
368                                 }
369                                 if(!foundID)
370                                 {
371                                         int id = idController;
372                                         _selectedObjects.push_back(id);
373                                 }
374
375                         }
376                         else if(command == INIT_CREATION_CONTOUR)
377                         {       
378                                 _worldState = INIT_CREATION_CONTOUR;
379                                 GObjectController* cont = _controllers[idController];
380                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
381                                 
382
383                                 // The last one is the controller of the connector
384                                 for(int i=0;i<_controllers.size();i++)
385                                 {
386                                         GObjectController* cont = _controllers[i];
387                                         if(cont->getGObjectType() == GPORT )
388                                         {
389                                                 GPortModel* port = (GPortModel*)cont->getModel();
390                                                 if(port->getPortType()==GINPUTPORT)
391                                                 {
392                                                         cont->SetActive(true);
393                                                 }
394                                                 else
395                                                 {
396                                                         cont->getView()->setState(NOTHING_HAPPENS);
397                                                         cont->getModel()->notifyObservers(_idManager);
398                                                         cont->SetActive(false);
399                                                 }
400                                         }
401                                         else
402                                         {
403                                                 cont->getView()->setState(NOTHING_HAPPENS);
404                                                 cont->getModel()->notifyObservers(_idManager);
405                                                 cont->SetActive(false);
406                                         }                               
407                                 }
408
409                                 _selectedObjects.clear();
410
411                                 createGConnector(startOutputPort);
412
413                         }
414                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
415                         {                               
416                                 _worldState = NOTHING_HAPPENS;
417                                 int id = _controllers.size()-1;
418                                 GObjectController* cont = _controllers[id];                     
419                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
420
421                                 GObjectController* finPort = _controllers[idController];
422                                 if(finPort->getGObjectType() == GPORT)
423                                 {
424                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
425                                         modelContour->setEndPort(modelPort);
426                                 }                       
427
428                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
429                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
430                                 connView->Refresh();
431
432                                 for(int i=0;i<_controllers.size();i++)
433                                 {
434                                         GObjectController* cont = _controllers[i];
435                                         if(cont->getView()!=NULL)
436                                         {
437                                                 cont->getView()->setState(NOTHING_HAPPENS);
438                                                 cont->getModel()->notifyObservers(_idManager);
439                                         }
440                                         cont->SetActive(true);                                                          
441                                 }
442                         }
443                         
444                 }
445         }
446
447         //=========================================================================
448
449         bool wxVtkSceneManager::OnMouseMove()
450         {
451                 int X,Y;
452                 wxVTKRenderWindowInteractor *wxVTKiren;
453                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
454                 wxVTKiren->GetEventPosition(X,Y);
455         
456                 
457                 if(_worldState == DRAG_OBJECTS)
458                 {                       
459                         for (int i=0; i<_selectedObjects.size(); i++)
460                         {
461                                 int id = _selectedObjects[i];
462                                 GObjectController* cont = _controllers[id];
463                                 if(_startDragging)
464                                 {
465                                         cont->getView()->setStartDragging(true);
466                                 }
467                                 cont->getView()->moveObject(X,Y);
468                                 cont->getView()->setState(DRAG);
469                                 cont->getModel()->notifyObservers(_idManager);
470                         }
471
472                         std::map<int, GObjectController*>::iterator it;
473
474                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
475                         {
476                                 GObjectController *desc = it->second;
477                                 if(desc->getGObjectType()==GCONNECTOR)
478                                 {
479                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
480                                         vconn->updateStartEndPoints();
481                                 }
482                         }
483                         
484                         _startDragging=false;
485
486                 }
487                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
488                 {
489                         std::map<int, GObjectController*>::iterator it;
490
491                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
492                         {
493                                 GObjectController *desc = it->second;
494                                 int type = desc->getGObjectType();
495                                 int state = desc->getView()->getState();
496                                 
497                                 if(state == HIGHLIGHTED){
498
499                                         updateStatusBar(desc->getStatusText());
500                                         if(type==GBLACKBOX)
501                                         {
502                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
503                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
504                                         }
505                                 }
506                         }
507                 }
508                 
509                 
510
511
512                 return true;
513         }
514
515         //=========================================================================
516         
517         bool wxVtkSceneManager::OnLeftButtonDown()
518         {
519                 
520                 if(_selectedObjects.size()!=0)
521                 {
522                         _worldState = DRAG_OBJECTS;
523                         _startDragging = true;
524
525                         for (int i = 0; i < _selectedObjects.size(); i++)
526                         {
527                                 int id = _selectedObjects[i];
528                                 GObjectController* cont = _controllers[id];
529                                 cont->getView()->setState(DRAG);
530                                 cont->getModel()->notifyObservers(_idManager);
531                         }
532                 }
533                 
534                 
535                 
536                 return true;
537         }
538
539         //=========================================================================
540         
541         bool wxVtkSceneManager::OnLeftButtonUp()
542         {
543                 if(_worldState == DRAG_OBJECTS)
544                 {
545                         _worldState = NOTHING_HAPPENS;
546
547                         for (int i = 0; i < _selectedObjects.size(); i++)
548                         {
549                                 int id = _selectedObjects[i];
550                                 GObjectController* cont = _controllers[id];
551                                 cont->getView()->setState(SELECTED);
552                                 cont->getModel()->notifyObservers(_idManager);
553                         }
554                 }
555                 return true;
556         }
557
558         //=========================================================================
559
560         bool wxVtkSceneManager::OnRightButtonUp()
561         {
562                 for (int i = 0; i < _selectedObjects.size(); i++)
563                 {
564                         int id = _selectedObjects[i];
565                         GObjectController* cont = _controllers[id];
566                         cont->SetActive(true);
567                         cont->getView()->setState(NOTHING_HAPPENS);
568                         cont->getModel()->notifyObservers(_idManager);
569                 }
570
571                 _selectedObjects.clear();
572
573                 return true;
574         }
575
576         //=========================================================================
577
578         bool wxVtkSceneManager::OnLeftDClick()
579         {
580                 int X,Y;
581                 wxVTKRenderWindowInteractor *wxVTKiren;
582                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
583                 wxVTKiren->GetEventPosition(X,Y);
584
585                 std::map<int, GObjectController*>::iterator it;
586
587                 bool clickOnObject = false;
588
589                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
590                 {
591                         GObjectController *cont = it->second;
592                         int type = cont->getGObjectType();
593                         
594                         if(cont->getView()->isPointInside(X,Y))
595                         {
596                                 if(type==GBLACKBOX)
597                                 {
598                                         for (int i=0; i<_selectedObjects.size(); i++)
599                                         {
600                                                 int id = _selectedObjects[i];
601                                                 GObjectController* control = _controllers[id];
602                                                 control->getView()->setState(NOTHING_HAPPENS);
603                                         }
604                                         _selectedObjects.clear();
605
606                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
607                                         _parent->editBlackBox(bbmodel);
608                                 }
609                                 clickOnObject = true;                   
610                         }
611                 }
612
613                 if(clickOnObject==false)
614                 {
615                         _parent->editDiagramParameters(this);
616                 }
617
618                 return true;
619         }
620         
621         //=========================================================================
622
623         bool wxVtkSceneManager::OnChar()
624         {       
625                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
626                 
627                 // KeyCode 127 : Delete Key
628                 // KeyCode 8 : Backspace Key
629                 if(keyCode == 8 || keyCode == 127)
630                 {
631                         if(_selectedObjects.size()>0)
632                         {
633                                 for(int i=0;i<_selectedObjects.size();i++)
634                                 {
635                                         int id = _selectedObjects[i];
636                                         removeObject(id);
637                                 }
638                                 _selectedObjects.clear();
639                         }
640                 }
641
642                 return true;
643         }
644
645         //=========================================================================
646
647         void wxVtkSceneManager::removeObject(int id)
648         {
649                 GObjectController *control = _controllers[id];
650                 std::vector<int> controllersToRemove;
651
652                 if(control->getGObjectType()==GBLACKBOX)
653                 {
654                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
655                         std::vector<GPortModel*>inputs = bbmod->getInputPorts();
656                         
657                         for(int i = 0;i<inputs.size();i++)
658                         {
659                                 controllersToRemove.push_back(inputs[i]->getObjectId());
660                         }
661
662                         std::vector<GPortModel*>outputs = bbmod->getOutputPorts();
663
664                         for(int i = 0;i<outputs.size();i++)
665                         {
666                                 controllersToRemove.push_back(outputs[i]->getObjectId());
667                         }
668
669                         controllersToRemove.push_back(control->getId());
670                 }
671
672                 for(int i = 0;i<controllersToRemove.size();i++)
673                 {
674                         int id = controllersToRemove[i];
675                         GObjectController *cont = _controllers[id];
676                         cont->removeFromScene();
677                         unregisterController((InteractorStyleMaracas*)cont);
678                         _controllers.erase(id);
679                 }
680
681
682         }
683
684         //=========================================================================
685
686         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
687         {
688                 _parent->displayBlackBoxInfo(packageName,boxName);
689         }
690
691         //=========================================================================
692
693         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
694         {
695                 _parent->updateStatusBar(textStatus);
696         }
697
698         //=========================================================================
699
700         std::string wxVtkSceneManager::getDiagramScript()
701         {
702                 bool existsExec=false;
703
704                 std::vector<std::string> packages;
705                 std::vector<int> boxes;
706                 std::vector<int> connections;
707                 std::vector<int> execBoxes;
708
709                 std::map<int, GObjectController*>::iterator it;
710
711                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
712                 {
713                         GObjectController *desc = it->second;
714                         int type = desc->getGObjectType();
715
716                         if(type==GBLACKBOX)
717                         {
718                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
719                                 
720                                 std::string pkg = mod->getBBTKPackage();
721                                 bool existsPkg = false;
722                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
723                                 {
724                                         if(packages[t]==pkg)
725                                         {
726                                                 existsPkg=true;
727                                         }
728                                 }
729                                 if(!existsPkg)
730                                 {
731                                         packages.push_back(pkg);
732                                 }
733
734
735                                 boxes.push_back(it->first);
736                                 if(mod->isExecutable())
737                                 {
738                                         execBoxes.push_back(it->first);
739                                         existsExec=true;
740                                 }
741                         }
742                         else if(type==GCONNECTOR)
743                         {
744                                 connections.push_back(it->first);
745                         }
746                 }
747
748                 std::string script = "";
749                 script+="# BBTK GEditor Script\n";
750                 script+="# ----------------------\n";
751                 if(existsExec)
752                 {
753                         int i;
754                         for(i = 0; i<packages.size();i++)
755                         {
756                                 script+="load ";
757                                 script+=packages[i];
758                                 script+="\n";
759                         }
760
761                         script+="load std\n";
762
763                         for(i = 0; i<boxes.size();i++)
764                         {
765                                 script+="new ";
766                                 int id = boxes[i];
767                                 GObjectController *control = _controllers[id];
768                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
769
770                                 script+=model->getBBTKType();
771                                 script+=" ";
772                                 script+=model->getBBTKName();
773                                 script+="\n";
774
775                                 std::vector<GPortModel*> inputs = model->getInputPorts();
776                                 for(int j = 0; j<inputs.size();j++)
777                                 {
778                                         GPortModel* inputPort = inputs[j];
779                                         if(inputPort->isValueSet())
780                                         {
781                                                 script+="set ";
782                                                 script+=model->getBBTKName();
783                                                 script+=".";
784                                                 script+=inputPort->getBBTKName();
785                                                 script+=" ";
786                                                 script+=inputPort->getValue();
787                                                 script+="\n";
788                                         }
789                                 }
790
791                         }
792
793                         for(i = 0; i<connections.size();i++)
794                         {
795                                 script+="connect ";
796                                 int id = connections[i];
797                                 GObjectController *control = _controllers[id];
798                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
799
800                                 //Start Connection info
801                                 GPortModel *start = model->getStartPort();
802                                 script+=start->getParentBox()->getBBTKName();
803                                 script+=".";
804                                 script+=start->getBBTKName();
805
806                                 script+=" ";
807
808                                 //End Connection info
809                                 GPortModel *end = model->getEndPort();
810                                 script+=end->getParentBox()->getBBTKName();
811                                 script+=".";
812                                 script+=end->getBBTKName();
813
814                                 script+="\n";
815                         }
816
817                         for(i = 0; i<execBoxes.size();i++)
818                         {
819                                 script+="exec ";
820                                 int id = execBoxes[i];
821                                 GObjectController *control = _controllers[id];
822                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
823
824                                 script+=model->getBBTKName();
825                                 script+="\n";
826                         }
827
828                 }
829
830                 return script;
831         }
832
833         //=========================================================================
834
835 }  // EO namespace bbtk
836
837 // EOF
838