Jump to content

Lisp lệnh MAT1 MAT2 MAT3 trong chủ đề “[Xin] lisp chuyển màu các thuộc tính dynamic block”

Chắc như vầy là ok rồi.

 

(vl-load-com)
(defun dxf(id v) (cdr (assoc id (entget v))))
 
(defun *error* ( msg )
  (if (< 0 dch) (unload_dialog dch))
  (setq *error* temperr)
  (princ)
)
(setq temperr *error*)
 
(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 / 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 a   (car (nentsel "\nChon Attribute:"))
tag (dxf 2 a)
ssl (vl-remove-if-not '(lambda(x) (getcolor tag x))
(acet-ss-to-list (ssget (list '(0 . "INSERT") (cons 66 1)))))
l  (mapcar '(lambda(x) (getcolor tag x)) ssl)
l (_dclsel (vl-sort l '(lambda(x y) (< (car x) (car y))))))
  
  (mapcar '(lambda(x) (setColor tag (car (setq tm (assoc (car (getcolor tag 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

  1. Chép mã nguồn, lưu thành tệp 291416_mat1_mat2_mat3.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 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 #291416 để đọc hướng dẫn và trao đổi thêm.

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