]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
A little bug to delete the boxes (only boxes) was fixed
[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                 
225                 model->setBBTKType(bbtkType);
226                 model->setBBTKName(bbtkName);
227
228                 model->addObserver(view);
229                 model->addObserver(this);
230
231                 //Associates the view with the correspondent renderer and the  model.
232                 //(NOTE: Refresh is only made by the view)
233                 view->setModel(model);
234                 view->setBaseView(_baseView);
235                 view->initVtkObjects();
236                 
237                 //Associates the controller with the correspondent model and view
238                 controller->setModelAndView(model,view);
239
240                 model->notifyObservers(_idManager);
241
242                 //Register the controller of the new object
243                 registerController((InteractorStyleMaracas*) controller);
244                 
245                 int newId = _controllers.size();
246                 controller->setId(newId);
247                 _controllers[newId] = controller;
248
249                 return (GPortController*)controller;
250         }
251
252         //=========================================================================
253
254         void wxVtkSceneManager::createGConnector(GPortModel* startPort)
255         {
256                 int type = GCONNECTOR;
257
258                 manualConnectorContourController* manContourControl     = new manualConnectorContourController();
259                 manualConnectorContourView* manContourView      = new manualConnectorContourView();
260                 manualContourModel* manContourModel     = new manualContourModel();
261
262                 GConnectorController* connectorcontroller = new GConnectorController();                         
263                 GConnectorModel* connectorModel = new GConnectorModel();
264                 GConnectorView* connectorView = new GConnectorView();
265                 connectorModel->setGObjectType(type);
266
267                 manContourModel->SetCloseContour(false);
268                 connectorModel->setStartPort(startPort);
269
270                 manContourView->SetModel( manContourModel );
271                 manContourView->SetWxVtkBaseView( _baseView );
272                 manContourView->SetRange( 0.5 );
273                 manContourView->SetZ( 900 );
274
275                 manContourView->SetColorNormalContour(0, 0, 1);
276                 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
277                 manContourView->SetColorSelectContour(1, 0.8, 0);
278                 manContourView->SetWidthLine(1);
279
280                 manContourControl->SetModelView( manContourModel , manContourView );
281                 
282                 manContourControl->CreateNewManualContour();
283
284                 manContourView->RefreshContour();
285
286
287                 double x,y,z;
288                 connectorModel->getInicPoint(x,y,z);
289
290                 manContourControl->SetState(1);
291                 manContourModel->SetCloseContour(false);
292                 
293                 manContourModel->AddPoint(x,y,z);
294                 manContourView->AddPoint();
295
296                 manContourModel->AddPoint(x,y,z);
297                 manContourView->AddPoint();
298
299                 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
300                 manContourControl->_bakIdPoint=bak;
301                 manContourView->Refresh();
302
303                 manContourControl->SetMoving( false );
304
305                 registerController((InteractorStyleMaracas*) connectorcontroller);
306
307                 connectorcontroller->setManualContourController(manContourControl);             
308                 connectorModel->setManualContourModel(manContourModel);
309                 connectorView->setManualContourView(manContourView);
310                 connectorView->setModel(connectorModel);
311                 connectorcontroller->setModelAndView(connectorModel,connectorView);
312
313                 int newId = _controllers.size();
314                 connectorcontroller->setId(newId);
315                 _controllers[newId] = connectorcontroller;
316
317         }
318
319         //=========================================================================
320
321         void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
322         {
323                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
324                 baseViewControlManager->AddInteractorStyleMaracas( param );
325         }
326
327         //=========================================================================
328
329         void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
330         {
331                 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
332                 baseViewControlManager->RemoveInteractorStyleMaracas( param );
333         }
334
335         //=========================================================================
336
337         vtkRenderer* wxVtkSceneManager::getRenderer()
338         {
339                 return _baseView->GetRenderer();
340         }
341
342         //=========================================================================
343
344         vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
345         {
346                 return _baseView->GetRenWin();
347         }
348         //=========================================================================
349
350                 
351         void wxVtkSceneManager::update(int idController,int command)
352         {
353                 if(command != NO_COMMAND)
354                 {
355                         if(command == ADD_TO_SELECTED)
356                         {
357                                 GObjectController* cont = _controllers[idController];
358
359                                 bool foundID=false;
360                                 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
361                                 {
362                                         int id = _selectedObjects[i];
363                                         if(id==idController)
364                                         {
365                                                 foundID = true;
366                                         }
367                                 }
368                                 if(!foundID)
369                                 {
370                                         int id = idController;
371                                         _selectedObjects.push_back(id);
372                                 }
373
374                         }
375                         else if(command == INIT_CREATION_CONTOUR)
376                         {       
377                                 _worldState = INIT_CREATION_CONTOUR;
378                                 GObjectController* cont = _controllers[idController];
379                                 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
380                                 
381
382                                 // The last one is the controller of the connector
383                                 for(int i=0;i<_controllers.size();i++)
384                                 {
385                                         GObjectController* cont = _controllers[i];
386                                         if(cont->getGObjectType() == GPORT )
387                                         {
388                                                 GPortModel* port = (GPortModel*)cont->getModel();
389                                                 if(port->getPortType()==GINPUTPORT)
390                                                 {
391                                                         cont->SetActive(true);
392                                                 }
393                                                 else
394                                                 {
395                                                         cont->getView()->setState(NOTHING_HAPPENS);
396                                                         cont->getModel()->notifyObservers(_idManager);
397                                                         cont->SetActive(false);
398                                                 }
399                                         }
400                                         else
401                                         {
402                                                 cont->getView()->setState(NOTHING_HAPPENS);
403                                                 cont->getModel()->notifyObservers(_idManager);
404                                                 cont->SetActive(false);
405                                         }                               
406                                 }
407
408                                 _selectedObjects.clear();
409
410                                 createGConnector(startOutputPort);
411
412                         }
413                         else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
414                         {                               
415                                 _worldState = NOTHING_HAPPENS;
416                                 int id = _controllers.size()-1;
417                                 GObjectController* cont = _controllers[id];                     
418                                 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
419
420                                 GObjectController* finPort = _controllers[idController];
421                                 if(finPort->getGObjectType() == GPORT)
422                                 {
423                                         GPortModel* modelPort = (GPortModel*)finPort->getModel();
424                                         modelContour->setEndPort(modelPort);
425                                 }                       
426
427                                 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();                        
428                                 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
429                                 connView->Refresh();
430
431                                 for(int i=0;i<_controllers.size();i++)
432                                 {
433                                         GObjectController* cont = _controllers[i];
434                                         if(cont->getView()!=NULL)
435                                         {
436                                                 cont->getView()->setState(NOTHING_HAPPENS);
437                                                 cont->getModel()->notifyObservers(_idManager);
438                                         }
439                                         cont->SetActive(true);                                                          
440                                 }
441                         }
442                         
443                 }
444         }
445
446         //=========================================================================
447
448         bool wxVtkSceneManager::OnMouseMove()
449         {
450                 int X,Y;
451                 wxVTKRenderWindowInteractor *wxVTKiren;
452                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
453                 wxVTKiren->GetEventPosition(X,Y);
454         
455                 
456                 if(_worldState == DRAG_OBJECTS)
457                 {                       
458                         for (int i=0; i<_selectedObjects.size(); i++)
459                         {
460                                 int id = _selectedObjects[i];
461                                 GObjectController* cont = _controllers[id];
462                                 if(_startDragging)
463                                 {
464                                         cont->getView()->setStartDragging(true);
465                                 }
466                                 cont->getView()->moveObject(X,Y);
467                                 cont->getView()->setState(DRAG);
468                                 cont->getModel()->notifyObservers(_idManager);
469                         }
470
471                         std::map<int, GObjectController*>::iterator it;
472
473                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
474                         {
475                                 GObjectController *desc = it->second;
476                                 if(desc->getGObjectType()==GCONNECTOR)
477                                 {
478                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
479                                         vconn->updateStartEndPoints();
480                                 }
481                         }
482                         
483                         _startDragging=false;
484
485                 }
486                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
487                 {
488                         std::map<int, GObjectController*>::iterator it;
489
490                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
491                         {
492                                 GObjectController *desc = it->second;
493                                 int type = desc->getGObjectType();
494                                 int state = desc->getView()->getState();
495                                 
496                                 if(state == HIGHLIGHTED){
497
498                                         updateStatusBar(desc->getStatusText());
499                                         if(type==GBLACKBOX)
500                                         {
501                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
502                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
503                                         }
504                                 }
505                         }
506                 }
507                 
508                 
509
510
511                 return true;
512         }
513
514         //=========================================================================
515         
516         bool wxVtkSceneManager::OnLeftButtonDown()
517         {
518                 
519                 if(_selectedObjects.size()!=0)
520                 {
521                         _worldState = DRAG_OBJECTS;
522                         _startDragging = true;
523
524                         for (int i = 0; i < _selectedObjects.size(); i++)
525                         {
526                                 int id = _selectedObjects[i];
527                                 GObjectController* cont = _controllers[id];
528                                 cont->getView()->setState(DRAG);
529                                 cont->getModel()->notifyObservers(_idManager);
530                         }
531                 }
532                 
533                 
534                 
535                 return true;
536         }
537
538         //=========================================================================
539         
540         bool wxVtkSceneManager::OnLeftButtonUp()
541         {
542                 if(_worldState == DRAG_OBJECTS)
543                 {
544                         _worldState = NOTHING_HAPPENS;
545
546                         for (int i = 0; i < _selectedObjects.size(); i++)
547                         {
548                                 int id = _selectedObjects[i];
549                                 GObjectController* cont = _controllers[id];
550                                 cont->getView()->setState(SELECTED);
551                                 cont->getModel()->notifyObservers(_idManager);
552                         }
553                 }
554                 return true;
555         }
556
557         //=========================================================================
558
559         bool wxVtkSceneManager::OnRightButtonUp()
560         {
561                 for (int i = 0; i < _selectedObjects.size(); i++)
562                 {
563                         int id = _selectedObjects[i];
564                         GObjectController* cont = _controllers[id];
565                         cont->SetActive(true);
566                         cont->getView()->setState(NOTHING_HAPPENS);
567                         cont->getModel()->notifyObservers(_idManager);
568                 }
569
570                 _selectedObjects.clear();
571
572                 return true;
573         }
574
575         //=========================================================================
576
577         bool wxVtkSceneManager::OnLeftDClick()
578         {
579                 int X,Y;
580                 wxVTKRenderWindowInteractor *wxVTKiren;
581                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
582                 wxVTKiren->GetEventPosition(X,Y);
583
584                 std::map<int, GObjectController*>::iterator it;
585
586                 bool clickOnObject = false;
587
588                 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
589                 {
590                         GObjectController *cont = it->second;
591                         int type = cont->getGObjectType();
592                         
593                         if(cont->getView()->isPointInside(X,Y))
594                         {
595                                 if(type==GBLACKBOX)
596                                 {
597                                         for (int i=0; i<_selectedObjects.size(); i++)
598                                         {
599                                                 int id = _selectedObjects[i];
600                                                 GObjectController* control = _controllers[id];
601                                                 control->getView()->setState(NOTHING_HAPPENS);
602                                         }
603                                         _selectedObjects.clear();
604
605                                         GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
606                                         _parent->editBlackBox(bbmodel);
607                                 }
608                                 clickOnObject = true;                   
609                         }
610                 }
611
612                 if(clickOnObject==false)
613                 {
614                         _parent->editDiagramParameters(this);
615                 }
616
617                 return true;
618         }
619         
620         //=========================================================================
621
622         bool wxVtkSceneManager::OnChar()
623         {       
624                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
625                 
626                 // KeyCode 127 : Delete Key
627                 // KeyCode 8 : Backspace Key
628                 if(keyCode == 8 || keyCode == 127)
629                 {
630                         if(_selectedObjects.size()>0)
631                         {
632                                 for(int i=0;i<_selectedObjects.size();i++)
633                                 {
634                                         int id = _selectedObjects[i];
635                                         removeObject(id);
636                                 }
637                                 _selectedObjects.clear();
638                         }
639                 }
640
641                 return true;
642         }
643
644         //=========================================================================
645
646         void wxVtkSceneManager::removeObject(int id)
647         {
648                 GObjectController *control = _controllers[id];
649                 std::vector<int> controllersToRemove;
650
651                 if(control->getGObjectType()==GBLACKBOX)
652                 {
653                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
654                         std::vector<GPortModel*> inputs = bbmod->getInputPorts();
655                         
656                         for(int i = 0;i<inputs.size();i++)
657                         {
658                                 controllersToRemove.push_back(inputs[i]->getObjectId());                                
659                         }
660
661                         std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
662
663                         for(int i = 0;i<outputs.size();i++)
664                         {
665                                 controllersToRemove.push_back(outputs[i]->getObjectId());
666                         }
667
668                         controllersToRemove.push_back(bbmod->getObjectId());
669                 }
670
671                 for(int i = 0;i<controllersToRemove.size();i++)
672                 {
673                         int id = controllersToRemove[i];
674                         GObjectController *cont = _controllers[id];                     
675                         cont->removeFromScene();
676                         unregisterController((InteractorStyleMaracas*)cont);                    
677                         _controllers.erase(id);
678                 }
679
680
681         }
682
683         //=========================================================================
684
685         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
686         {
687                 _parent->displayBlackBoxInfo(packageName,boxName);
688         }
689
690         //=========================================================================
691
692         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
693         {
694                 _parent->updateStatusBar(textStatus);
695         }
696
697         //=========================================================================
698
699         std::string wxVtkSceneManager::getDiagramScript()
700         {
701                 bool existsExec=false;
702
703                 std::vector<std::string> packages;
704                 std::vector<int> boxes;
705                 std::vector<int> connections;
706                 std::vector<int> execBoxes;
707
708                 std::map<int, GObjectController*>::iterator it;
709
710                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
711                 {
712                         GObjectController *desc = it->second;
713                         int type = desc->getGObjectType();
714
715                         if(type==GBLACKBOX)
716                         {
717                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
718                                 
719                                 std::string pkg = mod->getBBTKPackage();
720                                 bool existsPkg = false;
721                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
722                                 {
723                                         if(packages[t]==pkg)
724                                         {
725                                                 existsPkg=true;
726                                         }
727                                 }
728                                 if(!existsPkg)
729                                 {
730                                         packages.push_back(pkg);
731                                 }
732
733
734                                 boxes.push_back(it->first);
735                                 if(mod->isExecutable())
736                                 {
737                                         execBoxes.push_back(it->first);
738                                         existsExec=true;
739                                 }
740                         }
741                         else if(type==GCONNECTOR)
742                         {
743                                 connections.push_back(it->first);
744                         }
745                 }
746
747                 std::string script = "";
748                 script+="# BBTK GEditor Script\n";
749                 script+="# ----------------------\n";
750                 if(existsExec)
751                 {
752                         int i;
753                         for(i = 0; i<packages.size();i++)
754                         {
755                                 script+="include ";
756                                 script+=packages[i];
757                                 script+="\n";
758                         }
759
760                         script+="include std\n";
761
762                         for(i = 0; i<boxes.size();i++)
763                         {
764                                 script+="new ";
765                                 int id = boxes[i];
766                                 GObjectController *control = _controllers[id];
767                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
768
769                                 script+=model->getBBTKType();
770                                 script+=" ";
771                                 script+=model->getBBTKName();
772                                 script+="\n";
773
774                                 std::vector<GPortModel*> inputs = model->getInputPorts();
775                                 for(int j = 0; j<inputs.size();j++)
776                                 {
777                                         GPortModel* inputPort = inputs[j];
778                                         if(inputPort->isValueSet())
779                                         {
780                                                 script+="set ";
781                                                 script+=model->getBBTKName();
782                                                 script+=".";
783                                                 script+=inputPort->getBBTKName();
784                                                 script+=" ";
785                                                 script+=inputPort->getValue();
786                                                 script+="\n";
787                                         }
788                                 }
789
790                         }
791
792                         for(i = 0; i<connections.size();i++)
793                         {
794                                 script+="connect ";
795                                 int id = connections[i];
796                                 GObjectController *control = _controllers[id];
797                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
798
799                                 //Start Connection info
800                                 GPortModel *start = model->getStartPort();
801                                 script+=start->getParentBox()->getBBTKName();
802                                 script+=".";
803                                 script+=start->getBBTKName();
804
805                                 script+=" ";
806
807                                 //End Connection info
808                                 GPortModel *end = model->getEndPort();
809                                 script+=end->getParentBox()->getBBTKName();
810                                 script+=".";
811                                 script+=end->getBBTKName();
812
813                                 script+="\n";
814                         }
815
816                         for(i = 0; i<execBoxes.size();i++)
817                         {
818                                 script+="exec ";
819                                 int id = execBoxes[i];
820                                 GObjectController *control = _controllers[id];
821                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
822
823                                 script+=model->getBBTKName();
824                                 script+="\n";
825                         }
826
827                 }
828
829                 return script;
830         }
831
832         //=========================================================================
833
834 }  // EO namespace bbtk
835
836 // EOF
837