# Use Dynamo to create and manipulate Autocad / Civil 3D layers

**URL:** <https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567>\
**Category:** Civil 3D\
**Tags:** python\
**Created:** [July 22, 2019, 2:32pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567 "2019-07-22T14:32:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnoordzij](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jnoordzij/32/32419_2.png) [@jnoordzij](https://forum.dynamobim.com/u/jnoordzij)\
**Post date:** [July 22, 2019, 2:32pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/1 "2019-07-22T14:32:35Z")

</div>

Would anybody know how to create a layer in Autocad / Civil 3D, and set name and color, using Python in Dynamo?

---

<div class="post-metadata">

**Author:** ![Durmus\_Cesur](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/durmus_cesur/32/62638_2.png) [@Durmus\_Cesur](https://forum.dynamobim.com/u/Durmus_Cesur)\
**Post date:** [July 22, 2019, 2:50pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/2 "2019-07-22T14:50:23Z")

</div>

@jacob.small

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [July 22, 2019, 4:58pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/3 "2019-07-22T16:58:28Z")

</div>

In Dynamo for Civil 3D 2020 you can feed any string value into the “Layer” input of most nodes and it will make a new layer with that name. I have not seen anyone set layer colors though. Object color is doable via OOTB nodes though.

---

<div class="post-metadata">

**Author:** ![jnoordzij](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jnoordzij/32/32419_2.png) [@jnoordzij](https://forum.dynamobim.com/u/jnoordzij)\
**Post date:** [July 22, 2019, 6:13pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/4 "2019-07-22T18:13:59Z")

</div>

I know that. But setting layer color is essential in my case. I did not find any OOTB nodes to do that, so i am eager to try some AutoCAD dotNET API stuff through Dynamo and Python. But cant find a good reference for this 🙂

---

<div class="post-metadata">

**Author:** ![Paolo\_Emilio\_Serra1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paolo_emilio_serra1/32/12902_2.png) [@Paolo\_Emilio\_Serra1](https://forum.dynamobim.com/u/Paolo_Emilio_Serra1)\
**Post date:** [July 23, 2019, 10:25am UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/5 "2019-07-23T10:25:58Z")

</div>

Hi try this [Link](https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-7BF88244-D866-40D1-8C21-7DD1A2C633DF-htm.html) to get an idea of what is the code to create a Layer.  
Attached an implementation with Python in Dynamo for Civil 3D.[CreateLayerByNameColor.dyn](https://forum.dynamobim.com/uploads/short-url/jUWurvVUIYyIFP013PCFA9THG9j.dyn) (8.7 KB)

---

<div class="post-metadata">

**Author:** ![jnoordzij](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jnoordzij/32/32419_2.png) [@jnoordzij](https://forum.dynamobim.com/u/jnoordzij)\
**Post date:** [July 23, 2019, 12:49pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/6 "2019-07-23T12:49:27Z")

</div>

Thanks for helping out, @Paolo_Emilio_Serra1. Will check this out.

---

<div class="post-metadata">

**Author:** ![mbrayN4L27](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@mbrayN4L27](https://forum.dynamobim.com/u/mbrayN4L27)\
**Post date:** [January 17, 2020, 10:17pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/7 "2020-01-17T22:17:00Z")

</div>

Hi @Paolo_Emilio_Serra1 thanks for posting the CreateLayerByNameColor script - this is super helpful! I am trying to do the same thing but instead set the color by ACI. I was reading that “ColorMethod.ByAci” can be used for this task but am getting an error in my python code when I write it this way. Do you have any suggestions?  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/2/622bf4ed8016721ccad728ca80cbbb6089ae8004.png)

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [April 17, 2020, 3:22pm UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/8 "2020-04-17T15:22:46Z")

</div>

```
"""
Copyright 2019 Autodesk, Inc. All rights reserved.

This file is part of the Civil 3D Python Module.

"""
__author__ = 'Paolo Emilio Serra - paolo.serra@autodesk.com'
__modifiedby__ = 'Deivid Steffens - deividsteffens@msn.com'
__copyright__ = '2020'
__version__ = '1.0.0'

import clr

# Add Assemblies for AutoCAD and Civil 3D APIs
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('AeccPressurePipesMgd')
clr.AddReference('acdbmgdbrep')
clr.AddReference('System.Windows.Forms')

# Add standard Python references
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math

# Add references to manage arrays, collections and interact with the user
from System import *
from System.IO import *
from System.Collections.Specialized import *
from System.Windows.Forms import MessageBox

# Create an alias to the Autodesk.AutoCAD.ApplicationServices.Application class
import Autodesk.AutoCAD.ApplicationServices.Application as acapp

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
from Autodesk.AutoCAD.Colors import Color
from Autodesk.AutoCAD.Colors import ColorMethod
# Import references for PropertySets
from Autodesk.Aec.PropertyData import *
from Autodesk.Aec.PropertyData.DatabaseServices import *

# Import references for Civil 3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

adoc = acapp.DocumentManager.MdiActiveDocument
ed = adoc.Editor

# Example function
def create_layer(name, index):

	indx = int(index)
	
	# Fail gracefully
	if not isinstance(name, str) or len(name) == 0:
		raise Exception('The name is not a valid string')
	if not isinstance(indx, int) or indx < 0 or indx > 255:
		raise Exception('The index component is not a valid integer')
	
	# replace invalid characters in the name
	name = name.replace(':', '_').replace(';', '_').replace(',', '_')
		
	global adoc
	result = False

	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				# Get the Layer Table Object
				lt = t.GetObject(db.LayerTableId, OpenMode.ForRead)
				# Check if the layer already exists
				if not lt.Has(name):
					with LayerTableRecord() as ltr:
						ltr = LayerTableRecord()
						ltr.Name = name
						ltr.Color = Color.FromColorIndex(ColorMethod.ByAci,indx)
						lt.UpgradeOpen()
						lt.Add(ltr)
						lt.DowngradeOpen()
						t.AddNewlyCreatedDBObject(ltr, True)
				else:
					ltr = t.GetObject(lt[name], OpenMode.ForWrite)
				# Set the color
				if ltr is not None:
					ltr.Color = Color.FromColorIndex(ColorMethod.ByAci,indx)
				
				result = True
				t.Commit()
		
	return result
	

if IN[0] == None or IN[1] == None:
	OUT = create_layer. __doc__
else:
	OUT = create_layer(IN[0], IN[1])
```

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 20, 2022, 5:09am UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/10 "2022-09-20T05:09:39Z")

</div>

with this script I get error: Autodesk.AutoCAD.ApplicationServices is not a package , line 27  
import Autodesk.AutoCAD.ApplicationServices.Application as acapp

---

<div class="post-metadata">

**Author:** ![kbzawork](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@kbzawork](https://forum.dynamobim.com/u/kbzawork)\
**Post date:** [September 20, 2022, 8:46am UTC](https://forum.dynamobim.com/t/use-dynamo-to-create-and-manipulate-autocad-civil-3d-layers/38567/11 "2022-09-20T08:46:13Z")

</div>

I thought that including line-type property while creating the new layer would be “straight-forward”. Looks like the line-type project is LinetypeObjectId. I managed to access the line-type table object, but havent figured out how to transfer the ObjectId of an existing line-type table record.
