Jump to content

Lisp lệnh NHAPATT trong chủ đề “Chuyển từ excel sang Block ATTDEF”



Chào các bác,
Đây là cái hai lisp cải tiến thêm chút xíu cho phép người dùng chỉ cần nhập lần lượt các STT của các dòng cần nhập và sau đó enter là xong. Tuy nhiên có hai điều mình thắc mắc là với cùng cấu trúc, chỉ khác về cách kiểm tra giá trị STT mà tại sao hai lisp này cho kết quả hoàn toàn khác nhau.
Các bác xài thử và cho mình lời giải đáp nhé.
Lisp 1:


Xin chân thành cám ơn các bác.

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 121010_nhapatt.lsp.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh NHAPATT để chạy lisp.

Mã nguồn 121010_nhapatt.lsp (trích từ bài viết gốc)

(defun Separate (S sym / i L ch)
(setq i 0 L nil)
(while (      (setq i (1+ i) ch (substr S i 1))
     (if (= ch sym) (progn
(setq
      L (append L (list (substr S 1 (- i 1))))
      S (substr S (1+ i) (- (strlen S) i))
      i 0
)
     ))	
)
(append L (list S))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:nhapatt ( / fn f S L STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT 
                           DVT Gchu PT PT1 en pts)
(setq  fn (getfiled "Select Data File" "" "csv" 0)
       ;;;;;;;;;;;;;;; f (open fn "r")
)
;;;;(read-line f)

(command "undo" "be")
(setq PT (getpoint (getvar "lastpoint") "\n Chon diem nhap"))
(if (= (setq rep (strcase (getstring "\n Ban muon xuat het cac du lieu (y or n): "))) "Y")
   (progn
           (setq f (open fn "r"))
           (while (setq S (read-line f))
                    (setq L (separate S ",")
                            STT (nth 1 L)
               	 Forder (nth 2 L)
                	TenCkien (nth 3 L)
                	TTCT (nth 6 L)
                	TenCT (nth 4 L)
               	 LoaiVL (nth 5 L)
               	 Day (nth 11 L)
                	Rong (nth 9 L)
                	Dai (nth 10 L)
               	 SlgCT (nth 8 L)
                	DVT (nth 7 L)
                	Gchu (nth 12 L)                 
         	      )
;;;;;;;;;;(alert (strcat "\n Ten chi tiet là " TenCT))
;;;;;;;;;;(if (and (= (setq ans (strcase (getstring "\n Ban muon nhap chi tiet nay (y or n): "))) "Y") PT)
                   (Command "insert" "Ghicaukien" PT "" "" "" 
                                      STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT DVT Gchu )
                   (setq en (entlast)
                           pts (acet-ent-geomextents en)
                           PT (list (caar pts) (+ (cadr (cadr pts)) (- (cadr (cadr pts)) (cadr (car pts)))))
                   )              
             )
             (close f)
    )
    (progn
             ;;;;;;;;(alert "\n Chu y nhap khoang trang truoc va sau so thu tu cua cac dong can nhap")
(While (/= (setq ddl (getstring T "\n Nhap so thu tu cua dong du lieu muon nhap: ")) "" ) 
                       (setq dls (append dls (list ddl)))
             )
             (if dls 
             (progn         
             (setq f (open fn "r"))
             (while (setq S (read-line f))
                    (setq L (separate S ",")
                            STT (nth 1 L)
               	 Forder (nth 2 L)
                	TenCkien (nth 3 L)
                	TTCT (nth 6 L)
                	TenCT (nth 4 L)
               	 LoaiVL (nth 5 L)
               	 Day (nth 11 L)
                	Rong (nth 9 L)
                	Dai (nth 10 L)
               	 SlgCT (nth 8 L)
                	DVT (nth 7 L)
                	Gchu (nth 12 L)                 
         	      )
                   (if (member STT dls)
                       (command "insert" "Ghicaukien" PT "" "" "" 
                                      STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT DVT Gchu ) 
                   )
                   (setq en (entlast)
                           pts (acet-ent-geomextents en)
                           PT (list (caar pts) (+ (cadr (cadr pts)) (- (cadr (cadr pts)) (cadr (car pts)))))
                   )              
           )            
           (close f)
           )
           ) 
     )
)  

(command "undo" "e")

(princ)
)

(defun Separate (S sym / i L ch)
(setq i 0 L nil)
(while (      (setq i (1+ i) ch (substr S i 1))
     (if (= ch sym) (progn
(setq
      L (append L (list (substr S 1 (- i 1))))
      S (substr S (1+ i) (- (strlen S) i))
      i 0
)
     ))	
)
(append L (list S))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:nhapatt ( / fn f S L STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT 
                           DVT Gchu PT PT1 en pts)
(setq  fn (getfiled "Select Data File" "" "csv" 0)
       ;;;;;;;;;;;;;;; f (open fn "r")
)
;;;;(read-line f)

(command "undo" "be")
(setq PT (getpoint (getvar "lastpoint") "\n Chon diem nhap"))
(if (= (setq rep (strcase (getstring "\n Ban muon xuat het cac du lieu (y or n): "))) "Y")
   (progn
           (setq f (open fn "r"))
           (while (setq S (read-line f))
                    (setq L (separate S ",")
                            STT (nth 1 L)
               	 Forder (nth 2 L)
                	TenCkien (nth 3 L)
                	TTCT (nth 6 L)
                	TenCT (nth 4 L)
               	 LoaiVL (nth 5 L)
               	 Day (nth 11 L)
                	Rong (nth 9 L)
                	Dai (nth 10 L)
               	 SlgCT (nth 8 L)
                	DVT (nth 7 L)
                	Gchu (nth 12 L)                 
         	      )
;;;;;;;;;;(alert (strcat "\n Ten chi tiet là " TenCT))
;;;;;;;;;;(if (and (= (setq ans (strcase (getstring "\n Ban muon nhap chi tiet nay (y or n): "))) "Y") PT)
                   (Command "insert" "Ghicaukien" PT "" "" "" 
                                      STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT DVT Gchu )
                   (setq en (entlast)
                           pts (acet-ent-geomextents en)
                           PT (list (caar pts) (+ (cadr (cadr pts)) (- (cadr (cadr pts)) (cadr (car pts)))))
                   )              
             )
             (close f)
    )
    (progn
             (alert "\n Chu y nhap khoang trang truoc va sau so thu tu cua cac dong can nhap")
(If (/= (setq ddl (getstring T "\n Nhap so thu tu cua các dong du lieu muon nhap: ")) "" )  
             (progn         
             (setq f (open fn "r"))
             (while (setq S (read-line f))
                    (setq L (separate S ",")
                            STT (nth 1 L)
               	 Forder (nth 2 L)
                	TenCkien (nth 3 L)
                	TTCT (nth 6 L)
                	TenCT (nth 4 L)
               	 LoaiVL (nth 5 L)
               	 Day (nth 11 L)
                	Rong (nth 9 L)
                	Dai (nth 10 L)
               	 SlgCT (nth 8 L)
                	DVT (nth 7 L)
                	Gchu (nth 12 L)                 
         	      )
                   (if (wcmatch ddl (strcat "*" " " STT " " "*" ))
                       (command "insert" "Ghicaukien" PT "" "" "" 
                                      STT Forder TenCkien TTCT TenCT LoaiVL Day Rong Dai SlgCT DVT Gchu ) 
                   )
                   (setq en (entlast)
                           pts (acet-ent-geomextents en)
                           PT (list (caar pts) (+ (cadr (cadr pts)) (- (cadr (cadr pts)) (cadr (car pts)))))
                   )              
           )            
           (close f)
           )
           ) 
     )
)  

(command "undo" "e")

(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Chuyển từ excel sang Block ATTDEF. Xem đúng bài viết #121010 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 44)