PurgeUnusedLayers

Short description

  • Purges unused layers using a dialog that scans the drawing and deletes layers with no entities on them.
  • Protects critical layers and the current layer and optionally skips Xref layers.
purgeunusedlayers 000

Command:

  • Command: PURGEUNUSEDLAYERS

Description:

  • Creates a single file workflow by embedding a DCL definition inside the LISP and writing it to the temp folder at runtime.
  • Loads the dialog, scans all layers, identifies unused layers, then lets you purge selected layers or purge all unused layers.
  • Always prevents deletion of layer 0, layer DEFPOINTS, and the current layer.

Helper function: (if any)​

  • PUL:_DCLLines builds the DCL text as a list of lines used to generate the dialog file.
  • PUL:_WriteDCL writes the embedded DCL content to a temp .dcl file.
  • PUL:_EnsureDCL forces a fresh DCL write to avoid stale dialog definitions.
  • PUL:_SplitByComma parses the user skip list from the dialog into layer names.
  • PUL:_AllLayers collects all layer names from the drawing using tblnext.
  • PUL:_ScanUnusedLayers detects unused layers by checking for entities on each layer using ssget with a layer filter.
  • PUL:_TryLayDel attempts deletion via -LAYDEL and uses tblsearch to confirm removal.
  • PUL:_PurgeLayers purges a list of layers, temporarily suppressing command echo and returning deleted and failed counts.

Functionalities:

  • Dialog options: toggle to Skip Xref layers that match *|* and an edit box for additional never delete layers.
  • Scan: refreshes the unused layer list based on current settings and updates the status count.
  • Purge Selected: deletes only the layers selected in the list box.
  • Purge All: deletes all currently detected unused layers in one operation.
  • Safety rules: never deletes 0, DEFPOINTS, or the current layer and can exclude Xref layers when enabled.

Result:

  • Shows a populated list of unused layers and a status line with the total unused layers found.
  • After purge, prints results to the command line with counts for Deleted and Failed deletions.
  • If nothing is selected or nothing is unused, reports the condition instead of deleting anything.
  • Cancelled is reported when the dialog is closed without purging.

Images, animations etc.

purgeunusedlayers 000
Pixel

XAML code:

<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:src="clr-namespace:Autodesk.Windows;assembly=AdWindows">
 
 <src:RibbonToolTip x:Key="PurgeUnusedLayers">
 <src:RibbonToolTip.ExpandedContent>
 <StackPanel>
 <TextBlock Background="AntiqueWhite" TextAlignment="Left">
<Bold>Function Syntax: PurgeUnusedLayers</Bold>
<LineBreak/>
<Hyperlink>N/A</Hyperlink>


<Bold>Command name</Bold><LineBreak/>
<Run>PURGEUNUSEDLAYERS (type </Run><Run Foreground="DarkRed">PURGEUNUSEDLAYERS</Run><Run> at the AutoCAD command line)</Run><LineBreak/>
<LineBreak/>

<Bold>Description</Bold><LineBreak/>
<Run>This routine provides a dialog-based tool to </Run><Run Foreground="DarkRed">scan</Run><Run> the drawing for unused layers and then </Run><Run Foreground="DarkRed">delete</Run><Run> them either by selecting specific layers or purging all unused layers at once.</Run><LineBreak/>
<LineBreak/>

<Bold>Prerequisites</Bold><LineBreak/>
<Run>• Requires Visual LISP COM support via </Run><Run Foreground="DarkRed">(vl-load-com)</Run><Run>.</Run><LineBreak/>
<Run>• Requires permission to write a temporary DCL file in the folder provided by </Run><Run Foreground="DarkRed">TEMPPREFIX</Run><Run>.</Run><LineBreak/>
<LineBreak/>

<Bold>User interaction</Bold><LineBreak/>
<Run>• The command opens a </Run><Run Foreground="DarkRed">DCL dialog</Run><Run> titled Purge Unused Layers.</Run><LineBreak/>
<Run>• The dialog supports:</Run><LineBreak/>
<Run>  • Toggle: </Run><Run Foreground="DarkRed">Skip Xref layers (*|*)</Run><Run>.</Run><LineBreak/>
<Run>  • Edit box: </Run><Run Foreground="DarkRed">Never delete these layers</Run><Run> (comma-separated list, default 0,DEFPOINTS).</Run><LineBreak/>
<Run>  • Button: </Run><Run Foreground="DarkRed">Scan</Run><Run> to refresh the unused layer list.</Run><LineBreak/>
<Run>  • List box: multi-select list of detected unused layers.</Run><LineBreak/>
<Run>  • Button: </Run><Run Foreground="DarkRed">Purge Selected</Run><Run>.</Run><LineBreak/>
<Run>  • Button: </Run><Run Foreground="DarkRed">Purge All</Run><Run>.</Run><LineBreak/>
<Run>  • Button: </Run><Run Foreground="DarkRed">Close</Run><Run>.</Run><LineBreak/>
<LineBreak/>

<Bold>High-level workflow</Bold><LineBreak/>
<Run>• Builds the dialog definition in memory using </Run><Run Foreground="DarkRed">PUL:_DCLLines</Run><Run> and writes it to a temp DCL file every run.</Run><LineBreak/>
<Run>• Loads the DCL with </Run><Run Foreground="DarkRed">load_dialog</Run><Run>, opens it with </Run><Run Foreground="DarkRed">new_dialog</Run><Run>, and runs it with </Run><Run Foreground="DarkRed">start_dialog</Run><Run>.</Run><LineBreak/>
<Run>• Performs an initial </Run><Run Foreground="DarkRed">scan</Run><Run> when the dialog opens, then updates automatically when the user changes options.</Run><LineBreak/>
<Run>• Scanning logic checks each layer name for entity usage by running </Run><Run Foreground="DarkRed">ssget "_X"</Run><Run> filtered by layer.</Run><LineBreak/>
<Run>• Results are displayed in a list box and a status label shows the count of unused layers.</Run><LineBreak/>
<Run>• If the user chooses to purge, the routine attempts deletion using </Run><Run Foreground="DarkRed">-LAYDEL</Run><Run> and verifies success by checking layer existence in the table afterwards.</Run><LineBreak/>
<LineBreak/>

<Bold>Functionalities</Bold><LineBreak/>
<Run>• Detects unused layers by scanning the drawing database.</Run><LineBreak/>
<Run>• Excludes the </Run><Run Foreground="DarkRed">current layer (CLAYER)</Run><Run> from purge candidates.</Run><LineBreak/>
<Run>• Always protects </Run><Run Foreground="DarkRed">0</Run><Run> and </Run><Run Foreground="DarkRed">DEFPOINTS</Run><Run> from deletion, even if the user does not list them.</Run><LineBreak/>
<Run>• Allows user-defined protected layers via a comma-separated list.</Run><LineBreak/>
<Run>• Optionally excludes layers that look like xref layers by pattern </Run><Run Foreground="DarkRed">*|*</Run><Run>.</Run><LineBreak/>
<Run>• Supports multi-select purging of only the layers chosen in the dialog.</Run><LineBreak/>
<Run>• Supports one-click purging of all currently detected unused layers.</Run><LineBreak/>
<Run>• Reports results after purge: number deleted and number failed.</Run><LineBreak/>
<LineBreak/>

<Bold>Unused-layer detection rules</Bold><LineBreak/>
<Run>• Builds a full layer list using </Run><Run Foreground="DarkRed">tblnext "LAYER"</Run><Run>.</Run><LineBreak/>
<Run>• A layer is considered unused if no entities exist on it, determined by:</Run><LineBreak/>
<Run>  • <Run Foreground="DarkRed">ssget "_X" (list (cons 8 layerName))</Run> returns nil.</Run><LineBreak/>
<Run>• A layer is excluded from the candidate list if:</Run><LineBreak/>
<Run>  • It is in the user-defined skip list.</Run><LineBreak/>
<Run>  • It is the current layer from </Run><Run Foreground="DarkRed">CLAYER</Run><Run>.</Run><LineBreak/>
<Run>  • Skip Xref layers is enabled and the name matches </Run><Run Foreground="DarkRed">*|*</Run><Run>.</Run><LineBreak/>
<LineBreak/>

<Bold>Purge behavior</Bold><LineBreak/>
<Run>• Purge Selected:</Run><LineBreak/>
<Run>  • Reads the list_box selection indexes returned as a space-separated string.</Run><LineBreak/>
<Run>  • Converts indexes to a list using </Run><Run Foreground="DarkRed">PUL:_ParseIdxList</Run><Run>.</Run><LineBreak/>
<Run>  • Maps indexes back to layer names and attempts to delete only those layers.</Run><LineBreak/>
<Run>• Purge All:</Run><LineBreak/>
<Run>  • Attempts to delete every layer in the current unused-layer list.</Run><LineBreak/>
<LineBreak/>

<Bold>Important commands and calls highlighted</Bold><LineBreak/>
<Run>• <Run Foreground="DarkRed">PUL:_EnsureDCL</Run> — writes the DCL file to the temp directory each run to avoid stale dialogs.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">action_tile</Run> — wires Scan and option changes to live refresh of results.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">tblnext</Run> — enumerates layers in the drawing.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">ssget "_X"</Run> with layer filter — detects whether any entities exist on a layer.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">-LAYDEL</Run> via </Run><Run Foreground="DarkRed">vl-cmdf</Run><Run> — attempts to delete layers.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">vl-catch-all-apply</Run> — prevents hard failures if -LAYDEL errors on protected or constrained layers.</Run><LineBreak/>
<Run>• <Run Foreground="DarkRed">tblsearch "LAYER"</Run> — verifies whether a layer was actually removed.</Run><LineBreak/>
<LineBreak/>

<Bold>Outputs and messages</Bold><LineBreak/>
<Run>• On load: prints </Run><Run Foreground="DarkRed">Loaded: PURGEUNUSEDLAYERS (dialog-based unused layer purge)</Run><Run>.</Run><LineBreak/>
<Run>• If cancelled: prints </Run><Run Foreground="DarkRed">Cancelled</Run><Run>.</Run><LineBreak/>
<Run>• If Purge Selected with nothing selected: prints </Run><Run Foreground="DarkRed">No layers selected</Run><Run>.</Run><LineBreak/>
<Run>• If no unused layers exist: prints </Run><Run Foreground="DarkRed">No unused layers found</Run><Run>.</Run><LineBreak/>
<Run>• After purging: prints </Run><Run Foreground="DarkRed">Deleted: N | Failed: M</Run><Run>.</Run><LineBreak/>
<LineBreak/>

<Bold>Operational notes and limitations</Bold><LineBreak/>
<Run>• 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.</Run><LineBreak/>
<Run>• 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.</Run><LineBreak/>
<Run>• The Skip Xref option relies on name matching </Run><Run Foreground="DarkRed">*|*</Run><Run>, which is a conventional xref layer naming pattern.</Run><LineBreak/>



</TextBlock>
 
 <!-- Use MediaElement for GIF (static first frame) and Image as fallback -->

 
 
 </StackPanel>
 </src:RibbonToolTip.ExpandedContent>
 </src:RibbonToolTip>
</ResourceDictionary>

Additional info:

Share this page:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Tags: 🏷️ Autocad Lisps, 🏷️ Layers, 🏷️ Layers_menu
0
Would love your thoughts, please comment.x
()
x