Modify text Style on Dynamo

This isn’t quite the same as what @Kulkul did, but give it a try. Provide a string input for the name of the text style you want to change to and it will change all Text and MText in model space.

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# 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 *
from Autodesk.AutoCAD.GraphicsInterface import TextStyle

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

errorReport = None

def set_all_texts_style(style):
	
	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)
				
				try:
					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)
					elif not style:
						return "No input"
					else:
						return 'The text style "{}" does not exist.'.format(style)
				except:
					import traceback
					errorReport == traceback.format_exc()

				t.Commit()
	if errorReport == None:
		return output
	else:
		return errorReport
	
OUT = set_all_texts_style(IN[0])

Adapted and modified slightly from @Paolo_Emilio_Serra1’s original script here:

4 Likes