Block and Attribute Information

Block and Attribute Information

OceanaPolynom
Advocate Advocate
446 Views
7 Replies
Message 1 of 8

Block and Attribute Information

OceanaPolynom
Advocate
Advocate
Hello

I have a simple block consisting of a line with some attributed text.

I would like to use VBA in AutoCad 2008 to

1 Get the text

2 Get the co-ordinate values of both endpoints of the line

Thank you

John
0 Likes
447 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
as long as you haven't scaled and/or rotated the block, it's rather easy.
Get the block by its name and depending which space you're working in:

Dim obj as AcadObject
Dim blref as AcadBlockReference
Dim bl as AcadBlock
Dim blent as AcadEntity
Dim l as AcadLine
Dim pt1, pt2
Dim i as Integer
Dim ii as Integer
Dim atts as Variant
Dim s as String
for i=0 to Thisdrawing.Modelspace.count-1
set obj=Thisdrawing.ModelSpace(i)
if obj.Objectname="AcDbBlockReference" then
set blref=obj
if blref.Name="Wanted Name" then
set bl=Thisdrawing.Blocks(blref.name)
for each blent in bl
if blent.objectname="AcDbLine" then
set l = blent
pt1=l.startpoint
pt2=l.endpoint
end if
next
if blref.HasAttributes = True then
s=""
set atts=blr.GetAtrributes()
for ii=LBound(atts) to UBound(atts)
s= s & atts(ii).Tagstring & " > " & atts(ii).Textstring & vbCr
next
end if
end if
end if

next
Msgbox s

Be aware the the line.start and .endpoint are in WCS, this is because one read the block definition (tablerecord),
AND AS IF THE BLOCK IS INSERTED AT 0,0,0 WCS!!! You wil have to compensate with the actual insertionpoint of the blref.
so: pt1(0)=pt1(0)+blref.Insertionpoint(0) etc.
Once scaled and or rotated, you will be needing matrix calculations.
Why not direct into the Blocks? Because every insert can have different attributes, and these are only included in the tabledefinition by a pointer
0 Likes
Message 3 of 8

OceanaPolynom
Advocate
Advocate
Hello

Thank you for your reply

I am getting the correct attribute value.
I had to leave the variable " atts " undeclared in order to get the code to work
There is a typo in the statement " set atts=blr.GetAtrributes() "
Should be " set atts=blref.GetAttributes() "



Many thanks
John
0 Likes
Message 4 of 8

Anonymous
Not applicable
sorry about the typo, but at least you got the general idea. The whole scheme works also in nested blocks and xref's, as long as one understands the difference between the Block and BlockReference.
0 Likes
Message 5 of 8

OceanaPolynom
Advocate
Advocate
Thanks again I learned alot
John
0 Likes
Message 6 of 8

Anonymous
Not applicable
Hi I am trying to do very much the same thing my problem is a run time error 13 type mismatch when I try to process this line

Set atts = blref.GetAttributes

Any help you can give I have checked all the declarations I can post the full code if that will help?
0 Likes
Message 7 of 8

norman.yuan
Mentor
Mentor
Remove "Set" to the beginning of the code line:

'You should have this line prior to the code
Dim atts as Variant
...
atts=blref.GetAttributes


Reason of this error:

"Set" keyword is only used to assign value to a Object reference, while AcadBlockReference.GetAttributes() does not return an instance of Object, instead, it returns a Variant, which is an array of AcadAttributeReference. So, you do not use "Set" for that line of code.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 8

Anonymous
Not applicable
Thanks fixed

jeremy
0 Likes