Function Syntax: Command Version: 1 Date: 09.12.2025 AI+Hyperlinkhttpsforums.augi.comshowthread.php107096-Slopes-and-angles-of-a-polyline-in-2d-autocadHyperlink Description This LISP implements a “slope label placer” with a DCL dialog. After the dialog, it runs in one of two input modes: 1) 2 Points: repeatedly pick point pairs; each pair creates one MTEXT label centered on the segment. 2) Select Line/Segment: select LINE/LWPOLYLINE/POLYLINE; every segment of each selected object gets a label. Each label is an MTEXT entity placed on the current layer, rotated to the segment direction, offset normal to the segment either “above” or “below.” The label text is a slope string in a chosen format, optionally followed by the segment angle on a second line. Both slope and angle can have independent prefix/suffix strings. The routine also exposes unit controls (LUNITS/AUNITS/LUPREC/AUPREC/LIGHTINGUNITS) directly in the dialog via a “Set” button. Command - slope_p — launches the dialog and then runs the chosen mode. Global state / defaults The code uses global variables (persist only for the AutoCAD session unless you add getenv/setenv): - *PLTXT-DCL* : temp DCL file path (created once per session; not deleted in this file). - *PLTXT-TXTHGT* : text height; default to TEXTSIZE (fallback 2.0). - *PLTXT-HSCALE* : horizontal scale applied to DX in slope computation; default 1.0. - *PLTXT-VSCALE* : vertical scale applied to DY in slope computation; default 1.0. - *PLTXT-ABOVE* : T means place label on one side (“above”); NIL means the opposite side (“below”). Default T. - SPU_MODE : "pts" or "ent". Default "ent". - SPU_FMT : slope format, default "pct". Choices: "pct", "permille", "hvdec", "vhdec". - SPU_AFMT : angle text format, default "deg". Choices: "deg", "dms", "grad", "rad". - SPU_COMB : include angle line? Default NIL (angle line off). - Prefix/Suffix strings (default empty): - SPU_SLP_PRE, SPU_SLP_SUF (for slope line) - SPU_ANG_PRE, SPU_ANG_SUF (for angle line) DCL dialog content (PLTXT-MAKE-DCL + PLTXT-SHOW-DIALOG) The dialog is written to a temporary DCL file once (per session) and reused. It includes: 1) Place text above segment? Yes/No radio (controls *PLTXT-ABOVE*). 2) Input radio: 2 Points vs Select Line/Segment (controls SPU_MODE). 3) Label Format radio column (controls SPU_FMT): - Slope % (pct) - Slope per-mille (permille) - Slope H:V (x.xx:1) (hvdec) - Slope V:H (1:x.xx) (vhdec) 4) Angle Label Format radio column (controls SPU_AFMT) plus Combine toggle (controls SPU_COMB). - Combine ON means: angle is added as a second line in MTEXT. Combine OFF means: slope only. - Note: The function name suggests enabling/disabling the angle radios, but the current implementation actually disables radios when Combine is ON (see “Notable quirks” below). 5) Prefix / Suffix edit boxes for slope and angle lines (stored in the SPU_* globals). 6) Parameters: Text Size, Horizontal Scale, Vertical Scale (populates *PLTXT-* globals). 7) Units: popups for Length Type (LUNITS), Angle Type (AUNITS), Lighting (LIGHTINGUNITS), plus Len Prec (LUPREC) and Ang Prec (AUPREC) edit boxes and a “Set” button applying them immediately. 8) OK/Cancel. Unit handling - SPU_SetUnits reads dialog values and sets system variables: - LUPREC clamped 0..8 - AUPREC clamped 0..8 - LUNITS mapped from popup: Scientific(1), Decimal(2), Engineering(3), Architectural(4), Fractional(5) - AUNITS mapped from popup: Decimal Degrees(0), DMS(1), Grads(2), Radians(3), Surveyor’s(4) - LIGHTINGUNITS mapped from popup: Generic(0), American(1), International(2) - SPU-GetLightingUnits and SPU-SetLightingUnits wrap getvar/setvar with catch-all for safety in versions where LIGHTINGUNITS may not exist. Geometry intake: how segments are generated In entity mode, selection is restricted to: (ssget '((0 . "LINE,LWPOLYLINE,POLYLINE"))) For each selected entity, PLTXT-GetSegListFromEntity returns a list of segments, each segment being a pair of UCS points: - LINE → one segment: (start end). - LWPOLYLINE / POLYLINE → extracts vertices and returns consecutive pairs: (p0 p1), (p1 p2), ... (no closing segment is added even if polyline is closed). All points are transformed from WCS (0) to UCS (1) using (trans ... 0 1) before processing. Label creation (PLTXT-CREATE-MTEXT) For each segment defined by points p0 and p1: 1) Computes direction: - dx = x1-x0, dy = y1-y0, ang = (angle p0 p1). 2) Applies horizontal/vertical scaling to the rise/run computation only: - dxs = dx * *PLTXT-HSCALE* - dys = dy * *PLTXT-VSCALE* These do not change the MTEXT insertion point or rotation; they only alter the computed slope value. 3) Computes slope value (signed): - If dxs == 0: treated as “infinite/vertical” special case (see below). - Else slope = dys/dxs, and neg is true if slope < 0 (sign preserved). 4) Formats slope text depending on SPU_FMT using LUPREC as the numeric precision: - "pct": slope% = ± (abs(slope)*100) with trailing "%". - "permille": ± (abs(slope)*1000) with trailing "\\U+2030" (per-mille symbol). - "hvdec": prints ±(run/rise):1. If rise==0, ratio=0.0 (so “0.00:1”). - "vhdec": prints ±1:(rise/run). If run==0, ratio=0.0 (so “1:0.00”). Vertical special case (dxs==0) outputs fixed strings: - pct: "INF%" - permille: "INF\\U+2030" - hvdec: "0:1" - vhdec: "1:0" 5) Applies slope prefix/suffix: sTxt = slp_pre + baseSlope + slp_suf 6) Optional angle line (only if SPU_COMB is true): - Angle string computed by PLTXT-AngToString using SPU_AFMT and AUPREC. - Formats: - deg: decimal degrees + "%%d" (AutoCAD degree symbol). - dms: D° M' S" with seconds to AUPREC decimal places. - grad: (ang * 200/pi) + "g" - rad: raw radians + " rad" - Prefix/suffix applied to the angle line: ang_pre + angBase + ang_suf. - MTEXT content becomes either: - slope only: sTxt - slope + angle: sTxt \\P angTxt (\\P is the MTEXT newline). 7) Placement & rotation: - Midpoint: (xm, ym) = midpoint(p0,p1). - Normal vector (left-hand normal for the segment): nx = -sin(ang), ny = cos(ang). - Offset distance: off = 2.0 * textHeight. - If *PLTXT-ABOVE* is T: insertion point = midpoint + normal*off; else midpoint - normal*off. - MTEXT rotation set to ang (group 50). 8) Creates an MTEXT entity via entmake with key fields: - Layer: current layer (CLAYER) - Style: current TEXTSTYLE - Height: *PLTXT-TXTHGT* - Width: height * 20.0 (wrap width; may cause wrapping on long prefixes). - Attachment: group 71 = 5 (middle center attachment point). - Text: group 1 = computed content. Run-time loops - PLTXT-Loop-2Pts: loops prompting for first point then second point; Enter terminates. Each valid pair creates one label. - PLTXT-Loop-Ent: single selection set; each selected line or polyline has all its segments labeled. No interactive repetition except you can run the command again. Notable quirks / implementation notes 1) Combine toggle naming vs behavior: The comment states “Enable/disable angle format radios based on SPU_COMB,” and the UI label is “Combine.” But the current implementation of SPU_UpdateAngEnable does: (mode_tile k (if SPU_COMB 0 1)) In DCL, mode_tile 0 = enabled, 1 = disabled. Therefore, when Combine is ON, angle radios are enabled; when Combine is OFF, they are disabled. That is the reverse of what the helper name suggests, but it is consistent with “only allow choosing angle format when you intend to show the angle.” Functionally, it is reasonable; the comment in the code is just misleading. 2) Angle radios remain selectable even if Combine is ON, but the chosen angle format only matters when Combine is ON (because otherwise angTxt is nil). 3) Closed polylines are treated as open for labeling: The code labels only consecutive vertex pairs; it does not include the closing segment from last vertex back to first, even if the polyline is closed. If you want to label the closing segment too, PLTXT-GetSegListFromEntity would need to add (last first) when closed. 4) Vertical slope handling is “INF” strings: For dxs==0, it does not attempt to compute sign; it outputs fixed strings (INF%). If you care about up vs down, you would need to incorporate dys sign into the INF case. 5) Text width fixed at 20x height: Long prefixes/suffixes may wrap unexpectedly. If you want always-single-line text, you would typically set width large or omit group 41, or compute width from string length. 6) DCL file lifecycle: The DCL is created once and stored in *PLTXT-DCL*. This code does not delete it at the end of the command. Over time, this leaves a temp DCL file behind until the OS temp directory is cleaned. If you want cleanup, you would delete it on unload or at command end when acceptable. 7) No undo grouping: The routine creates MTEXT entities but does not wrap operations in UNDO marks/groups. Users can still undo normally, but if you want single-step undo for an entire run, you would add an UNDO group/mark pattern around the loops. Net effect Running slope_p gives a configurable workflow for stamping slope labels along individual picked segments or along all segments of selected lines/polylines. Slope formatting supports percent, per-mille, and ratio styles, with optional angle annotation in several angular unit formats, and with user-controlled unit precision and system unit settings directly from the dialog.