00001 #ifndef _instruction_h_
00002 #define _instruction_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include "../../IPSA.h"
00031 #include "../../IpsaSimulator.h"
00032 #include <string>
00033 #include <vector>
00034 #include <map>
00035
00036 #include <Inventor/nodes/SoNode.h>
00037 #include <Inventor/actions/SoSearchAction.h>
00038
00039 class IPSA_VIEWER_IMPORT_EXPORT Instruction
00040 {
00041 public:
00042 Instruction(const std::string& code);
00043 virtual ~Instruction() {;}
00044 bool findAndExecute(std::vector<std::string>& instr, std::ostringstream& answer, unsigned int& numberOfReturnValues);
00045
00046 typedef enum
00047 {
00048 eOK = 1,
00049 eUNKNOWN_ERROR = 0,
00050 eJOINT_NOT_FOUND = -1,
00051 eBODY_NOT_FOUND = -2,
00052 eJOINT_TYPE_INCOMPATIBLE = -3,
00053 eNODE_IS_NOT_A_BODY = -4,
00054 eNO_ODE_BODY_FOUND = -5,
00055 eBAD_FILENAME = -6
00056 } tExecResultType;
00057
00058 std::string getCmdString();
00059 std::string getCmdHelpString();
00060 unsigned int getCommandParameterCount();
00061
00062 template <typename T>
00063 static T GetNodeOfTypeByName(const std::string& name);
00064
00065 template <typename T>
00066 static std::map<std::string, std::string> GetAllNodesOfTypeFromViewer(IpsaSimulator* simulator);
00067
00068 protected:
00069 const std::string I_CODE;
00070 std::vector<std::string> instrdata;
00071 virtual tExecResultType vExecute(std::vector<std::string>& Instr, std::ostringstream& answer, unsigned int& numberOfReturnValues)
00072 {numberOfReturnValues = 0; return eUNKNOWN_ERROR;}
00073 };
00074
00075 template <typename T>
00076 T Instruction::GetNodeOfTypeByName(const std::string& name)
00077 {
00078 SoNode* node = SoNode::getByName(name.c_str());
00079 return dynamic_cast<T> (node);
00080 }
00081
00082 template <typename T>
00083 std::map<std::string, std::string> Instruction::GetAllNodesOfTypeFromViewer(IpsaSimulator* simulator)
00084 {
00085 SoSearchAction sa;
00086 sa.setType(T::getClassTypeId());
00087 sa.setInterest(SoSearchAction::ALL);
00088 sa.apply(simulator->getSceneGraph());
00089 SoPathList& pl = sa.getPaths();
00090
00091 std::map < std::string, std::string > nodeNameMap;
00092
00093 for (int i = 0; i < pl.getLength(); i++)
00094 {
00095 SoPath* p = pl[i];
00096 T* b = dynamic_cast<T*> (p->getNodeFromTail(0));
00097 if (NULL != b)
00098 nodeNameMap[b->getName().getString()] = b->getTypeId().getName().getString();
00099 }
00100 return nodeNameMap;
00101 }
00102
00103 #endif