Jump to content

Lisp to select blocks using wildcard


Fett2oo5

Recommended Posts

Hello, I am in search for a means to select all blocks on a drawing given a wildcard.

The particular wildcard we would be using is *- Dy

I have been searching for a while now, and have seen suggestions of:

Qselect, which has to be done from a dialog box, and doesn't allow the use of wildcards.

Filter, doesn't seem to fit my needs because the names of the blocks don't seem to be available, only the AutoCAD name assignments ( I don't know what these are called but it labels blocks like this: U41, U42, U43, etc.)

 

Is it possible to have a LISP select all blocks by a wildcard in the name?

 

I am not that well versed in LISP routines, my only experience is with the beginner tutorials.

Any help on this is greatly appreciated.

Edited by Fett2oo5
LeeMac is awesome
Link to comment
Share on other sites

Qselect, which has to be done from a dialog box, and doesn't allow the use of wildcards.

 

Actually QSELECT does allow wilcards if you select 'Wildcard Match' from the 'Operator' drop-down; however, QSELECT doesn't work with dynamic block names, since the command doesn't account for anonymous dynamic block references.

 

When the dynamic properties of a dynamic block are changed, AutoCAD will automatically generate a new anonymous block definition for the block reference, which is then linked to the original dynamic block definition. Such anonymous block definitions have names *U###, as you have witnessed.

 

For your task, I would therefore suggest the following code:

(defun c:bsel ( / i p s )
   (setq p "*- Dy" ;; Block name pattern
         p (strcase p)
   )
   (if (setq s (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," p)))))
       (repeat (setq i (sslength s))
           (if (not (wcmatch (strcase (LM:blockname (vlax-ename->vla-object (ssname s (setq i (1- i)))))) p))
               (ssdel (ssname s i) s)
           )
       )
   )
   (sssetfirst nil s)
   (princ)
)

;; Block Name  -  Lee Mac
;; Returns the true (effective) name of a supplied block reference

(defun LM:blockname ( obj )
   (if (vlax-property-available-p obj 'effectivename)
       (defun LM:blockname ( obj ) (vla-get-effectivename obj))
       (defun LM:blockname ( obj ) (vla-get-name obj))
   )
   (LM:blockname obj)
)

(vl-load-com) (princ)

Link to comment
Share on other sites

Lee... You are amazing. The ease and clarity you bring to Lisp and VBA writing is astonishing.

Thank you very much for helping me.

 

Each time I see one of your examples, or full projects, I learn a great deal.

Thank you for being such a major contributor to the CAD and programming community.

Link to comment
Share on other sites

  • 1 year later...

Lee, with just minor modifications, it seems like this routine would also be able to find all blocks that begin with P302 > and explode them.

 

 

is it a matter of changing the ssdel to ssexp and (setq p "P320*" ;; Block name pattern ?

Link to comment
Share on other sites

You can change the block name pattern to "P32*", but there is no such function as 'ssexp' - you will instead need to either use the ActiveX explode method, or call the EXPLODE command.

Link to comment
Share on other sites

Lee,

 

 

so I would replace (ssdel (ssname s i) s) to something like

 

 

(setq ent (ssname s i) )

(command "_explode" ent)

 

 

 

 

on second thought I probably do not need the extra step of setting up a variable ent to then explode.

 

 

(command "_explode"(ssname s i) s)

 

 

 

 

 

 

>

Edited by MarkytheSparky
on second thought...
Link to comment
Share on other sites

Note that the repeat loop is preparing the selection set to remove anonymous dynamic block references which do not meet the block name criteria, the final (sssetfirst) expression is then highlighting the resulting selection set. Therefore, you can call the EXPLODE command with this resulting set:

(defun c:bsel ( / i p q s )
   (setq p "*- Dy" ;; Block name pattern
         p (strcase p)
   )
   (if (setq s (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," p)))))
       (repeat (setq i (sslength s))
           (if (not (wcmatch (strcase (LM:blockname (vlax-ename->vla-object (ssname s (setq i (1- i)))))) p))
               (ssdel (ssname s i) s)
           )
       )
   )
   (if (and s (< 0 (sslength s)))
       (progn
           (setq q (getvar 'qaflags))
           (setvar 'qaflags 1)
           (command "_.explode" s "")
           (setvar 'qaflags q)
       )
   )
   (princ)
)

;; Block Name  -  Lee Mac
;; Returns the true (effective) name of a supplied block reference

(defun LM:blockname ( obj )
   (if (vlax-property-available-p obj 'effectivename)
       (defun LM:blockname ( obj ) (vla-get-effectivename obj))
       (defun LM:blockname ( obj ) (vla-get-name obj))
   )
   (LM:blockname obj)
)

(vl-load-com) (princ)

Note that QAFLAGS must be modified to allow the EXPLODE command to accept a selection set in AutoLISP.

 

The above is untested.

Link to comment
Share on other sites

sooooo close, but no BOOM

 

 

I have three blocks that show up when I invoke the command RENAME

 

 

A320_05_ARCH-PP131

A320_05_ARCH-PQQ132

A320_05_ARCH-PA133

 

 

 

 

anything after the A320 is a wildcard

 

 

after running the LISP I can still see the block names.

Link to comment
Share on other sites

Heck - now that I'm excited about this...

 

 

You could probably use the same code (slightly modified) to rename some blocks... like my titleblocks

 

 

13203-TACO-22x34 - 05 - Final Assembly Line-1999464-ARCH THIRD FLOOR - AREA 1

 

 

rename to

 

 

 

 

13203-TACO-22x34_005_Border

 

 

 

 

man - I could automate that step too...

 

 

save my hands form carpal tunnel.

 

 

 

 

Thank you so far Lee Mac.

Link to comment
Share on other sites

These number come from Revit export! Am I correct MarkytheSparky? This is just what I was looking for also! Great contributions for CAD management! Thanks Lee Mac

Link to comment
Share on other sites

Darn it...

 

 

I have managed to explode all the blocks that begin with A320, but now I have a bunch of blocks on LAYER 0 that need to be exploded.

 

 

This is a second step!

 

 

Block name does not matter - but they all are on layer 0

Link to comment
Share on other sites

  • 1 year later...

Dear Lee, BSEL works on all database, great. I have tried to modify it to crossing window selection, I have tried to replace (ssget "_X") with (ssget "_+.:E:S") and then (ssget "_:E:S") and then (ssget "_:E") and I have no luck :-( neither one gave me the chanc. What do I do wrong?

Best regards!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...