Function Syntax: OffsetAndLayer Version: 1 Date: 05.08.2025 N/A Description This routine defines a command OffsetAndLayer that offsets selected planar curve objects by a user-specified distance, forces the offset result to the user-chosen direction (Inward or Outward), and places the newly created offset entity on a user-selected layer. It uses a runtime-generated DCL dialog for direction/distance/layer selection and stores the last used values in environment variables for reuse. User workflow 1) Run OffsetAndLayer. 2) A dialog appears to set: - Offset Direction: Inward / Outward (radio buttons). - Offset Distance (string parsed by distof). - Layer filter (text filter). - Layer list (popup of matching non-xref layers). 3) After OK, you select objects to offset using a selection set limited to curve types. 4) Each selected object is offset; the newly created offset entity is moved to the chosen layer. 5) One UNDO mark is started at the beginning and ended at completion, so the entire command should undo as a single step (subject to how internal command calls behave in your environment). Supported selection types The selection filter is: ((0 . "*POLYLINE,ARC,CIRCLE,ELLIPSE,SPLINE")) Meaning it targets: - LWPOLYLINE / POLYLINE (and other “*POLYLINE” types), - ARC, CIRCLE, ELLIPSE, SPLINE. Additionally it explicitly skips 3D polylines by testing for the subclass marker: - Skips if (100 . "AcDb3dPolyline") is found in (entget ent). It also requires the curve be planar via (vlax-curve-isPlanar ent). Dialog (DCL) behavior The DCL file is written to a temporary file at runtime and deleted after use. The dialog contains: - A boxed radio column for direction: “Inward” and “Outward”. - An edit box for offset distance (offset). - An edit box for layer filtering (filter). - A popup list of layers (layerlist). - OK/Cancel buttons (via ok_cancel;). Layer list population: - Layers are collected from the drawing excluding xref-dependent names containing "|". - The filter edit box applies a wildcard match *filter* (case-insensitive). - The popup is repopulated on each filter change. Persistence (last used settings) The routine stores and restores its settings using environment variables: - OFFSETVAL : last offset distance (string). Default is "0.20" if missing. - OFFSETLAYER : last chosen target layer (string). - OFFSETDIR : direction flag: "0" = Inward, "1" = Outward. Default is "0". On dialog load, it attempts to preselect the last layer by locating it in the currently matched layer list. Offset-direction enforcement logic The routine offsets using COM (vla-offset) and then verifies whether the resulting entity went the requested direction by comparing areas. For each selected entity: 1) It computes the original area: - (setq oarea (vla-get-Area obj)) 2) It attempts (vla-offset obj *OIOdist). 3) It takes the newly created offset entity as (entlast) and compares area: - If direction requested is In, it expects the new area to be smaller than the original. - If direction requested is Out, it expects the new area to be larger than the original. 4) If the comparison shows the offset went the wrong way, it deletes that offset result and retries with the negative distance (- *OIOdist). 5) Once the correct-direction offset is produced, it assigns the new entity to the chosen layer using (vla-put-layer newEnt layername). This “area-based direction check” is a pragmatic way to decide inside/outside for closed planar curves. It is most reliable when the input object supports area in a meaningful way and is closed (or otherwise yields a stable area value). Failure handling and “too small” tracking - The variable nogo counts objects that could not be offset inward (or failed the retry path), and prints a message at the end if nogo > 0. - Offsetting is wrapped with vl-catch-all-apply in one branch, but there are also direct vla-offset calls (not wrapped) in the fallback branch, which can raise errors for certain objects/distances depending on geometry constraints. UNDO behavior - The code calls (vla-startundomark doc) at the start and (vla-endundomark doc) at the end. - The error handler also calls (vla-endundomark doc) to avoid leaving an open mark. - Note: Because the routine uses entity creation and deletion and may invoke offset multiple times per entity, what constitutes “one undo step” can vary slightly by AutoCAD version and settings, but the intent is clearly a single-grouped operation for the user. Key limitations and edge cases 1) Area dependency: The direction check depends on vla-get-Area. If the object does not support Area (or is open such that area is not meaningful), this can error or misclassify direction. The selection filter includes SPLINE/ELLIPSE/ARC, which may or may not have a stable Area interpretation depending on closure and object type. 2) Open curves: For open curves, “inward/outward” is not well-defined geometrically; the area test can be unreliable or fail. Practically, this routine is best suited to closed planar curves where offsetting produces a similar closed curve with comparable area semantics. 3) Offset results from vla-offset: For some entities, vla-offset can return multiple entities or produce results not captured by (entlast) the way you expect, especially in more complex cases. Your implementation assumes one new entity per offset attempt. 4) Layer list refresh: When the filter changes, the popup resets selection to index 0. If the user previously had a different layer selected, that selection is not preserved across filter edits unless you add logic to retain it when present in matched_layers. 5) Dialog Cancel / exit paths: The routine uses (exit) in several places. In AutoLISP, exit can terminate the calling context abruptly; if you want safer behavior inside commands, returning cleanly (with (princ)) is usually preferred. Summary OffsetAndLayer is a dialog-driven offset utility that (a) remembers last distance/layer/direction via environment variables, (b) offsets planar curve entities via COM, (c) uses an area comparison to force “Inward” vs “Outward” behavior, and (d) assigns the newly created offset entity to a user-selected layer. It is most dependable on closed planar curves where Area is well-defined and stable.