Results 1 to 20 of 21

Thread: Solved: Read field in AUTOCAD block attribute

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Regular fixo's Avatar
    Joined
    Jul 2006
    Location
    Sankt-Petersburg
    Posts
    99
    Location
    Hi ALe, I posted solution on swamp as well
    Here is a quick and dirty code
    [Code]
    <CommandMethod("foo")>
    Public Sub 
    GetFieldFromAttribute()
    Dim doc As Document = 
    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = 
    doc.Database
    Dim ed As Editor = doc.Editor
    Try

    Dim tab As String = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ctab").ToString
    Dim blockname As String = ed.GetString(vbLf & "Enter blockname:").StringResult
    Dim opts As PromptSelectionOptions = New 
    PromptSelectionOptions
    opts.SingleOnly =True
    opts.MessageForRemoval = vbLf & "Select block only"
    opts.MessageForAdding = vbLf & "Select single block"
    Dim filt As SelectionFilter = New SelectionFilter({New TypedValue(0, "insert"), 
    New TypedValue(2, blockname), New TypedValue(410, tab)})
    Dim psr As PromptSelectionResult = ed.GetSelection(opts, filt)
    If psr.Status <> PromptStatus.OK Then Exit
        Sub
        If psr.Value.Count <> 1 Then Exit 
        Sub
        Dim id As ObjectId = psr.Value.GetObjectIds(0)
        If id.IsNull Then Exit 
            Sub
            Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim obj As DBObject = tr.GetObject(psr.Value.GetObjectIds(0), OpenMode.ForRead)
            If TypeOf obj Is BlockReference Then
                Dim bref As BlockReference = CType(tr.GetObject(id, OpenMode.ForRead), BlockReference)
                Dim attcoll As AttributeCollection = bref.AttributeCollection
                Dim att As AttributeReference
                For Each aid As ObjectId In attcoll
                    Dim aobj As DBObject = tr.GetObject(aid, OpenMode.ForRead)
                    att = TryCast(DirectCast(aobj, AttributeReference), AttributeReference)
                    If att Is Nothing Then Exit 
                    Sub
                    If att.Tag.Equals("LENGTH", StringComparison.CurrentCultureIgnoreCase) Then
                       If att.HasFields Then
                            Dim fld As Field = tr.GetObject(att.GetField(), OpenMode.ForRead)
                            Dim code As String = fld.GetFieldCode
                            Dim pos1 As Int32 = code.IndexOf("_ObjId")
                            Dim pos2 As Integer = code.IndexOf(">%")
                            Dim leng1 As Integer = "_ObjId".Length
                            Dim leng2 As Integer = ">%".Length
                            Dim idstr As String = code.Substring(pos1 + leng1 + 1, pos2 - (pos1 + leng1) - 1)
                            MsgBox("ID from field code: " & idstr)
                            Dim objId As ObjectId = New ObjectId(New IntPtr(Convert.ToInt32(idstr)))
                            Dim fieldchild As DBObject = tr.GetObject(objId, OpenMode.ForRead)
                            If TypeOf fieldchild Is Polyline Then
                                Dim poly As Polyline = DirectCast(fieldchild, Polyline)
                                MsgBox("Compare:" & vbLf & "Return from attribute: " & 
                                att.TextString & vbLf & "Return from Polyline: " & 
                                poly.Area.ToString)
                            End If
                            Exit For
                        End If
                    End If
                Next
            End If
            End Using
            Catch ex As 
            Autodesk.AutoCAD.Runtime.Exception
            ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
         End Try
    End Sub
    Last edited by Aussiebear; 12-30-2024 at 01:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •