Community
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.
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
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.