; CADPlotting.com - AutoCAD for Mac Page Setup Assistant v1.0
; Phase 10D2
;
; Commands:
;   CPMACHELP      - Show target Page Setup names and workflow
;   CPMACPAGESETUP - Open AutoCAD Page Setup Manager
;   CPMACSCAN      - List named Page Setups stored in this drawing
;   CPMACCHECK     - Check the 14 CADPlotting target names
;   CPMACREPORT    - Save a detailed text report for validation
;
; The Mac version uses native AutoLISP dictionary/DXF access.
; It does not depend on the Windows automation interface.

(defun cp:mac-targets ()
  (list
    "CP-PDF-A4-P" "CP-PDF-A4-L"
    "CP-PDF-A3-P" "CP-PDF-A3-L"
    "CP-PDF-A2-P" "CP-PDF-A2-L"
    "CP-PDF-A1-P" "CP-PDF-A1-L"
    "CP-PDF-A0-P" "CP-PDF-A0-L"
    "CP-PDF-ARCH-D-P" "CP-PDF-ARCH-D-L"
    "CP-PDF-ARCH-E-P" "CP-PDF-ARCH-E-L"
  )
)

(defun cp:mac-massoc (key alist / item out)
  (setq out '())
  (foreach item alist
    (if (= key (car item))
      (setq out (cons (cdr item) out))
    )
  )
  (reverse out)
)

(defun cp:mac-page-setup-pairs (/ d names ents i out)
  (setq d (dictsearch (namedobjdict) "ACAD_PLOTSETTINGS"))
  (if (not d)
    nil
    (progn
      (setq names (cp:mac-massoc 3 d))
      (setq ents  (cp:mac-massoc 350 d))
      (setq i 0 out '())
      (while (and (< i (length names)) (< i (length ents)))
        (setq out (cons (cons (nth i names) (nth i ents)) out))
        (setq i (1+ i))
      )
      (reverse out)
    )
  )
)

(defun cp:mac-get (code ed default)
  (if (assoc code ed) (cdr (assoc code ed)) default)
)

(defun cp:mac-yesno (flag bit)
  (if (/= 0 (logand flag bit)) "Yes" "No")
)

(defun cp:mac-rotation-label (v)
  (cond
    ((= v 0) "0 degrees")
    ((= v 1) "90 CCW")
    ((= v 2) "180 degrees")
    ((= v 3) "90 CW")
    (T (itoa v))
  )
)

(defun cp:mac-plot-type-label (v)
  (cond
    ((= v 0) "Display")
    ((= v 1) "Extents")
    ((= v 2) "Limits")
    ((= v 3) "View")
    ((= v 4) "Window")
    ((= v 5) "Layout")
    (T (itoa v))
  )
)

(defun cp:mac-detail-lines (pair / name ed flags)
  (setq name (car pair))
  (setq ed (entget (cdr pair)))
  (setq flags (cp:mac-get 70 ed 0))
  (list
    (strcat "Name: " name)
    (strcat "  Device: " (cp:mac-get 2 ed ""))
    (strcat "  Paper: " (cp:mac-get 4 ed ""))
    (strcat
      "  Physical size: "
      (rtos (cp:mac-get 44 ed 0.0) 2 2)
      " x "
      (rtos (cp:mac-get 45 ed 0.0) 2 2)
      " mm"
    )
    (strcat "  Style sheet: " (cp:mac-get 7 ed ""))
    (strcat "  Plot type: " (cp:mac-plot-type-label (cp:mac-get 74 ed -1)))
    (strcat "  Rotation: " (cp:mac-rotation-label (cp:mac-get 73 ed -1)))
    (strcat "  Plot styles enabled: " (cp:mac-yesno flags 32))
    (strcat "  Lineweights enabled: " (cp:mac-yesno flags 128))
    (strcat
      "  Non-printable margins L/B/R/T: "
      (rtos (cp:mac-get 40 ed 0.0) 2 2) " / "
      (rtos (cp:mac-get 41 ed 0.0) 2 2) " / "
      (rtos (cp:mac-get 42 ed 0.0) 2 2) " / "
      (rtos (cp:mac-get 43 ed 0.0) 2 2) " mm"
    )
  )
)

(defun cp:mac-print-lines (lines)
  (foreach s lines (princ (strcat "\n" s)))
)

(defun cp:mac-find-name-ci (name pairs / u)
  (setq u (strcase name))
  (vl-some
    '(lambda (p)
       (if (= (strcase (car p)) u) p nil)
     )
    pairs
  )
)

(defun c:CPMACHELP ()
  (princ "\nCADPlotting AutoCAD for Mac Page Setup Assistant")
  (princ "\n------------------------------------------------")
  (princ "\nTarget named Page Setups:")
  (foreach n (cp:mac-targets) (princ (strcat "\n  " n)))
  (princ "\n")
  (princ "\nRecommended workflow:")
  (princ "\n1. Open a clean standards drawing or a COPY.")
  (princ "\n2. Run CPMACPAGESETUP.")
  (princ "\n3. In Page Setup Manager, create the CP-PDF-* names you need.")
  (princ "\n4. Choose the installed AutoCAD PDF device, exact paper, Layout area, full-size output and approved CTB/STB if applicable.")
  (princ "\n5. Run CPMACCHECK to verify the target names.")
  (princ "\n6. Run CPMACREPORT and save the report.")
  (princ "\n7. Preview and create a PDF; measure one known dimension.")
  (princ "\n8. Only after validation, save the standards drawing as DWT.")
  (princ)
)

(defun c:CPMACPAGESETUP ()
  (princ "\nOpening AutoCAD Page Setup Manager...")
  (princ "\nCreate named setups using the CP-PDF-* names shown by CPMACHELP.")
  (command "_.PAGESETUP")
  (princ)
)

(defun c:CPMACSCAN (/ pairs)
  (setq pairs (cp:mac-page-setup-pairs))
  (princ "\nCADPlotting - Named Page Setups in current drawing")
  (princ "\n-------------------------------------------------")
  (princ (strcat "\nPlatform: " (getvar "PLATFORM")))
  (princ (strcat "\nAutoCAD version: " (getvar "ACADVER")))
  (princ (strcat "\nCurrent layout: " (getvar "CTAB")))
  (if (not pairs)
    (princ "\nNo named Page Setups were found in ACAD_PLOTSETTINGS.")
    (foreach p pairs
      (progn
        (princ "\n")
        (cp:mac-print-lines (cp:mac-detail-lines p))
      )
    )
  )
  (princ)
)

(defun c:CPMACCHECK (/ pairs targets found missing p)
  (setq pairs (cp:mac-page-setup-pairs))
  (setq targets (cp:mac-targets))
  (setq found 0 missing 0)
  (princ "\nCADPlotting - Target Page Setup check")
  (princ "\n-------------------------------------")
  (foreach n targets
    (setq p (cp:mac-find-name-ci n pairs))
    (if p
      (progn
        (setq found (1+ found))
        (princ (strcat "\n[OK]      " n))
      )
      (progn
        (setq missing (1+ missing))
        (princ (strcat "\n[MISSING] " n))
      )
    )
  )
  (princ
    (strcat
      "\n\nFound "
      (itoa found)
      " of "
      (itoa (length targets))
      " target named Page Setups. Missing: "
      (itoa missing)
      "."
    )
  )
  (princ "\nUse CPMACPAGESETUP to create or edit missing setups.")
  (princ)
)

(defun c:CPMACREPORT (/ pairs targets path f p)
  (setq pairs (cp:mac-page-setup-pairs))
  (setq targets (cp:mac-targets))
  (setq path
    (getfiled
      "Save CADPlotting Mac Page Setup Report"
      "CADPlotting-Mac-PageSetup-Report.txt"
      "txt"
      1
    )
  )
  (if (not path)
    (princ "\nReport cancelled.")
    (progn
      (setq f (open path "w"))
      (write-line "CADPlotting.com - AutoCAD for Mac Page Setup Report" f)
      (write-line "================================================" f)
      (write-line (strcat "Platform: " (getvar "PLATFORM")) f)
      (write-line (strcat "AutoCAD version: " (getvar "ACADVER")) f)
      (write-line (strcat "Drawing: " (getvar "DWGNAME")) f)
      (write-line "" f)
      (write-line "Target Page Setups" f)
      (write-line "------------------" f)
      (foreach n targets
        (setq p (cp:mac-find-name-ci n pairs))
        (if p
          (progn
            (write-line (strcat "[OK] " n) f)
            (foreach s (cp:mac-detail-lines p) (write-line s f))
            (write-line "" f)
          )
          (write-line (strcat "[MISSING] " n) f)
        )
      )
      (write-line "" f)
      (write-line "All named Page Setups found in this drawing" f)
      (write-line "------------------------------------------" f)
      (if pairs
        (foreach p pairs
          (progn
            (foreach s (cp:mac-detail-lines p) (write-line s f))
            (write-line "" f)
          )
        )
        (write-line "None." f)
      )
      (close f)
      (princ (strcat "\nReport saved to: " path))
    )
  )
  (princ)
)

(princ "\nCADPlotting Mac Page Setup Assistant loaded.")
(princ "\nCommands: CPMACHELP, CPMACPAGESETUP, CPMACSCAN, CPMACCHECK, CPMACREPORT.")
(princ)
