Function Syntax: DuplicateLayer Version: 4.1 Date: Function Syntax: AI+https://autolisps.blogspot.com/p/duplicatelayer.html Command names • DUPLICATELAYER (type DUPLICATELAYER at the AutoCAD command line) • DUPLICATELAYERCURRENT (type DUPLICATELAYERCURRENT at the AutoCAD command line) Description This set of routines duplicates an existing layer by creating a new layer that inherits key properties from a source layer, prompts the user for the new name in a dialog, and then sets the new layer as the current layer. For DUPLICATELAYER, it can also move the selected object onto the new layer if a checkbox is enabled. Prerequisites • Requires Visual LISP COM support via (vl-load-com). • Requires permission to write a temporary DCL file for the rename dialog using vl-filename-mktemp. User interaction • Both commands show a DCL dialog titled Duplicate Layer. • The dialog contains: • Edit box for the New Layer Name. • Toggle checkbox: Assign selected object to new layer (default checked). • OK and Cancel buttons. High-level workflow • Creates a temporary DCL file each run, loads it, shows the dialog, then deletes the file. • Collects the new layer name and checkbox state from the dialog result. • Checks whether the target layer already exists via tblsearch "LAYER". • If the layer does not exist, creates it by copying properties from the source layer using CreateLayerLike. • Sets the newly created or existing target layer as the current layer by setting CLAYER. • For DUPLICATELAYER only, optionally reassigns the selected entity to the new layer and updates it in the drawing. Functionalities • Duplicates layer styling by copying properties from a source layer: Color. Linetype. Lineweight. Description. PlotStyleName is copied only if it is available and readable, typically in STB-based drawings. • Prompts for a new layer name with a default suggestion based on the source layer. • Avoids duplicate creation by reusing an existing layer if the chosen name already exists. • Sets the duplicated layer as the current layer after creation or reuse. • Optionally assigns the originally selected object to the new layer (DUPLICATELAYER only). Command behavior details • DUPLICATELAYER: • Prompts the user to select an entity. • Uses that entity layer as the source layer to duplicate. • Shows the dialog with default name Copy_of_ plus the source layer name. • If the checkbox is enabled, moves the selected entity to the new layer using entmod and refreshes it using entupd. • DUPLICATELAYERCURRENT: • Uses the current layer from CLAYER as the source. • Shows the dialog with default name Copy_of_ plus the current layer name. • Creates or reuses the layer, then sets it as current. • The checkbox is shown by the dialog, but the command does not use it and does not move any objects. Important commands and calls highlighted CreateLayerLike — creates a new layer and copies layer properties from the source layer using ActiveX. ShowRenameDialog — writes a temporary DCL dialog, captures user input, deletes the DCL file. entsel — selects the reference entity for DUPLICATELAYER. entget and assoc 8 — reads the selected entity layer name. tblsearch "LAYER" — checks whether a layer already exists. vla-Add and vla-Item — creates or retrieves layer objects in the Layers collection. setvar "CLAYER" — sets the current layer. entmod and entupd — updates the selected entity to the new layer and refreshes it. Outputs and messages • If an entity is not selected (DUPLICATELAYER): prints Nothing selected. • If the dialog is cancelled: prints Operation cancelled. • If the layer is created: prints Layer "name" created. • If the layer already exists: prints Layer "name" already exists, using it. • If assignment is enabled and completed: prints Selected object assigned to new layer. • Always reports the new current layer name after success. Operational notes and limitations • CreateLayerLike assumes the source layer exists; if the source layer name is invalid, the ActiveX call to vla-Item can error. • PlotStyleName is conditionally copied only when the property is available and readable; this is designed to avoid errors in CTB-based drawings or when PlotStyleName is not exposed. • The dialog always defaults the checkbox to enabled, but only DUPLICATELAYER actually applies it. • The code does not explicitly validate layer naming rules beyond non-empty text; names invalid for AutoCAD may fail at layer creation time.