NHỜ SỬA LISP CHÈN BLOCK THEO FILE DCL
4 giờ trước, TrNghia.Do đã nói:Lisp của bạn Duy hay và đáp ứng đc yêu cầu của mình nhưng cần nhiều file quá, cấu trúc tạo hộp thoại cũng rất dài mình đọc chả hiểu gì cả, nó khác với cách tạo bảng mà mình đọc quá. Có cách viết nào sử dụng file lisp ban đầu mình gửi mà đáp ứng đc những cái mình mong muốn ko hả bạn.
Của bạn chỉ cần sửa chút xíu là được:
;;;Three List Tile Dependency Example - Lee Mac
;;;Requires ListTileDependency.lsp to be loaded.
(defun c:vd5 (/ *error* dch dcl des lst dwg_name lst_name)
(defun *error* (msg)
(if (= 'file (type des))
(close des))
(if (< 0 dch)
(unload_dialog dch))
(if (and (= 'str (type dcl)) (findfile dcl))
(vl-file-delete dcl))
(if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
(princ (strcat "\nError: " msg)))
(princ))
(setq lst '(("TBS35"
("TBS35-30" ("TBS35-30-1" "TBS35-30-3" "TBS35-30-5"))
("TBS35-35" ("TBS35-35-1" "TBS35-35-3" "TBS35-35-5"))
("TBS35-40" ("TBS35-40-1" "TBS35-40-3" "TBS35-40-5")))
("TBS45"
("TBS45-40" ("TBS45-40-1" "TBS35-40-3" "TBS35-40-5"))
("TBS45-50" ("TBS45-50-1" "TBS35-50-3" "TBS35-50-5"))
("TBS45-75" ("TBS45-75-1" "TBS35-75-3" "TBS35-75-5")))))
(if (and (setq dcl (vl-filename-mktemp "tmp.dcl"))
(setq des (open dcl "w"))
(foreach str '("lbx : list_box" "{" " alignment = centered;"
" fixed_width = true;" " fixed_height = true;" " width = 25;"
" height = 10;" "}" "test : dialog" "{"
" label = \"TAPPER BLOCK SETS - TBS\";" " spacer;"
" : boxed_row" " {" " : lbx { key = \"lb0\";}"
" : lbx { key = \"lb1\";}" " : lbx { key = \"lb2\";}"
" }" " spacer;" " ok_cancel;" "}")
(write-line str des))
(not (setq des (close des)))
(< 0 (setq dch (load_dialog dcl)))
(new_dialog "test" dch))
(progn (or ##rtn## (setq ##rtn## '(0 0 0)))
(LM:dcld:action '("lb0" "lb1" "lb2") 'lst '##rtn##)
(if (= 1 (start_dialog))
(progn (setq lst_name (LM:dcld:getitems ##rtn## lst)
dwg_name (last lst_name))
(insert_blk dwg_name))
(princ "\n*Cancel*"))))
(*error* nil)
(princ))
;;; HAM INSERT ***
(defun insert_blk (name / path)
(setq path "E:/Nghia_AutoLisp 2012/DCL/Tapper block sets TBS/")
(if (not (tblsearch "BLOCK" name))
(setq name (strcat path name ".dwg")))
(command "_.insert" name pause "" "" ""))
;;;*******************************************
;; DCL List-Tile Dependency - Lee Mac
;; Configures action_tile statements for the list of keys to enabled dependency between the DCL tiles.
;; key - List of DCL tile keys in order of dependency
;; lst-sym - Quoted variable containing list data
;; rtn-sym - Quoted variable containing initial selection indexes
(defun LM:dcld:action (key lst-sym rtn-sym)
(defun LM:dcld:addlist (key lst)
(start_list key)
(foreach itm lst (add_list itm))
(end_list))
(defun LM:dcld:getlists (idx lst)
(if (cdr idx)
(cons (mapcar 'car lst) (LM:dcld:getlists (cdr idx) (cdr (nth (car idx) lst))))
lst))
(defun LM:substnth (itm idx lst)
(if lst
(if (zerop idx)
(cons itm (cdr lst))
(cons (car lst) (LM:substnth itm (1- idx) (cdr lst))))))
(defun LM:dcld:actions (key lst-sym rtn-sym lvl / fun)
(setq fun
(if (cdr key)
(list 'lambda
'(val lst / tmp)
(list 'setq
rtn-sym
(list 'LM:substnth '(atoi val) lvl rtn-sym)
'tmp
(list 'LM:dcld:getlists rtn-sym 'lst))
(list 'LM:dcld:addlist (cadr key) (list 'nth (1+ lvl) 'tmp))
(list 'if
(list '<=
(list 'length (list 'nth (1+ lvl) 'tmp))
(list 'nth (1+ lvl) rtn-sym))
(list 'setq rtn-sym (list 'LM:substnth 0 (1+ lvl) rtn-sym)))
(list (LM:dcld:actions (cdr key) lst-sym rtn-sym (1+ lvl))
(list 'set_tile (cadr key) (list 'itoa (list 'nth (1+ lvl) rtn-sym)))
'lst))
(list 'lambda
'(val lst)
(list 'setq rtn-sym (list 'LM:substnth '(atoi val) lvl rtn-sym)))))
(action_tile (car key) (vl-prin1-to-string (list fun '$value lst-sym)))
fun)
(mapcar 'LM:dcld:addlist key (LM:dcld:getlists (eval rtn-sym) (eval lst-sym)))
((eval (LM:dcld:actions key lst-sym rtn-sym 0)) (set_tile (car key) (itoa (car (eval rtn-sym))))
(eval lst-sym))
(princ))
;; DCL List-Tile Dependency - Get Items - Lee Mac
;; Returns a list of the items selected from each dependent list tile.
;; idx - List of selection indexes
;; lst - List data
(defun LM:dcld:getitems (idx lst / tmp)
(if (cdr idx)
(cons (car (setq tmp (nth (car idx) lst)))
(LM:dcld:getitems (cdr idx) (cdr tmp)))
(list (nth (car idx) (car lst)))))
- Tải lisp
- 423148_vd5.lsp
Cách dùng
- Tải tệp 423148_vd5.lsp về máy.
- 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ẽ.
- Gõ lệnh VD5 để chạy lisp.
Mã nguồn 423148_vd5.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/171162-help-nh%E1%BB%9D-s%E1%BB%ADa-lisp-ch%C3%A8n-block-theo-file-dcl/
;;;Three List Tile Dependency Example - Lee Mac
;;;Requires ListTileDependency.lsp to be loaded.
(defun c:vd5 (/ *error* dch dcl des lst dwg_name lst_name)
(defun *error* (msg)
(if (= 'file (type des))
(close des))
(if (< 0 dch)
(unload_dialog dch))
(if (and (= 'str (type dcl)) (findfile dcl))
(vl-file-delete dcl))
(if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
(princ (strcat "\nError: " msg)))
(princ))
(setq lst '(("TBS35"
("TBS35-30" ("TBS35-30-1" "TBS35-30-3" "TBS35-30-5"))
("TBS35-35" ("TBS35-35-1" "TBS35-35-3" "TBS35-35-5"))
("TBS35-40" ("TBS35-40-1" "TBS35-40-3" "TBS35-40-5")))
("TBS45"
("TBS45-40" ("TBS45-40-1" "TBS35-40-3" "TBS35-40-5"))
("TBS45-50" ("TBS45-50-1" "TBS35-50-3" "TBS35-50-5"))
("TBS45-75" ("TBS45-75-1" "TBS35-75-3" "TBS35-75-5")))))
(if (and (setq dcl (vl-filename-mktemp "tmp.dcl"))
(setq des (open dcl "w"))
(foreach str '("lbx : list_box" "{" " alignment = centered;"
" fixed_width = true;" " fixed_height = true;" " width = 25;"
" height = 10;" "}" "test : dialog" "{"
" label = \"TAPPER BLOCK SETS - TBS\";" " spacer;"
" : boxed_row" " {" " : lbx { key = \"lb0\";}"
" : lbx { key = \"lb1\";}" " : lbx { key = \"lb2\";}"
" }" " spacer;" " ok_cancel;" "}")
(write-line str des))
(not (setq des (close des)))
(< 0 (setq dch (load_dialog dcl)))
(new_dialog "test" dch))
(progn (or ##rtn## (setq ##rtn## '(0 0 0)))
(LM:dcld:action '("lb0" "lb1" "lb2") 'lst '##rtn##)
(if (= 1 (start_dialog))
(progn (setq lst_name (LM:dcld:getitems ##rtn## lst)
dwg_name (last lst_name))
(insert_blk dwg_name))
(princ "\n*Cancel*"))))
(*error* nil)
(princ))
;;; HAM INSERT ***
(defun insert_blk (name / path)
(setq path "E:/Nghia_AutoLisp 2012/DCL/Tapper block sets TBS/")
(if (not (tblsearch "BLOCK" name))
(setq name (strcat path name ".dwg")))
(command "_.insert" name pause "" "" ""))
;;;*******************************************
;; DCL List-Tile Dependency - Lee Mac
;; Configures action_tile statements for the list of keys to enabled dependency between the DCL tiles.
;; key - [lst] List of DCL tile keys in order of dependency
;; lst-sym - [sym] Quoted variable containing list data
;; rtn-sym - [sym] Quoted variable containing initial selection indexes
(defun LM:dcld:action (key lst-sym rtn-sym)
(defun LM:dcld:addlist (key lst)
(start_list key)
(foreach itm lst (add_list itm))
(end_list))
(defun LM:dcld:getlists (idx lst)
(if (cdr idx)
(cons (mapcar 'car lst) (LM:dcld:getlists (cdr idx) (cdr (nth (car idx) lst))))
lst))
(defun LM:substnth (itm idx lst)
(if lst
(if (zerop idx)
(cons itm (cdr lst))
(cons (car lst) (LM:substnth itm (1- idx) (cdr lst))))))
(defun LM:dcld:actions (key lst-sym rtn-sym lvl / fun)
(setq fun
(if (cdr key)
(list 'lambda
'(val lst / tmp)
(list 'setq
rtn-sym
(list 'LM:substnth '(atoi val) lvl rtn-sym)
'tmp
(list 'LM:dcld:getlists rtn-sym 'lst))
(list 'LM:dcld:addlist (cadr key) (list 'nth (1+ lvl) 'tmp))
(list 'if
(list '<=
(list 'length (list 'nth (1+ lvl) 'tmp))
(list 'nth (1+ lvl) rtn-sym))
(list 'setq rtn-sym (list 'LM:substnth 0 (1+ lvl) rtn-sym)))
(list (LM:dcld:actions (cdr key) lst-sym rtn-sym (1+ lvl))
(list 'set_tile (cadr key) (list 'itoa (list 'nth (1+ lvl) rtn-sym)))
'lst))
(list 'lambda
'(val lst)
(list 'setq rtn-sym (list 'LM:substnth '(atoi val) lvl rtn-sym)))))
(action_tile (car key) (vl-prin1-to-string (list fun '$value lst-sym)))
fun)
(mapcar 'LM:dcld:addlist key (LM:dcld:getlists (eval rtn-sym) (eval lst-sym)))
((eval (LM:dcld:actions key lst-sym rtn-sym 0)) (set_tile (car key) (itoa (car (eval rtn-sym))))
(eval lst-sym))
(princ))
;; DCL List-Tile Dependency - Get Items - Lee Mac
;; Returns a list of the items selected from each dependent list tile.
;; idx - [lst] List of selection indexes
;; lst - [lst] List data
(defun LM:dcld:getitems (idx lst / tmp)
(if (cdr idx)
(cons (car (setq tmp (nth (car idx) lst)))
(LM:dcld:getitems (cdr idx) (cdr tmp)))
(list (nth (car idx) (car lst)))))Thảo luận gốc
Lisp được chia sẻ trong chủ đề [HELP] NHỜ SỬA LISP CHÈN BLOCK THEO FILE DCL. Xem đúng bài viết #423148 để đọc hướng dẫn và trao đổi thêm.