Lisp lệnh MAT0 MAT1 MAT2 MAT4 MAT3 trong chủ đề “[Xin] lisp chuyển màu các thuộc tính dynamic block”
Bạn thử lại với mat3 xem.
(vl-load-com)
(defun c:mat0()
(setq attchuan (dxf 2 (car (nentsel "\nChon Attribute de lam chuan:"))))
(if (not attchuan) (setq attchuan "KT"))
)
(defun dxf(id v) (cdr (assoc id (entget v))))
(defun *error* ( msg )
(if (< 0 dch) (unload_dialog dch))
(setq *error* temperr)
(vl-bt)
(princ)
)
(setq temperr *error*)
(if (not attchuan) (setq attchuan "KT"))
(defun setColor(tag tval col v kieu / obj)
(setq obj (vlax-ename->vla-object v))
(if (= "AcDbText" (vla-get-objectname obj))
(cond ((and (not kieu) (= tval (vla-get-TextString obj)))
(vla-put-Color obj col))
(kieu (vla-put-Color obj col) (vla-put-TextString obj tval)))
(foreach item (vlax-safearray->list (vlax-variant-value (vla-GetAttributes obj)))
(cond ((or (and (not kieu) tag (= tag (vla-get-TagString item)) (= tval (vla-get-TextString item)))
(and (not kieu) (not tag) (= attchuan (vla-get-TagString item)) (= tval (vla-get-TextString item)))
(and (not kieu) (not tag) (= tval (vla-get-TextString item))))
(vla-put-Color item col))
((or (and kieu tag (= tag (vla-get-TagString item)))
(and kieu (not tag) (= attchuan (vla-get-TagString item))))
(vla-put-Color item col) (vla-put-TextString item tval)))))
)
(defun getColor(tag v / rt obj)
(setq rt nil
obj (vlax-ename->vla-object v))
(if (= "AcDbText" (vla-get-objectname obj))
(setq rt (list (vla-get-TextString obj) (vla-get-Color obj)))
(foreach item
(vlax-safearray->list (vlax-variant-value
(vla-GetAttributes (vlax-ename->vla-object v))))
(if (or (and tag (= tag (vla-get-TagString item)))
(and (not tag) (= attchuan (vla-get-TagString item))))
(setq rt (list (vla-get-TextString item) (vla-get-Color item))))
))
rt
)
;;Doi mau cua att hoac text
(defun C:mat1(/ a col kt tag)
(command "undo" "be")
(setq a (car (nentsel "\nChon Attribute hoac Text:"))
col (acad_colordlg (dxf 62 a))
kt (dxf 1 a)
tag (dxf 2 a))
(setColor tag kt col (if tag (dxf 330 a) a) nil)
(mapcar '(lambda(x) (setColor tag kt col x nil)) (acet-ss-to-list
(ssget '((-4 . "<or")(0 . "TEXT")(-4 . "<and")(0 . "INSERT")(66 . 1)(-4 . "and>")(-4 . "or>")))))
(command "undo" "e")
)
;;Doi mau va noi dung cua att hoac text
(defun C:mat2(/ a col kt tag)
(command "undo" "be")
(setq a (car (nentsel "\nChon Attribute hoac Text:"))
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 '((-4 . "<or")(0 . "TEXT")(-4 . "<and")(0 . "INSERT")(66 . 1)(-4 . "and>")(-4 . "or>")))))
(command "undo" "e")
)
;;Doi mau va noi dung cua tag hoac text
(defun C:mat4(/ a col kt tag)
(command "undo" "be")
(setq a (car (nentsel "\nChon Attribute hoac Text:"))
col (acad_colordlg (dxf 62 a))
kt (dxf 1 a)
tag (dxf 2 a))
(setColor tag kt col (if tag (dxf 330 a) a) nil)
(mapcar '(lambda(x) (setColor tag kt col x t)) (acet-ss-to-list
(ssget '((-4 . "<or")(0 . "TEXT")(-4 . "<and")(0 . "INSERT")(66 . 1)(-4 . "and>")(-4 . "or>")))))
(command "undo" "e")
)
(defun c:mat3(/ a tag l ssl tm)
;; Author: Lee McDonell, Copyright © 2011 - www.lee-mac.com ;;
(defun _dclsel ( l / str name sup file dch l1 l2 col col1)
(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"))
(setq col (atoi (nth (atoi (get_tile "imgC")) l2)))
(if (setq col1 (acad_colordlg col))
(setq col col1))
(_dclimg "img" col)
(start_list "imgC")
(mapcar 'add_list (setq l2 (LM:SubstNth (itoa col) (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 col (atoi (car l2)))
(_dclimg "img" col)
(action_tile "tags" "(act1)")
(action_tile "imgC" "(act2)")
(start_dialog)
(if (< 0 dch) (unload_dialog dch))
(mapcar 'cons l1 l2)
)
(setq ssl (acet-ss-to-list (ssget '((-4 . "<or")(0 . "TEXT")(-4 . "<and")
(0 . "INSERT")(66 . 1)(-4 . "and>")(-4 . "or>"))))
ssl (vl-remove-if-not '(lambda(x) (getcolor attchuan x)) ssl)
l (mapcar '(lambda(x) (getcolor attchuan x)) ssl)
l (_dclsel (vl-sort l '(lambda(x y) (< (car x) (car y))))))
(mapcar '(lambda(x) (setColor attchuan (car (setq tm (assoc (car (getcolor attchuan x)) l)))
(atoi (cdr tm)) x nil)) ssl)
(princ)
)
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 292171_mat0_mat1_mat2_mat4_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 MAT0 MAT1 MAT2 MAT4 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 #292171 để đọc hướng dẫn và trao đổi thêm.