;;; ---------------------------------------------------------------------
;;;  GAPFLAG-bgol.lsp
;;;  Command:  GPF
;;;
;;;  PURPOSE
;;;  Scans a selection of polylines and detects which ones are OPEN
;;;  (start point does not coincide with end point). Each open polygon's
;;;  gap is flagged with a connecting line plus a circle marker for easy
;;;  visual identification, before running area/hatch/boundary
;;;  operations that require closed geometry.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   GPF   at the command line and press Enter.
;;;    3. Select the polygons (closed or potentially-open) to check.
;;;    4. Every open polygon found gets its gap marked with a line and a
;;;       circle on layer "BGOL-GPF"; properly closed polygons are left
;;;       unmarked. A summary is printed to the command line.
;;;
;;;  NOTES
;;;    - Both the DXF Closed flag AND a direct start/end point comparison
;;;      are checked, so gaps are still caught even on entities whose
;;;      Closed flag is inconsistent with their actual geometry.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of open-polygon detection
;;;      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:GPF-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 1)          ; red
                   '(6 . "Continuous")))
  )
)

(defun BGOL:GPF-MarkGap (p1 p2 lname / midpt rad)
  (entmake (list '(0 . "LINE")
                 '(100 . "AcDbEntity")
                 (cons 8 lname)
                 '(100 . "AcDbLine")
                 (cons 10 p1)
                 (cons 11 p2)))
  (setq midpt (list (/ (+ (car p1) (car p2)) 2.0) (/ (+ (cadr p1) (cadr p2)) 2.0)))
  (setq rad (max 3.0 (* 1.5 (distance p1 p2))))
  (entmake (list '(0 . "CIRCLE")
                 '(100 . "AcDbEntity")
                 (cons 8 lname)
                 '(100 . "AcDbCircle")
                 (cons 10 midpt)
                 (cons 40 rad)))
)

(defun C:GPF ( / ss n i ent obj p1 p2 openflag cnt opencnt)
  (BGOL:GPF-EnsureLayer "BGOL-GPF")
  (setq ss (ssget '((0 . "LWPOLYLINE"))))
  (if ss
    (progn
      (setq cnt 0)
      (setq opencnt 0)
      (setq n (sslength ss))
      (setq i 0)
      (while (< i n)
        (setq ent (ssname ss i))
        (setq obj (vlax-ename->vla-object ent))
        (setq openflag nil)
        (if (= (vlax-get obj 'Closed) :vlax-false)
          (progn
            (setq p1 (vlax-curve-getStartPoint obj))
            (setq p2 (vlax-curve-getEndPoint obj))
            (if (not (equal p1 p2 1e-6))
              (setq openflag T)
            )
          )
        )
        (if openflag
          (progn
            (BGOL:GPF-MarkGap p1 p2 "BGOL-GPF")
            (setq opencnt (1+ opencnt))
          )
        )
        (setq cnt (1+ cnt))
        (setq i (1+ i))
      )
      (princ (strcat "\nGPF: checked " (itoa cnt) " polyline(s); flagged " (itoa opencnt) " open polygon(s) on BGOL-GPF."))
    )
    (princ "\nNo LWPOLYLINE selected.")
  )
  (princ)
)

(princ "\nGAPFLAG-bgol.lsp loaded. Type GPF to flag open polygons with a gap marker.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
