]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/wxVtkSceneManager.cxx
The creation of a panel which enables the user to set in a basic way the values of...
[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                         else if(command == EDIT_BLACKBOX)
444                         {
445                                 GObjectController* cont = _controllers[idController];
446
447                                 for (int i=0; i<_selectedObjects.size(); i++)
448                                 {
449                                         int id = _selectedObjects[i];
450                                         GObjectController* cont = _controllers[id];
451                                         cont->getView()->setState(NOTHING_HAPPENS);
452                                 }
453                                 _selectedObjects.clear();
454
455                                 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
456                                 _parent->editBlackBox(bbmodel);
457
458                         }
459                 }
460         }
461
462         //=========================================================================
463
464         bool wxVtkSceneManager::OnMouseMove()
465         {
466                 int X,Y;
467                 wxVTKRenderWindowInteractor *wxVTKiren;
468                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
469                 wxVTKiren->GetEventPosition(X,Y);
470         
471                 
472                 if(_worldState == DRAG_OBJECTS)
473                 {                       
474                         for (int i=0; i<_selectedObjects.size(); i++)
475                         {
476                                 int id = _selectedObjects[i];
477                                 GObjectController* cont = _controllers[id];
478                                 if(_startDragging)
479                                 {
480                                         cont->getView()->setStartDragging(true);
481                                 }
482                                 cont->getView()->moveObject(X,Y);
483                                 cont->getView()->setState(DRAG);
484                                 cont->getModel()->notifyObservers(_idManager);
485                         }
486
487                         std::map<int, GObjectController*>::iterator it;
488
489                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
490                         {
491                                 GObjectController *desc = it->second;
492                                 if(desc->getGObjectType()==GCONNECTOR)
493                                 {
494                                         GConnectorView* vconn = (GConnectorView*)desc->getView();
495                                         vconn->updateStartEndPoints();
496                                 }
497                         }
498                         
499                         _startDragging=false;
500
501                 }
502                 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
503                 {
504                         std::map<int, GObjectController*>::iterator it;
505
506                         for(it = _controllers.begin(); it != _controllers.end(); ++it)
507                         {
508                                 GObjectController *desc = it->second;
509                                 int type = desc->getGObjectType();
510                                 int state = desc->getView()->getState();
511                                 
512                                 if(state == HIGHLIGHTED){
513
514                                         updateStatusBar(desc->getStatusText());
515                                         if(type==GBLACKBOX)
516                                         {
517                                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
518                                                 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
519                                         }
520                                 }
521                         }
522                 }
523                 
524                 
525
526
527                 return true;
528         }
529
530         //=========================================================================
531         
532         bool wxVtkSceneManager::OnLeftButtonDown()
533         {
534                 
535                 if(_selectedObjects.size()!=0)
536                 {
537                         _worldState = DRAG_OBJECTS;
538                         _startDragging = true;
539
540                         for (int i = 0; i < _selectedObjects.size(); i++)
541                         {
542                                 int id = _selectedObjects[i];
543                                 GObjectController* cont = _controllers[id];
544                                 cont->getView()->setState(DRAG);
545                                 cont->getModel()->notifyObservers(_idManager);
546                         }
547                 }
548                 
549                 
550                 
551                 return true;
552         }
553
554         //=========================================================================
555         
556         bool wxVtkSceneManager::OnLeftButtonUp()
557         {
558                 if(_worldState == DRAG_OBJECTS)
559                 {
560                         _worldState = NOTHING_HAPPENS;
561
562                         for (int i = 0; i < _selectedObjects.size(); i++)
563                         {
564                                 int id = _selectedObjects[i];
565                                 GObjectController* cont = _controllers[id];
566                                 cont->getView()->setState(SELECTED);
567                                 cont->getModel()->notifyObservers(_idManager);
568                         }
569                 }
570                 return true;
571         }
572
573         //=========================================================================
574
575         bool wxVtkSceneManager::OnRightButtonUp()
576         {
577                 for (int i = 0; i < _selectedObjects.size(); i++)
578                 {
579                         int id = _selectedObjects[i];
580                         GObjectController* cont = _controllers[id];
581                         cont->SetActive(true);
582                         cont->getView()->setState(NOTHING_HAPPENS);
583                         cont->getModel()->notifyObservers(_idManager);
584                 }
585
586                 _selectedObjects.clear();
587
588                 return true;
589         }
590
591         //=========================================================================
592
593         bool wxVtkSceneManager::OnChar()
594         {       
595                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
596                 
597                 // KeyCode 127 : Delete Key
598                 // KeyCode 8 : Backspace Key
599                 if(keyCode == 8 || keyCode == 127)
600                 {
601                         if(_selectedObjects.size()>0)
602                         {
603                                 for(int i=0;i<_selectedObjects.size();i++)
604                                 {
605                                         int id = _selectedObjects[i];
606                                         removeObject(id);
607                                 }
608                                 _selectedObjects.clear();
609                         }
610                 }
611
612                 return true;
613         }
614
615         //=========================================================================
616
617         void wxVtkSceneManager::removeObject(int id)
618         {
619                 GObjectController *control = _controllers[id];
620                 std::vector<int> controllersToRemove;
621
622                 if(control->getGObjectType()==GBLACKBOX)
623                 {
624                         GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
625                         std::vector<GPortModel*>inputs = bbmod->getInputPorts();
626                         
627                         for(int i = 0;i<inputs.size();i++)
628                         {
629                                 controllersToRemove.push_back(inputs[i]->getObjectId());
630                         }
631
632                         std::vector<GPortModel*>outputs = bbmod->getOutputPorts();
633
634                         for(int i = 0;i<outputs.size();i++)
635                         {
636                                 controllersToRemove.push_back(outputs[i]->getObjectId());
637                         }
638
639                         controllersToRemove.push_back(control->getId());
640                 }
641
642                 for(int i = 0;i<controllersToRemove.size();i++)
643                 {
644                         int id = controllersToRemove[i];
645                         GObjectController *cont = _controllers[id];
646                         cont->removeFromScene();
647                         unregisterController((InteractorStyleMaracas*)cont);
648                         _controllers.erase(id);
649                 }
650
651
652         }
653
654         //=========================================================================
655
656         void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
657         {
658                 _parent->displayBlackBoxInfo(packageName,boxName);
659         }
660
661         //=========================================================================
662
663         void wxVtkSceneManager::updateStatusBar(std::string textStatus)
664         {
665                 _parent->updateStatusBar(textStatus);
666         }
667
668         //=========================================================================
669
670         std::string wxVtkSceneManager::getDiagramScript()
671         {
672                 bool existsExec=false;
673
674                 std::vector<std::string> packages;
675                 std::vector<int> boxes;
676                 std::vector<int> connections;
677                 std::vector<int> execBoxes;
678
679                 std::map<int, GObjectController*>::iterator it;
680
681                 for(it = _controllers.begin(); it != _controllers.end(); ++it)
682                 {
683                         GObjectController *desc = it->second;
684                         int type = desc->getGObjectType();
685
686                         if(type==GBLACKBOX)
687                         {
688                                 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
689                                 
690                                 std::string pkg = mod->getBBTKPackage();
691                                 bool existsPkg = false;
692                                 for(int t = 0;t<packages.size() && existsPkg == false;t++)
693                                 {
694                                         if(packages[t]==pkg)
695                                         {
696                                                 existsPkg=true;
697                                         }
698                                 }
699                                 if(!existsPkg)
700                                 {
701                                         packages.push_back(pkg);
702                                 }
703
704
705                                 boxes.push_back(it->first);
706                                 if(mod->isExecutable())
707                                 {
708                                         execBoxes.push_back(it->first);
709                                         existsExec=true;
710                                 }
711                         }
712                         else if(type==GCONNECTOR)
713                         {
714                                 connections.push_back(it->first);
715                         }
716                 }
717
718                 std::string script = "";
719                 script+="# BBTK GEditor Script\n";
720                 script+="# ----------------------\n";
721                 if(existsExec)
722                 {
723                         int i;
724                         for(i = 0; i<packages.size();i++)
725                         {
726                                 script+="load ";
727                                 script+=packages[i];
728                                 script+="\n";
729                         }
730
731                         script+="load std\n";
732
733                         for(i = 0; i<boxes.size();i++)
734                         {
735                                 script+="new ";
736                                 int id = boxes[i];
737                                 GObjectController *control = _controllers[id];
738                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
739
740                                 script+=model->getBBTKType();
741                                 script+=" ";
742                                 script+=model->getBBTKName();
743                                 script+="\n";
744
745                                 std::vector<GPortModel*> inputs = model->getInputPorts();
746                                 for(int j = 0; j<inputs.size();j++)
747                                 {
748                                         GPortModel* inputPort = inputs[j];
749                                         if(inputPort->isValueSet())
750                                         {
751                                                 script+="set ";
752                                                 script+=model->getBBTKName();
753                                                 script+=".";
754                                                 script+=inputPort->getBBTKName();
755                                                 script+=" ";
756                                                 script+=inputPort->getValue();
757                                                 script+="\n";
758                                         }
759                                 }
760
761                         }
762
763                         for(i = 0; i<connections.size();i++)
764                         {
765                                 script+="connect ";
766                                 int id = connections[i];
767                                 GObjectController *control = _controllers[id];
768                                 GConnectorModel *model = (GConnectorModel*)control->getModel();
769
770                                 //Start Connection info
771                                 GPortModel *start = model->getStartPort();
772                                 script+=start->getParentBox()->getBBTKName();
773                                 script+=".";
774                                 script+=start->getBBTKName();
775
776                                 script+=" ";
777
778                                 //End Connection info
779                                 GPortModel *end = model->getEndPort();
780                                 script+=end->getParentBox()->getBBTKName();
781                                 script+=".";
782                                 script+=end->getBBTKName();
783
784                                 script+="\n";
785                         }
786
787                         for(i = 0; i<execBoxes.size();i++)
788                         {
789                                 script+="exec ";
790                                 int id = execBoxes[i];
791                                 GObjectController *control = _controllers[id];
792                                 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
793
794                                 script+=model->getBBTKName();
795                                 script+="\n";
796                         }
797
798                 }
799
800                 return script;
801         }
802
803         //=========================================================================
804
805 }  // EO namespace bbtk
806
807 // EOF
808