试一试,添加了更多错误捕捉,以允许已经存在的文件名。
- ;;---------------------=={ Layer 2 DWG }==--------------------;;
- ;; ;;
- ;; WBlocks all active layers to separate drawings, saved to ;;
- ;; the specified directory ;;
- ;;------------------------------------------------------------;;
- ;; Author: Lee McDonnell, 2010 ;;
- ;; ;;
- ;; Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;;
- ;; Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
- ;;------------------------------------------------------------;;
- (defun c:Layer2DWG ( / *error* _UniqueFilename _UniqueItem _LayerList doc docname SelSets Path ss )
- (vl-load-com)
- ;; © Lee Mac 2010
- (defun *error* ( msg )
- (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
- (princ (strcat "\n** Error: " msg " **")))
- (princ)
- )
- (defun _UniqueFileName ( seed )
- (
- (lambda ( i / filename )
- (if (findfile (setq filename (strcat seed ".dwg")))
- (while (findfile (setq filename (strcat seed "(" (itoa (setq i (1+ i))) ").dwg"))))
- )
- filename
- )
- 1
- )
- )
- (defun _UniqueItem ( collection seed )
- (
- (lambda ( i )
- (while (LM:Itemp collection (strcat seed (itoa (setq i (1+ i))))))
- (strcat seed (itoa i))
- )
- 0
- )
- )
- (defun _LayerList ( doc / l )
- (vlax-for layer (vla-get-layers doc)
- (if
- (not
- (or
- (eq :vlax-false (vla-get-layeron layer))
- (wcmatch (vla-get-name layer) "*|*")
- )
- )
- (setq l (cons (vla-get-name layer) l))
- )
- )
- (reverse l)
- )
- (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
- docname (vl-filename-base (vla-get-Name doc))
- SelSets (vla-get-SelectionSets doc))
- (if (setq Path (LM:DirectoryDialog "Select Directory for New Files" nil 0))
- (progn
- (setq ss (vla-Add SelSets (_UniqueItem SelSets "LayerSave")))
-
- (mapcar
- (function
- (lambda ( layer )
- (LM:DXF->Variants (list (cons 8 layer)) 'typ 'val)
- (vla-Select ss acSelectionSetAll nil nil typ val)
- (if (not (zerop (vla-get-Count ss)))
- (progn
- (vla-WBlock doc (_UniqueFilename (strcat Path "\" docname "_" layer)) ss)
- (princ (strcat "\n>>> Extracted Layer: " layer))
- )
- (princ (strcat "\n[ Nothing Found on Layer: " layer " ]"))
- )
- (vla-clear ss)
- )
- )
- (_LayerList doc)
- )
- (vl-catch-all-apply 'vla-delete (list ss))
- )
- (princ "\n*Cancel*")
- )
- (princ)
- )
- ;;-------------------=={ Directory Dialog }==-----------------;;
- ;; ;;
- ;; Displays a dialog prompting the user to select a folder ;;
- ;;------------------------------------------------------------;;
- ;; Author: Lee McDonnell, 2010 ;;
- ;; ;;
- ;; Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;;
- ;; Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
- ;;------------------------------------------------------------;;
- ;; Arguments: ;;
- ;; msg - message to display at top of dialog ;;
- ;; dir - root directory (or nil) ;;
- ;; flag - bit coded flag specifying dialog display settings ;;
- ;;------------------------------------------------------------;;
- ;; Returns: Selected folder filepath, else nil ;;
- ;;------------------------------------------------------------;;
- (defun LM:DirectoryDialog ( msg dir flag / Shell Fold FObj Path ac )
- ;; © Lee Mac 2010
- (setq Shell (vla-getInterfaceObject (setq ac (vlax-get-acad-object)) "Shell.Application")
- Fold (vlax-invoke-method Shell 'BrowseForFolder (vla-get-HWND ac) msg flag dir))
- (vlax-release-object Shell)
|