- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.