.NET

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

Get block attributes

586 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
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 2 of 3 (570 Views)

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*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 3 of 3 (555 Views)

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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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