;;; ---------------------------------------------------------------------
;;;  ARCFLAT-bgol.lsp
;;;  Command:  AFLT
;;;
;;;  PURPOSE
;;;  Removes arc segments (non-zero bulge vertices) from selected
;;;  LWPOLYLINE entities, including arcs created by FILLET, which
;;;  AutoCAD's native PEDIT > Decurve option cannot process. The result
;;;  is a polyline made solely of straight-line segments.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   AFLT   at the command line and press Enter.
;;;    3. Select one or more polylines containing arc/bulge segments.
;;;    4. A new straight-segment-only polyline is created for each
;;;       selected entity on layer "BGOL-AFLT". The original polylines
;;;       are left untouched.
;;;
;;;  NOTES
;;;    - This routine sets all bulge values to 0 (straight chord between
;;;      each pair of original vertices) for a fast, simple result. Edit
;;;      BGOL:AFLT-FlattenOne to sample multiple points per arc via
;;;      vlax-curve-getPointAtParam for a shape-preserving alternative.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of arc-removal / un-fillet
;;;      utilities common in CAD/survey drafting workflows, but the
;;;      code, command name and file name here are new and are NOT
;;;      copied from, nor identical to, any specific third-party
;;;      product.
;;;
;;;  ---------------------------------------------------------------------
;;;  Developed and shared for the CAD community by:  BGol Community
;;;  Community website:                              https://bgol.in/
;;;  License:  Open-source & free to use, modify, and share for all.
;;;            Provided "as is", without warranty of any kind.
;;;  ---------------------------------------------------------------------

(vl-load-com)

(defun BGOL:AFLT-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 3)          ; green
                   '(6 . "Continuous")))
  )
)

;; Build a flat (bulge-free) LWPOLYLINE from an entget assoc list of an
;; existing LWPOLYLINE, stripping all group 42 (bulge) entries.
(defun BGOL:AFLT-FlattenOne (edata lname / newdata pair arcCount)
  (setq arcCount 0)
  (foreach pair edata
    (if (= (car pair) 42)
      (if (/= (cdr pair) 0.0) (setq arcCount (1+ arcCount)))
    )
  )
  (setq newdata '())
  (foreach pair edata
    (cond
      ((= (car pair) 42) nil)                    ; drop bulge
      ((= (car pair) 5) nil)                      ; drop handle
      ((= (car pair) 8) (setq newdata (append newdata (list (cons 8 lname)))))
      ((member (car pair) '(-1 330 350 360)) nil)  ; drop entity/owner refs
      (t (setq newdata (append newdata (list pair))))
    )
  )
  (list newdata arcCount)
)

(defun C:AFLT ( / ss n i ename edata res newdata arcCount totalArcs made)
  (BGOL:AFLT-EnsureLayer "BGOL-AFLT")
  (princ "\nSelect polyline(s) to remove arcs from: ")
  (setq ss (ssget '((0 . "LWPOLYLINE"))))
  (if ss
    (progn
      (setq n (sslength ss))
      (setq made 0)
      (setq totalArcs 0)
      (setq i 0)
      (while (< i n)
        (setq ename (ssname ss i))
        (setq edata (entget ename))
        (setq res (BGOL:AFLT-FlattenOne edata "BGOL-AFLT"))
        (setq newdata (car res))
        (setq arcCount (cadr res))
        (setq totalArcs (+ totalArcs arcCount))
        (if (entmake newdata)
          (setq made (1+ made))
        )
        (setq i (1+ i))
      )
      (princ (strcat "\nAFLT: " (itoa made) " polyline(s) de-curved, "
                      (itoa totalArcs) " arc segment(s) flattened (layer BGOL-AFLT)."))
    )
    (princ "\nNo polylines selected.")
  )
  (princ)
)

(princ "\nARCFLAT-bgol.lsp loaded. Type AFLT to remove arc/fillet segments from a polyline.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
