Jump to content
benhubel

Lisp to create numbered folders based on selection

Recommended Posts

benhubel

I have some drawings which contain hundreds of numbers (example: 1-218b), each of which is on geometry which will be written as a block into its own folder. I am trying to write a Lisp that will create all of the folders based on the selected text. I have found a few posts about writing a directory, but it's all super confusing to me.

 

General routine process:

1. Get selected text objects

2. Prompt for directory to create new folders in

3. Create a folder for each selected text object, named based on the text

 

It's ok to assume that the text is already selected when the Lisp starts. If no text is selected, I'd just send an error message to the console and end the routine.

 

It's preferable that the directory prompt is GUI based so that I can navigate to the directory, but I am absolutely clueless here. If needed, I can just copy and paste the directory path into the console.

 

The routine doesn't have to work with MText. The files contain only single line text.

 

If I can get any code working, I'll post it, but I think it's going to take a good long while.

Share this post


Link to post
Share on other sites
Lee Mac

Here's a start:

(defun c:createfolders ( / dir idx sel )
   (if (and (setq sel (ssget '((0 . "TEXT") (1 . "~*[\\/:*?\"<>|]*"))))
            (setq dir (LM:browseforfolder "Select the output directory:" nil 0))
       )
       (repeat (setq idx (sslength sel))
           (vl-mkdir (strcat dir "\\" (cdr (assoc 1 (entget (ssname sel (setq idx (1- idx))))))))
       )
   )
   (princ)
)

;; Browse for Folder  -  Lee Mac
;; Displays a dialog prompting the user to select a folder.
;; msg - [str] message to display at top of dialog
;; dir - [str] [optional] root directory (or nil)
;; bit - [int] bit-coded flag specifying dialog display settings
;; Returns: [str] Selected folder filepath, else nil.

(defun LM:browseforfolder ( msg dir bit / err fld pth shl slf )
   (setq err
       (vl-catch-all-apply
           (function
               (lambda ( / app hwd )
                   (if (setq app (vlax-get-acad-object)
                             shl (vla-getinterfaceobject app "shell.application")
                             hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                             fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg bit dir)
                       )
                       (setq slf (vlax-get-property fld 'self)
                             pth (vlax-get-property slf 'path)
                             pth (vl-string-right-trim "\\" (vl-string-translate "/" "\\" pth))
                       )
                   )
               )
           )
       )
   )
   (if slf (vlax-release-object slf))
   (if fld (vlax-release-object fld))
   (if shl (vlax-release-object shl))
   (if (vl-catch-all-error-p err)
       (prompt (vl-catch-all-error-message err))
       pth
   )
)

(vl-load-com) (princ)

 

The above uses my Browse for Folder function.

Share this post


Link to post
Share on other sites
benhubel

That is EXACTLY what I was trying to do!

I wonder if I can convince my boss to hire you to do some coding for us. You've already saved me a good amount of time. I'll throw the idea his way.

Thank you very much for writing this!

Share this post


Link to post
Share on other sites
Lee Mac

You're welcome! - I'm glad it helps, and I look forward to hearing from you.

 

Lee

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×