In order to use RhinoScript from a Rhino.NET plug-in, we have to take several steps:
The plug-in must create a COM interop wrapper for Rhinoscript so all of the COM methods can become available to .NET. This is easier to do than it sounds. With your plug-in project open inside of 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
The MRhinoApp class has a function called GetRhinoScriptInterface that can be used by a plug-in 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.
Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
As IRhinoCommand.result
'attempt to get the RhinoScript interface
Dim obj As Object = RhUtil.RhinoApp.GetRhinoScriptInterface()
'cast the object to a 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 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
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
//attempt to get the 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 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;
}