Function Syntax: CopySelection Version: 1 Date: 01.11.2025 AI + https://www.theswamp.org/index.php?topic=59415.0 Purpose This program duplicates a selected segment of an LWPolyline between two user picked points, and creates the new polyline on a user selected layer chosen from a dialog with live name filtering or by picking a layer from an object in the drawing. Primary Command CopySelection prompts for one LWPolyline, two points on it, then creates a new LWPolyline segment copy on a chosen layer. User Workflow 1. Run CopySelection in the AutoCAD command line. 2. Select one LWPOLYLINE entity when prompted. 3. Specify the first point, then specify the second point, both of which are snapped to the closest points on the selected polyline. 4. A layer picker dialog opens, where the user can filter layers by name, select a layer from a dropdown list, or pick a layer from an existing object in the drawing. 5. If the user confirms a layer selection, the program duplicates only the polyline segment between the two points and creates a new polyline on the chosen layer. Key Inputs and Validation
  • Polyline selection The command requires selecting an entity and verifies it is of type LWPOLYLINE.
  • Two distinct points The user must pick two different points, the routine re projects each pick to the closest point on the polyline using vlax-curve-getclosestpointto, and it rejects identical points with a distinct points required message.
  • Layer selection The user must choose a valid layer in the dialog, otherwise the operation is cancelled without creating a new polyline.
Layer Picker Dialog The command dynamically writes a temporary DCL file and opens a dialog titled Set Layer for New Polyline. It supports filtering and a pick from drawing workflow. Layer Filtering Behavior
  • The dialog has an edit box labeled Filter by Name that filters the layer list to matching entries.
  • The filter uses wildcard matching logic by surrounding the typed text with * so it behaves like contains match, and it uses case insensitive comparison by converting names to uppercase.
  • The list of available layers excludes Xref dependent layers by skipping names that match the pattern containing a pipe character, which are typically Xref layer names.
  • The dialog remembers the last used layer and last used filter using environment variables so subsequent runs start from the previous context.
Pick Layer From Drawing The dialog includes a button Pick Layer From Drawing that closes the dialog temporarily, prompts the user to select an object, reads that object layer, and then re opens the dialog with that layer pre selected if it exists in the layer list. Persistent Settings The command stores and retrieves user preferences using environment variables.
  • PLDUPE_LastLayer stores the last chosen layer name, initially set to the original layer of the selected polyline as the default baseline.
  • PLDUPE_LastFilter stores the last typed filter text for the layer dialog.
Polyline Segment Duplication Logic After layer confirmation, the routine duplicates only the portion of the polyline between the snapped points p and q, using curve parameters and vertex reconstruction logic derived from PLDUPE style behavior. How the Segment Range is Determined
  • It computes curve parameters m and n at points p and q using vlax-curve-getparamatpoint.
  • If the parameters are reversed, it swaps m and n and also swaps p and q so the segment is processed in forward order along the polyline.
  • It extracts LWPolyline vertex data using LM:LWVertices, capturing each vertex point and width and bulge data for reconstruction.
Bulge and Width Handling
  • The routine includes a tangent helper tan and uses it to adjust bulge values when trimming at fractional parameters.
  • If the start point p lies within a segment rather than exactly at a vertex, it inserts a new synthetic vertex at p with interpolated start width and adjusted bulge for the partial segment.
  • If the end point q lies within a segment rather than exactly at a vertex, it inserts a new synthetic vertex at q and adjusts widths and bulge accordingly to end the duplicated segment cleanly.
New Polyline Creation The new polyline is created using entmakex, reusing most of the original polyline entity data but substituting specific fields for the new entity.
  • Layer The entity data is updated so group 8 is set to the user selected layer name.
  • Closed flag The closed bit is removed from group 70 so the new segment is not forced closed.
  • Extrusion and normal The routine preserves the polyline normal vector using group 210 from the original entity data to keep the new polyline oriented correctly in 3D context.
  • Elevation and header data It preserves elevation and header fields from the original LWPolyline by carrying forward group 39 and associated header data where applicable.
Temporary File Cleanup The command writes the dialog definition to a temporary DCL file and deletes it after the dialog workflow completes using vl-file-delete. Important Commands and Aspects CopySelection main command entry point. entsel used to select the LWPolyline and to pick an object for layer extraction. getpoint used to specify the two points, then projected to the polyline. vlax-curve-getclosestpointto snaps picked points onto the polyline accurately. PLDUPE_LastLayer remembers last selected layer across runs. PLDUPE_LastFilter remembers last filter text across runs. Pick Layer From Drawing lets the user derive a layer choice from an existing object. entmakex creates the new polyline representing the duplicated segment. Messages and Edge Cases
  • If no entity is selected at the start, it prints a no entity selected message and exits with no changes.
  • If the selected object is not an LWPolyline, it prints a not an LWPolyline message and exits with no changes.
  • If the two picked points resolve to the same closest point on the polyline, it forces the user to pick a distinct second point.
  • If the user cancels the dialog or does not confirm a layer, it prints a no layer selected operation cancelled message and does not create a new polyline.
  • If the user picks a layer from an object that is not present in the layer list, it reports that the picked layer was not found and does not apply it as the selection.
Operational Summary This program will require the user to select an LWPOLYLINE, then specify two distinct points which are projected onto that polyline, then choose a target layer using a dialog with name filtering or by picking a layer from an object, after which the program reconstructs and duplicates only the polyline segment between the two points including widths and bulges, creates a new polyline via entmakex on the selected layer, preserves the original normal orientation, and cleans up the temporary dialog file while remembering the last used layer and filter for the next run.