.NET

Reply
Valued Contributor
73 Posts
0 Kudos
Registered: ‎10-10-2011
Post 1 of 3

Get block attributes

630 Views, 2 Replies
02-29-2012 02:18 AM
Function ReadDWG(ByVal DWGName As String)
        Dim AcadApp As AcadApplication
        Dim AcadDoc As AcadDocument
        Dim Extracted_title As String
        RichTextBox1.AppendText(Path.GetFileName(DWGName))
        RibbonLabel1.Text = "Opening AutoCAD"

        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
        Dim block_exists As Boolean


        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 = "planstempel" Then
                        block_exists = True
                        ' 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.
Msgbox (AttList(_i).TagString)
                            
                                Me.Refresh()
                            End If
                        Next
                        Exit For
                    End If
                End If
            End If
        Next Obj
        AcadDoc.Close(False)
        RibbonLabel1.Text = "Finished Retrieving Block Attributes"
        If block_exists Then
            Return Extracted_title
        Else
            Return "ERROR"
        End If

        
    End Function

 I am using the above code to read block attributes depending on the name.
But now I need to read the active drawing (drawing will be open and should read from the active document).
In the above code I have referenced interop.dll. without that can I retrieve the block attributes.
Can anyone tell me how to read the block attributes from the active document.
Thank you.

Distinguished Contributor
136 Posts
5 Kudos
Registered: ‎07-24-2009
Post 2 of 3

Re: Get block attributes

02-29-2012 08:04 AM in reply to: muthineni

Give this a try. 

 

Function ReadActiveDWG()
        Dim AcadApp As AcadApplication
        Dim AcadDoc As AcadDocument
        Dim Extracted_title As String
        'RichTextBox1.AppendText(Path.GetFileName(DWGName)) 
        RibbonLabel1.Text = "Searching Active AutoCAD Drawing" '>>CHANGED HERE

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

        End Try
        AcadApp.Visible = True
        Try
            AcadDoc = ThisDrawing '>>CHANGED HERE SEE BELOW
            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
        Dim block_exists As Boolean


        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 = "planstempel" Then
                        block_exists = True
                        ' 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.
Msgbox (AttList(_i).TagString)
                            
                                Me.Refresh()
                            End If
                        Next
                        Exit For
                    End If
                End If
            End If
        Next Obj
        AcadDoc.Close(False)
        RibbonLabel1.Text = "Finished Retrieving Block Attributes"
        If block_exists Then
            Return Extracted_title
        Else
            Return "ERROR"
        End If

        
    End Function
	Public ReadOnly Property ThisDrawing() As Autodesk.AutoCAD.ApplicationServices.Document
	'Use the AcDbMgd.dll, I think it is? (.NET Api)
        Get
            Return Autodesk.AutoCAD. _
            ApplicationServices.Application. _
            DocumentManager.MdiActiveDocument.AcadDocument()
        End Get
    End Property

 

*Expert Elite*
753 Posts
54 Kudos
Registered: ‎02-13-2008
Post 3 of 3

Re: Get block attributes

02-29-2012 11:11 AM in reply to: VB_Autocad_guy

You can not use AcDbMgd.dll or AcMgd.dll in an out of process application.

 

I don't have any COM code to play with, but it should just be AcadDoc = AcadApp.ActiveDocument.

Dave O.                                                                  Sig-Logos32.png
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!