CADXChange

모든 LISP 명령

CXPTS내보내기와 가져오기 (CSV)버전 1.0.0AutoCAD에서 테스트함

CSV 파일로 이름 있는 점 찍기

측량 데이터 같은 점을 스프레드시트 파일에서 찍습니다. 이름, X, Y, 선택 사항인 Z가 하나의 점이 되고 이름이 옆에 쓰입니다.

사용 방법

  1. 이름, x, y, 선택 사항으로 z 열이 있는 CSV를 준비합니다.
  2. CXPTS를 입력하고 파일을 고릅니다.
  3. 점과 이름이 현재 도면층에 나타납니다.

알아 두세요

  • 실행하는 동안만 점 스타일을 잘 보이는 십자로 바꾸고, 끝나면 원래대로 되돌립니다.

AutoCAD에서 불러오기

  1. 파일을 원하는 곳에 저장합니다.
  2. APPLOAD를 입력하고 파일을 선택한 뒤 '로드'를 누릅니다.
  3. 명령 이름을 입력합니다: CXPTS.

시작할 때마다 쓰려면 같은 대화상자에서 '시작 그룹'에 추가하거나, 선택 사항인 로더로 폴더 전체를 한 번에 불러옵니다. 선택 사항인 로더에 대해

AutoCAD(정식 버전)에서 테스트했습니다. AutoLISP를 실행하는 다른 프로그램에서도 될 수 있지만 테스트하지 않았습니다.

코드

숨겨진 부분이 없는 일반 텍스트입니다. cxpts.lsp 이름의 파일에 복사하거나 내려받으세요.

;;; ============================================================================
;;; CXPTS - Plot named points from a CSV file
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxpts
;;; Tested in: AutoCAD (full version). Other programs that run AutoLISP may work but are not tested.
;;; Copyright (c) CADXChange. Free for personal and commercial use; do not resell or
;;; republish it as a collection. Terms: https://cadxchange.net/legal/lisp-terms
;;; ============================================================================

;; Plot survey-style points from CSV ROWS: name, x, y, optionally z. Each point becomes a POINT object with its name
;; written next to it as text of height HEIGHT on layer LAYER. A row whose x and y are not numbers (a header) is
;; skipped. Returns how many points were plotted.
(defun cx:pts-run (rows height layer / n z)
  (setq n 0)
  (foreach r rows
    (if (and (cdr r) (cddr r) (numberp (read (cadr r))) (numberp (read (caddr r))))
      (progn
        (setq z (if (and (cdddr r) (numberp (read (cadddr r)))) (atof (cadddr r)) 0.0))
        (entmakex (list '(0 . "POINT") (cons 8 layer) (list 10 (atof (cadr r)) (atof (caddr r)) z)))
        (entmakex (list '(0 . "TEXT") (cons 8 layer)
                        (list 10 (+ (atof (cadr r)) (* 0.6 height)) (+ (atof (caddr r)) (* 0.3 height)) 0.0)
                        (cons 40 height) (cons 1 (car r)) (cons 7 (getvar "TEXTSTYLE")) '(100 . "AcDbText")))
        (setq n (1+ n)))))
  n)

(defun c:CXPTS ( / *error* cx:sv file rows)
  (setq *error* cx:err)
  (if (setq file (cx:file-open "CSV file with name, x, y, z"))
    (if (setq rows (cx:csv-rows file))
      (progn
        (cx:start '(("CMDECHO" . 0) ("PDMODE" . 35) ("PDSIZE" . 0)))
        (princ (strcat "\n" (itoa (cx:pts-run rows (getvar "TEXTSIZE") (getvar "CLAYER"))) " point(s) plotted."))
        (cx:end))
      (princ "\nThe file could not be read.")))
  (princ))


;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----

;; Close the undo step and put the system variables back.
(defun cx:end ()
  (foreach v cx:sv (setvar (car v) (cdr v)))
  (command "_.UNDO" "_End")
  (setq cx:sv nil)
  (princ))

;; Error handler: restores everything, stays quiet on Esc.
(defun cx:err (msg)
  (if (> (getvar "CMDACTIVE") 0) (command))
  (cx:end)
  (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
    (princ (strcat "\nError: " msg)))
  (princ))

;; Ask which CSV file to read. Returns a path or nil. With FILEDIA 0 the name is typed at the command line.
(defun cx:file-open (title / f)
  (if (= (getvar "FILEDIA") 0)
    (progn
      (setq f (getstring T (strcat "\n" title ": ")))
      (if (= f "") nil f))
    (getfiled title (getvar "DWGPREFIX") "csv" 4)))

;; All lines of a text file, or nil when it cannot be opened.
(defun cx:read-lines (file / f l out)
  (if (setq f (open file "r"))
    (progn
      (while (setq l (read-line f)) (setq out (cons l out)))
      (close f)
      (reverse out))))

;; One CSV line to a list of strings (handles quotes and doubled quotes).
(defun cx:csv-parse (line / out cur q i c)
  (setq out nil cur "" q nil i 1)
  (while (<= i (strlen line))
    (setq c (substr line i 1))
    (cond
      ((and q (= c "\"") (= (substr line (1+ i) 1) "\""))
       (setq cur (strcat cur "\"") i (1+ i)))
      ((= c "\"") (setq q (not q)))
      ((and (= c ",") (not q)) (setq out (cons cur out) cur ""))
      (T (setq cur (strcat cur c))))
    (setq i (1+ i)))
  (reverse (cons cur out)))

;; Remove spaces and tabs at both ends.
(defun cx:trim (s / a b)
  (setq a 1 b (strlen s))
  (while (and (<= a b) (member (substr s a 1) '(" " "\t"))) (setq a (1+ a)))
  (while (and (>= b a) (member (substr s b 1) '(" " "\t"))) (setq b (1- b)))
  (substr s a (max 0 (1+ (- b a)))))

;; The rows of a CSV file as lists of strings, empty lines skipped; nil when the file cannot be read.
(defun cx:csv-rows (file / out)
  (foreach l (cx:read-lines file)
    (if (/= (cx:trim l) "") (setq out (cons (cx:csv-parse l) out))))
  (reverse out))

;; Save the system variables in VARS ((name . value) ...), set them, and open one undo step.
;; The caller declares cx:sv as a local so the saved values live exactly as long as the command.
(defun cx:start (vars)
  (setq cx:sv (mapcar (function (lambda (v) (cons (car v) (getvar (car v))))) vars))
  (command "_.UNDO" "_Begin")
  (foreach v vars (setvar (car v) (cdr v)))
  (princ))

(princ "\nCXPTS loaded. Type CXPTS to start.")
(princ)

버전 기록

버전 1.0.0 · 2026년 10월 27일

첫 번째 릴리스.

다른 명령: 내보내기와 가져오기 (CSV)