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.
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 -
{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}
Can't find what you're looking for? Ask the community or share your knowledge.