Execute C# Code directly in Rhino

Hello,

I am wondering if there is a way to compile and run C# code directly in Rhino.

I know that there’s a C# editor in Grasshopper to do this. But there’s no such an editor or plugin for Rhino.

I can afford to have my own Rhino plugin to do this work. I’ve looked into CSharpCompiler Class in .NET but don’t have a clear idea yet.

I am wondering if there is a RhinoCommon api for this, the similar like PythonScript Class but for C#?

Or there is another way to directly use .NET api to do it?

Any suggestions would be much appreciated.

Thanks ahead!

Yse, there is a RhinoCommon for .NET (C# & VB.NET), and yes, you can write a plugin which you install directly into Rhino (this is ho0w many commands are actually added to Rhino, “natively” so to speak).

There are examples of how you write a plugin (= a command plugin) for Rhino, using VS, on these pages (common API page):

RhinoCommon:
https://developer.rhino3d.com/api/RhinoCommon/html/R_Project_RhinoCommon.htm

// Rolf

With the way of working described below, it is possible to separate declaration and implementation of C# commands. The implementation assembly is read from byte stream, and can be reloaded when the command executes. That way it can always be rebuilt while Rhino is running, and the latest version is loaded as the command executes. We use this approach, and it saves a ton of time because you don’t have to restart Rhino each time the implementation of a command changes. We also use it for Grasshopper assemblies.

3 Likes

Thanks Rolf.

Sorry for not being clear. What I meant is to execute C# code dynamically. Just like RhinoPythonEditor in Rhino, but instead, a C# editor.

I was planning to have a RhinoPlugin to compile and run C# code. It doesn’t need to be a proper editor, but it should be able to execute the code…

Any thoughts on this?

2 Likes

Thanks Menno!

this looks very promising to build the tool that I want.

Will give you a feedback once I had the chance to try it out.

Hi,

I am new user to Rhino.I want to connect to running instance of Rhino and I want to get current file name using c#.I have tried Marshal.GetActiveObject.Its throwing error.Any documents or code would be helpful.

Hi @niveshaanandhan,

You can do so using COM (e.g. ActiveX) and the RhinoScript object.

Program.cs (2.0 KB)

– Dale

If you are happy to make your own plugin for this you could try to host https://github.com/aelij/RoslynPad in a Rhino plugin. (I am working on something similar but with F#.)

1 Like