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.

1 Like

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"
7 Likes

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

Hi @mzjensen 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"
1 Like

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

2 Likes

@mzjensen
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

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

3 Likes