Short description
Purpose: Create a simple two vertex centerline between two selected open curves using only their endpoints Supported objects: LINE, LWPOLYLINE, POLYLINE, ARC, SPLINE, ELLIPSE when they are open curves Usage style: Quick central axis between two roughly parallel edges selected as first and last items in a selection set
Command:
Command name: CPL_2 How to run: Type CPL_2 then press Enter Selection: Use any selection method to choose at least two open curves; the routine uses the first and the last selected entities Silent rules: If fewer than two objects are selected or if the chosen curves are closed, the routine simply ends without creating anything
Description:
The routine takes the first and last entities from the user selection and reads their geometric start and end points through the curve API, which works for lines, polylines, arcs, splines, and ellipses. It rejects closed curves by checking that each start point is different from its corresponding end point, which prevents creating a centerline from closed loops. From these endpoints, it computes the midpoint between the two starts and the midpoint between the two ends in the XY plane, then creates a two vertex LWPOLYLINE that connects those midpoints, effectively representing a simple centerline between the two boundary curves.
Helper function: (if any)
• vlax-curve-getStartPoint and getEndPoint Used to obtain pure geometry for any supported curve object through the curve API • vlax-make-safearray Builds a safe array of doubles containing the vertex coordinates for the new polyline • vlax-make-variant Wraps the safe array into a variant value required by the COM method • vla-AddLightWeightPolyline Called on ModelSpace to actually create the two vertex LWPOLYLINE object • vl-load-com Ensures the Visual LISP COM interface is loaded before any COM calls are made
Functionalities:
• Automatic curve type handling Uses the generic curve API so the same logic works across lines, polylines, arcs, splines, and ellipses • First and last driven logic Only the first and the last objects in the selection are used, which allows flexible window or crossing selection while still defining two sides • Closed curve safeguard Skips processing when a selected curve has identical start and end points, avoiding problematic closed input • Midpoint centerline Calculates midpoints between the two start points and between the two end points then draws the centerline through these midpoints • Planar output Uses only X and Y from the picked curves and ignores Z so the resulting LWPOLYLINE is a clean two dimensional object • Silent behaviour No command line prompts are issued; if the conditions are not met, nothing is created and the routine exits quietly
Result:
• Geometric result A new two vertex LWPOLYLINE is added in ModelSpace that represents the centerline between the two selected boundary curves • Non destructive The original entities remain unchanged; only an additional centerline object is created • Workflow benefit Provides an extremely fast way to generate a central axis for profiles, corridors, or similar parallel edge situations
Learn more: Open Initial Website Source Code
Images, animations etc.
Log in to download.
Log in
Log in
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="CenterPline_2">
<src:RibbonToolTip.ExpandedContent>
<StackPanel>
<TextBlock Background="AntiqueWhite" TextAlignment="Left">
<Bold>Function Syntax: CPL_2</Bold>
<LineBreak/>
<Bold>Version: 2 Date: 06.09.2025</Bold>
<LineBreak/>
<LineBreak/>
<Hyperlink>AI + https://www.cadtutor.net/forum/topic/49702-centerline-between-two-polylines/</Hyperlink>
<LineBreak/>
<Bold>Description</Bold><LineBreak/>
This routine defines a single command, <Run Foreground="Red">CPL_2</Run>, intended to draw a simple “centerline” between two selected open curves using only COM/ActiveX methods. It is designed to be silent on load (only <Run Foreground="DarkRed">(princ)</Run> at the end) and does not depend on command calls like OFFSET/LOFT/INTERSECT.<LineBreak/>
<LineBreak/>
<Bold>What the command does</Bold><LineBreak/>
When you run <Run Foreground="Red">CPL_2</Run>:<LineBreak/>
1) You select objects using a normal selection set (<Run Foreground="DarkRed">ssget</Run>). It accepts any pick/window/crossing selection.<LineBreak/>
2) It takes only:<LineBreak/>
- the <Run Foreground="DarkRed">first</Run> entity in the selection set (index 0), and<LineBreak/>
- the <Run Foreground="DarkRed">last</Run> entity in the selection set (index n-1).<LineBreak/>
Everything else in the selection is ignored.<LineBreak/>
3) It converts those two enames to VLA objects and reads their geometry using the curve API:<LineBreak/>
- <Run Foreground="DarkRed">vlax-curve-getStartPoint</Run><LineBreak/>
- <Run Foreground="DarkRed">vlax-curve-getEndPoint</Run><LineBreak/>
This supports LINE, (LW)POLYLINE, ARC, SPLINE, ELLIPSE and other “curve-like” objects compatible with the curve functions.<LineBreak/>
4) It guards against closed curves by rejecting any object where start and end points are equal within tolerance (1e-9). If either selected curve appears closed, nothing is created.<LineBreak/>
5) It computes two midpoint locations (2D):<LineBreak/>
- midpoint between the two <Run Foreground="DarkRed">start points</Run> (one from each curve), and<LineBreak/>
- midpoint between the two <Run Foreground="DarkRed">end points</Run> (one from each curve).<LineBreak/>
The midpoint computation uses X/Y only; Z is implicitly ignored/forced to 2D because a LightweightPolyline is a 2D entity.<LineBreak/>
6) It creates a new 2-vertex <Run Foreground="DarkRed">LWPOLYLINE</Run> in ModelSpace via COM:<LineBreak/>
- Builds a COM-safe <Run Foreground="DarkRed">SAFEARRAY</Run> of 4 doubles: [xm1, ym1, xm2, ym2].<LineBreak/>
- Wraps it in a VARIANT and passes it to <Run Foreground="DarkRed">ModelSpace.AddLightWeightPolyline</Run>.<LineBreak/>
<LineBreak/>
<Bold>Inputs and outputs</Bold><LineBreak/>
- Input: a selection set with at least 2 entities.<LineBreak/>
- Output: one new <Run Foreground="DarkRed">2-vertex LWPOLYLINE</Run> in ModelSpace, representing the straight “centerline” between the two computed midpoints.<LineBreak/>
- If the selection has fewer than 2 entities, or if either of the chosen entities appears closed, the command exits without creating anything.<LineBreak/>
<LineBreak/>
<Bold>Important behavioral details</Bold><LineBreak/>
- <Run Foreground="DarkRed">Selection order matters</Run>: the routine uses the first and last objects in the selection set. For window/crossing selection, AutoCAD’s internal ordering may not match the user’s visual expectation. If you want explicit control, select exactly two objects and pick them in the intended order.<LineBreak/>
- <Run Foreground="DarkRed">Not a “true” geometric centerline</Run>: it does not analyze the curves’ shapes or spacing along their lengths. It simply connects the midpoint between their startpoints to the midpoint between their endpoints. For non-parallel, differently oriented, or differently parameterized curves, the result may not lie between the curves as a consistent medial axis—this is a deliberate simplification.<LineBreak/>
- <Run Foreground="DarkRed">2D output</Run>: Z is dropped. If your curves are not planar in WCS XY or have varying elevation, the created LWPOLYLINE will still be placed as a 2D polyline in the current plane (effectively Z=0 behavior for coordinates passed).<LineBreak/>
- <Run Foreground="DarkRed">No layer/linetype/color handling</Run>: the new polyline will inherit current properties (current layer, color ByLayer, etc.) unless the environment dictates otherwise. The routine does not explicitly set layer or other properties.<LineBreak/>
<LineBreak/>
<Bold>Where this is a good fit</Bold><LineBreak/>
- You have two open boundary curves that conceptually represent left/right rails or edges, and you want a fast, lightweight centerline approximation in one action.<LineBreak/>
- You prefer a COM-only implementation (no heavy command macros) and want minimal side effects (no system variables changed).<LineBreak/>
<LineBreak/>
<Bold>Where it may not be appropriate</Bold><LineBreak/>
- If you need a centerline that tracks curvature and maintains “middle” distance along the entire length (true medial/offset-based centerline).<LineBreak/>
- If you need 3D fidelity (Z-aware centerline).<LineBreak/>
- If either selected curve is closed (it intentionally skips those).<LineBreak/>
</TextBlock>
<Grid>
<Image Source="PNG" Stretch="Uniform"/>
</Grid>
<Grid>
<MediaElement
Source="centerPline.gif"
Stretch="Uniform"
Visibility="Visible"/>
</Grid>
</StackPanel>
</src:RibbonToolTip.ExpandedContent>
</src:RibbonToolTip>
</ResourceDictionary>
Additional info:
Learn more:
Open Website
Share this page:
Subscribe
Login
0 Comments
Oldest
Tags: 🏷️ Autocad Lisps, 🏷️ Polyline menu
Tags: 🏷️ Autocad Lisps, 🏷️ Polyline menu
