Error when setting object Text Style

I am following this guide by @mzjensen to set a text style. For some reason the line:

obj.TextStyle = text_styles[style]

gives me exception error.
image

Below is cleaned up code:

with adoc.LockDocument():
    with adoc.Database as db:
        with db.TransactionManager.StartTransaction() as t:				
            text_styles = t.GetObject(db.TextStyleTableId, OpenMode.ForRead)
            
            style = "SHR"
            ln = int("6FEA", 16)
            hn = Handle(ln)
            id = db.GetObjectId(False, hn, 0)
            obj = t.GetObject(id, OpenMode.ForWrite)
            #below line fails. I am able to get obj, id, hn, ln append to the output array so I see no issue with that line
            obj.TextStyleId = text_styles[style]
            output.append(obj)  
            t.Commit()

I think I am missing something or doing something wrong. Any ideas?

bump. any suggestions? I think I have tried all suggestions that I could find online and all of those point to the above suggestion. I am lost at what I am doing wrong…

I think the object is not in a state where it can be modified. But you can also try to verify that:

  • Text style SHR is in the text style table
  • 6FEA corresponds to valid object in the database
  • Object can be modified
  • Another process is not holding a lock on the object

Thank you for you reply. I have checked the first two items in your list.

  • SHR is a default text style that AutoCAD created. I am testing the script on a blank drawing with just a MText object.
  • 6FEA is indeed the handle ID of the MText object I created.

I don’t think there is any process that would lock the object as it is plain drawing with that object only.

Are you able to test the script on your end and confirm if it is working?

image

image

I will try to check and update you.

1 Like

I checked and verified that I was receiving the same error and I was able to fix the problem by installing DynamoIronPython package. Can you try to install DynamoIronPython 2.7 package and check again?

My Python code:

"""
Copyright 2019 Autodesk, Inc. All rights reserved.

This file is part of the Civil 3D Python Module.

"""
__author__ = 'Paolo Emilio Serra - paolo.serra@autodesk.com'
__copyright__ = '2019'
__version__ = '1.0.0'

import clr

# Add Assemblies for AutoCAD and Civil 3D APIs
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')

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

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

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.GraphicsInterface import TextStyle

adoc = acapp.DocumentManager.MdiActiveDocument
ed = adoc.Editor

# Example function
def set_all_bylayer():
	"""
	Sets the object color to ByLayer
	:returns: The list of objects in the drawing
	"""
	global adoc
	output = []
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
				btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
				for oid in btr:
					obj = t.GetObject(oid, OpenMode.ForWrite)
					obj.ColorIndex = 256
					obj.Linetype = 'ByLayer'
					output.append(obj)
				t.Commit()
	return output


def set_all_texts_style(style):
	"""
	Sets all teh DBText and MText objects to ht especified style:
	:param style: the name of the target text style
	:return: the list of Text entities
	"""
	global adoc
	output = []
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
				btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
				
				text_styles = t.GetObject(db.TextStyleTableId, OpenMode.ForRead)
				
				if text_styles.Has(style):
					for oid in btr:
						obj = t.GetObject(oid, OpenMode.ForWrite)
						if isinstance(obj, (DBText, MText)):
							obj.TextStyleId = text_styles[style]
							output.append(obj)
				else:
					return 'The text style {} could not be found'.format(style)

				t.Commit()
	return output
	
OUT = set_all_texts_style(IN[0]), set_all_bylayer()

FYI @mzjensen .

Thanks for reporting back and yes, I am able to change the text style using 2.7 version. I will use 2.7 for this requirement. Hoping it gets the fix in the new version soon.

It might be the index approach that does not work in Python, in this line:

text_styles[style]

Maybe there is a GetTextStyle() method as replacement?

That indeed could be the case because it errors out on below line as well.

btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
1 Like