Accessing object properties in an AutoCAD .NET application

This post takes the sample code shown in the last entry, converts it to VB.NET and adds in some code to access specific entity properties rather than simply calling list().

I won't step through the code this time, but here's the section of code that gets and dumps out the various entity properties:

Dim ent As Entity = CType(tr.GetObject(objId, OpenMode.ForRead), Entity)

' This time access the properties directly

ed.WriteMessage(vbLf + "Type:         " + ent.GetType().ToString)

ed.WriteMessage(vbLf + "  Handle:     " + ent.Handle().ToString)

ed.WriteMessage(vbLf + "  Layer:      " + ent.Layer().ToString)

ed.WriteMessage(vbLf + "  Linetype:   " + ent.Linetype().ToString)

ed.WriteMessage(vbLf + "  Lineweight: " + ent.LineWeight().ToString)

ed.WriteMessage(vbLf + "  ColorIndex: " + ent.ColorIndex().ToString)

ed.WriteMessage(vbLf + "  Color:      " + ent.Color().ToString)

ent.Dispose()

We're using the ToString property to help generate a string to display through the AutoCAD command-line - otherwise the code should be fairly obvious.

Here's the complete VB.NET sample:

Imports Autodesk.AutoCAD

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

Namespace SelectionTest

  Public Class PickfirstTestCmds

    ' Must have UsePickSet specified

    <CommandMethod("PFT", _

      (CommandFlags.UsePickSet _

        Or CommandFlags.Redraw _

        Or CommandFlags.Modal))> _

    Public Shared Sub PickFirstTest()

      Dim doc As Document = _

        Application.DocumentManager.MdiActiveDocument

      Dim ed As Editor = doc.Editor

      Try

        Dim selectionRes As PromptSelectionResult

        selectionRes = ed.SelectImplied

        ' If there's no pickfirst set available...

        If (selectionRes.Status = PromptStatus.Error) Then

          ' ... ask the user to select entities

          Dim selectionOpts As PromptSelectionOptions

          selectionOpts = New PromptSelectionOptions

          selectionOpts.MessageForAdding = _

            vbLf & "Select objects to list: "

          selectionRes = ed.GetSelection(selectionOpts)

        Else

          ' If there was a pickfirst set, clear it

          ed.SetImpliedSelection(Nothing)

        End If

        ' If the user has not cancelled...

        If (selectionRes.Status = PromptStatus.OK) Then

          ' ... take the selected objects one by one

          Dim tr As Transaction = _

            doc.TransactionManager.StartTransaction

          Try

            Dim objIds() As ObjectId = _

              selectionRes.Value.GetObjectIds

            For Each objId As ObjectId In objIds

              Dim ent As Entity = _

                CType(tr.GetObject(objId, OpenMode.ForRead), Entity)

              ' This time access the properties directly

              ed.WriteMessage(vbLf + "Type:        " + _

                ent.GetType().ToString)

              ed.WriteMessage(vbLf + "  Handle:    " + _

                ent.Handle().ToString)

              ed.WriteMessage(vbLf + "  Layer:      " + _

                ent.Layer().ToString)

              ed.WriteMessage(vbLf + "  Linetype:  " + _

                ent.Linetype().ToString)

              ed.WriteMessage(vbLf + "  Lineweight: " + _

                ent.LineWeight().ToString)

              ed.WriteMessage(vbLf + "  ColorIndex: " + _

                ent.ColorIndex().ToString)

              ed.WriteMessage(vbLf + "  Color:      " + _

                ent.Color().ToString)

              ent.Dispose()

            Next

            ' Although no changes were made, use Commit()

            ' as this is much quicker than rolling back

            tr.Commit()

          Catch ex As Autodesk.AutoCAD.Runtime.Exception

            ed.WriteMessage(ex.Message)

            tr.Abort()

          End Try

        End If

      Catch ex As Autodesk.AutoCAD.Runtime.Exception

        ed.WriteMessage(ex.Message)

      End Try

    End Sub

  End Class

End Namespace

16 responses to “Accessing object properties in an AutoCAD .NET application”

  1. Great,

    As GetType(), Handle(), etc. are Entity's properties and Entity wrapper is derived from object it would appear at a PropertyGrid control?
    So it should accept something like:

    propertyGrid1.SelectedObject = ent;

    Have you tried that?
    Regards,
    Fernando Malard.

  2. Hi Fernando,

    Yes, this will show the various properties of a selected object... you would want to filter them in some way, though, as there are a lot of properties that no user will get any value from (one of the reasons we don't automatically display all COM properties in the Property Palette).

    Cheers,

    Kean

  3. Francisco Lomas Avatar

    Hi!! What about accesing object data?? Example you define an object class with custom attributes, how can you get the custom attributes??

  4. Do you mean Map3D Object Data? Presumably you'd be able to use the Map3D .NET API to access that in a similar way... (I'm not a Map3D specialist.)

    Kean

  5. I'm rather new to writing (even attempting to write) code for AutoCAD use. However I was wondering about also including a section to display the block name if the object selected is a block reference. I would also like any suggestions to books or other documentation to assist with learning/utilizing “.net” language for AutoCAD usage.
    Thanks in advanced,
    Billy

  6. Hi!
    I´m new in AutoCAD Map 3D.
    I´m very interesting about selected drawing object and Object DataTable.
    My entity is Polyline. But I need read its fields from dataTable.
    If I use AutoCad Interface is: Map / Data Objects / Edit Data Object, but how I do it in VB.net or C#?
    Thank in advanced,
    Sorry.. my english is very bad, my first language is Spanish

  7. I'm afraid I don't know anything about the Map 3D API. I suggest trying our online discussion groups.

    Kean

  8. Hi Kean,
    For the line       Dim doc As Document = _
            Application.DocumentManager.MdiActiveDocument
    I get common runtime Language detected an invalid program error.

    I am using vs2012, acad2013

    Can you help?

  9. Kean Walmsley Avatar

    Hi Mandar,

    Sorry - I have no way of knowing what's going wrong, here.

    Please post steps to reproduce the problem to the AutoCAD .NET Discussion Group (or to ADN, if you're a member).

    Regards,

    Kean

  10. if i have a line in block table record, but i want to change the property of line such as start point. i don't know solve it, please help me

  11. Sounds like you need to work through the AutoCAD .NET Tutorial (and look at some other into material on the AutoCAD Developer Center: autodesk.com/developautocad). Assuming you're asking about .NET, of course (you haven't said).

    You would typically access the Line in the BlockTableRecord via a Transaction and then change its StartPoint (etc.) via object properties.

    HTH,

    Kean

  12. thank you
    Now i'm have a new question. i wanna make a event, when i change the start point of line in cad screen then raise event startPointChange. help me raise event.

  13. Sorry for not being more clear with my last answer: this isn't a support forum.

    Please post to forums.autodesk.com if you need support.

    Kean

  14. thank you so much!!!

  15. Can you help me?
    Now,in my project, i want if i click to a object, my palette's visible=true.

  16. No, I can't. I simply don't have the time. As requested, please post your questions to the AutoCAD .NET Discussion Forum (this blog is *not a support forum*).

    Kean

Leave a Reply to andreafuhr Cancel reply

Your email address will not be published. Required fields are marked *