Using DistanceToAlignment method in for loop

Hi,
I’d like to query the distances of an alignment from my centerline at every round station using DistanceToAlignment method.
If I use it for an exact station, it works, but when I try to use it in a loop it crashes.
Do you have any idea, what I did wrong?

# 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.
centerline_dyn = IN[0]   # Centerline alignment (Dynamo object)
exg_pav_edge_lt_dyn = IN[1]   # Existing pavement edge - left side (Dynamo object)

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

with adoc.LockDocument():
    with adoc.Database as db:

        with db.TransactionManager.StartTransaction() as t:
            
            import math
            
            # Unwrapping the dynamo objects to access Civil 3D objects.
            centerline = t.GetObject(centerline_dyn.InternalObjectId, OpenMode.ForRead)
            exg_pav_edge_lt = t.GetObject(exg_pav_edge_lt_dyn.InternalObjectId, OpenMode.ForRead)
            
            # Original start and end stations of centerline.
            start_station = centerline.StartingStation
            end_station = centerline.EndingStation
            
            # Rounded versions of start_station and end_station.
            start_station_rnd = math.ceil(start_station)
            end_station_rnd = math.floor(end_station)
            
            # All round station of centerline.
            station = range(start_station_rnd, end_station_rnd + 1)
            
            # Existing lane width on left side at an exact station. - THIS WORKS
            #exg_lane_width_lt = centerline.DistanceToAlignment(station[50], exg_pav_edge_lt, 0, 0)
            
            # Existing lane width on left side at each station. - THIS DOES NOT WORK
            exg_lane_width_lt = []
            for i in station:
                exg_lane_width_lt.append(centerline.DistanceToAlignment(i, exg_pav_edge_lt, 0, 0)
            
            t.Commit()
            pass

# Assign your output to the OUT variable.
OUT = exg_lane_width_lt

DistanceToAlignment_issue.dwg (927.3 KB)
DistanceToAlignment_issue.dyn (8.5 KB)

1 Like

There’s a parentheses missing at the end of this line:

exg_lane_width_lt.append(centerline.DistanceToAlignment(i, exg_pav_edge_lt, 0, 0)

You can also use try/except if you want to make sure it doesn’t crash if it can’t get the distance to the alignment for any possible reason.

Thanks a lot!
I can’t believe it, it’s not the first occasion :slightly_frowning_face:

An additional question:
Do you get the same warning text as me when you run the wrong script?

Warning: CPythonEvaluator.EvaluatePythonScript operation failed. 
SyntaxError : ('invalid syntax', ('<string>', 61, 13, '            t.Commit()\n'))

Yeah, I get the exact same text message.

1 Like