How to use civil 3D commands in a Python Script?

Is there any library that I can load to use all Civil 3D commands to use in Python Script?

Like these commands (Just examples):

image
image
image

1 Like

Would be cool to be able to string together civil 3d commands as python in dynamo. Arcgis pro has a feature where you can conduct an action and then copy a code version from a history window into a python window. Super useful. I’d love something similar for civil 3d.

2 Likes

This will send a command to the command line.

import System
from System import *

input = IN[0]

app = System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application")
adoc = app.ActiveDocument

adoc.SendCommand(input)							

OUT = str(input) + " sent to command line"
12 Likes

Thanks Zachri! You’re the man! This will be super useful!

Hi @zachri.jensen i am trying to use the code with a simple example that is to copy but it does not work for me or layers I am doing something wrong, a query can be linked to several commands, you could put an example, thanks

Animation

@Christhian put a space or a “\n” character at the end so you are sending a way to trigger the command. What you have shown right now would be the equivalent of typing “Copy” into the command line but then not pressing spacebar or enter.

OUT = str(input) + " sent to command line" + " "

Animation

Ah OK, it’s a Python engine issue. Try this instead.

from Autodesk.AutoCAD.ApplicationServices import *

input = IN[0]

adoc = Application.DocumentManager.MdiActiveDocument
adoc.SendStringToExecute(input, True, False, False)

OUT = input + " sent to command line"
2 Likes

Animation1

It still does not work, it will have something to do with it being civil 2022?

If you’ve already run the graph once and didn’t change any of the inputs, then the node won’t run again. You could add a simple toggle to run it again even if the input string didn’t change.

from Autodesk.AutoCAD.ApplicationServices import *

input = IN[0]
toggle = IN[1]

adoc = Application.DocumentManager.MdiActiveDocument
adoc.SendStringToExecute(input, True, False, False)
    
OUT = input + " sent to command line"

Toggle

4 Likes

@zachri.jensen
If it works but it does not give me direct to select the object, if I do not have to click and hit enter so that it just works, I do not know if it will be for dynamo or some configuration that I must perform in civil 3d

An additional question that I have is that I could copy and make the copy rotate 45 degrees automatically, sorry if without leaving the first question I consult another.

Animation11

In the example, you are adding a space to the output (the OUT variable), which is not what is actually sent to the command line. The adoc.SendStringToExecute() method is what is actually doing the work. You’ll notice in the recording I attached earlier that I put a space in the input string node, so it says "Copy " (notice the space). If you want to add it directly to the code, it needs to be concatenated with the input string, not the output. So either input + " " or input + "\n".

Try the Object.Copy and Object.Transform nodes. If that doesn’t work, it’s probably good to open a new thread like you mentioned.

1 Like

@zachri.jensen Great, works great now, thank you very much for the time and advice.
Thanks

3 Likes

@andre.demski @Christhian @WrightEngineering

FYI Camber v2.0.0 has a node to send commands to the command line.

9 Likes

Nice!!

Great

It’s a really great advantage with this node, @zachri.jensen
I wonder, is there a way to have more than one command in the line. I have tried but haven’t succeed yet.
And sometimes I realize that a command will not be runned again from Dynamo, do I need to insert some kind of trigger to “update” the script or should I run it from the Dynamo Player instead?

Thanks for all your hard work and all your effort!!

Regards,
Patrick

One way is to separate the commands with a space or \n character to chain them together.

Just wanted to share, here’s a simple example of using Document.SendCommand. After using Dynamo for placing a bunch of blocks and changing text in attribute tags in those blocks. I’m using this node to run ATTSYNC, so the tag text appears in the right place. The “x” variable is just there so the node won’t run until the node upstream of it has run.

7 Likes

Nice Ken. Thanks for sharing.

1 Like