AcadBlock vs. AcadBlockReference

AcadBlock vs. AcadBlockReference

Anonymous
Not applicable
1,194 Views
8 Replies
Message 1 of 9

AcadBlock vs. AcadBlockReference

Anonymous
Not applicable
I am trying to list the attributes of a Block.

I have code like this:
dim strBlockName as String

...
strBlockName = "MyBlock"

Now I want to get a AcadBlockRefernce to the block named strBlockName so
that i can use AcadBlockReference.GetAttributes
...

I can use this to get a Block object

Dim entBlock as AcadBlock

Set entblock = ThisDrawing.Blocks.Item(strBlockName)

But I do not know how to turn this into an AcadBlockReference

Any help is appreciated,
Bob Harden
bob_harden@removespamharden-assoc.com (delete removespam to send message)
0 Likes
1,195 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Bob Harden wrote in message <7vlbfs$jpc19@adesknews2.autodesk.com>...
>
>I can use this to get a Block object
>
>Dim entBlock as AcadBlock
>
>Set entblock = ThisDrawing.Blocks.Item(strBlockName)
>
>But I do not know how to turn this into an AcadBlockReference
>

Hello Bob,

You're approaching this from the wrong side. What you are
after is the attributes associated with a block INSTANCE,
not the block DEFINITION, right?

Okay, so first you need to get the block instance (or reference).
You could do this by getting a selection set, or by having the
user pick an instance. If using a selection set, you could let
the user manually select entities, or automatically build the
set.

Does this help?

rdh.
0 Likes
Message 3 of 9

Anonymous
Not applicable
Richard D. Howard [Autodesk GIS] wrote in
message news:7vlc88$joe15@adesknews2.autodesk.com...
>
> Bob Harden wrote in message <7vlbfs$jpc19@adesknews2.autodesk.com>...
> >
> >I can use this to get a Block object
> >
> >Dim entBlock as AcadBlock
> >
> >Set entblock = ThisDrawing.Blocks.Item(strBlockName)
> >
> >But I do not know how to turn this into an AcadBlockReference
> >
>
>
> Hello Bob,
>
> You're approaching this from the wrong side. What you are
> after is the attributes associated with a block INSTANCE,
> not the block DEFINITION, right?
>
> Okay, so first you need to get the block instance (or reference).
> You could do this by getting a selection set, or by having the
> user pick an instance. If using a selection set, you could let
> the user manually select entities, or automatically build the
> set.
>
> Does this help?
>
> rdh.
>
>
>
>
>
>
0 Likes
Message 4 of 9

Anonymous
Not applicable
Richard, I see what you mean. In actuality, I have the name of a block in a
string variable. Now using that name, I want to find out if any attributes
are defined for the block and if they are waht the tag is for the
attributes.

While I could search the drawing database for an instance of the block and
then use get attributes t determine the tags, is there a way to just lookup
a block definition and the associated attribute tags given the block name?

Thanks,
Bob Harden

Richard D. Howard [Autodesk GIS] wrote in
message news:7vlc88$joe15@adesknews2.autodesk.com...
>
> Bob Harden wrote in message <7vlbfs$jpc19@adesknews2.autodesk.com>...
> >
> >I can use this to get a Block object
> >
> >Dim entBlock as AcadBlock
> >
> >Set entblock = ThisDrawing.Blocks.Item(strBlockName)
> >
> >But I do not know how to turn this into an AcadBlockReference
> >
>
>
> Hello Bob,
>
> You're approaching this from the wrong side. What you are
> after is the attributes associated with a block INSTANCE,
> not the block DEFINITION, right?
>
> Okay, so first you need to get the block instance (or reference).
> You could do this by getting a selection set, or by having the
> user pick an instance. If using a selection set, you could let
> the user manually select entities, or automatically build the
> set.
>
> Does this help?
>
> rdh.
>
>
>
>
>
>
0 Likes
Message 5 of 9

Anonymous
Not applicable
Bob Harden wrote in message <7vlhk6$jof25@adesknews2.autodesk.com>...
>
>While I could search the drawing database for an instance of the block and
>then use get attributes t determine the tags, is there a way to just lookup
>a block definition and the associated attribute tags given the block name?
>

Oh - I'm sorry. I misunderstood. I thought you needed the
attributereference information - that is, specific values on
block inserts.

Unfortunately the block object does not provide a nice neat
attribute collection member. Instead, you need to iterate over
each entity in the definition and see if it is an attribute entity.

Here's a little example:

Sub TestAttributes()
On Error Resume Next
Dim strName As String
Dim oBlock As AcadBlock
Dim oEnt As AcadEntity
Dim oAttribute As AcadAttribute

'' get the block name
strName = InputBox("Enter a block name: ")
If "" = strName Then Exit Sub ' exit if no old name

'' get the block definition
Set oBlock = ThisDrawing.Blocks(strName)
If oBlock Is Nothing Then ' exit if not found
MsgBox "Block '" & strName & "' not found"
Exit Sub
End If

'' iterate the block definition
For Each oEnt In oBlock

'' if the entity is an attribute
If oEnt.ObjectName = "AcDbAttributeDefinition" Then

'' cast to an attribute interface
Set oAttribute = oEnt

'' dump some attribute info to the debug window
Debug.Print ""
Debug.Print " Tag: " & oAttribute.TagString
Debug.Print "Prompt: " & oAttribute.PromptString
Debug.Print " Value: " & oAttribute.TextString
Debug.Print " Mode: " & oAttribute.Mode
End If
Next
End Sub

Cheers-

rdh.
0 Likes
Message 6 of 9

Anonymous
Not applicable
Richard, Thanks very much for your valuable help.

Bob Harden

Richard D. Howard [Autodesk GIS] wrote in
message news:7vnf85$o8p18@adesknews2.autodesk.com...
>
> Bob Harden wrote in message <7vlhk6$jof25@adesknews2.autodesk.com>...
> >
> >While I could search the drawing database for an instance of the block
and
> >then use get attributes t determine the tags, is there a way to just
lookup
> >a block definition and the associated attribute tags given the block
name?
> >
>
>
> Oh - I'm sorry. I misunderstood. I thought you needed the
> attributereference information - that is, specific values on
> block inserts.
>
> Unfortunately the block object does not provide a nice neat
> attribute collection member. Instead, you need to iterate over
> each entity in the definition and see if it is an attribute entity.
>
> Here's a little example:
>
> Sub TestAttributes()
> On Error Resume Next
> Dim strName As String
> Dim oBlock As AcadBlock
> Dim oEnt As AcadEntity
> Dim oAttribute As AcadAttribute
>
> '' get the block name
> strName = InputBox("Enter a block name: ")
> If "" = strName Then Exit Sub ' exit if no old name
>
> '' get the block definition
> Set oBlock = ThisDrawing.Blocks(strName)
> If oBlock Is Nothing Then ' exit if not found
> MsgBox "Block '" & strName & "' not found"
> Exit Sub
> End If
>
> '' iterate the block definition
> For Each oEnt In oBlock
>
> '' if the entity is an attribute
> If oEnt.ObjectName = "AcDbAttributeDefinition" Then
>
> '' cast to an attribute interface
> Set oAttribute = oEnt
>
> '' dump some attribute info to the debug window
> Debug.Print ""
> Debug.Print " Tag: " & oAttribute.TagString
> Debug.Print "Prompt: " & oAttribute.PromptString
> Debug.Print " Value: " & oAttribute.TextString
> Debug.Print " Mode: " & oAttribute.Mode
> End If
> Next
> End Sub
>
>
> Cheers-
>
> rdh.
>
>
>
0 Likes
Message 7 of 9

Anonymous
Not applicable
Richard, when I tried the sample code, I got a "Type Mismatch" error at the
line:

For Each oEnt In oBlock

I changed the dimensioning to

Dim oEnt as Object

I also changed the line If oEnt.ObjectName =
"AcDbAttributeDefinition" Then

to If oEnt.EntityName = "AcDbAttributeDefinition" Then

Seems like things are working now.

As I said, Thanks for your valuable help.

Bob Harden

Bob Harden wrote in message
news:7vnq7n$q2m17@adesknews2.autodesk.com...
> Richard, Thanks very much for your valuable help.
>
> Bob Harden
>
> Richard D. Howard [Autodesk GIS] wrote in
> message news:7vnf85$o8p18@adesknews2.autodesk.com...
> >
> > Bob Harden wrote in message <7vlhk6$jof25@adesknews2.autodesk.com>...
> > >
> > >While I could search the drawing database for an instance of the block
> and
> > >then use get attributes t determine the tags, is there a way to just
> lookup
> > >a block definition and the associated attribute tags given the block
> name?
> > >
> >
> >
> > Oh - I'm sorry. I misunderstood. I thought you needed the
> > attributereference information - that is, specific values on
> > block inserts.
> >
> > Unfortunately the block object does not provide a nice neat
> > attribute collection member. Instead, you need to iterate over
> > each entity in the definition and see if it is an attribute entity.
> >
> > Here's a little example:
> >
> > Sub TestAttributes()
> > On Error Resume Next
> > Dim strName As String
> > Dim oBlock As AcadBlock
> > Dim oEnt As AcadEntity
> > Dim oAttribute As AcadAttribute
> >
> > '' get the block name
> > strName = InputBox("Enter a block name: ")
> > If "" = strName Then Exit Sub ' exit if no old name
> >
> > '' get the block definition
> > Set oBlock = ThisDrawing.Blocks(strName)
> > If oBlock Is Nothing Then ' exit if not found
> > MsgBox "Block '" & strName & "' not found"
> > Exit Sub
> > End If
> >
> > '' iterate the block definition
> > For Each oEnt In oBlock
> >
> > '' if the entity is an attribute
> > If oEnt.ObjectName = "AcDbAttributeDefinition" Then
> >
> > '' cast to an attribute interface
> > Set oAttribute = oEnt
> >
> > '' dump some attribute info to the debug window
> > Debug.Print ""
> > Debug.Print " Tag: " & oAttribute.TagString
> > Debug.Print "Prompt: " & oAttribute.PromptString
> > Debug.Print " Value: " & oAttribute.TextString
> > Debug.Print " Mode: " & oAttribute.Mode
> > End If
> > Next
> > End Sub
> >
> >
> > Cheers-
> >
> > rdh.
> >
> >
> >
>
>
0 Likes
Message 8 of 9

Anonymous
Not applicable
Bob Harden wrote in message <7vnsjd$q2n9@adesknews2.autodesk.com>...
>Richard, when I tried the sample code, I got a "Type Mismatch" error at the
>line:
>
> For Each oEnt In oBlock
>

Ah - yes my sample is for Acad2000. Earlier versions will
require the alterations you suggest.

rdh.
0 Likes
Message 9 of 9

Anonymous
Not applicable
Test
0 Likes