Library lisp routine

Library lisp routine

rfigueroa24PK4
Enthusiast Enthusiast
1,809 Views
44 Replies
Message 1 of 45

Library lisp routine

rfigueroa24PK4
Enthusiast
Enthusiast

Hello all, 

So I tried to have Chatgpt create a lisp routine that would allow me to TYPE a product name into the command line and it search for it in a designated location for it to insert and drop into the model space drawing. For instance, I type in "HD-RXC-4KZ-101" into the command line, and have it search in my C:\CAD\LIBRARY\AV-300 ONE-LINE STANDARD BLOCKS folders where I have multiple different brand folders with all the model equipment in them. it should be able to find it and have me drop it into my drawings. But no matter how I word it, it never works so I gave up lol. Can anyone help me with this situation? Thank you!

0 Likes
Accepted solutions (1)
1,810 Views
44 Replies
Replies (44)
Message 2 of 45

chriscowgill7373
Mentor
Mentor

to pull off what you are describing, you'd have to write a custom routine for every block you want to insert.  That seems counter productive.

There are other ways you could possibly do it, but not without some sort of interaction, perhaps creating a custom dialog box with some pull downs in it.

Or, if they are all grouped by brand, you could setup shortcuts to set the block library path to go to that brand folder and then you choose the block from the palette.


Christopher T. Cowgill, P.E.

AutoCAD Certified Professional
Civil 3D Certified Professional
Civil 3D 2026 on Windows 11

Please select the Accept as Solution button if my post solves your issue or answers your question.

Message 3 of 45

Kent1Cooper
Consultant
Consultant

If it's not too impractical to put all the Blocks folders into your Support File Search Path list, then you don't need any custom routine(s).  Start an -INSERT command [in the command line instead of the dialog box because of the hyphen prefix], and type in the Block name, and it will find it without the need for you to navigate to it.

Kent Cooper, AIA
Message 4 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

So my previous company had this exact lisp routine where we would type the product name and it would find it automatically and we just drop it in. It was a game changer and truly made things 100% faster for sure

0 Likes
Message 5 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

So my previous company had this exact lisp routine where we would type the product name and it would find it automatically and we just drop it in. It was a game changer and truly made things 100% faster

0 Likes
Message 6 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

I just tried your method and it does not work.

0 Likes
Message 7 of 45

Kent1Cooper
Consultant
Consultant

@rfigueroa24PK4 wrote:

So my previous company had this exact lisp routine where we would type the product name and it would find it ....


And how did it work?  Was it something like: you enter its custom command name to call it up, and it asks for the product name?  Was the product name the same as the drawing file name to be Inserted as a Block?  If that's all, just put in -INSERT instead of some custom command name, and it will ask you for a Block name.  No routine required, and no more work for the User, but for it to find things, it does require having all applicable folders included in the Support File Search Path list.  If the former routine didn't involve that, do you know anything about how it found files?

Kent Cooper, AIA
Message 8 of 45

Kent1Cooper
Consultant
Consultant

@rfigueroa24PK4 wrote:

I just tried your method and it does not work.


I can't trouble-shoot that, not having access to your folder structure or Support File Search Path list or exactly what you entered.  It works for me -- simply -INSERT and the name of a drawing file that's in a folder in the list [without needing to spell out the file path], and in it comes.

EDIT:  Also, "it does not work" is never enough information.  What happens?  What are you giving it, exactly?  How far does it get?  Are there any messages?  Etc., etc.

Kent Cooper, AIA
0 Likes
Message 9 of 45

apjones
Collaborator
Collaborator

What Kent is telling you is this... 

 

If you add the folder where your blocks live to your support file search path, it will work.

 

Support Folder.jpg

Pete

>Please Accept as Solution and give Kudos as appropriate to further enhance these forums. Thank you!
Message 10 of 45

Kent1Cooper
Consultant
Consultant

@apjones wrote:

.... If you add the folder where your blocks live to your support file search path, it will work.


Theirs are not all in just one folder [see Message 1], but I don't think there's any limit to the number of folders that can be in the SFSP list.  The unlikely but possible problem would be if there could be parts in different brand folders but with the same file name.  In that case, the plain -INSERT and name approach will use the version in the folder that's highest in the list [the first one it finds].  But I assume that's unlikely because it would probably also have been a problem in their older routine if all they needed to give it was the name.

Kent Cooper, AIA
Message 11 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

If the product I wanted was "HD-RXC-4KZ-101" i would literally just type that name in and hit enter, click where i wanted on the screen and it would be inserted. that simple, nothing else. i just tried you method again by putting the folder path in the search file path in options and it gave me this message after trying to look up a device in the one-line standards blocks folder. That folder contains hundreds of other folders/models and each with multiple dwg files of devices/models.

rfigueroa24PK4_0-1784056682630.png

 

0 Likes
Message 12 of 45

DGCSCAD
Advisor
Advisor

With that kind of folder structure, you might be better off just using the search option in Explorer, then drag/drop your (found) block into your dwg.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 13 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

So even though there is a lisp routine that was created that can do exactly this, you are saying its easier to just do it through explorer and not use the lisp routine?

0 Likes
Message 14 of 45

Kent1Cooper
Consultant
Consultant

@rfigueroa24PK4 wrote:

If the product I wanted was "HD-RXC-4KZ-101" i would literally just type that name in ....


I agree with the earlier comment that it sounds like you must have a whole separate custom command defined for each individual product, each knowing its own folder location.  Can that be the case, with hundreds of them?  Is there some big .lsp file containing all those routines?

Kent Cooper, AIA
0 Likes
Message 15 of 45

Sea-Haven
Mentor
Mentor

Me like the others, had like 500+ blocks each in a type directory, we would select the correct type and an image of the blocks would be displayed and say name, then choose. It uses a custom POP menu, written in notepad, a lisp was used to make images. Can provide more detail.

SeaHaven_0-1784090792193.pngSeaHaven_1-1784090841660.png

 

I agree with the others if you have all your blocks in one support path, then just typing correct should insert. The other way maybe is to enter a few characters and a matching list is displayed saves typos.

 

Just a PS years ago a support path you added .. two periods at end and that meant look in sub directory also, "D:\Bigal\Blocks.." not sure if still supported in 26 etc. It was undocumented.

 

 

0 Likes
Message 16 of 45

DGCSCAD
Advisor
Advisor

@rfigueroa24PK4 wrote:

So even though there is a lisp routine that was created that can do exactly this, you are saying its easier to just do it through explorer and not use the lisp routine?


Just a suggestion for an alternate non-LISP solution, which would basically emulate the same functionality.

 

After a bit of a think, here's a quick outline of how a LISP function might work:

  • Create a list of those hundreds of folders located under the parent: vl-directory-files
  • Iterate through each item in the list: foreach
  • Look for your part: wcmatch
  • If found, insert

I'm sure there are others here that can improve/expand upon that.

 

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 17 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

Thank you, Ill look into this method as well.

0 Likes
Message 18 of 45

Sea-Haven
Mentor
Mentor
Accepted solution

Maybe something like this. note hard coded for top directory.

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/library-lisp-routine/td-p/14198067
; look for block in a diretcory by name 
; By AlanH July 2026

(defun c:GetFolderDWGs ( / selectedFolder allDWGs dwgname dwgn)
(vl-load-com)
(defun FindDWGsRecursive (path / files)
    (if (/= (substr path (strlen path)) "\\")
      (setq path (strcat path "\\"))
    )
    (setq files (mapcar '(lambda (x) (strcat path x)) (vl-directory-files path "*.dwg" 1)))
    (foreach item (vl-directory-files path nil -1)
      (if (not (member item '("." "..")))
        (setq files (append files (FindDWGsRecursive (strcat path item))))
      )
    )
    files
)
(setq allDWGs (FindDWGsRecursive "D:\\autodesk\\blocks")) ; change to your top directory
(alert (strcat "Found  " (rtos (length alldwgs) 2 0) "  DWG Files:\n")) ; change alert to princ
(setq dwgname  (strcase (getstring "\nEnter block name " T)))
(foreach dwgn allDWGs
  (if (wcmatch (strcase dwgn) (strcat "*" dwgname "*"))
    (command "-insert" dwgn (getpoint "\nPick insertion point ") 1 1 0)
  )
)
  (princ)
)
(c:GetFolderDWGs)

 

PS when testing 548 dwg's found.

Message 19 of 45

rfigueroa24PK4
Enthusiast
Enthusiast

When I dropped it in it says "0 dwg's found". Is it maybe because when I read your routine it has a different folder path? I see this path in the lisp, D:\\autodesk\\blocks")) 

0 Likes
Message 20 of 45

Kent1Cooper
Consultant
Consultant

@rfigueroa24PK4 wrote:

When I dropped it in it says "0 dwg's found". .... 


Read the end of Message 18's first sentence, and the comment at the end of line 19 in the code part.

Kent Cooper, AIA
0 Likes