;;; ==========================================================================
;;; CROWSFOOT.LSP - Creates a Crow's Foot arrow indicator at a given point
;;; Command shortcut: CF
;;; ==========================================================================

(defun c:CF ( / oldOsnap oldCmdecho ptStart ptDir ds mf arrowwide arrowLen angleRad ptBack ptLeft ptRight)
  ;; Save current system variables to restore later
  (setq oldOsnap (getvar "OSMODE"))
  (setq oldCmdecho (getvar "CMDECHO"))
  
  (setvar "CMDECHO" 0)
  
  ;; Get drawing scale factor, default to 100.0 if set to 0
  (setq ds (get_scale_factor))
  (if (= ds 0.0) (setq ds 100.0))
  
  ;; Determine if Metric/Feet, expect "M" if metric, "I" if feet
  (setq mf (get_Metric_Imperial))
  
  ;; Compute the dynamic polyline width based on Metric vs Imperial unit settings
  ;; Feel free to modify constants .0003 or .0013 to change width
  (cond
    ((= mf "M") (setq arrowwide (* 0.0003 ds)))
    ((= mf "I") (setq arrowwide (* 0.0013 ds)))
    (t          (setq arrowwide (* 0.0003 ds))) ;; Fallback safety default
  )
  
  ;; Define base arrow size multiplier based on Metric vs Imperial
  ;; Feel free to adjust 0.0025 or .1045 constants to change overall size)
  ;;(setq arrowLen (* ds 0.0025))
  (cond
    ((= mf "M") (setq arrowLen (* 0.0025 ds)))
    ((= mf "I") (setq arrowLen (* 0.1045 ds)))
    (t          (setq arrowLen (* 0.0025 ds))) ;; Fallback safety default
  )
  
  ;; Get user inputs (OSMODE remains active here so you can snap to your boundary vertex)
  (setq ptStart (getpoint "\nSpecify vertex point for Crow's Foot: "))
  (if ptStart
    (progn
      (setq ptDir (getpoint ptStart "\nSpecify direction of line/boundary: "))
      
      (if ptDir
        (progn
          ;; Turn off Object Snap ONLY during creation to prevent geometry distortion
          (setvar "OSMODE" 0)
          
          ;; Calculate fundamental angle of the line
          (setq angleRad (angle ptStart ptDir))
          
          ;; Calculate the structural points of the crow's foot
          ;; Center spine endpoint
          (setq ptBack (polar ptStart angleRad arrowLen))
          
          ;; Left prong endpoint (35 degrees out from center)
          (setq ptLeft (polar ptStart (+ angleRad (dtor 35.0)) arrowLen))
          
          ;; Right prong endpoint (35 degrees out from center)
          (setq ptRight (polar ptStart (- angleRad (dtor 35.0)) arrowLen))
          
          ;; Draw the Crow's Foot using explicit inline width parameters [1, 2]
          ;; Syntax: Command -> StartPt -> Width -> StartingWidth -> EndingWidth -> EndPt -> Finish
          (command "_.pline" ptStart "_W" arrowwide arrowwide ptBack "")
          (command "_.pline" ptLeft "_W" arrowwide arrowwide ptStart ptRight "")
          
          (princ "\nCrow's Foot successfully created.")
        )
      )
    )
  )
  
  ;; Restore system variables (including OSMODE) back to their initial state
  (setvar "OSMODE" oldOsnap)
  (setvar "CMDECHO" oldCmdecho)
  (princ)
)

;;; Helper function to convert degrees to radians
(defun dtor (deg)
  (* pi (/ deg 180.0))
)

(princ "\nCrow's Foot routine loaded. Type CF to execute.")
(princ)
