Lisp lệnh MAT1 MAT2 MAT3 trong chủ đề “[Xin] lisp chuyển màu các thuộc tính dynamic block”
Tôi viết lại gọn hơn và thêm cơ chế bắt lỗi, bạn thử lại xem sao.
(vl-load-com)
(defun dxf(id v) (cdr (assoc id (entget v))))
(defun setColor(tag tval col v kieu)
(foreach item
(vlax-safearray->list (vlax-variant-value
(vla-GetAttributes (vlax-ename->vla-object v))))
(cond ((and (not kieu) (= tag (vla-get-TagString item)) (= tval (vla-get-TextString item)))
(vla-put-Color item col))
((and kieu (= tag (vla-get-TagString item)))
(vla-put-Color item col)
(vla-put-TextString item tval))
))
)
(defun getColor(tag v / rt)
(setq rt nil)
(foreach item
(vlax-safearray->list (vlax-variant-value
(vla-GetAttributes (vlax-ename->vla-object v))))
(if (= tag (vla-get-TagString item))
(setq rt (list (vla-get-TextString item) (vla-get-Color item)))
))
rt
)
(defun C:mat1(/ a col kt tag)
(setq a (car (nentsel "\nChon Attribute:"))
col (acad_colordlg (dxf 62 a))
kt (dxf 1 a)
tag (dxf 2 a))
(setColor tag kt col (dxf 330 a) nil)
(mapcar '(lambda(x) (setColor tag kt col x nil))
(acet-ss-to-list (ssget (list '(0 . "INSERT") (cons 66 1)))))
(princ)
)
(defun C:mat2(/ a col kt tag)
(setq a (car (nentsel "\nChon Attribute:"))
col (dxf 62 a)
kt (dxf 1 a)
tag (dxf 2 a))
(mapcar '(lambda(x) (setColor tag kt col x t))
(acet-ss-to-list (ssget (list '(0 . "INSERT") (cons 66 1)))))
(princ)
)
(defun c:mat3(/ a tag l ssl tm)
;; Author: Lee McDonell, Copyright © 2011 - www.lee-mac.com ;;
(defun _dclsel ( l / file tmp dch l1 l2 *attcolour*)
(defun _dclimg ( k c )
(start_image k) (fill_image 0 0 (dimx_tile k) (dimy_tile k) c) (end_image)
)
(defun _unique ( l ) (if l (cons (car l) (_unique (vl-remove (car l) (cdr l))))))
(defun LM:SubstNth ( a n l / i )
(setq i -1)
(mapcar '(lambda ( x ) (if (= (setq i (1+ i)) n) a x)) l)
)
(defun act1()
(set_tile "imgC" "") (set_tile "imgC" (get_tile "tags"))
(_dclimg "img" (atoi (nth (atoi (get_tile "imgC")) l2)))
)
(defun act2()
(set_tile "tags" "") (set_tile "tags" (get_tile "imgC"))
(_dclimg "img" (setq *attcolour* (acad_colordlg (atoi (nth (atoi (get_tile "imgC")) l2)))))
(start_list "imgC")
(mapcar 'add_list (setq l2 (LM:SubstNth (itoa *attcolour*) (atoi (get_tile "imgC")) l2)))
(end_list)
(set_tile "imgC" "") (set_tile "imgC" (get_tile "tags"))
)
(setq l (_unique l)
str (strcat
"ATTCOL : dialog { label = \"Attribute Colour\"; spacer;"
" : row { "
" : list_box { label = \"Select Tags\"; key = \"tags\"; width = 20.0; "
" fixed_width = true; multiple_select = false ; alignment = centered; }"
" : list_box { label = \"Colour\" ; key = \"imgC\" ; width = 8.0 ; "
" fixed_width = true; multiple_select = false ; alignment = centered; }"
" }"
" : row { "
" : image_button { key = \"img\"; alignment = centered; height = 3.0; fixed_width = false;"
" fixed_height = true; }"
" }"
" spacer; ok_cancel;"
"}"
))
(if (not (findfile (setq name
(strcat (substr (setq sup (vla-get-SupportPath (vla-get-Files (vla-get-Preferences (vlax-get-acad-object)))))
1 (vl-string-search ";" sup)) "\\ATTCOL.DCL"))))
(progn (setq file (open name "w"))
(write-line str file)
(close file))
)
(setq dch (load_dialog name))
(new_dialog "ATTCOL" dch)
(setq l1 (mapcar 'car l)
l2 (mapcar '(lambda(x) (itoa (cadr x))) l))
(start_list "tags") (mapcar 'add_list l1) (end_list)
(start_list "imgC") (mapcar 'add_list l2) (end_list)
(set_tile "tags" "0") (set_tile "imgC" "0")
(setq *attcolour* (atoi (car l2)))
(_dclimg "img" *attcolour*)
(action_tile "tags" "(act1)")
(action_tile "imgC" "(act2)")
(start_dialog)
(if (< 0 dch) (unload_dialog dch))
(mapcar 'cons l1 l2)
)
;;=======================================;;
(setq a (car (nentsel "\nChon Attribute:"))
tag (dxf 2 a))
(vl-catch-all-error-p (vl-catch-all-apply '(lambda()
(setq l (_dclsel (mapcar '(lambda(x) (getcolor tag x))
(setq ssl (acet-ss-to-list (ssget (list '(0 . "INSERT") (cons 66 1)))))))))))
(mapcar '(lambda(x) (setColor tag (car (setq tm (assoc (car (getcolor tag x)) l)))
(atoi (cdr tm)) x nil)) ssl)
(princ)
)
Cám ơn bạn Tot77 rất nhiệt tình, mình cũng đã check lại với lisp sau của bạn nhưng vẫn bị tình trạng đơ như vậy. mình đã thử nhiều lần và thấy CAD bị lỗi khi ta ấn Cancel trong khi chọn màu và mình cũng đã chụp lại màn hình của CAD bị đơ khi ấy thông báo như ảnh đính kèm. mình dùng win 7 64 bit, CAD 2010 
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
- Chép mã nguồn, lưu thành tệp 291403_mat1_mat2_mat3.lsp.
- 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 MAT1 MAT2 MAT3 để chạy lisp.
Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Xin] lisp chuyển màu các thuộc tính dynamic block. Xem đúng bài viết #291403 để đọc hướng dẫn và trao đổi thêm.