Function Syntax: DuplicateToSelectedLayer Version: 1 Date: 12.07.2025 N/A Command name DuplicateToSelectedLayer (type DuplicateToSelectedLayer at the AutoCAD command line) Description This routine lets the user select one or more entities, choose a target layer from a dialog, then duplicates the entities onto that layer, applies draw order so the new copies are brought to front, and leaves the duplicated objects as the current selection for immediate follow-on operations. User interaction • The user is prompted to select entities to duplicate using any normal selection method. • A dialog titled Select Target Layer opens with a dropdown list of available layers. • The user clicks OK to run the duplication or Cancel to exit. High-level workflow • Reads the last used target layer from the environment variable DuplicateToLayer_LastLayer. • Builds a list of all drawing layers, excluding xref layers whose names match *|*. • Writes a temporary DCL file for the layer-selection dialog and loads it using load_dialog and new_dialog. • Populates the dropdown list with sorted layer names. • Sets the dropdown default selection to the last used layer when available. • On OK: • Stores the chosen layer back to DuplicateToLayer_LastLayer for next time. • Temporarily sets CLAYER to the chosen layer. • Runs the AutoCAD COPY command on the selection set with zero displacement (0,0 to 0,0), producing duplicates in place. • Retrieves the newly created entities via ssget "_P" (previous selection set). • Forces the copied entities to the chosen layer using CHPROP with the LA option. • Brings the copied entities to the front using DRAWORDER with Front. • Makes the copied entities the active selection using sssetfirst. • Restores the original current layer. Functionalities • Duplicates any selected entities in place (no movement) using COPY with zero displacement. • Lets the user choose the destination layer from a dialog dropdown. • Excludes xref layers from the destination list using the name pattern *|*. • Remembers the last chosen destination layer across sessions using getenv and setenv. • Moves the duplicated entities onto the chosen layer using CHPROP. • Changes draw order so duplicates are front-most. • Leaves the duplicated entities selected for immediate follow-up editing. Important commands and calls highlighted ssget — collects the initial entity selection set. vlax-for over Layers — enumerates layer names from the drawing. wcmatch "*|*" — filters out xref layers from the target layer list. getenv and setenv — persists the last used layer choice. load_dialog and new_dialog — loads the temporary DCL dialog. COPY — duplicates entities with zero displacement. ssget "_P" — retrieves the newly copied entities as the previous selection set. CHPROP with LA — assigns the new copies to the target layer. DRAWORDER Front — brings duplicates to the front. sssetfirst — makes the duplicated entities the active selection. setvar "CLAYER" — temporarily switches and then restores the current layer. Outputs and messages • If nothing is selected initially: prints No objects selected. • If the dialog cannot load: prints Unable to load dialog. • If cancelled in the dialog: prints Operation cancelled. • On success: prints the number of duplicated entities and confirms the target layer name, and indicates that the duplicates were brought to front. Operational notes and limitations • The routine uses AutoCAD commands (COPY, CHPROP, DRAWORDER) rather than entmakex, so it depends on command availability and user command settings. • COPY is executed with base point 0,0 and displacement 0,0, so duplicates are created directly on top of the originals. • The copied entities are identified using ssget "_P"; if another command changes the previous selection unexpectedly, the captured set may differ. • Xref layers are excluded only by name pattern; non-xref layers containing a pipe character could also be excluded.