Some way to add sample sources with Dynamo in Civil 3D?

Hi! I recently started to learn and use Dynamo in my workflows and I’m impressed with it. One of my current task is to add a couple of surfaces as sample sources to some Section Groups and I thought Dynamo could help me.

I found the popular and helpful Civil3DToolkit but also I couldn’t find some node to do the job. I searched in Sections, Corridors, Basselines, SectionViews but… nothing. So, as this topic says, is there someway to add sample sources using Dynamo? Maybe I’m looking for nothing mplemented yet (⌒_⌒;)

Note: I’m asking mainly for nodes but I know basic Python so a custom code solution would be welcome too.

Thanks.

Hi @MichaelMX,

Welcome to the Forum. You can do it using the .net API and the python script node.
You can use this code as a starting point:

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# 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 *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

sampleline = IN[0]

def addsource(sampleline):

	with adoc.LockDocument():
	    with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
	        	#Get sample line ID
				slId = sampleline.InternalObjectId
				#Get sample line group ID
				slgroupId = slId.GroupId
				#Open SLGroup
				slg = t.GetObject(slgroupId, OpenMode.ForWrite)
				#Get sources
				slgs = slg.GetSectionSources()
				#Get source item
				#source = slgs.Item[2]
				#Add the source item above
				#source.IsSampled = True
				#Add all sources
				for source in slgs:
					source.IsSampled = True
				# Commit before end transaction
				t.Commit()

	return slgs
# Assign your output to the OUT variable.
OUT = addsource(sampleline)



Add SLsource.dyn (5.5 KB)

Sorry, I missed the part that you wanted to add surfaces.

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# 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 *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

sampleline = IN[0]

def addsource(sampleline):

	with adoc.LockDocument():
	    with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
	        	#Get sample line ID
				slId = sampleline.InternalObjectId
				#Get sample line group ID
				slgroupId = slId.GroupId
				#Open SLGroup
				slg = t.GetObject(slgroupId, OpenMode.ForWrite)
				#Get sources
				slgs = slg.GetSectionSources()
				#Get source item
				source = slgs.Item[0]
				#Add the source item above
				#source.IsSampled = True
				#Add all sources
				for source in slgs:
					st = source.SourceType
					if str(st) == "TinSurface":
						source.IsSampled = True
					else:
						pass
				# Commit before end transaction
				t.Commit()

	return st
# Assign your output to the OUT variable.
OUT = addsource(sampleline)

Add SLsource.dyn (5.6 KB)

Thanks, I’ll try it and study that API you mention above. Sorry for taking too long to mark your answer as correct.

Hi. I’m trying to add sources (surfaces and corridor) to sample lines. It seems that your Python code can do that but I don’t have so much Python knowledge to understand it. Can you give reference to how to use that? Or do you know a better way to add sources? I hope you can help me. Thanks you so much.

Hi @kovacsv,
I have problems to use your example Python code. I have Civil 3D 2023. There is error and Civil 3D shuts down. Have you tested this on 2023 version with Python3?

Hi @Drbohlav ,

I made this almost 2 years ago, I’m sure it was made in IronPyton 2.7. Try running it with 2.7 maybe it drops an error message. We used another version of this with corridors a few months ago, we did not experience crashes.

Hi @Drbohlav and @raul.07.11 , I have modified the code by @kovacsv to work with the CPython3 engine and to add surfaces and corridors. I am a complete beginner with Python, but I tested it with Civil 3D 2025 and it works.

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# 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 *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

# IN[0] is a single Autodesk.AutoCAD.DynamoNodes.Object
sampleline = IN[0]


def addsource(sampleline):
    # Lock the document explicitly
    doc_lock = adoc.LockDocument()
    try:
        # Get the database and transaction manager
        db = adoc.Database
        tm = db.TransactionManager

        # Start a transaction explicitly
        t = tm.StartTransaction()
        try:
            # Get sample line ID
            slId = sampleline.InternalObjectId
            # Open the sample line object
            slObject = t.GetObject(slId, OpenMode.ForRead)
            # Get sample line group ID
            slGroupId = slObject.GroupId            
            # Open SLGroup
            slGroupObject = t.GetObject(slGroupId, OpenMode.ForWrite)
            # For debug / info, get sample line group name
            slGroup = slGroupObject.Name
            # Get sources
            slSource = slGroupObject.GetSectionSources()

            # Initialize sourceType in case there are no sources
            sourceType = None
            # Initialize sourceType in case there are no sources
            sourceName = None
            # For debug / info, store all sourceType values
            sourceTypes = []
            # For debug / info, store all sourceName values
            sourceNames = []

            # Add all sources
            for source in slSource:  
                # sourceType is an int
                sourceType = source.SourceType             
                # sourceName is a string
                sourceName = source.SourceName
                sourceNames.append(sourceName)
                # From observation, 0 is TinSurface and 2 is Corridor
                if sourceType == 0:
                    source.IsSampled = True
                    sourceTypes.append("TinSurface")
                if sourceType == 2:
                    source.IsSampled = True
                    sourceTypes.append("Corridor")
                else:
                    pass

            # Commit before end transaction
            t.Commit()

        finally:
            # Ensure the transaction is disposed properly
            # (guard against older API versions that may not have Dispose)
            if hasattr(t, "Dispose"):
                t.Dispose()

    finally:
        # Release the document lock
        if hasattr(doc_lock, "Dispose"):
            doc_lock.Dispose()
    # Return a list of added source names and types
    return [slGroup, sourceNames, sourceTypes]

# Assign output to the OUT variable.
OUT = addsource(sampleline)
Caution

However, I found Civil 3D shuts down if I use it in a flow with SectionView.BySampleLine, but I will open a new topic for that issue.