.NET

Reply
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 1 of 3 (318 Views)

Get attribute from a block

318 Views, 2 Replies
02-02-2012 09:47 PM

I need to open a drawing and read the block attributes and find a block with name "ABC" and attribute named "Text" in "ABC" block. for which i am using the below code which is working perfectly.

Try
            AcadApp = GetObject(, "AutoCAD.Application")
        Catch ex As Exception
            AcadApp = CreateObject("AutoCAD.Application")

        End Try
        AcadApp.Visible = True
        Try
            AcadDoc = AcadApp.Documents.Open(DWGName, True)
            RichTextBox1.AppendText("   -   Opened File Successfully" & vbCrLf)
            Me.Refresh()

        Catch ex As Exception
            RichTextBox1.AppendText("   -   Can not Open this Drawing" & vbCrLf)
            Me.Refresh()
            Exit Function
        End Try

        Dim Obj As Object
        For Each Obj In AcadDoc.ModelSpace
            If Obj.ObjectName = "AcDbBlockReference" Then
                RibbonLabel1.Text = "Find Block: " & Obj.Name
                Me.Refresh()
                ' Check for attributes.
                If Obj.HasAttributes Then

                    Dim AttList As Object
                    Me.Refresh()

                    If Obj.Name.ToString.ToLower = "abc" Then
                        ' Build a list of attributes for the current block.
                        AttList = Obj.GetAttributes

                        ' Cycle throught the list of attributes.
                        For _i = LBound(AttList) To UBound(AttList)
                            ' Check for the correct attribute tag.
                            If AttList(_i).TagString = "Text" Then
                                return AttList(_i).TextString.ToString
                                End If

                                Me.Refresh()
                            End If
                        Next
                    End If
                End If
            End If
        Next Obj

 But i want to know whether is there any way to get the block named "ABC" directly without looping through all the objects.

Thank you.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,099
Registered: ‎06-29-2007
Message 2 of 3 (313 Views)

Re: Get attribute from a block

02-02-2012 10:43 PM in reply to: muthineni

Hi,

 

>> whether is there any way to get the block named "ABC" directly

Yes, there are two possibilities if you work managed (DLL in AutoCAD) or of you work COM-based (because you control AutoCAD from an external EXE):

 

Managed:

Get the BlockTableRecord for Block "ABC"

then you have access to the function GetBlockReferenceIDs ==> that gives you the ObjectID's of all references of this block-definition

 

COM + SelectionSet

Create a SelectionSet with a filter

(0 . INSERT) to define it searches for a BlockReference

(2 . "ABC") to have the BlockName defined you look for

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 3 (276 Views)

Re: Get attribute from a block

02-04-2012 06:24 AM in reply to: muthineni

{code}

Try this 5-mintes solution

(Win7 A2009 .NETRFramework 3.5)

Public Class Form1
    Dim DWGName As String = "C:\Test\WorkingDrawing.dwg"
    Dim AcadApp
    Dim AcadDoc
    Dim oSsets
    Dim oSset
    Dim ftype(0 To 3) As Short
    Dim fdata(0 To 3) As Object
    Public Function DoYourWork() As Boolean
        Dim bln As Boolean = False
        Try
            AcadApp = GetObject(, "AutoCAD.Application")
        Catch ex As Exception
            AcadApp = CreateObject("AutoCAD.Application")
        End Try
        AcadApp.Visible = True
        Try
            AcadDoc = AcadApp.Documents.Open(DWGName, True)
            RichTextBox1.AppendText(" - Opened File Successfully" & vbCrLf)
            Me.Refresh()
        Catch ex As Exception
            RichTextBox1.AppendText(" - Can not Open this Drawing" & vbCrLf)
            Me.Refresh()
            bln = False
            Return bln
            Exit Function
        End Try
        Dim pfset As Object
        pfset = AcadDoc.PickFirstSelectionSet
        pfset.Clear()
        ftype(0) = 0 : ftype(1) = 2 : ftype(2) = 66 : ftype(3) = 410
        fdata(0) = "insert" : fdata(1) = "abc" : fdata(2) = 1 : fdata(3) = "Model"
        Call pfset.Select(5, Nothing, Nothing, ftype, fdata)'<--5= acSelectionSetAll

        MsgBox("Selected: " & pfset.Count.ToString)
        Dim Obj As Object
        Dim msg As String = ""
        For Each Obj In pfset

            If Obj.ObjectName = "AcDbBlockReference" Then
                RibbonLabel1.Text = "Find Block: " & Obj.Name
                Me.Refresh()
                ' Check for attributes.
                If Obj.HasAttributes Then
                    Dim AttList As Object
                    Me.Refresh()
                    If Obj.Name.ToString.ToLower = "abc" Then
                        ' Build a list of attributes for the current block.
                        AttList = Obj.GetAttributes ' Cycle throught the list of attributes.
                        For _i = LBound(AttList) To UBound(AttList)
                            '' Check for the correct attribute tag.
                            If AttList(_i).TagString = "Text" Then
                                Return AttList(_i).TextString.ToString

                                Me.Refresh()
                            End If
                        Next
                    End If
                End If
            End If
        Next Obj
        Return True
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        DoYourWork()
    End Sub

End Class

{/code}

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.