@nikko_1983 wrote:
Yes, i want this to work with multiple files. And i don't understand your last question about ignoring XREFs. Thank you for your patience.
Works on multiple files The program will process ALL dwg files on the selected folder, additional coding is required otherwise such as a list box.
(Defun C:Dellayeronlist ( / _LayDel f AllDrawingFiles odbx layerIsCurrent layerNotDeleted)
;; pBe July 2022 ;;
(defun _LayDel (DBXDoc llst / Blocks layers clayer RevertBackTo status layeritem)
(setq Blocks (vla-get-blocks DBXDoc)
layers (vla-get-layers DBXDoc)
status '("LayerOn" "lock" "Freeze")
)
;;; Collect layer items and save the status ;;;
(vlax-for itm layers
(setq RevertBackTo
(cons
(append (list (Vla-get-name itm) itm
(mapcar '(lambda (p) (vlax-get itm p))
status )))
RevertBackTo ))
(mapcar '(lambda (p v)
(vl-catch-all-error-p (vl-catch-all-apply 'vlax-put (list itm p v))))
status '(-1 0 0)
)
)
;; Delete objects under listed layers including blocks
(vlax-for blk Blocks
(if (eq :vlax-false (vla-get-isXref blk))
(vlax-for h blk
(if (and
(vlax-write-enabled-p h)
(member (vla-get-layer h) llst)
)
(vla-delete h)
)
)
)
)
;;; Set back the original status and delete listed layers
(foreach lns RevertBackTo
(setq layeritem (cadr lns))
(cond
( (not (member (strcase (Car lns)) llst))
(mapcar '(lambda (j m) (vlax-put layeritem j m)) status
(caddr lns)
)
)
( (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete (list layeritem)))
(setq clayer (Car lns)))
)
)
clayer
)
;; This can be a sub to prompt the user for ;;
;; layernames or read a external file to be ;;
;; used as a source of layer names. ;;
(setq layerlist '("LAYERNAME1" "LAYERNAME2"))
;; Works on multiple files ;;
;; The program will process ALL dwg files ;;
;; on the selected folder, additional ;;
;; coding is required otherwise such as ;;
;; a list box. ;;
(if (and
(setq f (acet-ui-pickdir "Select Target Folder"))
(setq AllDrawingFiles (vl-directory-files f "*.dwg"))
)
(progn
(if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
(setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
(setq odbx (vlax-get-or-create-object
(strcat "ObjectDBX.AxDbDocument."
(substr (getvar "ACADVER") 1 2)
)
)
)
)
(foreach dwg AllDrawingFiles
(if (vl-catch-all-error-p
(vl-catch-all-apply 'vla-open (list odbx (strcat f "\\" dwg)))
)
(princ (strcat "Odbx connection no available with file :\n" dwg))
(progn
(setq layerIsCurrent (_LayDel odbx layerlist))
(vla-saveas odbx (strcat f "\\" dwg))
(and layerIsCurrent
(setq layerNotDeleted (cons (list dwg layerIsCurrent)
layerNotDeleted)))
)
)
)
(vlax-release-object odbx)
(foreach itm layerNotDeleted (print itm))
)
)
(princ)
)
Command: Dellayeronlist
Please note:
A layer name may not be deleted if the layer name is the current layer. the program will spit out a list at the showing name of the drawing ang the layer name
("Drawing name.dwg" "Layername")
Also
(setq layerlist '("LAYERNAME1" "LAYERNAME2")) <--- target layer names
This can be a sub to prompt the user for layernames or read a external file to be used as a source of layer names
HTH