developer:runrhinocommandfromplugincommand


How to run a Rhino command from a plug-in command

Developer: C++, .NET
Summary: Discusses the proper techniques to use when running Rhino command from within the context of a plug-in command.

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

More Information

This kind of command can be very dangerous. Please be sure you understand the following:

  1. If you are not very familiar with how C++ references work, you should only call CRhinoApp::RunScript() from within a CRhinoScriptCommand derived command.
  2. If you are very familiar with C++ references, then please observe the following rules:
    1. If you get a reference or pointer to any part of the Rhino run-time database, this reference or pointer will not be valid after you call CRhinoApp::RunScript().
    2. If you get a reference or a pointer, then call CRhinoApp::RunScript(), and then use the reference, Rhino will probably crash.
    3. All pointers and references used by the command should be scoped such that they are only valid for the time between calls to CRhinoApp::RunScript().

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.

Example

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.

Downsides

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.


developer/runrhinocommandfromplugincommand.txt · Last modified: 2010/10/05 15:37 by dale Driven by DokuWiki Recent changes RSS feed

 © 1997-2012 

McNeel North America Europe Latin AmericaAsia