Accessing Rhino COM from Console App in C#.NET?

Hi all,
I can open a new session of Rhino with C# from a console application without problems. Here’s a sample…

namespace thing1
{
    class Program
    {
        static void Main(string[] args)
        {
            string rhinoId = "Rhino5x64.Application";
            System.Type type = System.Type.GetTypeFromProgID(rhinoId);
            dynamic Rhino2 = System.Activator.CreateInstance(type);
            Rhino5x64.Rhino5x64Application RHINO = (Rhino5x64.Rhino5x64Application)Rhino2;
            RHINO.Visible = 1;
        }
    }
}

However, how do I get anything useful from that code? I found several samples showing basically only accessing plug-ins, but I need to know something useful, like points, layers, windows, etc.

I was hoping to be “using Rhino” via the RhinoCommon, but I can’t seem to recast the Rhino5x64Application into a usable RhinoApp.

So, my questions are, can I recast Rhino5x64Application to RhinoApp (or equivalent) and if yes, how?

If not, how can I make a C# console app (or script from any external program) that will find/create a session of Rhino and allow me access to the RhinoApp functionality?

(Side note: I can’t make another plug-in that will be installed in Rhino, then run that from my Rhino5x64Application, because I can’t rely on my clients to ensure they will install that plug-in prior to running my exe.)

You can create an installer (for example using MSI) that will install your executable, but also create registry entries for one or more plug-ins. The next time when Rhino starts, the plug-in will be loaded, even if Rhino is started using COM interop. So, it should be possible to have 1) your executable and 2) a plug-in in Rhino that is loaded when your executable is installed.

For more info on the registry keys to set, see http://wiki.mcneel.com/developer/installingandregisteringaplugin

Another possibility is set the keys in your program (if they don’t exist), then start Rhino. Rhino should subsequently load the plug-in.

So it sounds like there is not a way to access the RhinoApp via COM?

“…it should be possible to have … 2) a plug-in in Rhino that is loaded when your executable is installed.”
“Rhino should subsequently load the plug-in.”

Just a reminder, I’m not trying to develop a Rhino plug-in. Ultimately, the goal is to run this from a different application (CATIA) but I’m just using a console app to simplify and separate the coding.

How do I access the RhinoCommon library via C#?

I think it comes down to what you want to do. If you could give some more info on that it can help.

For example, part of Rhino’s functionality is available as open source (based on OpenNURBS). If this functionality is sufficient, you could just use the NuGet package of Rhino3dmIO and don’t bother about COM or RhinoCommon at all. https://www.nuget.org/packages?q=3dm

On the other hand, using COM you can send any scripted command to Rhino. As virtually all commands are scriptable, Rhino functionality is exposed in that way. And, again, using a plug-in on Rhino side to accept one command, or plugin object, through COM from CATIA seems like a possible solution to me.

I’m trying to take an exported folder of IGS files, Import them one at at time in Rhino, search thru the geometry in Rhino, delete any surfaces that fall into a series of conditions I have, then export the remaining geometry into a SAT file format.

For the time being, I’m parallel coding my CATIA stuff in C# in a different VS2010 session. That’s going fine. But the Rhino development is new-ish to me (I used to use Rhino all the time years and years ago). I assumed COM access to RhinoApp would be easy, but I’m finding it hard to do. I’ve been able to open Rhino easily, but then using that for something isn’t working. I noticed that when i load the RhinoCommon reference and set up the “using” declaration, it formats fine. But whenever I code a line, it always says that I can’t use RhinoApp types as a static variable, which is fine, but if I can’t create an instance of Rhino, then what can I do with it?

It seems like this guy was trying to do the same thing as me…

http://www.vbforums.com/showthread.php?737581-Attach-to-running-instance-of-application-via-process

Have you considered not doing this from CATIA, but just letting the user open Rhino, load the files, then run some commands from a Rhino plug-in that you make to identify the required IGES geometry, then save the that to Acis SAT files?

These actions could, at a later stage, be automated from CATIA over COM using SendScript functionality. But that is purely optional - not a design choice made at the start.

Coding Rhino plug-ins has nothing to do with COM. You can download template projects for plug-in development through Visual Studio 2010 Extension Manager or here http://visualstudiogallery.msdn.microsoft.com/16053049-7db2-4c9f-961a-53274ac92ace.

More info on plug-in development can be found here http://wiki.mcneel.com/developer/rhinocommon and here http://4.rhino3d.com/5/rhinocommon/

HTH,

Menno

I honestly don’t want the client to even know Rhino is running. I plan to comment out the visibility function once I’m done with this.

I’ve found a workaround that should be ok for now, which uses the Rhino5x64Application method named “RunScript.” It’s basically the RVB equivalent of StartCommand (I think that’s Rhino’s method for sending text to the command line, from memory.)

I’ll just translate the IGS in via a transparent Import string via the command line in Rhino, then translate it immediately back out into SAT. I’ll have to do the conditionals on the geometry in CATIA prior to the IGS export, but it’s slower than Rhino, so I’m bummed out.

But this looks like a quick way to make this work. I assume I can still send strings to the command line via the method RunScript with the application visibility set to false, right?

Hi Nick,

Here are a couple of examples to look at:

https://github.com/dalefugier/TestDotNetAutomation
https://github.com/dalefugier/SampleCsAutomation
https://github.com/mcneel/rhinoscript/blob/master/BatchRender.vbs

There is not much you can do with the Rhino object. But from the Rhino object, you can get the RhinoScript object.

http://4.rhino3d.com/5/rhinoscript/index.html

There is not much you can do with the Rhino object. But from the Rhino object, you can get the RhinoScript object.
http://4.rhino3d.com/5/rhinoscript/index.html

I’d love to access the RhinoScript library, but when I include it with my references, I’m unable to create an instance of anything in the RhinoScript namespace.

Do you have a code that transfers the newly opened Rhino application via System.Activator.CreateInstance(type) into something that will create an instance of something from the RhinoScript library?

You cannot use System.Activator.CreateInstance to create an instance of the RhinoScript object but the object is dependent on Rhino. Thus, Rhino is the only thing that can create an instance of this object.

I figured. But hey, the RunScript thing worked well. It’s not a permanent solution, but with a small 1 second pause between commands, it converted the SAT files from the IGS directory without issue. It worked on even crazy surfaces, and it was run from CATIA. woohoo.

The rhino session stayed invisible, so the user won’t even know what is happening.

One thing though, I couldn’t figure out how to make the “-Exit” command transparent. It always wanted to ask me if I wanted to save changes. I got around it by making two preceding steps (deleting all, then savingAs dummy.3dm), then it would exit without issue. But that was the only real hiccup.

thanks for all your help. Good to know in the future that Rhino5x64Application can only use RunScript, and accessing RhinoApp or RhinoScript is only possible thru the application. Kind of a bummer though. Revit is doing the same thing with their program. Seems like COM functionality is gradually being phased out… at least CATIA is holding on to it for now. :smile:

http://4.rhino3d.com/5/rhinoscript/document_methods/documentmodified.htm

Thanks, I knew about DocumentModified, but I couldn’t access it from the Rhino5x64Application type. Is there an equivalent to DocumentModified that could be typed into the command line in Rhino?

1 Like

Sorry, no. But the Rhino object will return the RhinoScript object. With this, you can then call the DocumentModified method.

Here is an example that might be helpful:

https://github.com/dalefugier/TestDotNetAutomation/blob/master/Main.cs

Excuse me guys, in which .dll is the namespace
Rhino5x64.Rhino5x64Application
situated? I can’t find it :confused:

EDIT:
Nevermind, I found it!
It’s in the COM interface!