Author Topic: Different Property Names in Different Releases  (Read 1802 times)

0 Members and 1 Guest are viewing this topic.

kaefer

  • Guest
Different Property Names in Different Releases
« on: September 14, 2010, 10:00:34 AM »
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.
Code: [Select]
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

kaefer

  • Guest
Re: Different Property Names in Different Releases
« Reply #1 on: September 15, 2010, 01:24:46 PM »
I just checked how many more classes are there with name changes like those mentioned above,
courtesy of http://code.msdn.microsoft.com/fds.

Between 17.2 and 18.0 (2011 isn't really out yet in our national vertical product line) it's just DBText, Dimension and MText; if we concern ourselves only with those derived from Entity and residing in Autodesk.AutoCAD.DatabaseServices.

I hope the pic will make it...