Coding Python Nodes for Dynamo for Civil 3D in an External IDE

Coding python nodes for beginners here. What is a good way to set up an external IDE for python node development for civil 3d?

I see online that people have used “ctypes” to utilize .dll files in python. I also see that the default python nodes in dynamo “import clr”. I assume that’s referring to this package → https://pypi.org/project/pythonnet/
That would have to be installed into the external environment and then is that all that is needed? Then, the boilerplate code from the python nodes can be copied over into a jupyter notebook or any other IDE?

Does anyone have examples of doing this?

Looks like “clr” is allowing me to import the .dll files. and the

from Autodesk.AutoCAD.Runtime import *

type of code line is working fine but code such as:

import Autodesk.AutoCAD.ApplicationServices.Application as acapp

is not working. Guessing this is an issue with not having IronPython2 in my jupyter notebook IDE?

Guessing I just had to format it like this! Seems to be working…

Trying to recreate this script in an external IDE to test but running into a problem already: Rename XREFs in multiple dwgs - #29 by WrightEngineering

I’ve tried to use dir(Autodesk.AutoCAD.ApplicationServices.Application) and I can see that “.DocumentManager” is an available member but I get a The type initializer for '<Module>' threw an exception. error when I try to use it.

@JacobSmall do you think this is because I am missing ironpython in an external IDE? I remember us talking about needing ironpython to get this to work for some reason. Is it possible to open and iterate through documents in an external IDE without ironpython? I seem to have been able to successfully import the .dlls and am very happy to be in a jupyter notebook instead of a python node but it would help if it was functional :sweat_smile:

I think you need to save this and call it from inside the Dynamo for Civil 3D environment, although I haven’t attempted this type of setup before. Reason I think this is the case is that the Python file itself doesn’t have direct hooks into the active AutoCAD instance; DLLs are loaded sure, but you could load those without even having AutoCAD open after all.

There are two ways I have seen this done, load the .py in as a new library so you can call the definitions it contains, or by reading in the raw text of the file and using the Pythton Script from String node.

That said, if you’re going to go though the trouble of setting up an external IDE, why not shuffle over to writing a zero touch node instead of Python based ones? They’ll be faster and more reliable as a result, with (as I understand it) minimal if any added effort beyond where you are going with this.

Jacob, Thanks for the reply!

That makes a lot of sense. In Dynamo, the Dynamo application is pointing to the active autoCAD application instance. I think reading the raw text of the file and using the python script from string node seems really useful but would mostly just be helping me not copy and paste as much to the python node in Dynamo.

I think the main draw for me to be able to use a jupyter notebook is that I can easily test and dissect code in bite size pieces without having to add comments repeatedly, easy access to documentation in the application without having to run the dynamo graph, ect. It has proved incredibly useful to me with ArcGIS Pro but I know there is something similar there where I have to actually load my jupyter notebook from the python environment that is installed with ArcGIS Pro to be able to code with “arcpy”.

But I think the advantages are very limited if I can’t actually run any code. I know @mzjensen had recommended using an external IDE such as pycharm for python nodes. Now I see that he was probably referring to coding in that environment and then copying it over? Not actually running the code in the external IDE.

Also, I hadn’t heard of “zero touch nodes” before. But after looking it up I see that they are custom nodes such as the nodes in the camber package, ect. My intent with this was to write code that can be copied and pasted into a python node to help those on the forums with discrete issues that don’t necessarily warrant its own node and can be edited by the individual in dynamo. I believe if I were to write “zero touch” nodes, they wouldn’t be editable by the users and they would be difficult to share without releasing as a package, correct? Also, I unfortunately only know Python/HTML/CSS/Javascript right now and have no C# experience. (Although I do plan to work through Anton’s book on C# for AutoCAD now that it is in English).

I think the following may be a good question for @solamour: Is there a way to “hook” into an active instance of the Civil 3D/AutoCAD application in an external IDE when using python?

1 Like

That’s correct. It would be a challenge to actually hook it up to an AutoCAD process (via COM API). There’s this example for Revit, but I haven’t seen it done with AutoCAD. Probably because if you’re going to go through all of that work, you may as well just write ZT nodes.

2 Likes

Hmmm. Yeah and even that doesn’t seem to be running the file directly through pycharm. Looks like there is some “wrapper” that needs to be used? Would be really cool if I could connect a jupyter notebook directly to an AutoCAD process but even that example with revit doesn’t seem to have that functionality. Super cool though!! Maybe if I can learn more about the COM API I can get something working but it’s definitely beyond my skill set at the moment. For now I’ll probably be coding in an IDE and copying it over but I’ll definitely keep this on my “long list” :slight_smile: Thanks for the info!!

1 Like

As it stands right now, unfortunately not. Python itself is technically operating in this way with Dynamo though, as we use a bridge to transfer data between both worlds. The Python node itself will execute in the Python VM, and the results will get spit back out in Dynamo after being passed back into Dynamo’s world.

1 Like

Very interesting. So there is a bridge between the Python VM and Dynamo that is built into the dynamo source code? Which is written in C#? Is it possible to build a bridge using C# as well? If so, do you know of any good learning materials? I may be above my head but I’m just curious :slight_smile:

It’s beyond me too :smiley: The developers on the team are bonkers awesome.

We use Python.NET as the CPython3 flavour that can talk to the world of .NET which could be worth exploring :raised_hands:

2 Likes

Amazing! Thanks for sharing.

1 Like

I have a colleague in our consulting org who swears by writing his python code in .py files, and reading those into the Python environment as discussed above. If you’re just looking for the nicer IDE, the “save the file > load the file” might be the best path forward for now.

2 Likes

It was worth a try but still didn’t work. :joy: I installed jupyter notebook with pip into the embed python environment that is installed with dynamo for C3D. Still getting the same initializer error when trying to get acapp.DocumentManager. I can run a jupyter notebook from that environment but it still doesn’t know how to connect into dynamo as sol has mentioned. There is no bridge when an IDE is opened, even from the same environment. Maybe this would be obvious to someone more knowledgeable but I thought I should at least give it a shot.

@Michael_Kirschner2 @martin.misol any thoughts on how @WrightEngineering could start to integrate Jupyter Notebooks into Dynamo? Good places to look, or documentation to read?