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?

2 Likes

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.

@jacob.small 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.

1 Like

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 @zachri.jensen 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:

3 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?

1 Like

@WrightEngineering I have been trying to do this exact same thing for a couple of weeks now to no avail. As a civil engineer, I am only versed in high level languages like Python and Matlab, with no C# experience. And while it seems my life would be easier by just learning C#, I would very much rather just use the tools I already know and write my stuff for AutoCAD/Civil3D in Python. Writing and debugging code inside Dynamo is just tedious, and it would be awesome if those two things could be done from an external IDE, that would also have access to the appropriate namespaces to help with autocompletion suggestions.
It seems that the best solution for now is writing the code on an external IDE, albeit without access to the Autodesk namespaces, and importing it through dynamo’s ‘Python Script From String’ node. Then we would have to debug by commenting stuff out and playing with the OUT variable in a back and forth between Dynamo and the IDE.

I think this should be a common situation. Languages like Python and Matlab are the ones civil engineers are usually familiar with. It seems odd that there’s not more people trying to hook into an active instance of AutoCAD/Civil3D. I’ll keep looking and report back if I find a better solution!

2 Likes

Thanks for asking about this. I’d also benefit from an external IDE of some sort.

@WrightEngineering my fastest workflow right now is to copy-paste back and forth between VS-Jupyter notebook and each of my Dynamo nodes. I create “inputs” similar to what the nodes would receive - can’t test C3D or ACAD objects in ipynb files though.

@zachri.jensen Thanks for all your work! Let us know when more things come out, would appreciate this a lot

1 Like

@duyematsu I thought I was the only person working like that. Have you across something new which we can use to our advantage of using IDEs for coding and debugging?

Also, on a side note do you know how I can use

import clr

and other imports in an external IDE? Or do they work only within Dynamo?
Do I need to install something to access the dll files?

Sorry for asking so many questions but these would really make working with dynamo much easier.

1 Like

@ab.chakraborty very late reply haha

Nothing exciting in my VS Code - mostly used for checking syntax errors and working w/ strings and lists. Was very useful for quickly testing regex functions.