;;; AutoCATS 리습 자료실
;; 명령: OBJINFO
;; 기능: 객체별 종류·핸들·레이어·길이·면적을 명령행에 출력합니다.
;; 출처: https://www.autocats.co.kr/lisp/object-info
;; 버전: 1.0 (2026-10-23)
;; 라이선스: 자유 사용·수정, 재배포 시 출처(autocats.co.kr) 표기
;; 안전: 잠긴 레이어·외부참조 보호, 변경은 UNDO 그룹, 명령별 범위는 상세 가이드 참고

(vl-load-com)

(defun autocats-objinfo-get (ent property / result)
  (setq result (vl-catch-all-apply
      '(lambda ()
        (if (= property 'Area)
          (progn (command "_.AREA" "_Object" ent) (getvar "AREA"))
          (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))))))
  (if (vl-catch-all-error-p result) nil result))

(defun autocats-objinfo-metric (ent property / data kind)
  (setq data (entget ent) kind (cdr (assoc 0 data)))
  (if (or (/= property 'Area) (member kind '("CIRCLE" "REGION" "HATCH"))
      (and (= kind "LWPOLYLINE") (= 1 (logand 1 (cdr (assoc 70 data))))))
    (autocats-objinfo-get ent property)))
(defun autocats-objinfo-data (ent / data)
  (setq data (entget ent))
  (list (cdr (assoc 0 data)) (cdr (assoc 5 data)) (cdr (assoc 8 data))
    (autocats-objinfo-metric ent 'Length) (autocats-objinfo-metric ent 'Area)))
(defun autocats-objinfo-format (value) (if (numberp value) (rtos value 2 4) "-"))

(defun c:OBJINFO (/ *error* undo-open index row selection)
  (defun *error* (msg)
    (if undo-open (progn (command-s "_.UNDO" "_End") (setq undo-open nil)))
    (if (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*,*취소*")))
      (princ (strcat "\n[OBJINFO] 중단: " msg)))
    (princ))

  (setq selection (ssget) index 0)
  (if selection (repeat (sslength selection)
      (setq row (autocats-objinfo-data (ssname selection index)))
      (princ (strcat "\n" (car row) " #" (cadr row) " / " (caddr row)
          " / L=" (autocats-objinfo-format (nth 3 row)) " / A=" (autocats-objinfo-format (nth 4 row))))
      (setq index (1+ index))))

  (princ))

(princ "\n[AutoCATS] OBJINFO 로드 완료")
(princ)
