Hello everyone, I hope you’re doing well.
I’m trying to retrieve all alignment style names in Dynamo as a list of strings from the current drawing. I wrote the code below, which successfully retrieves all alignment styles from the current drawing, but I can’t access their names as strings.
Does anyone have an idea how to get their names as a string?
Here is the code:
import clr
import sys
# 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 *
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
cdoc = CivilApplication.ActiveDocument
# Create a list to hold our output
style_objects = []
# Start Document Lock and Transaction
with adoc.LockDocument():
with adoc.Database as db:
with db.TransactionManager.StartTransaction() as t:
try:
# 1. Access the collection of Alignment Styles
# This must be indented inside the transaction block
styleIds = cdoc.Styles.AlignmentStyles
# 2. Loop through every ID found in the collection
for styleId in styleIds:
# Open the style object for reading
style = t.GetObject(styleId, OpenMode.ForRead)
# Add the Style Object to our list
style_objects.append(style)
# 3. Commit the transaction
t.Commit()
except Exception as ex:
# If something goes wrong, abort the transaction
t.Abort()
style_objects = ["Error: " + str(ex)]
# Assign your output to the OUT variable.
OUT = style_objects
Here’s you code in a readable format - please use the </> menu item or use triple backticks like this:
```python Code goes here
```
import clr
import sys
# 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
cdoc = CivilApplication.ActiveDocument
# Create a list to hold our output
style_objects = []
# Start Document Lock and Transaction
with adoc.LockDocument():
with adoc.Database as db:
with db.TransactionManager.StartTransaction() as t:
try:
# 1. Access the collection of Alignment Styles
# This must be indented inside the transaction block
styleIds = cdoc.Styles.AlignmentStyles
# 2. Loop through every ID found in the collection
for styleId in styleIds:
# Open the style object for reading
style = t.GetObject(styleId, OpenMode.ForRead)
# Add the Style Object to our list
style_objects.append(style)
# 3. Commit the transaction
t.Commit()
except Exception as ex:
# If something goes wrong, abort the transaction
t.Abort()
style_objects = ["Error: " + str(ex)]
# Assign your output to the OUT variable.
OUT = style_objects
The cdoc.Styles.AlignmentStyles returns a AlignmentStyleCollection see docs here
So you can grab the AlignmentStyle objects like this (untested) style_objects = list(cdoc.Styles.AlignmentStyles)
thank you for the replay. I upgraded the code based on your solution. I get all style IDs instead of their names. Is their a way to convert the IDs to their name.
import clr
import sys
# 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 *
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
cdoc = CivilApplication.ActiveDocument
# Create a list to hold our output
style_objects = list(cdoc.Styles.AlignmentStyles)
OUT = style_objects