+
+ bool wxVtkSceneManager::OnLeftDClick()
+ {
+ int X,Y;
+ wxVTKRenderWindowInteractor *wxVTKiren;
+ wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk3DBaseView()->GetWxVTKRenderWindowInteractor();
+ wxVTKiren->GetEventPosition(X,Y);
+
+ std::map<int, GObjectController*>::iterator it;
+
+ bool clickOnObject = false;
+
+ for(it = _controllers.begin(); it != _controllers.end() && clickOnObject==false; ++it)
+ {
+ GObjectController *cont = it->second;
+ int type = cont->getGObjectType();
+
+ if(cont->getView()->isPointInside(X,Y))
+ {
+ if(type==GBLACKBOX)
+ {
+ for (int i=0; i<_selectedObjects.size(); i++)
+ {
+ int id = _selectedObjects[i];
+ GObjectController* control = _controllers[id];
+ control->getView()->setState(NOTHING_HAPPENS);
+ }
+ _selectedObjects.clear();
+
+ GBlackBoxModel *bbmodel = (GBlackBoxModel*)cont->getModel();
+ _parent->editBlackBox(bbmodel);
+ }
+ clickOnObject = true;
+ }
+ }
+
+ if(clickOnObject==false)
+ {
+ //_parent->editDiagramParameters(this);
+ }
+
+ return true;
+ }
+
+ //=========================================================================
+
+ bool wxVtkSceneManager::OnChar()
+ {
+ char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
+
+ // KeyCode 127 : Delete Key
+ // KeyCode 8 : Backspace Key
+ if(keyCode == 8 || keyCode == 127)
+ {
+ if(_selectedObjects.size()>0)
+ {
+ for(int i=0;i<_selectedObjects.size();i++)
+ {
+ int id = _selectedObjects[i];
+ deleteObject(id);
+ }
+ _selectedObjects.clear();
+ }
+ }
+
+ return true;
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::deleteObject(int id)
+ {
+ GObjectController *control = _controllers[id];
+ std::vector<int> controllersToRemove;
+
+ if(control->getGObjectType()==GBLACKBOX)
+ {
+ GBlackBoxModel *bbmod = (GBlackBoxModel*)control->getModel();
+ std::vector<GPortModel*> inputs = bbmod->getInputPorts();
+
+ bool boxConnected = false;
+
+ // Add box input controllers to be removed
+ for(int i = 0;i<inputs.size();i++)
+ {
+ controllersToRemove.push_back(inputs[i]->getObjectId());
+ if(inputs[i]->isConnected())
+ {
+ boxConnected = true;
+ }
+ }
+
+ std::vector<GPortModel*> outputs = bbmod->getOutputPorts();
+
+ // Add box output controllers to be removed
+ for(int i = 0;i<outputs.size();i++)
+ {
+ controllersToRemove.push_back(outputs[i]->getObjectId());
+ if(outputs[i]->isConnected())
+ {
+ boxConnected = true;
+ }
+ }
+
+ // Add connection controllers to be removed
+ std::map<int, GObjectController*>::iterator it;
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *cont = it->second;
+ int type = cont->getGObjectType();
+ if(type==GCONNECTOR)
+ {
+ GConnectorModel *conMod = (GConnectorModel*)cont->getModel();
+ if(conMod->getStartPort()!=NULL && conMod->getStartPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
+ {
+ controllersToRemove.push_back(conMod->getObjectId());
+ }
+ if(conMod->getEndPort()!=NULL && conMod->getEndPort()->getParentBox()->getObjectId() == bbmod->getObjectId())
+ {
+ controllersToRemove.push_back(conMod->getObjectId());
+ }
+ }
+ }
+
+ // Add box controller to be removed
+ controllersToRemove.push_back(bbmod->getObjectId());
+ }
+ else if(control->getGObjectType()==GCONNECTOR)
+ {
+ GConnectorModel *conMod = (GConnectorModel*)control->getModel();
+ controllersToRemove.push_back(conMod->getObjectId());
+ }
+
+ for(int i = 0;i<controllersToRemove.size();i++)
+ {
+ int id = controllersToRemove[i];
+ GObjectController *cont = _controllers[id];
+ cont->removeFromScene();
+ unregisterController((InteractorStyleMaracas*)cont);
+ _controllers.erase(id);
+ }
+
+
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::displayBlackBoxInfo(std::string packageName, std::string boxName)
+ {
+ _parent->displayBlackBoxInfo(packageName,boxName);
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::updateStatusBar(std::string textStatus)
+ {
+ _parent->updateStatusBar(textStatus);
+ }
+
+ //=========================================================================
+
+ std::string wxVtkSceneManager::getDiagramBBS()
+ {
+ bool existsExec=false;
+
+ std::vector<std::string> packages;
+ std::vector<int> boxes;
+ std::vector<int> connections;
+ std::vector<int> execBoxes;
+
+ std::map<int, GObjectController*>::iterator it;
+
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *desc = it->second;
+ int type = desc->getGObjectType();
+
+ if(type==GBLACKBOX)
+ {
+ GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
+
+ std::string pkg = mod->getBBTKPackage();
+ bool existsPkg = false;
+ for(int t = 0;t<packages.size() && existsPkg == false;t++)
+ {
+ if(packages[t]==pkg)
+ {
+ existsPkg=true;
+ }
+ }
+ if(!existsPkg)
+ {
+ packages.push_back(pkg);
+ }
+
+
+ boxes.push_back(it->first);
+ if(mod->isExecutable())
+ {
+ execBoxes.push_back(it->first);
+ existsExec=true;
+ }
+ }
+ else if(type==GCONNECTOR)
+ {
+ connections.push_back(it->first);
+ }
+ }
+
+ std::string script = "";
+ script+="# BBTK GEditor Script\n";
+ script+="# ----------------------\n";
+ if(existsExec)
+ {
+ int i;
+ for(i = 0; i<packages.size();i++)
+ {
+ script+="include ";
+ script+=packages[i];
+ script+="\n";
+ }
+
+ script+="include std\n";
+
+ for(i = 0; i<boxes.size();i++)
+ {
+ script+="new ";
+ int id = boxes[i];
+ GObjectController *control = _controllers[id];
+ GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+ script+=model->getBBTKType();
+ script+=" ";
+ script+=model->getBBTKName();
+ script+="\n";
+
+ std::vector<GPortModel*> inputs = model->getInputPorts();
+ for(int j = 0; j<inputs.size();j++)
+ {
+ GPortModel* inputPort = inputs[j];
+ if(inputPort->isValueSet())
+ {
+ script+="set ";
+ script+=model->getBBTKName();
+ script+=".";
+ script+=inputPort->getBBTKName();
+ script+=" ";
+ script+=inputPort->getValue();
+ script+="\n";
+ }
+ }
+
+ }
+
+ for(i = 0; i<connections.size();i++)
+ {
+ script+="connect ";
+ int id = connections[i];
+ GObjectController *control = _controllers[id];
+ GConnectorModel *model = (GConnectorModel*)control->getModel();
+
+ //Start Connection info
+ GPortModel *start = model->getStartPort();
+ script+=start->getParentBox()->getBBTKName();
+ script+=".";
+ script+=start->getBBTKName();
+
+ script+=" ";
+
+ //End Connection info
+ GPortModel *end = model->getEndPort();
+ script+=end->getParentBox()->getBBTKName();
+ script+=".";
+ script+=end->getBBTKName();
+
+ script+="\n";
+ }
+
+ for(i = 0; i<execBoxes.size();i++)
+ {
+ script+="exec ";
+ int id = execBoxes[i];
+ GObjectController *control = _controllers[id];
+ GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+ script+=model->getBBTKName();
+ script+="\n";
+ }
+
+ }
+
+ return script;
+ }
+
+ //=========================================================================
+
+ std::string wxVtkSceneManager::saveComplexBoxBBS(std::string cbName,std::string cbAuthor,std::string cbCategory,std::string cbDescription)
+ {
+
+ std::vector<std::string> packages;
+ std::vector<int> boxes;
+ std::vector<int> connections;
+ std::vector<int> execBoxes;
+
+ std::map<int, GObjectController*>::iterator it;
+
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *desc = it->second;
+ int type = desc->getGObjectType();
+
+ if(type==GBLACKBOX)
+ {
+ GBlackBoxModel *mod = (GBlackBoxModel*)desc->getModel();
+
+ std::string pkg = mod->getBBTKPackage();
+ bool existsPkg = false;
+ for(int t = 0;t<packages.size() && existsPkg == false;t++)
+ {
+ if(packages[t]==pkg)
+ {
+ existsPkg=true;
+ }
+ }
+ if(!existsPkg)
+ {
+ packages.push_back(pkg);
+ }
+
+
+ boxes.push_back(it->first);
+ if(mod->isExecutable())
+ {
+ execBoxes.push_back(it->first);
+ }
+ }
+ else if(type==GCONNECTOR)
+ {
+ connections.push_back(it->first);
+ }
+ }
+
+ std::string script = "";
+ int i;
+ for(i = 0; i<packages.size();i++)
+ {
+ script+="include ";
+ script+=packages[i];
+ script+="\n";
+ }
+
+ script+="include std\n";
+
+ // Definition of a complex box
+ script+="define ";
+ script+=cbName;
+ script+="\n";
+
+ script+="author \" ";
+ script+=cbAuthor;
+ script+="\"\n";
+
+ script+="description \" ";
+ script+=cbDescription;
+ script+="\"\n";
+
+ // Create boxes
+ for(i = 0; i<boxes.size();i++)
+ {
+ script+="new ";
+ int id = boxes[i];
+ GObjectController *control = _controllers[id];
+ GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+ script+=model->getBBTKType();
+ script+=" ";
+ script+=model->getBBTKName();
+ script+="\n";
+
+ std::vector<GPortModel*> inputs = model->getInputPorts();
+ for(int j = 0; j<inputs.size();j++)
+ {
+ GPortModel* inputPort = inputs[j];
+ if(inputPort->isValueSet())
+ {
+ script+="set ";
+ script+=model->getBBTKName();
+ script+=".";
+ script+=inputPort->getBBTKName();
+ script+=" ";
+ script+=inputPort->getValue();
+ script+="\n";
+ }
+ }
+
+ }
+
+ // Create connections in the script. If the connection is made with a complex port, it is created the input or output
+
+ std::string complexInputs="";
+ std::string complexOutputs="";
+
+ for(i = 0; i<connections.size();i++)
+ {
+ int id = connections[i];
+ GObjectController *control = _controllers[id];
+ GConnectorModel *model = (GConnectorModel*)control->getModel();
+
+ //Connection info
+ GPortModel *start = model->getStartPort();
+ GBoxModel *startBox =start->getParentBox();
+
+ GPortModel *end = model->getEndPort();
+ GBoxModel *endBox =end->getParentBox();
+
+ if(startBox->getGObjectType()==GCOMPLEXINPUTPORT)
+ {
+ complexInputs+="input ";
+ complexInputs+=startBox->getBBTKName();
+
+ complexInputs+=" ";
+ complexInputs+=endBox->getBBTKName();
+ complexInputs+=".";
+ complexInputs+=end->getBBTKName();
+
+ complexInputs+=" ";
+ complexInputs+="\" \"";
+
+ complexInputs+="\n";
+ }
+ else if(endBox->getGObjectType()==GCOMPLEXOUTPUTPORT)
+ {
+ complexOutputs+="output ";
+ complexOutputs+=endBox->getBBTKName();
+
+ complexOutputs+=" ";
+ complexOutputs+=startBox->getBBTKName();
+ complexOutputs+=".";
+ complexOutputs+=start->getBBTKName();
+
+ complexOutputs+=" ";
+ complexOutputs+="\" \"";
+
+ complexOutputs+="\n";
+ }
+ else
+ {
+ script+="connect ";
+ script+=startBox->getBBTKName();
+ script+=".";
+ script+=start->getBBTKName();
+
+ script+=" ";
+
+ //End Connection info
+ script+=endBox->getBBTKName();
+ script+=".";
+ script+=end->getBBTKName();
+
+ script+="\n";
+ }
+ }
+
+ for(i = 0; i<execBoxes.size();i++)
+ {
+ script+="exec ";
+ int id = execBoxes[i];
+ GObjectController *control = _controllers[id];
+ GBlackBoxModel *model = (GBlackBoxModel*)control->getModel();
+
+ script+=model->getBBTKName();
+ script+="\n";
+ }
+
+ script+="\n";
+ script+="# Complex input ports\n";
+ script+=complexInputs;
+
+ script+="\n";
+ script+="# Complex output ports\n";
+ script+=complexOutputs;
+
+ script+="\n";
+ script+="endefine";
+ script+="\n";
+
+ return script;
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::deleteAllBoxes()
+ {
+ std::map<int, GObjectController*>::iterator it;
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *cont = it->second;
+ cont->removeFromScene();
+ unregisterController((InteractorStyleMaracas*)cont);
+ }
+ _selectedObjects.clear();
+ _controllers.clear();
+ refreshScene();
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::refreshScene()
+ {
+ _baseView->RefreshView();
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::centerView()
+ {
+ double temp[3];
+ _baseView->GetRenderer()->GetActiveCamera()->GetFocalPoint(temp);
+ _baseView->GetRenderer()->GetActiveCamera()->SetFocalPoint(0,0,temp[2]);
+ _baseView->GetRenderer()->GetActiveCamera()->GetPosition(temp);
+ _baseView->GetRenderer()->GetActiveCamera()->SetPosition(0,0,temp[2]);
+ _baseView->RefreshView();
+
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::saveDiagram(std::string &content)
+ {
+ //Print boxes
+ char buffer [50];
+ std::vector<int> boxes = getBoxes();
+ int bsize = boxes.size();
+ content+="BOXES:";
+ sprintf (buffer, "%d", bsize);
+ content+=buffer;
+ content+="\n";
+
+ for(int i = 0;i<bsize;i++)
+ {
+ int id = boxes[i];
+ GObjectController *cont = _controllers[id];
+ cont->getModel()->save(content);
+ }
+
+ //Print connections
+ std::vector<int> connections = getConnections();
+ int csize = connections.size();
+ content+="CONNECTIONS:";
+ sprintf (buffer, "%d", csize);
+ content+=buffer;
+ content+="\n";
+
+ for(int i = 0;i<csize;i++)
+ {
+ int id = connections[i];
+ GObjectController *cont = _controllers[id];
+ cont->getModel()->save(content);
+ }
+
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::loadDiagram(ifstream &inputStream)
+ {
+
+ std::string line="";
+ char delims[] = ":";
+ char *result = NULL;
+ getline(inputStream,line);
+
+ bool start = false;
+ while ( !inputStream.eof() )
+ {
+ if(line=="" || line[0]=='#')
+ {
+ getline(inputStream,line);
+ }
+ else if(line=="APP_START")
+ {
+ start = true;
+ break;
+ }
+ }
+
+ if(start)
+ {
+ //----------
+ getline(inputStream,line);//BOXES:num
+ char boxes[9];
+ strcpy( boxes, line.c_str() );
+ result = strtok( boxes, delims );
+ result = strtok( NULL, delims );
+
+ int numBoxes;
+ std::istringstream is(result);
+ is >> numBoxes;
+
+ for(int i = 0;i<numBoxes;i++)
+ {
+ //----------
+ getline(inputStream,line);//BOX
+ getline(inputStream,line);//package:type:name
+ char box[100];
+ strcpy( box, line.c_str() );
+ result = strtok( box, delims );//package
+ std::string package(result);
+ result = strtok( NULL, delims );//type
+ std::string type(result);
+ result = strtok( NULL, delims );//name
+ std::string name(result);
+
+ getline(inputStream,line);//ISEXEC:TRUE|FALSE
+ char exec[15];
+ strcpy( exec, line.c_str() );
+ result = strtok( exec, delims );//ISEXEC
+ result = strtok( NULL, delims );//TRUE|FALSE
+ std::string isExec(result);
+
+ //----------
+ getline(inputStream,line);//xInic:yInic:zInic
+ char coord[80];
+ strcpy( coord, line.c_str() );
+ result = strtok( coord, delims );//xInic
+ std::string xInic(result);
+ result = strtok( NULL, delims );//yInic
+ std::string yInic(result);
+ result = strtok( NULL, delims );//zInic
+ std::string zInic(result);
+
+ double xIn, yIn, zIn;
+ std::istringstream xSt(xInic);
+ xSt >> xIn;
+ std::istringstream ySt(yInic);
+ ySt >> yIn;
+ std::istringstream zSt(zInic);
+ zSt >> zIn;
+
+ //----------
+ getline(inputStream,line);//xEnd:yEnd:zEnd
+ strcpy( coord, line.c_str() );
+ result = strtok( coord, delims );//xEnd
+ std::string xEnd(result);
+ result = strtok( NULL, delims );//yEnd
+ std::string yEnd(result);
+ result = strtok( NULL, delims );//zEnd
+ std::string zEnd(result);
+
+ double xEn, yEn, zEn;
+ std::istringstream xEt(xEnd);
+ xEt >> xEn;
+ std::istringstream yEt(yEnd);
+ yEt >> yEn;
+ std::istringstream zEt(zEnd);
+ zEt >> zEn;
+
+ int idBox = createGBlackBox(0,0,package,type);
+ GObjectController *cont = _controllers[idBox];
+ GBlackBoxModel *bbmod = (GBlackBoxModel*)cont->getModel();
+ bbmod->setBBTKName(name);
+ bbmod->setInicPoint(xIn,yIn,zIn);
+ bbmod->setFinalPoint(xEn,yEn,zEn);
+ if(isExec=="TRUE")
+ {
+ bbmod->setExecutable(true);
+ }
+ else if(isExec=="FALSE")
+ {
+ bbmod->setExecutable(false);
+ }
+
+ //----------
+ getline(inputStream,line);//PORT o FIN_BOX
+ std::string port=line.substr(0,4);
+ while(port=="PORT")
+ {
+ getline(inputStream,line);//name:value
+ char poort[20];
+ strcpy( poort, line.c_str() );
+ result = strtok( poort, delims );//name
+ std::string name(result);
+ result = strtok( NULL, delims );//value
+ std::string value(result);
+
+ bbmod->setValueToInput(name,value);
+
+ getline(inputStream,line);//PORT o FIN_BOX
+ port=line.substr(0,4);
+ }
+
+ bbmod->notifyObservers(_idManager);
+ }
+
+ /// CONNECTIONS
+ //----------
+ getline(inputStream,line);//CONNECTIONS:num
+ char conns[15];
+ strcpy( conns, line.c_str() );
+ result = strtok( conns, delims );
+ result = strtok( NULL, delims );
+
+ int numConns;
+ std::istringstream isCons(result);
+ isCons >> numConns;
+
+ for(int i = 0;i<numConns;i++)
+ {
+ //----------
+ getline(inputStream,line);//CONNECTION
+ getline(inputStream,line);//Startbox.PortName:EndBox.PortName
+
+ char connec[100];
+ strcpy( connec, line.c_str() );
+ result = strtok( connec, delims );
+ std::string nameStartBox(result);
+ result = strtok( NULL, delims );
+ std::string nameStartPort(result);
+ result = strtok( NULL, delims );
+ std::string nameEndBox(result);
+ result = strtok( NULL, delims );
+ std::string nameEndPort(result);
+
+ std::vector<int> lstB = getBoxes();
+
+ GPortModel *startP=NULL;
+ GPortModel *endP=NULL;
+ for(int j = 0;j<lstB.size();j++)
+ {
+ int idB = lstB[j];
+ GBlackBoxModel *bbMod = (GBlackBoxModel*)_controllers[idB]->getModel();
+ if(_controllers[idB]->getModel()->getBBTKName()==nameStartBox)
+ {
+ startP = bbMod->getOutputPort(nameStartPort);
+ }
+ else if(_controllers[idB]->getModel()->getBBTKName()==nameEndBox)
+ {
+ endP = bbMod->getInputPort(nameEndPort);
+ }
+ }
+
+ int idCon = createGConnector(startP);
+ _worldState = NOTHING_HAPPENS;
+ GConnectorController *tempp = (GConnectorController*)_controllers[idCon];
+
+ GConnectorModel *conMod = (GConnectorModel*)tempp->getModel();
+ vtkGConnectorView *conView = (vtkGConnectorView*)tempp->getView();
+ tempp->endContourCreation();
+ conMod->setEndPort(endP);
+ conView->updateStartEndPoints();
+
+ }
+ }
+
+ }
+
+ //=========================================================================
+
+ std::vector<int> wxVtkSceneManager::getBoxes()
+ {
+ std::vector<int> vect;
+ std::map<int, GObjectController*>::iterator it;
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *cont = it->second;
+ if(cont->getGObjectType()==GBLACKBOX)
+ {
+ vect.push_back(cont->getId());
+ }
+ }
+ return vect;
+ }
+
+ //=========================================================================
+
+ std::vector<int> wxVtkSceneManager::getConnections()
+ {
+ std::vector<int> vect;
+ std::map<int, GObjectController*>::iterator it;
+ for(it = _controllers.begin(); it != _controllers.end(); ++it)
+ {
+ GObjectController *cont = it->second;
+ if(cont->getGObjectType()==GCONNECTOR)
+ {
+ vect.push_back(cont->getId());
+ }
+ }
+ return vect;
+ }
+
+ //=========================================================================
+
+ bool wxVtkSceneManager::isComplexBox()
+ {
+ return _isComplexBox;
+ }
+
+ //=========================================================================
+
+ void wxVtkSceneManager::setComplexBox(bool val)
+ {
+ _isComplexBox=val;
+ }
+
+ //=========================================================================
+