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 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?
"""
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()
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.