[HELP] Using Dynamo Python Script to Set Applied Cant Value

Hello,

I’m trying to create a “Set Applied Cant” node using Python in Dynamo for Civil 3D. The idea is it would function similar to the existing “Set Slope” node in the Civil3D Toolkit with the exception that it would set the applied cant at the beginning and end of curve for a railway alignment instead of the superelevation slope for a centerline alignment. This is a first attempt to use Python and the Civil 3D .NET API for so I thought the forums would be a great place to ask for some assistance! I’ve attached a test script, but I’m getting this error message:

AttributeError: ‘Alignment’ object has no attribute ‘CANTCriticalStation’”.

I am confused because my input is a Rail Alignment and has applied cant value. Please see my attached script below.

import clr

clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('ProtoGeometry')

from Autodesk.DesignScript.Geometry import *

from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil.DatabaseServices.Styles import *

adoc = Application.DocumentManager.MdiActiveDocument
civdoc = CivilApplication.ActiveDocument

alignment = IN[0]
criticalstation1 = IN[1]
criticalstation2 = IN[2]
cantvalue = IN[3]

# This test script should set cant value at a specified station (input:station1). 
def setAppliedCant(alignment,station1,station2,appliedcantvalue):

	global adoc
	global civdoc
	
	output = []
	

	with adoc.LockDocument():
	
	    with adoc.Database as db:
	    
	        with db.TransactionManager.StartTransaction() as t:
	  
				alignmentId = alignment.InternalObjectId

				obj = t.GetObject(alignmentId, OpenMode.ForWrite)
				
				criteriaStationColl = obj.CANTCriticalStation
				
				SCSIN2 = criteriaStationColl.GetCriticalStationAt(station1, 0.01)
				
				#Set Applied cant value using input appliedcantvalue
				
				SCSIN2.AppliedCANT = appliedcantvalue

				t.Commit()
				
	return SCSIN2
	

OUT = setAppliedCant(IN[0],IN[1],IN[2],IN[3])

I’ve tried to also imitate the process with SuperelevationCriticalStations Class as opposed to CANTCriticalStations Class as a reverse engineering attempt. I made some progress this way, but I ended up with another error
SystemError: Operation is not valid due to the current state of the object.”
Please see the script below

import clr

clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AeccDbMgd')
clr.AddReference('ProtoGeometry')

from Autodesk.DesignScript.Geometry import *

from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil.DatabaseServices.Styles import *

adoc = Application.DocumentManager.MdiActiveDocument
civdoc = CivilApplication.ActiveDocument

alignment = IN[0]
station = IN[1]
supervalue = IN[2]

def setAppliedCant(alignment,criticalstation,appliedsuper):

	global adoc
	global civdoc
	
	output = []

	with adoc.LockDocument():
	
	    with adoc.Database as db:
	    
	        with db.TransactionManager.StartTransaction() as t:
	  
				alignmentId = alignment.InternalObjectId

				obj = t.GetObject(alignmentId, OpenMode.ForWrite)
				
				criteriaStationColl = obj.SuperelevationCriticalStations
				
				SCSIN2 = criteriaStationColl.GetCriticalStationAt(criticalstation, 0.01)
				
				SCSIN2.SetSlope(0,appliedsuper)

				t.Commit()
				
	return SCSIN2

OUT = setAppliedCant(IN[0],IN[1],IN[2])

There does seem to be some different properties between CANT and superelevation. For example, SetSlope in SuperelevationCriticalStation Class is a method however in CANTCriticalStation.AppliedCANT is a property. I am really stuck and any help or guidance to figure this out would be greatly appreciated.

Thank you very much!

Hi @alex.han,

The error you’re getting for the CANT critical station is a simple spelling error. The property is actually misspelled in the API as ‘CANTCriticalStaitons’ instead of ‘CANTCriticalStations’.

Hi mzjensen,

Thank you very much for the reply. The script works now! I could not believe the typo is in the API itself, haha

Really appreciate your help!

Hello, @alex.han
if you can attach the solution
Thanks you

Hi @hosneyalaa, you can just change the ‘CANTCriticalStations’ in the script above to ‘CANTCriticalStaitons’, and it should work. There is a typo in the API.

1 Like

@alex.han
Thank you all