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

**URL:** <https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779>\
**Category:** Civil 3D\
**Tags:** python, dynamo\
**Created:** [July 2, 2021, 5:21pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779 "2021-07-02T17:21:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre.demski](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/andre.demski/32/124887_2.png) [@andre.demski](https://forum.dynamobim.com/u/andre.demski)\
**Post date:** [July 2, 2021, 5:21pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/1 "2021-07-02T17:21:03Z")

</div>

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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/9/3980a801f133ad67af9ec3ae6be851f4d449d900.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/7/3766c8a1742d5e55f6b80a8a9ee6c8d2154401c2.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/c/ec442a5d41a01d3a1602443ee4f6d88fcd8f0178.png)

---

<div class="post-metadata">

**Author:** ![WrightEngineering](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/wrightengineering/32/115676_2.png) [@WrightEngineering](https://forum.dynamobim.com/u/WrightEngineering)\
**Post date:** [July 2, 2021, 6:54pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/2 "2021-07-02T18:54:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [July 2, 2021, 7:08pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/3 "2021-07-02T19:08:36Z")

</div>

This will send a command to the command line.

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![WrightEngineering](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/wrightengineering/32/115676_2.png) [@WrightEngineering](https://forum.dynamobim.com/u/WrightEngineering)\
**Post date:** [July 3, 2021, 10:18pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/4 "2021-07-03T22:18:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christhian](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Christhian](https://forum.dynamobim.com/u/Christhian)\
**Post date:** [August 2, 2021, 11:25pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/5 "2021-08-02T23:25:00Z")

</div>

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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/d/3d8c64efbeb006f40e91703fed29980b2fb162e2.gif)

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [August 2, 2021, 11:37pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/6 "2021-08-02T23:37:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christhian](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Christhian](https://forum.dynamobim.com/u/Christhian)\
**Post date:** [August 3, 2021, 12:33am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/7 "2021-08-03T00:33:16Z")

</div>

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

```

![Animation](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/0/f0cd09be43767e332b490a1929bf303f495d880e.gif)

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [August 3, 2021, 1:01am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/8 "2021-08-03T01:01:27Z")

</div>

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

```auto
from Autodesk.AutoCAD.ApplicationServices import *

input = IN[0]

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

OUT = input + " sent to command line"

```

---

<div class="post-metadata">

**Author:** ![Christhian](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Christhian](https://forum.dynamobim.com/u/Christhian)\
**Post date:** [August 3, 2021, 1:12am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/9 "2021-08-03T01:12:51Z")

</div>

![Animation1](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/4/74e95b202a9f5f06abba7a764d23996b6d76ee7e.gif)

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

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [August 3, 2021, 1:34am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/10 "2021-08-03T01:34:49Z")

</div>

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.

```auto
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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/9/19830770cc219b5414adb1f2b912e99b1201bbe6.gif)

---

<div class="post-metadata">

**Author:** ![Christhian](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Christhian](https://forum.dynamobim.com/u/Christhian)\
**Post date:** [August 3, 2021, 2:22am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/11 "2021-08-03T02:22:45Z")

</div>

@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](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/a/1ae424b6376b0a4278db0272c535b47818e8e349.gif)

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [August 3, 2021, 2:42am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/12 "2021-08-03T02:42:03Z")

</div>

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"`.

> [@Christhian](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Christhian](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@Christhian](https://forum.dynamobim.com/u/Christhian)\
**Post date:** [August 3, 2021, 2:45am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/13 "2021-08-03T02:45:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [January 6, 2022, 8:17pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/14 "2022-01-06T20:17:35Z")

</div>

@andre.demski @Christhian @WrightEngineering

FYI [Camber v2.0.0](https://github.com/mzjensen/Camber/releases/tag/v2.0.0) has a node to send commands to the command line.

 ![sendcommand](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/2/d22878676b1822937a9205ebdd13218d6f2a2d0c.png)

---

<div class="post-metadata">

**Author:** ![WrightEngineering](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/wrightengineering/32/115676_2.png) [@WrightEngineering](https://forum.dynamobim.com/u/WrightEngineering)\
**Post date:** [January 7, 2022, 12:28am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/15 "2022-01-07T00:28:50Z")

</div>

Nice!!

---

<div class="post-metadata">

**Author:** ![bechristhian](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@bechristhian](https://forum.dynamobim.com/u/bechristhian)\
**Post date:** [January 8, 2022, 5:32am UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/16 "2022-01-08T05:32:01Z")

</div>

Great

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [January 11, 2022, 9:48pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/17 "2022-01-11T21:48:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [January 11, 2022, 10:03pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/18 "2022-01-11T22:03:14Z")

</div>

> [@patrick.ericson](#):
>
> I wonder, is there a way to have more than one command in the line.

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/c/bc135bc24fcd8bce7bed1fad6df770aa5710dffe.png)

---

<div class="post-metadata">

**Author:** ![suraky](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@suraky](https://forum.dynamobim.com/u/suraky)\
**Post date:** [June 1, 2022, 11:25pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/19 "2022-06-01T23:25:36Z")

</div>

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.

 ![attsync](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/d/9ddd344cb3796d81560ea86b157302073b7ff630.jpeg)

---

<div class="post-metadata">

**Author:** ![WrightEngineering](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/wrightengineering/32/115676_2.png) [@WrightEngineering](https://forum.dynamobim.com/u/WrightEngineering)\
**Post date:** [June 1, 2022, 11:28pm UTC](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779/20 "2022-06-01T23:28:28Z")

</div>

Nice Ken. Thanks for sharing.

[Next page](https://forum.dynamobim.com/t/how-to-use-civil-3d-commands-in-a-python-script/65779.md?page=2)
