Can I operate AutoCAD in GhPython?


I want to call interfaces in Python to operate AutoCAD, but IronPython 2.7 does not seem to have this win32 library.
I tried to find relevant posts on the forum, but the results were too few.
So I would like to ask everyone, is there a similar library in GhPython that can implement this function?

May I ask, Why?!!

pywin32 is over 60% C++ and C code. GitHub - mhammond/pywin32: Python for Windows (pywin32) Extensions

I don’t know of a similar library but that doesn’t mean there isn’t one. You’ll need another approach. If you install it, does pywin32 work in a CPython component in Rhino 8? Otherwise, if there isn’t a pure Python alternative to pywin32, can you do what you want in AutoCad from the command line?

I haven’t tried Rhino8, can the System library in GhPython be implemented?

Hi again Niko,

Coincidentally I was just looking into COM for something else, Component Object Model - Wikipedia so now I understand much better what you’re attempting. It’s still very impressive!

From the normal Python ecosytem (i.e CPython), pure Python libraries are usually preferred for GhPython, as it’s IronPython. However IronPython also has its own super powers. One of which lets it call .Net libraries.

I could well have a requirement to use COM too, so please could you do me a favour and let me know if you are able to get any of these three techniques to work for COM interops?

https://ironpython.net/documentation/dotnet/dotnet.html#oleautomation-and-com-interop

Since ghPython is using IronPython (not CPython) as James mentioned, you wont be able to use the COM interface.

Here are some alternatives:

Make use of IronPython’s .NET intergration:
I havent done this for AutoCAD, but typically you’d load the assembly and then use it.
Here’s an example for excel:

import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel

[reference]

Rhino.Inside.AutoCAD:
Depending on what you want to achieve, you might also consider looking into Rhino.Inside.AutoCAD.

Use Python inside AutoCAD:
Not sure what you’re trying to achieve, but you might also use CadPythonShell to automate AutoCAD directly from inside itself.

Hope this helps.

I successfully implemented API access, but I was unable to instantiate this class in GhPython. I get stuck during the test run, but nothing happens

I think I need to implement a simple script in GhPython to operate AutoCad from the battery. But the situation is a bit complicated. The API interface I implemented called, but it didn’t work

Well done!

What does print(dir(...)) on the import modue show you is in it? Isn’t it just cad.AcadApplication, without Class on the end?

Autodesk.AutoCAD.Interop

I think you can also call C# libraries from IronPython (using C# generics too).