.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get block attributes

2 REPLIES 2
Reply
Message 1 of 3
muthineni
1264 Views, 2 Replies

Get block attributes

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.

2 REPLIES 2
Message 2 of 3
VB_Autocad_guy
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

 

Message 3 of 3

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost