Site Tools


How to Extend RhinoScript with a .NET Plug-in

Summary: Rhino 4.0 plug-ins can now add new scripting methods to RhinoScript

Rhino 4.0 plug-ins can extend the RhinoScript with new objects and methods to permit users to create elaborate scripts for plug-ins. The Rhino plug-in class has a virtual function named GetPlugInObjectInterface which plug-ins can override and return a COM enabled class. In .NET creating a COM enabled class is as easy as adding a ComVisible attribute to a class.

VB.NET

By adding the ComVisible attribute to a class, all the public functions inside of the class are accessible to COM (ie RhinoScript).

   <System.Runtime.InteropServices.ComVisible(True)> _
Public Class MyScriptClass
  Public Sub Print()
    RhUtil.RhinoApp.Print("Hello from my VB.NET plug-in!!" + vbCrLf)
  End Sub
 
  Public Sub MakePoint(ByVal x As Double, ByVal y As Double, ByVal z As Double)
    Dim pt As New On3dPoint(x, y, z)
    RhUtil.RhinoApp.ActiveDoc.AddPointObject(pt)
  End Sub
End Class

Override GetPlugInObjectInterface and return the com class.

'inside of your MRhinoPlugIn derived class
Public Overrides Function GetPlugInObjectInterface(ByRef iid As System.Guid) As Object
  Return New MyScriptClass()
End Function

Read below on how to use your class in RhinoScript.

C#

By adding the ComVisible attribute to a class, all the public functions inside of the class are accessible to COM (i.e. RhinoScript).

[System.Runtime.InteropServices.ComVisible(true)]
public class MyScriptClass
{
  public void Print()
  {
    RhUtil.RhinoApp().Print("Hello from my C# plug-in!\n");
  }
 
  public void MakePoint(double x, double y, double z)
  {
    On3dPoint pt = new On3dPoint(x, y, z);
    RhUtil.RhinoApp().ActiveDoc().AddPointObject(pt);
  }
}

Override GetPlugInObjectInterface and return the com class.

public override object GetPlugInObjectInterface(ref System.Guid iid)
{
  return new MyScriptClass();
}

Use the plug-in script functionality

Inside of a RhinoScript routine get a hold of your plug-in's script class and have fun!

The following is some sample RhinoScript. It assumes that your plug-in is named WikiSamples.

Dim customobj
On Error Resume Next
Set customobj = Rhino.GetPlugInObject("WikiSamples")
If Err Then
  MsgBox Err.Description
Else
  customobj.Print
  customobj.MakePoint 1,2,3
  Rhino.Redraw
End If
developer/sdksamples/extendrhinoscript.txt ยท Last modified: 2015/12/04 by sandy