COM application with RhinoCommon and Python

Hi,
I’ve been looking at some pages of the new rhinopython developer pages (nice work by the way!!). One of them attracted my attention.
It’s the developer tools diagram from Python Primer:

To be honest I never understood it, even back then when I read the Python Primer.

My question is: is it possible that with regards to Python programming language, COM application can only be accessed with rhinosciptsyntax?
Can it be accessed with Python through RhinoCommon as well?

I understand that rhinoscriptsyntax function is only a wrapper around RhinoCommon, but the RhinoScript in that photo makes me confused. Is it related to VBScript back in time when Rhino did not have Rhino Python Editor (Rhino 4)?

Thank you for the clarification.

Hi @djordje, the image is quite old. Not sure if it’s helpful, but have you seen this ?

c.

1 Like

Hi @clement.

Thank you for posting a link to that topic! I haven’t seen it previously.
I am wondering if it is possible to do something similar to that topic: access other application through Rhino Python’s Editor, or even Grasshopper ghpython components by the use of RhinoCommon, through COM application.

I understand this can be specific based on the “other” application, but is it at least possible to provide some short code example on how to set up the COM at the very beginning? Or close the connection at the very end of the script?

1 Like

Hi @djordje,

i did this for command line applications via ironpython running in a python script (or compiled script as plugin) using subprocess.Popen, in case of cmd line applications having a user interface i’ve used subprocess.Popen in conjuction with SendKeys.

If things where getting more complex i’ve used AutoIt which is pretty nice to learn as the syntax is very vb script like and it comes with hundrets of useful examples. The resulting AutoIt scripts can be compiled as exe and can be included and executed via a script or compiled python plugin. [quote=“djordje, post:3, topic:41256”]
Grasshopper ghpython components by the use of RhinoCommon, through COM application
[/quote]

I guess @Jess or @dale can answer this much better since i only have experience with above. So far i remember Dale mentioned that RhinoCommon access requires a plugin in a post on the old forum here.

c.

Thank you once again Clement!

I haven’t been using subprocess.popen previously and AutoIt indeed looks like a powerful language.
I am wondering if I could instead simply import all external application’s classes and methods with clr.AddReference and in this way not use subprocess.popen previously and AutoIt at all.

And thank you for the link to another useful topic! If I understood that topic correctly the author is trying to perform tasks in Rhino 4 and 5 without them being ran. While I would like to run Rhino 5 and with the use of Rhino Python Editor or ghpython component “communicate” with RSTAB application.

Hi @djordje just found this info, i guess you can access it via COM using vba and maybe even rhinoscript ?

c.

1 Like

This is very nice Clement!! Thank you once again!! (I am not going to use local website search ever again).

The provided examples were made in Visual Studio, with which I do not have much experience, but judging by them, the following code seems to be enough to connect Rhino 5 with RSTAB through IronPython editor:

from System.Runtime.InteropServices import Marshal
import clr
clr.AddReferenceToFileAndPath("c:/rstab8/rstab.dll")
import RSTAB
# gets interface to a running RSTAB
app = Marshal.GetActiveObject("RSTAB.Application")
# checks RS-COM license and locks the application for using by COM
app.LockLicense()


""" call RSTAB classes/methods with the use of RhinoCommon?? Not only rhinoscriptsyntax??"""


# unlocks the application and releases RS-COM license
app.UnlockLicense()
# releases COM object
app = None
# cleans Garbage Collector for releasing all COM interfaces and objects
System.GC.Collect()
System.GC.WaitForPendingFinalizers()

@djordje, have you been able to access anything using the imported RSTAB module ? I cannot test that since i do not have it installed.

c.

Hi Clement.
I have an older version of RSTAB 5, which does not seem to have .NET assemblies.
I have yet to install newer RSTAB demo version and test it upper code it’s working or not.

My last reply was not strictly directed towards you. You already helped me a lot, and I am very grateful on that!
I just wanted a reply on whether I would be able to use RhinoCommon instead of rhinoscriptsyntax.
And whether or not this is the common proper way of initializing the COM application:

from System.Runtime.InteropServices import Marshal
import clr
clr.AddReferenceToFileAndPath("someAssembly.dll")
import Assembly
app = Marshal.GetActiveObject("Assembly.Application")

Or does the last line depend on specific application with which Rhino is suppose to be communicating with?

The rhinoscriptsyntax library is itself written in RhinoCommon. Thus, Python scripts in Rhino can use either, or, or both.

Regarding your syntax for access a COM object, yes it looks reasonable. But if the object or application has been property registered (in the Windows Registry), then the “AddReferenceToFileAndPath” should be unnecessary.

– Dale

Thank you for the reply Dale!

Hi @djordje,
The “external” solution clement linked would be useful if your library will not work in IronPython. Then you could load it with Cpython and also the Rhino application and create geometry via the RhinoScript methods which are available through the COM object.

1 Like

Hi @Jess,
Thank you for the reply!!
Do you happen to have some short example code?

Hi djordje,

The example I’ve posted here: Python Automation
worked (after a restarting the computer).

It grabs an already running instance of Rhino (Interface). Of course you’ll need an independent installation of CPython and some environment to edit and run your script. I use Visual Studio also for that but I do not have a tutorial to set up everything.

Note: All this is really only useful if the library you want to use does not work with Rhino’s IronPython!

1 Like

Thank you for the reply once again @Jess !
So you have cPython v2.7.11 installed, and which comtypes version? 1.1.3?

I’ve comtypes version 1.1.2 which I think were part of my installation of cPython 2.7.11

Thank you once again @Jess!