Site Tools


How to Use RhinoScript from a Rhino.NET Plug-in

Version: Rhino 4

To use RhinoScript from a Rhino.NET plug-in, take several steps:

Create COM interop wrapper for RhinoScript

The plug-in must create a COM interop wrapper for RhinoScript so that all the COM methods can become available to .NET. This is easier to do than it sounds.

With your plug-in project open inside Visual Studio, go to the Project menu and select Add Reference…. In the references dialog, select the COM tab and pick RhinoScript]] 4.0 Type Library. If you don't see this item in the COM tab, select the Browse tab and pick RhinoScript.tlb located in the Rhino 4 plug-in directory. Once you've selected OK, Visual Studio will automatically create an interop DLL for you and place it in your project's bin directory. The interop file will be named Interop.RhinoScript4.DLL.

Using the RhinoScript Interface

The MRhinoApp class has a function called GetRhinoScriptInterface that a plug-in can use to get at the RhinoScript interface. Once you have the RhinoScript interface, you should be able to call any function that is defined by RhinoScript.

VB.NET

Public Shared Function Test As IRhinoCommand.result
  'attempt to get the [[developer:rhinoscript|RhinoScript]] interface
  Dim obj As Object = RhUtil.RhinoApp.GetRhinoScriptInterface()
  'cast the object to a [[developer:rhinoscript|RhinoScript]] interface
  Dim rs As RhinoScript4.IRhinoScript = CType(obj, RhinoScript4.IRhinoScript)
  If (rs Is Nothing) Then
    RhUtil.RhinoApp.Print("couldn't get RhinoScript" + vbCrLf)
    Return IRhinoCommand.result.failure
  End If
 
  'We have access to [[developer:rhinoscript|RhinoScript]]. Call some functions
  rs.EnableRedraw(False)
  Dim pt1 As Double() = {1, 2, 3}
  Dim pt2 As Double() = {1, 0, 0}
  rs.AddLine(pt1, pt2)
  rs.AddPoint(pt2)
  rs.AddSphere(pt1, 5)
  rs.EnableRedraw(True)
  Return IRhinoCommand.result.success
End Function

C#

public static IRhinoCommand.result Test()
{
  //attempt to get the [[developer:rhinoscript|RhinoScript]] interface
  object obj = RhUtil.RhinoApp().GetRhinoScriptInterface();
  RhinoScript4.IRhinoScript rs = (RhinoScript4.IRhinoScript)obj;
  if( rs == null )
  {
    RhUtil.RhinoApp().Print("could't get RhinoScript\n");
    return IRhinoCommand.result.failure;
  }
 
  //We have access to [[developer:rhinoscript|RhinoScript]]. Call some functions
  rs.EnableRedraw(false);
  double[] pt1 = {1, 2, 3};
  double[] pt2 = {1, 0, 0};
  rs.AddLine(pt1, pt2);
  rs.AddPoint(pt2);
  rs.AddSphere(pt1, 5);
  rs.EnableRedraw(true);
  return IRhinoCommand.result.success;
}
developer/sdksamples/rhinoscriptfromdotnet.txt · Last modified: 2016/03/22 by sandy