Dynamo Civil 3D Python Help For Catchment List Creation

Hi,

I have been trying to develop my skills working with the python node in dynamo and am having some trouble with my current project. Since the latest version of C3D 2021 exposes many members of the Catchment object type, I was hoping to put together a python script that would grab all of the catchments in a drawing and create a list of “Names” and another list of the “Area2d” property. My confusion mainly lies in which libraries and references I need to load. I have not been able to find very much sample python code for dynamo to help me with the boilerplate. Any help on this would be appreciated.

Hi @hestingjj,

Here is some heavily-commented code as an example. Give it a try and see if it works for you. This will output a list of dictionaries, but you certainly don’t have to do it that way.

# The CLR (Common Language Runtime) module needs to be imported to utilize .NET libraries.
import clr

# For this exercise, these are the three AutoCAD assemblies that need to be referenced. The three at the bottom might be required in other cases.
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
#clr.AddReference('AcCoreMgd')
#clr.AddReference('AecBaseMgd')
#clr.AddReference('AecPropDataMgd')

# These resources must be imported to access the AutoCAD database. The other three at the bottom might be needed in other cases.
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
#from Autodesk.AutoCAD.Runtime import *
#from Autodesk.AutoCAD.EditorInput import *
#from Autodesk.AutoCAD.Geometry import *

# These resources must be imported to access the Civil 3D database.
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil.ApplicationServices import *

# This is only necessary because the output of the function contains dictionaries.
from System.Collections.Generic import Dictionary

# Get active document
adoc = Application.DocumentManager.MdiActiveDocument
# In this case we don't need the editor because all we're doing is reading records in the database.
#editor = adoc.Editor

# Define a new function
def get_catchment_props():

	global adoc
	# Create list of dictionary keys
	keys = ["Name","StyleName"]
	# Create empty list to hold final result, which will be a list of dictionaries
	result = []
	
	# Initiate transaction
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				# Open block table for read, not for write in this case because we are only reading property values
				bt = t.GetObject(db.BlockTableId, OpenMode.ForRead)
				# Open model space block table record as for read
				btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead)
				# Loop through each object ID in the block table record
				for oid in btr:
					# Get object by object ID
					obj = t.GetObject(oid, OpenMode.ForRead)
					# Check if object is of type Catchment
					if isinstance(obj, Catchment):
						# Create empty list to contain object properties
						vals = []
						# Append properties to list
						vals.append(obj.Name)
						vals.append(obj.StyleName)
						# Create dictionary and add keys and values
						d = {k:v for k,v in zip(keys,vals)}
						dict = Dictionary[str,object](d)
						# Append dictionary to final list of results
						result.append(dict)
				# Close transaction
				t.Commit()
	return result
OUT = get_catchment_props()
3 Likes

@zachri.jensen This bit of code should help out a lot. I had a multitude of different bugs in the code I was working with and your heavy comments will be able to help me craft what I need. Thank you for the help! This was a great learning experience. I wish there was more dynamo python node help for Civil 3D available.

1 Like

I’ve had the same desire so I made a script with out of the box nodes to make a catchment report in excel. This will likely be of use to you, except in helping you with Python :wink:
I have other catchment related scripts included in my class dataset from AU2020 that you may be interested in (Analyze Gravity Networks: Beyond the Basics | Autodesk University)
Catchment-Properties Report by Layer to Excel.dyn (111.6 KB)

2 Likes