Function Syntax: PurgeUnusedLayers N/A Command name PURGEUNUSEDLAYERS (type PURGEUNUSEDLAYERS at the AutoCAD command line) Description This routine provides a dialog-based tool to scan the drawing for unused layers and then delete them either by selecting specific layers or purging all unused layers at once. Prerequisites • Requires Visual LISP COM support via (vl-load-com). • Requires permission to write a temporary DCL file in the folder provided by TEMPPREFIX. User interaction • The command opens a DCL dialog titled Purge Unused Layers. • The dialog supports: • Toggle: Skip Xref layers (*|*). • Edit box: Never delete these layers (comma-separated list, default 0,DEFPOINTS). • Button: Scan to refresh the unused layer list. • List box: multi-select list of detected unused layers. • Button: Purge Selected. • Button: Purge All. • Button: Close. High-level workflow • Builds the dialog definition in memory using PUL:_DCLLines and writes it to a temp DCL file every run. • Loads the DCL with load_dialog, opens it with new_dialog, and runs it with start_dialog. • Performs an initial scan when the dialog opens, then updates automatically when the user changes options. • Scanning logic checks each layer name for entity usage by running ssget "_X" filtered by layer. • Results are displayed in a list box and a status label shows the count of unused layers. • If the user chooses to purge, the routine attempts deletion using -LAYDEL and verifies success by checking layer existence in the table afterwards. Functionalities • Detects unused layers by scanning the drawing database. • Excludes the current layer (CLAYER) from purge candidates. • Always protects 0 and DEFPOINTS from deletion, even if the user does not list them. • Allows user-defined protected layers via a comma-separated list. • Optionally excludes layers that look like xref layers by pattern *|*. • Supports multi-select purging of only the layers chosen in the dialog. • Supports one-click purging of all currently detected unused layers. • Reports results after purge: number deleted and number failed. Unused-layer detection rules • Builds a full layer list using tblnext "LAYER". • A layer is considered unused if no entities exist on it, determined by: ssget "_X" (list (cons 8 layerName)) returns nil. • A layer is excluded from the candidate list if: • It is in the user-defined skip list. • It is the current layer from CLAYER. • Skip Xref layers is enabled and the name matches *|*. Purge behavior • Purge Selected: • Reads the list_box selection indexes returned as a space-separated string. • Converts indexes to a list using PUL:_ParseIdxList. • Maps indexes back to layer names and attempts to delete only those layers. • Purge All: • Attempts to delete every layer in the current unused-layer list. Important commands and calls highlighted PUL:_EnsureDCL — writes the DCL file to the temp directory each run to avoid stale dialogs. action_tile — wires Scan and option changes to live refresh of results. tblnext — enumerates layers in the drawing. ssget "_X" with layer filter — detects whether any entities exist on a layer. -LAYDEL via vl-cmdf — attempts to delete layers. vl-catch-all-apply — prevents hard failures if -LAYDEL errors on protected or constrained layers. tblsearch "LAYER" — verifies whether a layer was actually removed. Outputs and messages • On load: prints Loaded: PURGEUNUSEDLAYERS (dialog-based unused layer purge). • If cancelled: prints Cancelled. • If Purge Selected with nothing selected: prints No layers selected. • If no unused layers exist: prints No unused layers found. • After purging: prints Deleted: N | Failed: M. Operational notes and limitations • A layer can appear unused by entity scan but still fail deletion due to dependencies, locks, plot states, viewport usage, or other AutoCAD constraints; such cases increment the Failed count. • The unused detection checks only for entities on the layer; it does not explicitly inspect non-entity dependencies such as layer states, filters, dimension style references, or external constraints. • The Skip Xref option relies on name matching *|*, which is a conventional xref layer naming pattern.