Jump to content

Lisp to create numbered folders based on selection


benhubel

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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...