PDA

View Full Version : Dynamic Block Selection



Christmas050873782348
2020-08-24, 03:24 AM
Forum Members,

How would I write an AutoLISP command to get a selection set of all dynamic blocks who's name starts with "MyBlock"? Note the drawing has blocks of "MyBlock1", "MyBlock2", etc. etc. I would like to building a selection set that would contain all of these.

Christmas May

dlanor
2020-08-24, 07:51 AM
As they are dynamic blocks, any block references (inserts) where a dynamic property has be activated will be anonymous i.e. the block name will be something like *U????, and it is not possible to filter using the blocks effective name (real name). IMHO it is best to select all blocks and then filter out only those blocks you require whilst processing as non dynamic blocks with attributes can also be anonymous.

Basic concept is


(setq blockfilter "MYBLOCK*"); this filter set for any block whose effective name starts with MYBLOCK (upper case as wcmatch is case sensitive)
(setq ss (ssget "_X" '((0 . "INSERT"))))
(cond (ss
(repeat (setq cnt (sslength ss))
(setq obj (vlax-ename->vla-object (setq ent (ssname ss (setq cnt (1- cnt))))))
(cond ( (wcmatch (strcase (valx-get obj 'effectivename)) blockfilter) ;filter out only those blocks that match the filter
;option 1 do your stuff here, or convert to list for further processing
)
;option 2 to remove non matching blocks from selection set result in a selection set (ss) of matching blocks
;(t
; (ssdel ent ss)
;)
)
)
)
)


You have two options

option 1 process only those blocks that match. Best as you only iterate the selection set once
option 2 remove non matching blocks from selection set. You will then need to process the resultant selection set again

Christmas050873782348
2020-08-24, 05:42 PM
Assuming "Option 1" what line( s ) of code would be required to change the blocks color?
What line( s ) of code would be required to delete the blocks.

dlanor
2020-08-24, 08:06 PM
Assuming "Option 1" what line( s ) of code would be required to change the blocks color?
What line( s ) of code would be required to delete the blocks.

Option 1

COLOR - That would depend on the type of color ACI/RGB/Color Book. You could pop up the color dialog to pick a color. The return from this can be used to entmod the entity directly as it returns all the required dotted pairs. Otherwise ACI (integer color) is
straight forward single line of code RBG/Color book would require a truecolor object or some manipulation to convert to a truecolor (Lee Mac's color conversion routines).

examples if "int_val" is the integer (ACI) or calculated integer value (RGB) then

ACI (integer 0 => 256) 0 is byblock 256 is bylayer 1 is red etc (vlax-put obj 'color "int_val")
RGB (vlax-put-property obj 'truecolor "int_val")

I did some of these 6-12 Months ago, I'll see if I can find the code


DELETE- replace "do your stuff here" with (vla-delete obj). This will delete all blocks matching the "MYBLOCK*" filter