Get list of styles

I’m trying to get a list of alignment styles in a drawing, but I get an error.

It looks like the “Name” property is only available for “Set” and not “Get”. Does anyone know how to get the style name with the C3D API?


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

output = []

with adoc.LockDocument():
	with adoc.Database as db:
		with db.TransactionManager.StartTransaction() as t:
			styleCol = civdoc.Styles.AlignmentStyles

			for i in styleCol:
				styleObj = t.GetObject(i, OpenMode.ForRead)


OUT = output

As far as I can see it is comparable with my C# code where I collect the AlignmentStyle names. You can read the Name property and add it to a list.

My Python skills are not that good that I can solve your problem. Maybe you can check the styleObj fist (if it is an AlignmentStyle and not null). Does your drawing contain purged styles?

you can use COM in this case: styleObj.AcadObject.Name will work


Maybe a silly question :slight_smile:

To be clear: is it only possible to use COM API and not .NET API in Python?

That is not what I meant, I realize now my answer could be misleading.
I’m using .NET and/or COM objects via Python.
In this case I’m accessing the COM object (AcadObject) via .NET, this returns a dynamic object in C# terms, and then I can safely get the Name property on the object.


@keith.sowinski thanks for sharing your code. @Paolo_Emilio_Serra1 This is very interesting for me as I’m only familiar with .NET API, for those interested here’s the Python accessing the COM API. Is there a reason as to why certain objects are only exposed in COM API and not .NET?

# Load Python standard libraries
import clr

# Add Assemblies for AutoCAD and Civil 3D APIs

# Add standard Python references
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math

# Add references to manage arrays, collections and interact with the user
from System import *
from System.IO import *
from System.Collections.Specialized import *
from System.Windows.Forms import MessageBox

# Create an alias to the Autodesk.AutoCAD.ApplicationServices.Application class
import Autodesk.AutoCAD.ApplicationServices.Application as acApp

# Import references for Civil 3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *
from Autodesk.Civil.DatabaseServices.Styles import *

# 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 for PropertySets
from Autodesk.Aec.PropertyData import *
from Autodesk.Aec.PropertyData.DatabaseServices import *

# Declare global variables
aDoc = acApp.DocumentManager.MdiActiveDocument
ed = aDoc.Editor
cDoc = CivilApplication.ActiveDocument

# Functions

def get_alignment_styles():
    This method creates gets the alignment style names in the current document.
    :return: A list of alignment style names
    result = []
    # Get the active document in the AutoCAD session:
    with aDoc.LockDocument():
        with aDoc.Database as db:

            with db.TransactionManager.StartTransaction() as t:
                styleCol = cDoc.Styles.AlignmentStyles

                for i in styleCol:
                    styleObj = t.GetObject(i, OpenMode.ForRead)
    return result

# Output	
OUT = get_alignment_styles()
1 Like