How to create new IPSRV instructions

Example for creating a new IPSRV Instruction:
 #include "Instruction.h"
 #include <vector>
 #include <string>
 #include <sstream> // only necessary when something is written to the answer parameter

 class NewInstruction : public Instruction
 {
 public:
   NewInstruction(const std::string& code = std::string("newinstruction <parameter1> <parameter2"))
   :Instruction(code){;}
   virtual ~NewInstruction();
 protected:
   virtual tExecResultType vExecute(std::vector<std::string>& Instr, std::ostringstream& answer);
 };

 tExecuteResultType NewInstruction::vExecute(std::vector<std::string>& Instr, std::ostringstream& answer)
 {
    answer << "Hello World!" std::endl;
    return eOK;
 }
  1. Create a subclass of Instruction.
  2. Add additional members if necessary.
  3. Override the constructor if necessary (only when additional arguments are needed! See all available instructions for more information). The code string contains the instruction name followed by the parameters in <> and should be the last argument of the constructor. If possible use initialization lists (see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6).
    The custom parameters should always be the first ones in the parameter list of the constructor.
  4. Override the destructor if objects where allocated in the constructor. Otherwise leave it out of the header file.
  5. Override the Instruction::vExecute(...) method which does the actual work. The first argument is a vector of strings whose first element is the instruction name and the following elements contain the parameters. The second argument is an ostringstream into which the answer of the command is written. It is necessary to use
     #include <sstream> 
    
    in the file where Instruction::vExecute() is reimplemented if the ostringstream is used.
  6. After the class files have been created add the header file as an #include to $(IPSA_HOME)/include/IPSA/viewer/instructions/NVInstructions.h. In our case this would be:
     #include "NewInstruction.h" 
    
  7. The last thing to do is to register the new command. This is done by adding one line to NVInstructions::CreateInstructionMap(). In the example the line would looke like this:
     NVInstructions::AddEntry(newInstructionMap, new NewInstruction());
    
    NOTE: This line must be inserted before the class ViewerHelp is inserted as the last command

Get IPSA - Inventor Physics Simulation API at SourceForge.net. Fast, secure and Free Open Source software downloads
Generated on Sat Mar 10 20:00:17 2012 for IPSA by  doxygen 1.5.8