I’m triying to add label groups to a certain profile from a given Label Set already created. I’ve come so far, getting the correct Label Set and iterating each item for creating a new one (i’ve searched on forums and that seems the way to do this).
For development purposes only, the code below looks for a certain profile view and a specific profile to apply this labels. The idea is to work on something more general once i get the code working.
The main problem: I cant get the ProfileLineLabelGroup.Create() method to work and i don’t understand why.
Here’s a forum topic i’ve come across and found useful: Add Profile Label Set To Profile
Hope anybody can help me. Thank you!
import clr
import sys
# Add references to AutoCAD and Civil 3D assemblies
clr.AddReference('AcMgd') # AutoCAD Managed libraries
clr.AddReference('AcDbMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AeccDbMgd') # Civil 3D Managed libraries
# Import necessary namespaces
from Autodesk.AutoCAD.ApplicationServices import Application
from Autodesk.AutoCAD.DatabaseServices import OpenMode
from Autodesk.Civil.ApplicationServices import CivilApplication
# Import .NET System namespace for exception handling
clr.AddReference("System")
from System import Exception
# Get the current document and Civil 3D document
doc = Application.DocumentManager.MdiActiveDocument
db = doc.Database
civilDoc = CivilApplication.ActiveDocument
stylesRoot = civilDoc.Styles
# Input for target label set name
targetLabelSet_Name = IN[0]
# Initialize the output variable for Dynamo
OUT = None
# Single function for encapsulated logic
def process_alignments_and_labels():
"""
Process alignments, profile views, and labels in one encapsulated logic block.
"""
global OUT
try:
# Lock the document
lock = doc.LockDocument()
# Start a transaction
with db.TransactionManager.StartTransaction() as tr:
# Step 1: Retrieve target label set by name
targetLabelSet_Id = None
labelSetStyles = stylesRoot.get_LabelSetStyles()
profileLabelSetStyles = labelSetStyles.ProfileLabelSetStyles
profileLabelSetStylesIds = profileLabelSetStyles.ToObjectIds()
for styleId in profileLabelSetStylesIds:
obj = tr.GetObject(styleId, OpenMode.ForRead)
objName = obj.get_Name()
if objName == targetLabelSet_Name:
targetLabelSet_Id = styleId
print(f"Match found! Label Set: {objName}")
break
if not targetLabelSet_Id:
raise Exception(f"Target label set '{targetLabelSet_Name}' not found.")
# Step 2: Process alignments and profiles
alignment_ids = civilDoc.GetAlignmentIds()
fifth_alignment_id = alignment_ids[4] # Select the 5th alignment
fifth_alignment = tr.GetObject(fifth_alignment_id, OpenMode.ForRead)
print(f"Selected Alignment: {fifth_alignment.get_Name()}")
# Retrieve profile views for the alignment
profile_view_ids = fifth_alignment.GetProfileViewIds()
first_profile_view_id = profile_view_ids[0]
first_profile_view = tr.GetObject(first_profile_view_id, OpenMode.ForRead)
print(f"Associated Profile View: {first_profile_view.get_Name()}")
# Process profiles within the alignment
profile_ids = fifth_alignment.GetProfileIds()
for profile_id in profile_ids:
profile = tr.GetObject(profile_id, OpenMode.ForWrite)
profile_name = profile.get_Name()
# Check if profile name starts with "PL"
if profile_name.startswith("PL"):
print(f" Applying Label Set to Profile: {profile_name}")
# Apply the label set to the profile
labelSetStyle = tr.GetObject(targetLabelSet_Id, OpenMode.ForRead)
for labelItem in labelSetStyle:
label_type = labelItem.LabelStyleType
label_name = labelItem.get_LabelStyleName()
label_id = labelItem.get_LabelStyleId()
print(f" Label Type: {label_type}")
print(f" Label Name: {label_name}")
if label_type == 251704338: # Code for Line labels
labelItemStyleId = label_id
print(first_profile_view_id)
print(profile_id)
print(labelItemStyleId)
label_id = ProfileLineLabelGroup.Create(
first_profile_view_id,
profile_id,
labelItemStyleId
)
print(f" Created PVI Label Group for {profile_name}")
else:
continue
# Commit the transaction
tr.Commit()
except Exception as e:
# Assign the error message to OUT for output in Dynamo
OUT = f"An error occurred: {e}"
finally:
# Ensure the lock is released
if 'lock' in locals():
lock.Dispose()
# Execute the workflow
process_alignments_and_labels()