Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Nested Block Attribute Values

11 REPLIES 11
Reply
Message 1 of 12
EC-CAD
1200 Views, 11 Replies

Nested Block Attribute Values

I have a drawing that 'nests' blocks to 4 levels deep.
What I need is a 'list' of the 'tag' and 'value' .. of
each attribute within each level of nested blocks.
I can get the 'first' level..no problem, but I cannot seem
to 'dig' further..to find level 2, 3 or 4 nested block attribute
tagname / value.
Anyone got a Lisp to dig that deep ?
Customer says I cannot 'explode' anything.
Bob
11 REPLIES 11
Message 2 of 12
Anonymous
in reply to: EC-CAD

This gets all attributes in all blockreferences.
[code]
(defun getnestedattributes (doc / atts result)
(vlax-for blk (vla-get-blocks doc)
(vlax-for ent blk
(if (and (vl-string-search "BLOCKREF" (strcase (vla-get-objectname
ent)))
(eq (vla-get-hasattributes ent) :vlax-true)
)
(progn
(setq atts (vlax-invoke ent 'getattributes))
(foreach att atts
(setq result (cons (cons (vla-get-tagstring att) (vla-get-textstring
att)) result))
)
)
)
)
)
result
)
[/code]

wrote in message news:5265100@discussion.autodesk.com...
I have a drawing that 'nests' blocks to 4 levels deep.
What I need is a 'list' of the 'tag' and 'value' .. of
each attribute within each level of nested blocks.
I can get the 'first' level..no problem, but I cannot seem
to 'dig' further..to find level 2, 3 or 4 nested block attribute
tagname / value.
Anyone got a Lisp to dig that deep ?

Bob
Message 3 of 12
Anonymous
in reply to: EC-CAD

Maybe this will work. Whooped it up real fast.

(defun c:NestedAttValues (/ Sel Obj)

(defun PrintAttList (BlkObj)

(print
(list
(vla-get-Name Obj)
(mapcar '(lambda (x) (cons (vla-get-TagString x) (vla-get-TextString x)))
(vlax-invoke Obj 'GetAttributes))
)
)
)
;---------------------------------------------------------
(defun GetNestedBlocks (BlkName BlkCol)

(vlax-for Obj (vla-Item BlkCol BlkName)
(if (= (vla-get-ObjectName Obj) "AcDbBlockReference")
(progn
(PrintAttList Obj)
(GetNestedBlocks (vla-get-name Obj) BlkCol)
)
)
)
)
;-------------------------------------------------------
(setq BlkCol (vla-get-Blocks (vla-get-ActiveDocument
(vlax-get-Acad-Object))))
(if
(and
(setq Sel (entsel "\n Select block to get nested attributes: "))
(setq Obj (vlax-ename->vla-object (car Sel)))
(= (vla-get-ObjectName Obj) "AcDbBlockReference")
)
(progn
(PrintAttList Obj)
(GetNestedBlocks (vla-get-Name Obj) BlkCol)
)
)
(princ)
)

--

Tim
"A blind man lets nothing block his vision."


wrote in message news:5265100@discussion.autodesk.com...
I have a drawing that 'nests' blocks to 4 levels deep.
What I need is a 'list' of the 'tag' and 'value' .. of
each attribute within each level of nested blocks.
I can get the 'first' level..no problem, but I cannot seem
to 'dig' further..to find level 2, 3 or 4 nested block attribute
tagname / value.
Anyone got a Lisp to dig that deep ?
Customer says I cannot 'explode' anything.
Bob
Message 4 of 12
EC-CAD
in reply to: EC-CAD

WhooHoo !
Got it now.
Based on Tim's sample..
(defun MakeAttLists (BlkObj)
(if (eq (vla-get-Name BlkObj) "PART COUNTER WITH ITEM AND DESCRIPTION")
(progn
(setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes BlkObj))))
(foreach att atts
(setq att_tag (vla-get-TagString att))
(setq att_val (vla-get-TextString att))
(setq att_lst (cons att_tag att_lst))
(setq val_lst (cons att_val val_lst))
); foreach
); progn
); if
)
;---------------------------------------------------------
(defun GetNestedBlocks (BlkName BlkCol)
(vlax-for Obj (vla-Item BlkCol BlkName)
(if (= (vla-get-ObjectName Obj) "AcDbBlockReference")
(progn
(MakeAttLists Obj)
(GetNestedBlocks (vla-get-name Obj) BlkCol)
)
)
)
)
;-------------------------------------------------------
(defun C:GO8 ()
(setq att_lst nil)
(setq ss (ssget "X" (list (cons 0 "INSERT"))))
(if ss
(progn
(setq C 0)
(repeat (sslength ss)
(setq blk (ssname ss C))
(setq elist (entget blk))
(setq bn (cdr (assoc 2 elist))); blockname
(setq BlkCol (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-Acad-Object))))
(if
(and
(setq Obj (vlax-ename->vla-object blk))
(= (vla-get-ObjectName Obj) "AcDbBlockReference")
)
(progn
(MakeAttLists Obj)
(GetNestedBlocks (vla-get-Name Obj) BlkCol)
)
)
(setq C (+ C 1))
); repeat
); progn
); if
(princ)
); function

Above gets all block inserts, itterates through all
the blocks/inserts/nested blocks.. and picks up
attribute tag / value of 'wanted' block..that has 2
attributes. Lists them all.

Thanks Tim !
I see the 'double' loop in there, that calls the function..
'GetNestedBlocks' .. for each block..

Jeff,
Your example seems to pick up only (top) level, won't find
the nested at level 2 or more.

Bob
Message 5 of 12
Anonymous
in reply to: EC-CAD

Nice one Bob. I'm been practicing my recursive style of programming. This
is one case where it works well. Glad I could help.

--

Tim
"A blind man lets nothing block his vision."


wrote in message news:5265295@discussion.autodesk.com...
WhooHoo !
Got it now.
Based on Tim's sample..
(defun MakeAttLists (BlkObj)
(if (eq (vla-get-Name BlkObj) "PART COUNTER WITH ITEM AND DESCRIPTION")
(progn
(setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes
BlkObj))))
(foreach att atts
(setq att_tag (vla-get-TagString att))
(setq att_val (vla-get-TextString att))
(setq att_lst (cons att_tag att_lst))
(setq val_lst (cons att_val val_lst))
); foreach
); progn
); if
)
;---------------------------------------------------------
(defun GetNestedBlocks (BlkName BlkCol)
(vlax-for Obj (vla-Item BlkCol BlkName)
(if (= (vla-get-ObjectName Obj) "AcDbBlockReference")
(progn
(MakeAttLists Obj)
(GetNestedBlocks (vla-get-name Obj) BlkCol)
)
)
)
)
;-------------------------------------------------------
(defun C:GO8 ()
(setq att_lst nil)
(setq ss (ssget "X" (list (cons 0 "INSERT"))))
(if ss
(progn
(setq C 0)
(repeat (sslength ss)
(setq blk (ssname ss C))
(setq elist (entget blk))
(setq bn (cdr (assoc 2 elist))); blockname
(setq BlkCol (vla-get-Blocks (vla-get-ActiveDocument
(vlax-get-Acad-Object))))
(if
(and
(setq Obj (vlax-ename->vla-object blk))
(= (vla-get-ObjectName Obj) "AcDbBlockReference")
)
(progn
(MakeAttLists Obj)
(GetNestedBlocks (vla-get-Name Obj) BlkCol)
)
)
(setq C (+ C 1))
); repeat
); progn
); if
(princ)
); function

Above gets all block inserts, itterates through all
the blocks/inserts/nested blocks.. and picks up
attribute tag / value of 'wanted' block..that has 2
attributes. Lists them all.

Thanks Tim !
I see the 'double' loop in there, that calls the function..
'GetNestedBlocks' .. for each block..

Jeff,
Your example seems to pick up only (top) level, won't find
the nested at level 2 or more.

Bob
Message 6 of 12
EC-CAD
in reply to: EC-CAD

Tim,
I gave the user a modified version of the program
that either makes a list of the items found, or shoots
it to a .csv for Excel..and a little routine to add-in a
Bill of Materials. The guy is really happy with it.
Again, I thank you for your 'method' and code,,to
pull out the Attribute values from (any) nested blocks
that may be 'nested' in yet other blocks..etc.

Thanks !!

Bob Shaw
Control Systems & Services, Inc.
http://www.bobscadshop.com
Message 7 of 12
Anonymous
in reply to: EC-CAD

You're welcome Bob. It's nice when you can help someone who has helped so
many others, so I'm glad you were able to give the person something they
wanted, and can use.

--

Tim
"A blind man lets nothing block his vision."


wrote in message news:5266932@discussion.autodesk.com...
Tim,
I gave the user a modified version of the program
that either makes a list of the items found, or shoots
it to a .csv for Excel..and a little routine to add-in a
Bill of Materials. The guy is really happy with it.
Again, I thank you for your 'method' and code,,to
pull out the Attribute values from (any) nested blocks
that may be 'nested' in yet other blocks..etc.

Thanks !!

Bob Shaw
Control Systems & Services, Inc.
http://www.bobscadshop.com
Message 8 of 12
Anonymous
in reply to: EC-CAD

I should have pointed out one thing Bob. The routine I posted, doesn't look
for constant attributes. I don't know if that is a big deal or not, but it
wouldn't be hard to add it.

--

Tim
"A blind man lets nothing block his vision."


wrote in message news:5266932@discussion.autodesk.com...
Tim,
I gave the user a modified version of the program
that either makes a list of the items found, or shoots
it to a .csv for Excel..and a little routine to add-in a
Bill of Materials. The guy is really happy with it.
Again, I thank you for your 'method' and code,,to
pull out the Attribute values from (any) nested blocks
that may be 'nested' in yet other blocks..etc.

Thanks !!

Bob Shaw
Control Systems & Services, Inc.
http://www.bobscadshop.com
Message 9 of 12
EC-CAD
in reply to: EC-CAD

Tim,
I'm aware of how to check on constant attributes.
No problems.. have another glass on me !
This particular case.. is flags normal.

Cheers.

Bob
Message 10 of 12
MarkEnwiller
in reply to: EC-CAD

I'm in need of this same type of lisp tool. Could you send me the lisp & CAD file to I can see if it is actually what I need?

Thanks,
Mark
menwiller@cox.net
Message 11 of 12
EC-CAD
in reply to: EC-CAD

Mark,
Good to talk with you.
If you need further assist, you know how
to get ahold of me.

Bob
Message 12 of 12
nikm42Q9N
in reply to: Anonymous

I know this is old but how do you modify the code to find constant attribute values?

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

Post to forums  

Autodesk Design & Make Report

”Boost