@norman.yuan wrote:
I can still see 2 major issues:
1. You still use "block", even though I pointed out it is not clear what you want to list: block definition (with attribute definition), or block reference (with attribute reference). The result list would be different (unless the drawing is purged. that is, all block definitions in the drawing have block references created). In your previous reply you said "current block definition has attributes then get the value of the attribute". Mind you, attribute in block definition DOES NOT have value, attribute in block reference does. That is why you need to be clear: do you want to list block definitions, or do you want to list block references?
2. Since you are doing EXE, you can only use AutoCAD COM API. You CANNOT use AutoCAD .NET API. So, the code shown in your latest post does not help you at all.
Now, let's assume you still go with COM API. To list all block definitions that include attribute, you do (pseudo-code)
Dim ent As AcadEntity
Dim blk As AcadBlock
Dim hasAtt As Boolean
Dim count As Integer
For Each blk in ThisDrawing.Blocks
hasAtt=False
For Each ent In blk
If TypeOf ent Is AcadAttribute Then
MsgBox "Block """ & blk.Name & """ has attribute."
count+count+1
Exit For
End If
Next
Next
MsgBox count & " block definitions are found having attribute"
If you only want a block name list of VISIBLE blocks (block reference) that has attribute (attribute reference), then you can do:
Dim ss As AcadSelection
Dim ent As AcadEntity
Dim blk As AcadBlockReference
'' Create a selectionset to select everything in drawing (or with filter to select only "INSERT"
Set ss=ThisDrawing.SelectionSets.Add("test")
ss.Select acSelectionSetAll
For Each entin ss
If TypeOf ent Is AcadBlockReference Then
Set blk=ent
If blk.HasAttributes Then
MsgBox "Find block with attribute: """ & blk.EffectiveName & """."
End If
End If
Next
Notice I use EffectiveName, in case the blockreference is a dynamic one. Of course, since there could be multiple block references to a block definition, the names obtained here could be duplicated. Also just to be aware: block reference can have attribute, or not have attribute regardless if the block definition it is based on has attribute defined in it or not (yes, it is unusual, but could happen), that is why I ask you to be clear: do you want to list name out of block definitions, or do you want to list names out of block references.
Hello @norman.yuan,
I am little bit confused about terms that you mentioned. But according to your sentence: "Mind you, attribute in block definition DOES NOT have value, attribute in block reference does." and also according to my drawing (attached file), it seems that I am actually looking for value in the attribute of block reference.
I have tried first part of code that you provided and I have got the names of all block definitions. But, as you said, if the drawing space is empty and if I run the code again, I will get the same answer. It's because the drawing is not purged.
Second part of your code looks like VBA code. I have adapted to vb2015 syntax.
Dim ss As AcadSelectionSet
Dim ent As AcadEntity
Dim blk As AcadBlockReference
'' Create a selectionset to select everything in drawing (or with filter to select only "INSERT"
ss = dbxDoc.SelectionSets.Add("test")
ss.Select(AcSelect.acSelectionSetAll)
For Each ent In ss
If TypeOf ent Is AcadBlockReference Then
blk = ent
If blk.HasAttributes Then
MsgBox("Find block with attribute: " & blk.EffectiveName & ".")
End If
End If
Next
Now, when I have got the list of all visible blocks, I would like to list the attributes. According to my drawing, should be:
TITLEBLOCK and the attributes are: Circle Block 1
TITLEBLOCK and the attributes are: Circle Block 2
RECTANG_BLOCK and the attributes are: Top_Left text and Bottom_Left text and Bottom_Right text and Top_Right text
At the end of the video, you can see that I have tried to run the code again but second debugging was not successful. Would you like to explain me why?
Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.