One of the most common questions asked by new plug-in developers is how to run, or script, existing commands from a plug-in command. Rhino doesn't allow plug-in commands to run other command except under very special circumstances.
Here's the problem: If you have a command that is modifying the run-time database, and you run another command, problems can happen.
To work around this, the Rhino SDK provides a special kind of command class: CRhinoScriptCommand. If you derive your command from CRhinoScriptCommand instead of CRhinoCommand, you will be able to use CRhinoApp::RunScript() to run other command from within your command.
NOTE: There is an equivalent class in Rhino.NET named MRhinoScriptCommand that you can derive from
This kind of command can be very dangerous. Please be sure you understand the following:
This is because CRhinoApp::RunScript() can change the dynamic arrays in the run-time database. The result is that all pointers and references become invalid. Be sure to scope your variables between CRhinoApp::RunScript() calls.
Here's good scoping practice when your command is derived from CRhinoScriptCommand.
CRhinoCommand::result CCommandTest::RunCommand(
const CRhinoCommandContext& context )
{
{
// section A
... do some stuff ...
}
RhinoApp().RunScript(...);
{
// section B
... do some stuff ...
}
RhinoApp().RunScript(...);
{
// section C
... do some stuff ...
}
RhinoApp().RunScript(...);
{
// section D
... do some stuff ...
}
RhinoApp().RunScript(...);
{
// section E
... do some stuff ...
}
return CRhinoCommand::success;
}
Never allow references and pointers from one section to be used in another section.
In a CRhinoCommand, when the user enters a command beginning with a !, the command exits. There is no documented way to get this behavior from within a CRhinoScriptCommand.