Chuyển đến nội dung
Diễn đàn CADViet
boybdh

Xin lisp tọa độ

Các bài được khuyến nghị

Vào lúc 27/1/2025 tại 12:26, boybdh đã nói:

Bạn nào có lisp tọa độ như hình cho mình xịn thảm khảo với

image.png.2d6b736e239e0473c79045debf7597c5.png

tên lệnh: TOADOTHUADAT:

(defun get-polyline-points (ent / obj name pts i coords vtx pt)
  (vl-load-com)
  (setq obj (vlax-ename->vla-object ent))
  (setq name (vla-get-objectname obj))
  (cond
    ((= name "AcDbPolyline")
     (setq coords (vlax-get obj 'Coordinates))
     (setq pts '())
     (setq i 0)
     (while (< i (length coords))
       (setq pts (append pts (list (list (nth i coords) (nth (1+ i) coords)))))
       (setq i (+ i 2))
     )
     pts
    )
    ((= name "AcDb2dPolyline")
     (setq pts '())
     (setq vtx (entnext ent))
     (while (and vtx (= (cdr (assoc 0 (entget vtx))) "VERTEX"))
       (setq pt (cdr (assoc 10 (entget vtx))))
       (setq pts (append pts (list pt)))
       (setq vtx (entnext vtx))
     )
     (if (/= (car pts) (last pts))
       (setq pts (append pts (list (car pts))))
     )
     pts
    )
    (t nil)
  )
)

(defun distance2d (p1 p2)
  (distance (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
)

(defun c:TOADOTHUADAT ( / ent pts insPt i pt1 pt2 dai txt csvFile fn
                       rowHeight colW1 colW2 colW3 colW4 yrow xbase ytitle ytitle2 total-rows y-top y-bottom)
  (vl-load-com)
  (command "._undo" "_begin")
  (setq cmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq luubatdiem (getvar "osmode"))
  (setvar "osmode" 0)

  (setq rowHeight 5.0
        colW1 25.0 colW2 25.0 colW3 25.0 colW4 25.0
  )
  (setq ent (car (entsel "\nChon polyline khép kín: ")))
  (if (null ent)
    (progn (princ "\nKhong co doi tuong duoc chon.") (exit))
  )
  (setq pts (get-polyline-points ent))
  (if (null pts)
    (progn (princ "\nKhong lay duoc toa do.") (exit))
  )
  (setq insPt (getpoint "\nChon diem chen bang: "))
  (setq xbase (car insPt))

  ;; ===== TIÊU ĐỀ BẢNG =====
  (setq ytitle (cadr insPt))
  (setq ytitle2 (- ytitle rowHeight))

  ;; Hàng 1
  (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) (- ytitle (/ rowHeight 2.0)) ) 2.5 0 "Số hiệu đỉnh thửa")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ (+ colW2 colW3) 2.0)) ytitle) 2.5 0 "Tọa độ")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 colW3 (/ colW4 2.0)) (- ytitle (/ rowHeight 2.0)) ) 2.5 0 "Chiều dài (m)")

  ;; Hàng 2
  (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ colW2 2.0)) ytitle2) 2.5 0 "X")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 (/ colW3 2.0)) ytitle2) 2.5 0 "Y")

  ;; Vẽ dòng tiêu đề
  (command "LINE"
           (list xbase (+ ytitle (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3 colW4) (+ ytitle (/ rowHeight 2.0)) ) "")
  (command "LINE"
           (list xbase (- ytitle2 (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3 colW4) (- ytitle2 (/ rowHeight 2.0)) ) "")

  ;; Đường ngang ngăn giữa "Tọa độ" và "X/Y"
  (command "LINE"
           (list (+ xbase colW1) (- ytitle (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3) (- ytitle (/ rowHeight 2.0)) ) "")

  ;; ===== NỘI DUNG BẢNG =====
  (setq i 1)
  (foreach pt1 pts
    (setq pt2 (if (< i (length pts)) (nth i pts) (car pts)))
    (setq dai (distance2d pt1 pt2))
    (setq yrow (- ytitle2 (* i rowHeight)))

    ;; Ghi giá trị
    (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) yrow) 2.5 0 (rtos i 2 0))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ colW2 2.0)) yrow) 2.5 0 (rtos (cadr pt1) 2 2))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 (/ colW3 2.0)) yrow) 2.5 0 (rtos (car pt1) 2 2))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 colW3 (/ colW4 2.0)) yrow) 2.5 0 (rtos dai 2 2))

    ;; Vẽ dòng
    (command "LINE"
             (list xbase (- yrow (/ rowHeight 2.0)) )
             (list (+ xbase colW1 colW2 colW3 colW4) (- yrow (/ rowHeight 2.0)) ) "")
    (setq i (+ i 1))
  )

  ;; Cột dọc toàn bảng
  (setq total-rows (+ (length pts) 1))
  (setq y-top (+ ytitle (/ rowHeight 2.0)))
  (setq y-bottom (- ytitle2 (* total-rows rowHeight) (/ rowHeight 2.0)))

  (foreach x (list
               xbase
               (+ xbase colW1)
               (+ xbase colW1 colW2 colW3)
               (+ xbase colW1 colW2 colW3 colW4))
    (command "LINE" (list x y-top) (list x y-bottom) "")
  )
  (command "LINE" (list (+ xbase colW1 colW2) (- ytitle (/ rowHeight 2.0)) ) (list (+ xbase colW1 colW2) y-bottom) "")

  (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) (+ y-bottom (/ rowHeight 2.0)) ) 2.5 0 (rtos 1 2 0))
  (command "LINE" (list xbase y-bottom) (list (+ xbase colW1 colW2 colW3 colW4) y-bottom) "")

  ;; ===== XUẤT FILE CSV =====
  (setq csvFile (getfiled "Chon noi luu file CSV" "" "csv" 1))
  (if csvFile
    (progn
      (setq fn (open csvFile "w"))
      (write-line "STT,X,Y,Dai" fn)
      (setq i 1)
      (foreach pt1 pts
        (setq pt2 (if (< i (length pts)) (nth i pts) (car pts)))
        (setq dai (distance2d pt1 pt2))
        (write-line (strcat (itoa i) "," (rtos (cadr pt1) 2 4) "," (rtos (car pt1) 2 4) "," (rtos dai 2 4)) fn)
        (setq i (+ i 1))
      )
      (close fn)
      (princ (strcat "\nĐã xuất file CSV: " csvFile))
    )
  )

  (setvar "osmode" luubatdiem) 
  (setvar "CMDECHO" cmd)
  (command "._undo" "_end")
  (princ)
)
 

  • Like 1

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác
Vào lúc 31/7/2025 tại 16:47, trieubb đã nói:

tên lệnh: TOADOTHUADAT:

(defun get-polyline-points (ent / obj name pts i coords vtx pt)
  (vl-load-com)
  (setq obj (vlax-ename->vla-object ent))
  (setq name (vla-get-objectname obj))
  (cond
    ((= name "AcDbPolyline")
     (setq coords (vlax-get obj 'Coordinates))
     (setq pts '())
     (setq i 0)
     (while (< i (length coords))
       (setq pts (append pts (list (list (nth i coords) (nth (1+ i) coords)))))
       (setq i (+ i 2))
     )
     pts
    )
    ((= name "AcDb2dPolyline")
     (setq pts '())
     (setq vtx (entnext ent))
     (while (and vtx (= (cdr (assoc 0 (entget vtx))) "VERTEX"))
       (setq pt (cdr (assoc 10 (entget vtx))))
       (setq pts (append pts (list pt)))
       (setq vtx (entnext vtx))
     )
     (if (/= (car pts) (last pts))
       (setq pts (append pts (list (car pts))))
     )
     pts
    )
    (t nil)
  )
)

(defun distance2d (p1 p2)
  (distance (list (car p1) (cadr p1)) (list (car p2) (cadr p2)))
)

(defun c:TOADOTHUADAT ( / ent pts insPt i pt1 pt2 dai txt csvFile fn
                       rowHeight colW1 colW2 colW3 colW4 yrow xbase ytitle ytitle2 total-rows y-top y-bottom)
  (vl-load-com)
  (command "._undo" "_begin")
  (setq cmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq luubatdiem (getvar "osmode"))
  (setvar "osmode" 0)

  (setq rowHeight 5.0
        colW1 25.0 colW2 25.0 colW3 25.0 colW4 25.0
  )
  (setq ent (car (entsel "\nChon polyline khép kín: ")))
  (if (null ent)
    (progn (princ "\nKhong co doi tuong duoc chon.") (exit))
  )
  (setq pts (get-polyline-points ent))
  (if (null pts)
    (progn (princ "\nKhong lay duoc toa do.") (exit))
  )
  (setq insPt (getpoint "\nChon diem chen bang: "))
  (setq xbase (car insPt))

  ;; ===== TIÊU ĐỀ BẢNG =====
  (setq ytitle (cadr insPt))
  (setq ytitle2 (- ytitle rowHeight))

  ;; Hàng 1
  (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) (- ytitle (/ rowHeight 2.0)) ) 2.5 0 "Số hiệu đỉnh thửa")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ (+ colW2 colW3) 2.0)) ytitle) 2.5 0 "Tọa độ")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 colW3 (/ colW4 2.0)) (- ytitle (/ rowHeight 2.0)) ) 2.5 0 "Chiều dài (m)")

  ;; Hàng 2
  (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ colW2 2.0)) ytitle2) 2.5 0 "X")
  (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 (/ colW3 2.0)) ytitle2) 2.5 0 "Y")

  ;; Vẽ dòng tiêu đề
  (command "LINE"
           (list xbase (+ ytitle (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3 colW4) (+ ytitle (/ rowHeight 2.0)) ) "")
  (command "LINE"
           (list xbase (- ytitle2 (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3 colW4) (- ytitle2 (/ rowHeight 2.0)) ) "")

  ;; Đường ngang ngăn giữa "Tọa độ" và "X/Y"
  (command "LINE"
           (list (+ xbase colW1) (- ytitle (/ rowHeight 2.0)) )
           (list (+ xbase colW1 colW2 colW3) (- ytitle (/ rowHeight 2.0)) ) "")

  ;; ===== NỘI DUNG BẢNG =====
  (setq i 1)
  (foreach pt1 pts
    (setq pt2 (if (< i (length pts)) (nth i pts) (car pts)))
    (setq dai (distance2d pt1 pt2))
    (setq yrow (- ytitle2 (* i rowHeight)))

    ;; Ghi giá trị
    (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) yrow) 2.5 0 (rtos i 2 0))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 (/ colW2 2.0)) yrow) 2.5 0 (rtos (cadr pt1) 2 2))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 (/ colW3 2.0)) yrow) 2.5 0 (rtos (car pt1) 2 2))
    (command "TEXT" "J" "MC" (list (+ xbase colW1 colW2 colW3 (/ colW4 2.0)) yrow) 2.5 0 (rtos dai 2 2))

    ;; Vẽ dòng
    (command "LINE"
             (list xbase (- yrow (/ rowHeight 2.0)) )
             (list (+ xbase colW1 colW2 colW3 colW4) (- yrow (/ rowHeight 2.0)) ) "")
    (setq i (+ i 1))
  )

  ;; Cột dọc toàn bảng
  (setq total-rows (+ (length pts) 1))
  (setq y-top (+ ytitle (/ rowHeight 2.0)))
  (setq y-bottom (- ytitle2 (* total-rows rowHeight) (/ rowHeight 2.0)))

  (foreach x (list
               xbase
               (+ xbase colW1)
               (+ xbase colW1 colW2 colW3)
               (+ xbase colW1 colW2 colW3 colW4))
    (command "LINE" (list x y-top) (list x y-bottom) "")
  )
  (command "LINE" (list (+ xbase colW1 colW2) (- ytitle (/ rowHeight 2.0)) ) (list (+ xbase colW1 colW2) y-bottom) "")

  (command "TEXT" "J" "MC" (list (+ xbase (/ colW1 2.0)) (+ y-bottom (/ rowHeight 2.0)) ) 2.5 0 (rtos 1 2 0))
  (command "LINE" (list xbase y-bottom) (list (+ xbase colW1 colW2 colW3 colW4) y-bottom) "")

  ;; ===== XUẤT FILE CSV =====
  (setq csvFile (getfiled "Chon noi luu file CSV" "" "csv" 1))
  (if csvFile
    (progn
      (setq fn (open csvFile "w"))
      (write-line "STT,X,Y,Dai" fn)
      (setq i 1)
      (foreach pt1 pts
        (setq pt2 (if (< i (length pts)) (nth i pts) (car pts)))
        (setq dai (distance2d pt1 pt2))
        (write-line (strcat (itoa i) "," (rtos (cadr pt1) 2 4) "," (rtos (car pt1) 2 4) "," (rtos dai 2 4)) fn)
        (setq i (+ i 1))
      )
      (close fn)
      (princ (strcat "\nĐã xuất file CSV: " csvFile))
    )
  )

  (setvar "osmode" luubatdiem) 
  (setvar "CMDECHO" cmd)
  (command "._undo" "_end")
  (princ)
)
 

mình cảm ơn bạn nhiều nhé.

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay

×