Trying to create a lisp that will search a Blocks Name & Description and display the blocks in a list

Trying to create a lisp that will search a Blocks Name & Description and display the blocks in a list

scohen
Contributor Contributor
417 Views
5 Replies
Message 1 of 6

Trying to create a lisp that will search a Blocks Name & Description and display the blocks in a list

scohen
Contributor
Contributor

All,

 

I have been playing around with ChatGPT (I am not a programmer :-), to create a LISP routine that will search a Blocks Name & Description based upon some text a user types in, and then display the blocks that contain that search string in the command line.  ChatGPT did a good job with the name, but couldn’t figure out how to search the description.  Can anyone on here help?

 

Here's the code below.
======================================

(defun c:SearchBlocksByText ()
(setq searchText (getstring "\nEnter the text to search for in block names or descriptions: "))

(if (and searchText (/= searchText ""))
(progn
(setq foundBlocks '())
(setq ss (ssget "_X" (list (cons 0 "INSERT"))))

(if ss
(progn
(setq searchText (strcase searchText)) ; Normalize search text to lowercase

(repeat (sslength ss)
(setq ent (ssname ss 0))
(setq entData (entget ent))
(setq blockName (cdr (assoc 2 entData)))
(setq blockDescription (cdr (assoc 3 entData)))

(setq blockName (if blockName blockName ""))
(setq blockDescription (if blockDescription blockDescription ""))

(setq blockDescription (strcase blockDescription)) ; Normalize block description to lowercase

(if (or (wcmatch (strcase blockName) (strcat "*" searchText "*"))
(vl-string-search searchText blockDescription))
(setq foundBlocks (cons blockName foundBlocks))
)

(setq ss (ssdel ent ss))
)

(if foundBlocks
(progn
(setq blockCount (length foundBlocks))
(setq blockCountStr (itoa blockCount))
(prompt (strcat "\nFound " blockCountStr " blocks with matching text in name or description."))

(foreach blockName foundBlocks
(prompt (strcat "- " blockName))
)
)
(prompt "\nNo blocks found in the drawing.")
)
)
(prompt "\nNo blocks found in the drawing.")
)
)
(prompt "\nInvalid search text.")
)

(princ)
)

======================================

 

Thank you in advance.

 

-Seth

0 Likes
Accepted solutions (1)
418 Views
5 Replies
Replies (5)
Message 2 of 6

felix.corre
Advocate
Advocate

Hello,

you have to look for the description in the block table.

(defun c:SearchBlocksByText ()
(setq TblBk (SCAN_TB))
(setq searchText (getstring "\nEnter the text to search for in block names or descriptions: "))

(if (and searchText (/= searchText ""))
(progn
(setq foundBlocks '())
(setq ss (ssget "_X" (list (cons 0 "INSERT"))))

(if ss
(progn
(setq searchText (strcase searchText)) ; Normalize search text to lowercase

(repeat (sslength ss)
(setq ent (ssname ss 0))
(setq entData (entget ent))
(setq blockName (cdr (assoc 2 entData)))
(foreach elt TblBk
(if (= (cdr (assoc 2 elt)) blockName)
(setq blockDescription (cdr (assoc 4 elt)))
  )
  )

(setq blockName (if blockName blockName ""))
(setq blockDescription (if blockDescription blockDescription ""))

(setq blockDescription (strcase blockDescription)) ; Normalize block description to lowercase

(if (or (wcmatch (strcase blockName) (strcat "*" searchText "*"))
(vl-string-search searchText blockDescription))
(setq foundBlocks (cons blockName foundBlocks))
)

(setq ss (ssdel ent ss))
)

(if foundBlocks
(progn
(setq blockCount (length foundBlocks))
(setq blockCountStr (itoa blockCount))
(prompt (strcat "\nFound " blockCountStr " blocks with matching text in name or description."))

(foreach blockName foundBlocks
(prompt (strcat "- " blockName))
)
)
(prompt "\nNo blocks found in the drawing.")
)
)
(prompt "\nNo blocks found in the drawing.")
)
)
(prompt "\nInvalid search text.")
)

(princ)
)

(defun SCAN_TB( / tb TblBlk)
(setq TblBlk nil)
(setq tb (tblnext "BLOCK" T))
(while tb
(setq TblBlk (cons tb TblBlk))
(setq tb (tblnext "BLOCK"))
)
  TblBlk
)

 

Félix.

Message 3 of 6

scohen
Contributor
Contributor

Hi Felix,

 

Thanks for the response, and updated code.  I'm receiving the following error, "error: File load canceled:" .  Sorry, but like I said, I barely code :-), and it’s probably missing a simple parentheses somewhere.

 

Thanks again.

 

-Seth Cohen

0 Likes
Message 4 of 6

paullimapa
Mentor
Mentor

And in case there are dynamic blocks replace

(setq blockName (cdr (assoc 2 entData)))

with this

 (setq blockName (vla-get-effectivename(vlax-ename->vla-object ent)))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 6

felix.corre
Advocate
Advocate
Accepted solution

Hi,

It works fine for me. The lisp to load attached.

Félix

0 Likes
Message 6 of 6

scohen
Contributor
Contributor

Hi Felix,

 

Thanks so much!!!!  Works awesome.

 

-Seth

0 Likes