Jump to content

Lisp lấy số liệu từ hộp thoại

Chào bạn !

Bạ thử đoạn code này xem. Kết quả ra 1 list cac giá trị đã chọn. Bạn có thể ghi ra text hoặc xử lý nó theo mong muốn.

 

(defun c:ai (/ id str pos data sublist key De Fl)

     (defun set_list ()
      (start_list "Wx")
      (foreach x
	      (setq sublist
		     (vl-remove-if '(lambda (x) (/= (substr x 1 3) (strcat "W" $value "x"))) data)
		    )
	      (add_list x)
      )
      (end_list)      	
     )

    (defun set_value (/ subdata)
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))
       	     (setq subdata (member key data))            
             (set_tile "Depth"  (setq De (cadr subdata)))
       	     (set_tile "Flange" (setq Fl (caddr subdata)))
	     (mode_tile "accept" 0)
        )
             (mode_tile "accept" 1)
       )
     ) 


(setq f (open "d:/data.txt" "r"))
(setq str (read-line f))
(close f)
(setq data (append))

(while
(setq pos (vl-string-position 32 str))
(setq data (append data (list (substr str 1 pos))))
(setq str (vl-string-trim " " (substr str (+ pos 1)))) 
)
(setq data (append data (list str)))

(setq id (load_dialog "cadvietlisp")) 
(new_dialog "AISC" id)
  (mode_tile "accept" 1)
  (action_tile "W" "(set_list)(set_value)")
  (action_tile "Wx" "(set_value)")

(setq sta (start_dialog))
(unload_dialog id)

(if (= sta 1) (vl-remove 'nil (list key De Fl)))

)

 

 

Ketxu mới học DCL nên gà quá, thấy bác npham viết ngắn quá mà ham, nhưng bên mình xài không được, nên sửa lại 1 chút như thế này :

(defun c:RFT(/ sublist key lstdata data);Read File Txt 
(vl-load-com)  
(defun set_list ()
(start_list "Wx")
(foreach x (setq sublist (vl-remove-if '(lambda (x) (/= (substr (car x) 1 2) (strcat "W" $value))) lstdata))
	      (add_list (car x))
)
(end_list)      	
)
(defun set_value ()
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))	       	                 
             (set_tile "Depth"   (cadr key))
       	     (set_tile "Flange" (caddr key))	     
        )	             
       )
     )

 (defun split (str  delim /    lst i  )	
 (while (setq i (vl-string-search delim str))
   (setq
     lst (cons (substr str 1 i) lst)
     str    (substr str (+ 2 i))
     )
   )
   (setq lst (cons str lst))
 (reverse lst)
 )


(setq f (open (findfile "data.txt") "r"))
  (while (setq Line (read-line f))
(if (not (vl-string-search "Name" Line))
  (progn
    (setq data (split Line "\t"))
	(setq lstData (cons data lstData))
	)
)
)
(setq lstData  (vl-remove '("") (reverse lstData)))

(setq dcl_id (load_dialog "AISC.dcl")) ; Load the DCL file.
 (if (not (new_dialog "AISC" dcl_id))   ; Initialize the dialog.
   (exit)                                ; Exit if this doesn't 
                                         ; work.
 )

 (action_tile "W" "(set_list)(set_value)")
 (action_tile "Wx" "(set_value)")
 (start_dialog)   
 (unload_dialog dcl_id)  
 key
)

 

 

Đúng là Lisp mà bác npham viết chưa chạy được nên Tue_NV cũng tham gia sửa 1 tí ^_^

(defun c:ai (/ id str pos data sublist key De Fl)

     (defun set_list ()
      (start_list "Wx")
      (foreach x
	      (setq sublist
		     (vl-remove-if '(lambda (x) (/= (substr x 1 3) (strcat "W" $value "x"))) data);
		    )
	      (add_list x)
      )
      (end_list)      	
     )

    (defun set_value (/ subdata)
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))
       	     (setq subdata (member key data))            
             (set_tile "Depth"  (setq De (cadr subdata)))
       	     (set_tile "Flange" (setq Fl (caddr subdata)))
	     (mode_tile "accept" 0)
        )
             (mode_tile "accept" 1)
       )
     ) 

(setq f (open "d:/data.txt" "r"))
(setq data (append))

(while
(setq str (read-line f))
    (Repeat 2
(setq pos (vl-string-position 9 str))
(setq data (append data (list (substr str 1 pos))))
(setq str (vl-string-trim "\t" (substr str (+ pos 1)))) 
    )
(setq data (append data (list str)))
)

(setq id (load_dialog "AISC")) 
(new_dialog "AISC" id)
  (mode_tile "accept" 1)
  (action_tile "W" "(set_list)(set_value)")
  (action_tile "Wx" "(set_value)")

(setq sta (start_dialog))
(unload_dialog id)

(if (= sta 1) (vl-remove 'nil (list key De Fl)))
 (close f)
)

:rolleyes: Cơ bản với AutoCad (Autodesk) đã chạy ngon, còn ở cty mình dùng Cadian thì chưa chạy được, phải ngâm cứu cái đã. Mà nút thank đâu thế nhỉ, phải thanks các bác bằng lời thế này vậy. Chúc cả nhà chuẩn bị đón quốc khánh thật vui vẻ, hạnh phú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 166854_rft.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 RFT để chạy lisp.

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

(defun c:ai (/ id str pos data sublist key De Fl)

     (defun set_list ()
      (start_list "Wx")
      (foreach x
	      (setq sublist
		     (vl-remove-if '(lambda (x) (/= (substr x 1 3) (strcat "W" $value "x"))) data)
		    )
	      (add_list x)
      )
      (end_list)      	
     )

    (defun set_value (/ subdata)
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))
       	     (setq subdata (member key data))            
             (set_tile "Depth"  (setq De (cadr subdata)))
       	     (set_tile "Flange" (setq Fl (caddr subdata)))
	     (mode_tile "accept" 0)
        )
             (mode_tile "accept" 1)
       )
     ) 


(setq f (open "d:/data.txt" "r"))
(setq str (read-line f))
(close f)
(setq data (append))

(while
(setq pos (vl-string-position 32 str))
(setq data (append data (list (substr str 1 pos))))
(setq str (vl-string-trim " " (substr str (+ pos 1)))) 
)
(setq data (append data (list str)))

(setq id (load_dialog "cadvietlisp")) 
(new_dialog "AISC" id)
  (mode_tile "accept" 1)
  (action_tile "W" "(set_list)(set_value)")
  (action_tile "Wx" "(set_value)")

(setq sta (start_dialog))
(unload_dialog id)

(if (= sta 1) (vl-remove 'nil (list key De Fl)))

)

(defun c:RFT(/ sublist key lstdata data);Read File Txt 
(vl-load-com)  
(defun set_list ()
(start_list "Wx")
(foreach x (setq sublist (vl-remove-if '(lambda (x) (/= (substr (car x) 1 2) (strcat "W" $value))) lstdata))
	      (add_list (car x))
)
(end_list)      	
)
(defun set_value ()
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))	       	                 
             (set_tile "Depth"   (cadr key))
       	     (set_tile "Flange" (caddr key))	     
        )	             
       )
     )

 (defun split (str  delim /    lst i  )	
 (while (setq i (vl-string-search delim str))
   (setq
     lst (cons (substr str 1 i) lst)
     str    (substr str (+ 2 i))
     )
   )
   (setq lst (cons str lst))
 (reverse lst)
 )


(setq f (open (findfile "data.txt") "r"))
  (while (setq Line (read-line f))
(if (not (vl-string-search "Name" Line))
  (progn
    (setq data (split Line "\t"))
	(setq lstData (cons data lstData))
	)
)
)
(setq lstData  (vl-remove '("") (reverse lstData)))

(setq dcl_id (load_dialog "AISC.dcl")) ; Load the DCL file.
 (if (not (new_dialog "AISC" dcl_id))   ; Initialize the dialog.
   (exit)                                ; Exit if this doesn't 
                                         ; work.
 )

 (action_tile "W" "(set_list)(set_value)")
 (action_tile "Wx" "(set_value)")
 (start_dialog)   
 (unload_dialog dcl_id)  
 key
)

(defun c:ai (/ id str pos data sublist key De Fl)

     (defun set_list ()
      (start_list "Wx")
      (foreach x
	      (setq sublist
		     (vl-remove-if '(lambda (x) (/= (substr x 1 3) (strcat "W" $value "x"))) data);
		    )
	      (add_list x)
      )
      (end_list)      	
     )

    (defun set_value (/ subdata)
            (if sublist
         (progn
             (setq key (nth (atoi (get_tile "Wx")) sublist))
       	     (setq subdata (member key data))            
             (set_tile "Depth"  (setq De (cadr subdata)))
       	     (set_tile "Flange" (setq Fl (caddr subdata)))
	     (mode_tile "accept" 0)
        )
             (mode_tile "accept" 1)
       )
     ) 

(setq f (open "d:/data.txt" "r"))
(setq data (append))

(while
(setq str (read-line f))
    (Repeat 2
(setq pos (vl-string-position 9 str))
(setq data (append data (list (substr str 1 pos))))
(setq str (vl-string-trim "\t" (substr str (+ pos 1)))) 
    )
(setq data (append data (list str)))
)

(setq id (load_dialog "AISC")) 
(new_dialog "AISC" id)
  (mode_tile "accept" 1)
  (action_tile "W" "(set_list)(set_value)")
  (action_tile "Wx" "(set_value)")

(setq sta (start_dialog))
(unload_dialog id)

(if (= sta 1) (vl-remove 'nil (list key De Fl)))
 (close f)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu lisp] Lisp lấy số liệu từ hộp thoại. Xem đúng bài viết #166854 để đọc hướng dẫn và trao đổi thêm.

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