美文网首页狮猿社_Rhino
Rhino 如何在C#插件运行脚本

Rhino 如何在C#插件运行脚本

作者: 锦囊喵 | 来源:发表于2020-04-21 08:15 被阅读0次

    Run a Rhino command from a Plugin

    by Dale Fugier (Last modified: 15 Apr 2019)

    This guide covers the proper techniques when running a Rhino command from within the context of a plugin command.

    The Problem

    One of the most common questions asked by new plugin developers is how to run, or script, existing Rhino commands from a plugin command. Rhino doesn’t allow plugin commands to run other commands except under very special circumstances.

    Here’s the problem: If you have a command that is modifying the Rhino document, and you run another command, problems can happen.

    To work around this, the RhinoCommon provides a special kind of command called a script command. You can create a script command as follows…

    The Solution

    When defining your command class, make sure to add the ScriptRunner command style attribute. In other words, instead of defining your command classes like this:

    [System.Runtime.InteropServices.Guid(<<test_command_guid>>)]
    public class TestCommand : Rhino.Commands.Command
    
    
    Define your command classes like this:
    [
     System.Runtime.InteropServices.Guid(<<test_command_guid>>),
     Rhino.Commands.CommandStyle(Rhino.Commands.Style.ScriptRunner)
    ]
    public class TestCommand : Rhino.Commands.Command
    
    

    Then, from within your command class’s RunCommand() method, you can call RhinoApp.RunScript() to script the running of a Rhino command. For example…

    protected override Rhino.Commands.Result RunCommand(Rhino.RhinoDoc doc, Rhino.Commands.RunMode mode)
    {
      Rhino.RhinoApp.RunScript("_-Line 0,0,0 10,10,10", false);
      return Rhino.Commands.Result.Success;
    }
    
    

    Warnings

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

    1. If you are not very familiar with how references work, you should only call Rhino.RhinoApp.RunScript() from within a RhinoScriptCommand derived command.
    2. If you are very familiar with 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 Rhino.RhinoApp.RunScript().
      2. If you get a reference or a pointer, then call Rhino.RhinoApp.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 Rhino.RhinoApp.RunScript().

    This is because Rhino.RhinoApp.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 Rhino.RhinoApp.RunScript() calls.

    Never allow references and pointers from one section to be used in another section.

    In a normal command, when the user enters a command beginning with a !, the command exits. There is no documented way to get this behavior from within a script command.

    https://developer.rhino3d.com/guides/rhinocommon/run-rhino-command-from-plugin/

    相关文章

      网友评论

        本文标题:Rhino 如何在C#插件运行脚本

        本文链接:https://www.haomeiwen.com/subject/rtjyihtx.html