1 /*=========================================================================
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
33 * \brief Class bbtk::wxVtkSceneManager .
37 #include "wxVtkSceneManager.h"
43 //=========================================================================
44 wxVtkSceneManager::wxVtkSceneManager(wxGEditorTabPanel *parent, wxVtk3DBaseView *baseView,int idManager)
55 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget((wxDropTarget*)parent);
57 _worldState=NOTHING_HAPPENS;
58 registerController(this);
64 //=========================================================================
66 void wxVtkSceneManager::disconnectDrop()
68 _baseView->GetWxVTKRenderWindowInteractor()->SetDropTarget(NULL);
71 //=========================================================================
73 wxVtkSceneManager::~wxVtkSceneManager()
77 //=========================================================================
79 void wxVtkSceneManager::configureBaseView()
81 vtkInteractorStyleBaseView2D *interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
83 _baseView->SetInteractorStyleBaseView(interactorstylebaseview);
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);
91 _baseView->GetRenderer()->GetActiveCamera()->ParallelProjectionOn();
92 _baseView->GetRenderer()->ResetCamera(-100,100,-100,100,900,1000);
94 _baseView->GetRenderer()->SetBackground(0.9,0.9,0.9);
95 _baseView->GetRenderer()->GradientBackgroundOff();
99 //=========================================================================
101 int wxVtkSceneManager::createGBlackBox(int x, int y, std::string packageName, std::string boxType)
103 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
104 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
106 int type = GBLACKBOX;
108 //Create the MVC Objects
110 GBlackBoxModel *model = (GBlackBoxModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
111 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
112 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
114 BlackBoxDescriptor::Pointer descriptor = GObjectsMVCFactory::getInstance()->getBlackBoxDescriptor(packageName, boxType);
116 //Prepares the initial model
117 //The coordinates obtained are the following. Top-Left:x=0,y=0 Bottom-Right:x=width,y=height
120 double yy = windowHeight-y;
122 //z value is not important yet, because it is only used a parallel projection
125 _baseView->TransCoordScreenToWorld(xx,yy,zz);
126 model->setInicPoint(xx,yy,zz);
129 std::stringstream stream;
133 stream << "Box0" << _numBoxes;
137 stream << "Box" << _numBoxes;
139 std::string arraystring = stream.str();
141 model->setBBTKName(arraystring);
142 model->setBBTKType(boxType);
143 model->setBBTKPackage(packageName);
145 model->addObserver(view);
146 model->addObserver(this);
148 //Iterate and create the input ports
149 std::map<std::string, BlackBoxInputDescriptor*> descriptorInMap = descriptor->GetInputDescriptorMap();
150 std::map<std::string, BlackBoxInputDescriptor*>::iterator itInput;
153 for(itInput = descriptorInMap.begin(); itInput != descriptorInMap.end(); ++itInput)
155 BlackBoxInputDescriptor *desc = itInput->second;
156 createGInputPort(GINPUTPORT,i,model,desc);
160 //Iterate and create the output ports
161 std::map<std::string, BlackBoxOutputDescriptor*> descriptorOutMap = descriptor->GetOutputDescriptorMap();
162 std::map<std::string, BlackBoxOutputDescriptor*>::iterator itOutput;
165 for(itOutput = descriptorOutMap.begin();itOutput != descriptorOutMap.end(); ++itOutput)
167 BlackBoxOutputDescriptor *desc = itOutput->second;
168 createGOutputPort(GOUTPUTPORT,i,model,desc);
173 //Associates the view with the correspondent renderer and the model.
174 //(NOTE: Refresh is only made by the view)
175 view->setModel(model);
176 view->setBaseView(_baseView);
177 view->initVtkObjects();
179 //Associates the controller with the correspondent model and view
180 controller->setModelAndView(model,view);
182 //Resgiter change to the observers of the actual model
183 model->notifyObservers(_idManager);
185 //Register the controller of the new object
186 registerController((InteractorStyleMaracas*) controller);
188 //Add the object to the objects list
189 int newId = _controllers.size();
190 controller->setId(newId);
191 _controllers[newId] = controller;
196 //=========================================================================
198 int wxVtkSceneManager::createGComplexBoxInputPort(std::string inputName)
200 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
201 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
203 int type = GCOMPLEXINPUTPORT;
205 //Create the MVC Objects
207 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
208 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
209 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
211 //Prepares the initial model
214 double yy = windowHeight-5;
216 //z value is not important yet, because it is only used a parallel projection
219 _baseView->TransCoordScreenToWorld(xx,yy,zz);
220 model->setInicPoint(xx,yy,zz);
222 model->setBBTKName(inputName);
223 model->setBBTKType("ComplexInputPort");
224 model->setComplexPortType(type);
226 model->addObserver(view);
227 model->addObserver(this);
229 //create the output port
230 GPortController* portController = createGPort(GOUTPUTPORT,inputName,"ComplexInputPort",0,model);
231 model->addOutputPort((GPortModel*)portController->getModel());
233 //Associates the view with the correspondent renderer and the model.
234 //(NOTE: Refresh is only made by the view)
235 view->setModel(model);
236 view->setBaseView(_baseView);
237 view->initVtkObjects();
239 //Associates the controller with the correspondent model and view
240 controller->setModelAndView(model,view);
242 //Resgiter change to the observers of the actual model
243 model->notifyObservers(_idManager);
245 //Register the controller of the new object
246 registerController((InteractorStyleMaracas*) controller);
248 //Add the object to the objects list
249 int newId = _controllers.size();
250 controller->setId(newId);
251 _controllers[newId] = controller;
255 //=========================================================================
257 int wxVtkSceneManager::createGComplexBoxOutputPort(std::string outputName)
259 int windowWidth=_baseView->GetRenWin()->GetSize()[0];
260 int windowHeight=_baseView->GetRenWin()->GetSize()[1];
262 int type = GCOMPLEXOUTPUTPORT;
264 //Create the MVC Objects
266 GComplexBoxPortModel *model = (GComplexBoxPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
267 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
268 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
270 //Prepares the initial model
273 double yy = windowHeight-5;
275 //z value is not important yet, because it is only used a parallel projection
278 _baseView->TransCoordScreenToWorld(xx,yy,zz);
279 model->setInicPoint(xx,yy,zz);
281 model->setBBTKName(outputName);
282 model->setBBTKType("ComplexOutputPort");
283 model->setComplexPortType(type);
285 model->addObserver(view);
286 model->addObserver(this);
288 //create the output port
289 GPortController* portController = createGPort(GINPUTPORT,outputName,"ComplexInputPort",0,model);
290 model->addInputPort((GPortModel*)portController->getModel());
292 //Associates the view with the correspondent renderer and the model.
293 //(NOTE: Refresh is only made by the view)
294 view->setModel(model);
295 view->setBaseView(_baseView);
296 view->initVtkObjects();
298 //Associates the controller with the correspondent model and view
299 controller->setModelAndView(model,view);
301 //Resgiter change to the observers of the actual model
302 model->notifyObservers(_idManager);
304 //Register the controller of the new object
305 registerController((InteractorStyleMaracas*) controller);
307 //Add the object to the objects list
308 int newId = _controllers.size();
309 controller->setId(newId);
310 _controllers[newId] = controller;
314 //=========================================================================
316 int wxVtkSceneManager::createGInputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxInputDescriptor *desc)
318 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
319 blackBox->addInputPort((GPortModel*)portController->getModel());
320 return portController->getId();
323 //=========================================================================
325 int wxVtkSceneManager::createGOutputPort(int portType, int posinBox,GBoxModel *blackBox, BlackBoxOutputDescriptor *desc)
327 GPortController* portController = createGPort(portType,desc->GetName(),desc->GetTypeName(),posinBox,blackBox);
328 blackBox->addOutputPort((GPortModel*)portController->getModel());
329 return portController->getId();
332 //=========================================================================
334 GPortController* wxVtkSceneManager::createGPort(int portType,std::string bbtkName, std::string bbtkType, int posInBox,GBoxModel *blackBox)
338 //Create the MVC Objects
339 GPortModel *model = (GPortModel*)GObjectsMVCFactory::getInstance()->createGObjectModel(type);
340 vtkGObjectView *view = GObjectsMVCFactory::getInstance()->createGObjectView(type);
341 GObjectController* controller = GObjectsMVCFactory::getInstance()->createGObjectController(type);
343 model->registerInBox(blackBox,portType, posInBox);
345 model->setBBTKType(bbtkType);
346 model->setBBTKName(bbtkName);
348 model->addObserver(view);
349 model->addObserver(this);
351 //Associates the view with the correspondent renderer and the model.
352 //(NOTE: Refresh is only made by the view)
353 view->setModel(model);
354 view->setBaseView(_baseView);
355 view->initVtkObjects();
357 //Associates the controller with the correspondent model and view
358 controller->setModelAndView(model,view);
360 model->notifyObservers(_idManager);
362 //Register the controller of the new object
363 registerController((InteractorStyleMaracas*) controller);
365 int newId = _controllers.size();
366 controller->setId(newId);
367 _controllers[newId] = controller;
369 return (GPortController*)controller;
372 //=========================================================================
374 int wxVtkSceneManager::createGConnector(GPortModel* startPort)
376 int type = GCONNECTOR;
378 manualConnectorContourController* manContourControl = new manualConnectorContourController();
379 manualConnectorContourView* manContourView = new manualConnectorContourView();
380 manualContourModel* manContourModel = new manualContourModel();
382 GConnectorController* connectorcontroller = new GConnectorController();
383 GConnectorModel* connectorModel = new GConnectorModel();
384 vtkGConnectorView* connectorView = new vtkGConnectorView();
385 connectorModel->setGObjectType(type);
387 manContourModel->SetCloseContour(false);
388 connectorModel->setStartPort(startPort);
390 manContourView->SetModel( manContourModel );
391 manContourView->SetWxVtkBaseView( _baseView );
392 manContourView->SetRange( 0.5 );
393 manContourView->SetZ( 900 );
395 manContourView->SetColorNormalContour(0, 0, 1);
396 manContourView->SetColorEditContour(0.5, 0.5, 0.5);
397 manContourView->SetColorSelectContour(1, 0.8, 0);
398 manContourView->SetWidthLine(1);
399 manContourView->SetShowText(false);
401 manContourControl->SetModelView( manContourModel , manContourView );
403 manContourControl->CreateNewManualContour();
405 manContourView->RefreshContour();
409 connectorModel->getInicPoint(x,y,z);
411 manContourControl->SetState(1);
412 manContourModel->SetCloseContour(false);
414 manContourModel->AddPoint(x,y,z);
415 manContourView->AddPoint();
417 manContourModel->AddPoint(x,y,z);
418 manContourView->AddPoint();
420 int bak= manContourControl->GetNumberOfPointsManualContour() - 1;
421 manContourControl->_bakIdPoint=bak;
422 manContourView->Refresh();
424 manContourControl->SetMoving( false );
426 registerController((InteractorStyleMaracas*) connectorcontroller);
428 connectorcontroller->setManualContourController(manContourControl);
429 connectorModel->setManualContourModel(manContourModel);
430 connectorView->setManualContourView(manContourView);
431 connectorView->setModel(connectorModel);
432 connectorView->setBaseView(_baseView);
433 connectorcontroller->setModelAndView(connectorModel,connectorView);
435 connectorModel->addObserver(connectorView);
436 connectorModel->addObserver(this);
438 int newId = _controllers.size();
439 connectorcontroller->setId(newId);
440 _controllers[newId] = connectorcontroller;
445 //=========================================================================
447 void wxVtkSceneManager::registerController(InteractorStyleMaracas *param)
449 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
450 baseViewControlManager->AddInteractorStyleMaracas( param );
453 //=========================================================================
455 void wxVtkSceneManager::unregisterController(InteractorStyleMaracas *param)
457 vtkInteractorStyleBaseView* baseViewControlManager = (vtkInteractorStyleBaseView*)_baseView->GetInteractorStyleBaseView();
458 baseViewControlManager->RemoveInteractorStyleMaracas( param );
461 //=========================================================================
463 vtkRenderer* wxVtkSceneManager::getRenderer()
465 return _baseView->GetRenderer();
468 //=========================================================================
470 vtkRenderWindow* wxVtkSceneManager::getRenderWindow()
472 return _baseView->GetRenWin();
474 //=========================================================================
477 void wxVtkSceneManager::update(int idController,int command)
479 if(command != NO_COMMAND)
481 if(command == ADD_TO_SELECTED)
483 GObjectController* cont = _controllers[idController];
486 for (int i=0; i<_selectedObjects.size() && foundID==false; i++)
488 int id = _selectedObjects[i];
496 int id = idController;
497 _selectedObjects.push_back(id);
501 else if(command == INIT_CREATION_CONTOUR)
503 _worldState = INIT_CREATION_CONTOUR;
504 GObjectController* cont = _controllers[idController];
505 GPortModel* startOutputPort = (GPortModel*)cont->getModel();
508 // The last one is the controller of the connector
509 for(int i=0;i<_controllers.size();i++)
511 GObjectController* cont = _controllers[i];
512 if(cont->getGObjectType() == GPORT )
514 GPortModel* port = (GPortModel*)cont->getModel();
515 if(port->getPortType()==GINPUTPORT)
517 cont->SetActive(true);
521 cont->getView()->setState(NOTHING_HAPPENS);
522 cont->getModel()->notifyObservers(_idManager);
523 cont->SetActive(false);
528 cont->getView()->setState(NOTHING_HAPPENS);
529 cont->getModel()->notifyObservers(_idManager);
530 cont->SetActive(false);
534 _selectedObjects.clear();
536 createGConnector(startOutputPort);
539 else if(command == FIN_CREATION_CONTOUR && _worldState == INIT_CREATION_CONTOUR)
541 _worldState = NOTHING_HAPPENS;
542 int id = _controllers.size()-1;
543 GObjectController* cont = _controllers[id];
544 GConnectorModel* modelContour = (GConnectorModel*)cont->getModel();
546 GObjectController* finPort = _controllers[idController];
547 if(finPort->getGObjectType() == GPORT)
549 GPortModel* modelPort = (GPortModel*)finPort->getModel();
550 modelContour->setEndPort(modelPort);
553 manualConnectorContourController* manCont = ((GConnectorController*)cont)->getManualContourController();
554 manualConnectorContourView* connView = (manualConnectorContourView*)manCont->GetManualViewBaseContour();
557 for(int i=0;i<_controllers.size();i++)
559 GObjectController* cont = _controllers[i];
560 if(cont->getView()!=NULL)
562 cont->getView()->setState(NOTHING_HAPPENS);
563 cont->getModel()->notifyObservers(_idManager);
565 cont->SetActive(true);
572 //=========================================================================
574 bool wxVtkSceneManager::OnMouseMove()
577 wxVTKRenderWindowInteractor *wxVTKiren;
578 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
579 wxVTKiren->GetEventPosition(X,Y);
582 if(_worldState == DRAG_OBJECTS)
584 for (int i=0; i<_selectedObjects.size(); i++)
586 int id = _selectedObjects[i];
587 GObjectController* cont = _controllers[id];
590 cont->getView()->setStartDragging(true);
592 cont->getView()->moveObject(X,Y);
593 cont->getView()->setState(DRAG);
594 cont->getModel()->notifyObservers(_idManager);
597 std::map<int, GObjectController*>::iterator it;
599 for(it = _controllers.begin(); it != _controllers.end(); ++it)
601 GObjectController *desc = it->second;
602 if(desc->getGObjectType()==GCONNECTOR)
604 vtkGConnectorView* vconn = (vtkGConnectorView*)desc->getView();
605 vconn->updateStartEndPoints();
609 _startDragging=false;
612 else if(_worldState == NOTHING_HAPPENS || _worldState == INIT_CREATION_CONTOUR)
614 std::map<int, GObjectController*>::iterator it;
616 for(it = _controllers.begin(); it != _controllers.end(); ++it)
618 GObjectController *desc = it->second;
619 int type = desc->getGObjectType();
620 int state = desc->getView()->getState();
622 if(state == HIGHLIGHTED){
624 updateStatusBar(desc->getStatusText());
627 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
628 _parent->displayBlackBoxInfo(mod->getBBTKPackage(),mod->getBBTKType());
640 //=========================================================================
642 bool wxVtkSceneManager::OnLeftButtonDown()
644 if(_worldState==INIT_CREATION_CONTOUR)
646 bool isOverPort=false;
647 std::map<int, GObjectController*>::iterator it;
648 for(it = _controllers.begin(); it != _controllers.end() && isOverPort==false; ++it)
650 GObjectController *desc = it->second;
651 if(desc->getGObjectType()==GPORT)
653 GPortModel* portmod=(GPortModel*)desc->getModel();
654 vtkGObjectView* portView=desc->getView();
655 if(portmod->getPortType()==GINPUTPORT && portView->getState()==HIGHLIGHTED)
662 if(isOverPort==false)
664 _worldState=NOTHING_HAPPENS;
665 int lastId = _controllers.size()-1;
666 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
667 connector->removeFromScene();
668 unregisterController(connector);
669 _controllers.erase(lastId);
671 for(it = _controllers.begin(); it != _controllers.end(); ++it)
673 GObjectController *desc = it->second;
674 desc->SetActive(true);
675 desc->getView()->setState(NOTHING_HAPPENS);
676 desc->getModel()->notifyObservers(_idManager);
681 if(_selectedObjects.size()!=0)
683 _worldState = DRAG_OBJECTS;
684 _startDragging = true;
686 for (int i = 0; i < _selectedObjects.size(); i++)
688 int id = _selectedObjects[i];
689 GObjectController* cont = _controllers[id];
690 cont->getView()->setState(DRAG);
691 cont->getModel()->notifyObservers(_idManager);
700 //=========================================================================
702 bool wxVtkSceneManager::OnLeftButtonUp()
704 if(_worldState == DRAG_OBJECTS)
706 _worldState = NOTHING_HAPPENS;
708 for (int i = 0; i < _selectedObjects.size(); i++)
710 int id = _selectedObjects[i];
711 GObjectController* cont = _controllers[id];
712 cont->getView()->setState(SELECTED);
713 cont->getModel()->notifyObservers(_idManager);
719 //=========================================================================
721 bool wxVtkSceneManager::OnRightButtonUp()
723 if(_worldState==INIT_CREATION_CONTOUR)
725 _worldState=NOTHING_HAPPENS;
726 int lastId = _controllers.size()-1;
727 GConnectorController *connector = (GConnectorController*)_controllers[lastId];
728 connector->removeFromScene();
729 unregisterController(connector);
730 _controllers.erase(lastId);
732 std::map<int, GObjectController*>::iterator it;
733 for(it = _controllers.begin(); it != _controllers.end(); ++it)
735 GObjectController *desc = it->second;
736 desc->SetActive(true);
737 desc->getView()->setState(NOTHING_HAPPENS);
738 desc->getModel()->notifyObservers(_idManager);
743 for (int i = 0; i < _selectedObjects.size(); i++)
745 int id = _selectedObjects[i];
746 GObjectController* cont = _controllers[id];
747 cont->SetActive(true);
748 cont->getView()->setState(NOTHING_HAPPENS);
749 cont->getModel()->notifyObservers(_idManager);
752 _selectedObjects.clear();
757 //=========================================================================
759 bool wxVtkSceneManager::OnLeftDClick()
762 wxVTKRenderWindowInteractor *wxVTKiren;
763 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
764 wxVTKiren->GetEventPosition(X,Y);
766 std::map<int, GObjectController*>::iterator it;
768 bool clickOnObject = false;
770 for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
772 GObjectController *cont = it->second;
773 int type = cont->getGObjectType();
775 if(cont->getView()->isPointInside(X,Y))
779 for (int i=0; i<_selectedObjects.size(); i++)
781 int id = _selectedObjects[i];
782 GObjectController* control = _controllers[id];
783 control->getView()->setState(NOTHING_HAPPENS);
785 _selectedObjects.clear();
787 GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
788 _parent->editBlackBox(bbmodel);
790 clickOnObject = true;
794 if(clickOnObject==false)
796 //_parent->editDiagramParameters(this);
802 //=========================================================================
804 bool wxVtkSceneManager::OnChar()
806 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
808 // KeyCode 127 : Delete Key
809 // KeyCode 8 : Backspace Key
810 if(keyCode == 8 || keyCode == 127)
812 if(_selectedObjects.size()>0)
814 for(int i=0;i<_selectedObjects.size();i++)
816 int id = _selectedObjects[i];
819 _selectedObjects.clear();
826 //=========================================================================
828 void wxVtkSceneManager::deleteObject(int id)
830 GObjectController *control = _controllers[id];
831 std::vector<int> controllersToRemove;
833 if(control->getGObjectType()==GBLACKBOX)
835 GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
836 std::vector<GPortModel*> inputs = bbmod->getInputPorts();
838 bool boxConnected = false;
840 // Add box input controllers to be removed
841 for(int i = 0;i<inputs.size();i++)
843 controllersToRemove.push_back(inputs[i]->getObjectId());
844 if(inputs[i]->isConnected())
850 std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
852 // Add box output controllers to be removed
853 for(int i = 0;i<outputs.size();i++)
855 controllersToRemove.push_back(outputs[i]->getObjectId());
856 if(outputs[i]->isConnected())
862 // Add connection controllers to be removed
863 std::map<int, GObjectController*>::iterator it;
864 for(it = _controllers.begin(); it != _controllers.end(); ++it)
866 GObjectController *cont = it->second;
867 int type = cont->getGObjectType();
870 GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
871 if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
873 controllersToRemove.push_back(conMod->getObjectId());
875 if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
877 controllersToRemove.push_back(conMod->getObjectId());
882 // Add box controller to be removed
883 controllersToRemove.push_back(bbmod->getObjectId());
885 else if(control->getGObjectType()==GCONNECTOR)
887 GConnectorModel *conMod = (GConnectorModel*)control->getModel();
888 controllersToRemove.push_back(conMod->getObjectId());
891 for(int i = 0;i<controllersToRemove.size();i++)
893 int id = controllersToRemove[i];
894 GObjectController *cont = _controllers[id];
895 cont->removeFromScene();
896 unregisterController((InteractorStyleMaracas*)cont);
897 _controllers.erase(id);
903 //=========================================================================
905 void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
907 _parent->displayBlackBoxInfo(packageName,boxName);
910 //=========================================================================
912 void wxVtkSceneManager::updateStatusBar(std::string textStatus)
914 _parent->updateStatusBar(textStatus);
917 //=========================================================================
919 std::string wxVtkSceneManager::getDiagramScript()
921 bool existsExec=false;
923 std::vector<std::string> packages;
924 std::vector<int> boxes;
925 std::vector<int> connections;
926 std::vector<int> execBoxes;
928 std::map<int, GObjectController*>::iterator it;
930 for(it = _controllers.begin(); it != _controllers.end(); ++it)
932 GObjectController *desc = it->second;
933 int type = desc->getGObjectType();
937 GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
939 std::string pkg = mod->getBBTKPackage();
940 bool existsPkg = false;
941 for(int t = 0;t<packages.size() && existsPkg == false;t++)
950 packages.push_back(pkg);
954 boxes.push_back(it->first);
955 if(mod->isExecutable())
957 execBoxes.push_back(it->first);
961 else if(type==GCONNECTOR)
963 connections.push_back(it->first);
967 std::string script = "";
968 script+="# BBTK GEditor Script\n";
969 script+="# ----------------------\n";
973 for(i = 0; i<packages.size();i++)
980 script+="include std\n";
982 for(i = 0; i<boxes.size();i++)
986 GObjectController *control = _controllers[id];
987 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
989 script+=model->getBBTKType();
991 script+=model->getBBTKName();
994 std::vector<GPortModel*> inputs = model->getInputPorts();
995 for(int j = 0; j<inputs.size();j++)
997 GPortModel* inputPort = inputs[j];
998 if(inputPort->isValueSet())
1001 script+=model->getBBTKName();
1003 script+=inputPort->getBBTKName();
1005 script+=inputPort->getValue();
1012 for(i = 0; i<connections.size();i++)
1015 int id = connections[i];
1016 GObjectController *control = _controllers[id];
1017 GConnectorModel *model = (GConnectorModel*)control->getModel();
1019 //Start Connection info
1020 GPortModel *start = model->getStartPort();
1021 script+=start->getParentBox()->getBBTKName();
1023 script+=start->getBBTKName();
1027 //End Connection info
1028 GPortModel *end = model->getEndPort();
1029 script+=end->getParentBox()->getBBTKName();
1031 script+=end->getBBTKName();
1036 for(i = 0; i<execBoxes.size();i++)
1039 int id = execBoxes[i];
1040 GObjectController *control = _controllers[id];
1041 GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
1043 script+=model->getBBTKName();
1052 //=========================================================================
1054 void wxVtkSceneManager::deleteAllBoxes()
1056 std::map<int, GObjectController*>::iterator it;
1057 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1059 GObjectController *cont = it->second;
1060 cont->removeFromScene();
1061 unregisterController((InteractorStyleMaracas*)cont);
1063 _selectedObjects.clear();
1064 _controllers.clear();
1068 //=========================================================================
1070 void wxVtkSceneManager::refreshScene()
1072 _baseView->RefreshView();
1075 //=========================================================================
1077 void wxVtkSceneManager::centerView()
1080 _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
1081 _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
1082 _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
1083 _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
1084 _baseView->RefreshView();
1088 //=========================================================================
1090 void wxVtkSceneManager::saveDiagram(std::string &content)
1094 std::vector<int> boxes = getBoxes();
1095 int bsize = boxes.size();
1097 sprintf (buffer, "%d", bsize);
1101 for(int i = 0;i<bsize;i++)
1104 GObjectController *cont = _controllers[id];
1105 cont->getModel()->save(content);
1109 std::vector<int> connections = getConnections();
1110 int csize = connections.size();
1111 content+="CONNECTIONS:";
1112 sprintf (buffer, "%d", csize);
1116 for(int i = 0;i<csize;i++)
1118 int id = connections[i];
1119 GObjectController *cont = _controllers[id];
1120 cont->getModel()->save(content);
1125 //=========================================================================
1127 void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
1130 std::string line="";
1131 char delims[] = ":";
1132 char *result = NULL;
1133 getline(inputStream,line);
1136 while ( !inputStream.eof() )
1138 if(line=="" || line[0]=='#')
1140 getline(inputStream,line);
1142 else if(line=="APP_START")
1152 getline(inputStream,line);//BOXES:num
1154 strcpy( boxes, line.c_str() );
1155 result = strtok( boxes, delims );
1156 result = strtok( NULL, delims );
1159 std::istringstream is(result);
1162 for(int i = 0;i<numBoxes;i++)
1165 getline(inputStream,line);//BOX
1166 getline(inputStream,line);//package:type:name
1168 strcpy( box, line.c_str() );
1169 result = strtok( box, delims );//package
1170 std::string package(result);
1171 result = strtok( NULL, delims );//type
1172 std::string type(result);
1173 result = strtok( NULL, delims );//name
1174 std::string name(result);
1176 getline(inputStream,line);//ISEXEC:TRUE|FALSE
1178 strcpy( exec, line.c_str() );
1179 result = strtok( exec, delims );//ISEXEC
1180 result = strtok( NULL, delims );//TRUE|FALSE
1181 std::string isExec(result);
1184 getline(inputStream,line);//xInic:yInic:zInic
1186 strcpy( coord, line.c_str() );
1187 result = strtok( coord, delims );//xInic
1188 std::string xInic(result);
1189 result = strtok( NULL, delims );//yInic
1190 std::string yInic(result);
1191 result = strtok( NULL, delims );//zInic
1192 std::string zInic(result);
1194 double xIn, yIn, zIn;
1195 std::istringstream xSt(xInic);
1197 std::istringstream ySt(yInic);
1199 std::istringstream zSt(zInic);
1203 getline(inputStream,line);//xEnd:yEnd:zEnd
1204 strcpy( coord, line.c_str() );
1205 result = strtok( coord, delims );//xEnd
1206 std::string xEnd(result);
1207 result = strtok( NULL, delims );//yEnd
1208 std::string yEnd(result);
1209 result = strtok( NULL, delims );//zEnd
1210 std::string zEnd(result);
1212 double xEn, yEn, zEn;
1213 std::istringstream xEt(xEnd);
1215 std::istringstream yEt(yEnd);
1217 std::istringstream zEt(zEnd);
1220 int idBox = createGBlackBox(0,0,package,type);
1221 GObjectController *cont = _controllers[idBox];
1222 GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
1223 bbmod->setBBTKName(name);
1224 bbmod->setInicPoint(xIn,yIn,zIn);
1225 bbmod->setFinalPoint(xEn,yEn,zEn);
1228 bbmod->setExecutable(true);
1230 else if(isExec=="FALSE")
1232 bbmod->setExecutable(false);
1236 getline(inputStream,line);//PORT o FIN_BOX
1237 std::string port=line.substr(0,4);
1240 getline(inputStream,line);//name:value
1242 strcpy( poort, line.c_str() );
1243 result = strtok( poort, delims );//name
1244 std::string name(result);
1245 result = strtok( NULL, delims );//value
1246 std::string value(result);
1248 bbmod->setValueToInput(name,value);
1250 getline(inputStream,line);//PORT o FIN_BOX
1251 port=line.substr(0,4);
1254 bbmod->notifyObservers(_idManager);
1259 getline(inputStream,line);//CONNECTIONS:num
1261 strcpy( conns, line.c_str() );
1262 result = strtok( conns, delims );
1263 result = strtok( NULL, delims );
1266 std::istringstream isCons(result);
1269 for(int i = 0;i<numConns;i++)
1272 getline(inputStream,line);//CONNECTION
1273 getline(inputStream,line);//Startbox.PortName:EndBox.PortName
1276 strcpy( connec, line.c_str() );
1277 result = strtok( connec, delims );
1278 std::string nameStartBox(result);
1279 result = strtok( NULL, delims );
1280 std::string nameStartPort(result);
1281 result = strtok( NULL, delims );
1282 std::string nameEndBox(result);
1283 result = strtok( NULL, delims );
1284 std::string nameEndPort(result);
1286 std::vector<int> lstB = getBoxes();
1288 GPortModel *startP=NULL;
1289 GPortModel *endP=NULL;
1290 for(int j = 0;j<lstB.size();j++)
1293 GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
1294 if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
1296 startP = bbMod->getOutputPort(nameStartPort);
1298 else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
1300 endP = bbMod->getInputPort(nameEndPort);
1304 int idCon = createGConnector(startP);
1305 _worldState = NOTHING_HAPPENS;
1306 GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
1308 GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
1309 vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
1310 tempp->endContourCreation();
1311 conMod->setEndPort(endP);
1312 conView->updateStartEndPoints();
1319 //=========================================================================
1321 std::vector<int> wxVtkSceneManager::getBoxes()
1323 std::vector<int> vect;
1324 std::map<int, GObjectController*>::iterator it;
1325 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1327 GObjectController *cont = it->second;
1328 if(cont->getGObjectType()==GBLACKBOX)
1330 vect.push_back(cont->getId());
1336 //=========================================================================
1338 std::vector<int> wxVtkSceneManager::getConnections()
1340 std::vector<int> vect;
1341 std::map<int, GObjectController*>::iterator it;
1342 for(it = _controllers.begin(); it != _controllers.end(); ++it)
1344 GObjectController *cont = it->second;
1345 if(cont->getGObjectType()==GCONNECTOR)
1347 vect.push_back(cont->getId());
1353 //=========================================================================
1355 bool wxVtkSceneManager::isComplexBox()
1357 return _isComplexBox;
1360 //=========================================================================
1362 void wxVtkSceneManager::setComplexBox(bool val)
1367 //=========================================================================
1369 } // EO namespace bbtk