Hi all,
after a bit of frustration relating to the change in the TextStyleId property name in the DBText class, I'm pretty optimistic that I have found a workaround. That name change happened between the 2009 and 2010 releases and broke the otherwise extensive compatibility of the core functionality. The workaround involves Reflection to get at the appropriate PropertyInfo and provides an extension method to invoke the getter and setter thereof.
Apologies: This is a complete F# code sample. Translate into a real programming language at your own peril.
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Geometry
type acApp = Autodesk.AutoCAD.ApplicationServices.Application
let pinfo =
System.Reflection.BindingFlags.Public |||
System.Reflection.BindingFlags.Instance
|> typeof<DBText>.GetProperties
|> Array.find
(fun pi ->
pi.GetIndexParameters().Length = 0 &&
pi.Name = "TextStyle" || pi.Name = "TextStyleId" )
type DBText with
member x.MyTextStyleId
with get() =
pinfo.GetGetMethod().Invoke(x, [| |]) :?> ObjectId
and set(v: ObjectId) =
pinfo.GetSetMethod().Invoke(x, [| box v |]) |> ignore
[<Autodesk.AutoCAD.Runtime.CommandMethod("TEXTSTYLETEST")>]
let textStyleTestCmd() =
let doc = acApp.DocumentManager.MdiActiveDocument
let db = doc.Database
let ed = doc.Editor
let peo = new PromptEntityOptions("\nSelect Text: " )
peo.SetRejectMessage "\nOnly Text objects allowed. "
peo.AddAllowedClass(typeof<DBText>, false)
let per = ed.GetEntity peo
if per.Status = PromptStatus.OK then
let psr = ed.GetString("\nEnter TextStyleName: ")
if psr.Status = PromptStatus.OK then
use tr = db.TransactionManager.StartTransaction()
let tst =
tr.GetObject(db.TextStyleTableId, OpenMode.ForRead)
:?> TextStyleTable
if not(tst.Has psr.StringResult) then
ed.WriteMessage("\nTextstyle {0} not found. ", psr.StringResult)
else
let tx = tr.GetObject(per.ObjectId, OpenMode.ForRead) :?> DBText
let tstr =
tr.GetObject(tx.MyTextStyleId, OpenMode.ForRead) // Getter
:?> TextStyleTableRecord
ed.WriteMessage("\nCurrent textstyle is {0}. ", tstr.Name)
tx.UpgradeOpen()
tx.MyTextStyleId <- tst.[psr.StringResult] // Setter
tr.Commit()
Cheers, Thorsten