VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Compare block attributes

5 REPLIES 5
Reply
Message 1 of 6
tdaniel14
327 Views, 5 Replies

Compare block attributes

What i'm trying to do is loop through each block in a drawing, get the 3 attributes, compare the attributes to a row in an excel file, then import the excel information into the block.

What would be the best way to get the blocks and compare the attributes?

Thanks
5 REPLIES 5
Message 2 of 6
lucaso
in reply to: tdaniel14

just for the record: a block is a definition of a group of elements, stored in a tablerecord, an inserted block in the drawing is called a blockreference, which is the same as an insert. attributes are stored with the reference, not in the block. try it: you can move attributes with the grips without moving the insert. if you redefine the block with the attributes in a different place, the existing attributes will stay in the old place.
so find the inserts in your drawing, check if they have atts an read their tags and values. Place then in a redim preserve variable, and compare them later on to your excel-sheet. Put the appropiate objectid in that datastructure so you can easily indentfy the right insert.
Message 3 of 6
tdaniel14
in reply to: tdaniel14

thanks for the reply
Message 4 of 6
cadtown
in reply to: tdaniel14

Do you have just one target attributed block to search for or many of them in each dwg file ?
Message 5 of 6
tdaniel14
in reply to: tdaniel14

This particular drawing has 1500 blockreferences could be more or less.
Each has 3 attributes i need to compare with multiple rows of the excel sheet.
The excel sheet rows will vary from 5 to 150 and each row will have 3 values.
Message 6 of 6
lucaso
in reply to: tdaniel14

I'm not so into Excel, rather use a database for lists of objects with several attributes, the same way in fact Autocad has organized its objects.
Doesn't matter how many references you have in the drawing as long as you have enough memory...
Start off (Acad-VBA) by defining an structure:
Type att
Tag as String
Text as String
End Type

Type BlockAttribs
ObjectID as Long
Atts() as Att
End Type

'then declare the variable as an Array
Dim BA() as BlockAttribs

Public Sub Main
Dim obj as AcadObject
Dim blref as AcadBlockReference
Dim atts as Variant
Dim n as Integer
n=0
Dim i as Integer

For each obj in Thisdrawing.ModelSpace
if obj.ObjectName="AcDbBlockReference" then
set blref=obj
if blref.Name="WANTED NAME" then
Redim Preserve BA(n)
BA(n).ObjectID=blref.ObjectId
if blref.HasAttributes=True then
set atts=blref.GetAttributes
for i=LBound(atts) to UBound(atts)
Redim Preserve BGA(n).Att(i)
BGA(n).Att(i).Tag=atts(i).TagString
BGA(n).Att(i).Text=atts(i).TextString
next
end if ''HasAttributes
n=n+1 ''ready for the next one
end if ''if name is ok
end if ''if blockcref
Next
End Sub

You will get a global var BA. Edited by: lucaso on Mar 23, 2009 9:39 PM

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

Post to forums  

Autodesk Design & Make Report

”Boost