Jump to content

Lisp doi mau


Hề hề hề,
Xin lỗi vì bạn phải đợi lâu. Số là mình hơi dốt về các hàm vla- và vlax- nên phải mót thêm của các bác Ketxu, ĐoanVanHa mới hiểu được cái lisp của bạn gửi để chỉnh sửa nó theo yêu cầu của bạn.
Mình đã chỉnh sửa nó bằng cách bổ sung thêm hàm con changeallobjectcolor1 dựa trên cái hàm con cũ của bạn là changeallobjectcolor.
Như vậy khi chạy lisp sẽ hỏi bạn có muốn đổi màu toàn bộ bản vẽ hay không?? Nếu bạn trả lời y thì lisp sẽ chạy hàm con changeallobjectcolor và đổi màu toàn bộ bản vẽ. Còn nếu bạn trả lời n hay nhấn phím enter thì lisp sẽ yêu cầu bạn chọn các đối tượng cần đổi màu và chỉ đổi màu các đối tượng này bằng cách chạy hàm con changeallobjectcolor1.
Bạn hãy test thử và cho ý kiến nhé.


Bạn nhớ cám ơn các bác Ketxu và DoanVanHa vì có các bác ấy mình mới sửa được cái lisp này. Hy vọng bạn sẽ hài lòng với nó.
Chúc bạn vui.
PS: file lisp gốc bạn gửi bị sai cú pháp, bạn phải sửa lại cho đúng nhé. Sai ở chỗ tên hàm (defun c:doi mau (...) Phải sửa lại thành (defun c:doimau (....) nó mới chạy được (chú ý không có khoảng trắng giữa các ký tự của tên hàm tức là giữa doi và mau)
  • Tải lisp
  • 204362_doimau_colorxref_colorxl_colorxrefl.lsp

Cách dùng

  1. Tải tệp 204362_doimau_colorxref_colorxl_colorxrefl.lsp về máy.
  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 DOIMAU COLORXREF COLORXL COLORXREFL để chạy lisp.

Mã nguồn 204362_doimau_colorxref_colorxl_colorxrefl.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/64988-nh%E1%BB%9D-ch%E1%BB%89nh-s%E1%BB%ADa-lisp-doi-mau/

;; free lisp from cadviet.com

;;; Posted Vladimir Azarko (VVA)
;;; <a href="http://www.cadtutor.net/forum/showthread.php?t=533&page=2" target="_blank">
;;;;http://www.cadtutor.net/forum/showthread.p...=533&page=2</a>

(defun C:doimau (/ doc col)
 (vl-load-com)
 (setq doc (vla-get-activedocument (vlax-get-acad-object)))
 (setq ans (getstring "\n Ban muon doi mau toan bo ban ve <Y or N> : "))
 (vla-startundomark doc)
 (mip:layer-status-save)
 (if (and (setq col (acad_colordlg 7 t)) (= (strcase ans) "Y"))
   (ChangeAllObjectsColor doc col) ;_ col — color number
   (ChangeAllObjectsColor1 doc col)
 ) ;_ end of if
 (mip:layer-status-restore)
 (vla-endundomark doc)
 (princ)
) ;_ end of defun
;;;;;;;;;;;;;;;;;;
(defun C:COLORXREF (/ doc col)
 (vl-load-com)
 (alert
   "\This lisp change color xref\nONLY ON A CURRENT SESSION"
 ) ;_ end of alert
 (setq doc (vla-get-activedocument (vlax-get-acad-object)))
 (vla-startundomark doc)
 (mip:layer-status-save)
 (if (setq col (acad_colordlg 7 t))
   (ChangeXrefAllObjectsColor doc col) ;_ col — color number
 ) ;_ end of if
 (mip:layer-status-restore)
 (vla-endundomark doc)
 (princ)
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun C:COLORXL (/ doc col)
 (vl-load-com)
 (setq doc (vla-get-activedocument (vlax-get-acad-object)))
 (vla-startundomark doc)
 (if (setq col (acad_colordlg 7 t))
   (ChangeAllObjectsColor doc col) ;_ col — color number
 ) ;_ end of if
 (vla-endundomark doc)
 (princ)
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun C:COLORXREFL (/ doc col)
 (vl-load-com)
 (alert
   "\This lisp change color xref\nONLY ON A CURRENT SESSION"
 ) ;_ end of alert
 (setq doc (vla-get-activedocument (vlax-get-acad-object)))
 (vla-startundomark doc)
 (if (setq col (acad_colordlg 7 t))
   (ChangeXrefAllObjectsColor doc col) ;_ col — color number
 ) ;_ end of if
 (vla-endundomark doc)
 (princ)
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mip:layer-status-restore ()
 (foreach item *MIP_LAYER_LST*
   (if (not (vlax-erased-p (car item)))
     (vl-catch-all-apply
'(lambda ()
 (vla-put-lock (car item) (cdr (assoc "lock" (cdr item))))
 (vla-put-freeze
   (car item)
   (cdr (assoc "freeze" (cdr item)))
 ) ;_ end of vla-put-freeze
) ;_ end of lambda
     ) ;_ end of vl-catch-all-apply
   ) ;_ end of if
 ) ;_ end of foreach
 (setq *MIP_LAYER_LST* nil)
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mip:layer-status-save ()
 (setq *MIP_LAYER_LST* nil)
 (vlax-for item (vla-get-layers
 (vla-get-activedocument (vlax-get-acad-object))
) ;_ end of vla-get-layers
   (setq *MIP_LAYER_LST*
 (cons (list item
     (cons "freeze" (vla-get-freeze item))
     (cons "lock" (vla-get-lock item))
) ;_ end of cons
*MIP_LAYER_LST*
 ) ;_ end of cons
   ) ;_ end of setq
   (vla-put-lock item :vlax-false)
   (if (= (vla-get-freeze item) :vlax-true)
     (vl-catch-all-apply
'(lambda () (vla-put-freeze item :vlax-false))
     ) ;_ end of vl-catch-all-apply
   ) ;_ end of if
 ) ;_ end of vlax-for
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ChangeXrefAllObjectsColor (Doc Color / tmp txtstr)
 (vlax-for Blk (vla-get-Blocks Doc)
   (cond
     ((or (= (vla-get-IsXref Blk) :vlax-true)
 (and (= (vla-get-IsXref Blk) :vlax-false)
(wcmatch (vla-get-name Blk) "*|*")
 ) ;_ end of and
  	) ;_ end of or
  	(vlax-for Obj Blk
(if (and (vlax-write-enabled-p Obj)
(vlax-property-available-p Obj 'Color)
   ) ;_ end of and
 (vla-put-Color Obj Color)
) ;_ end of if
(if (and (vlax-write-enabled-p Obj)
(vlax-property-available-p Obj 'TextString)
  ) ;_ end of and
(progn
  (setq txtstr
 (if (vlax-method-applicable-p Obj 'FieldCode)
     (vla-FieldCode Obj)
     (vlax-get-property Obj 'TextString))
)
  (setq tmp 0)
   (while (setq tmp (VL-STRING-SEARCH "\\C" txtstr tmp))
(setq txtstr
(vl-string-subst
(strcat (substr txtstr (1+ tmp) 2)(itoa Color) ";")
(substr txtstr (1+ tmp) (- (1+ (VL-STRING-SEARCH ";" txtstr tmp)) tmp))
txtstr
tmp)
  )
(setq tmp (+ tmp 3))
)
  (vla-put-Textstring Obj txtstr)
  )
) ;_ end of if
(if (and (vlax-write-enabled-p Obj)
(= (vla-get-ObjectName obj) "AcDbBlockReference")
(= (vla-get-HasAttributes obj) :vlax-true)
   ) ;_ end of and
 (foreach att (vlax-safearray->list
(vlax-variant-value (vla-GetAttributes obj))
) ;_ end of vlax-safearray->list
   (if (and (vlax-write-enabled-p att)
(vlax-property-available-p att 'Color)
) ;_ end of and
     (vla-put-Color att Color)
   ) ;_ end of if
 ) ;_ end of foreach
) ;_ end of if
(if (and (vlax-write-enabled-p Obj)
(wcmatch (vla-get-Objectname Obj) "*Dimension*,AcDb*Leader")
   ) ;_ end of and
 (progn
   (vl-catch-all-apply 'vla-put-ExtensionLineColor (list Obj Color))
   (vl-catch-all-apply 'vla-put-TextColor (list Obj Color))
   (vl-catch-all-apply 'vla-put-DimensionLineColor (list Obj Color))
   (if (vlax-property-available-p Obj 'LeaderLineColor)
     (progn
(setq tmp (vla-getinterfaceobject(vlax-get-acad-object)(strcat "AutoCAD.AcCmColor."
(substr (getvar "ACADVER") 1 2))))
(vla-put-colorindex  tmp  Color)
(vl-catch-all-apply 'vla-put-LeaderLineColor (list Obj tmp))
)
     )
 ) ;_ end of progn
) ;_ end of if
  	) ;_ end of vlax-for
     )
     ((= (vla-get-IsLayout Blk) :vlax-true)
  	(vlax-for Obj Blk
(if
 (and (vlax-write-enabled-p Obj)
(vlax-property-available-p Obj 'Color)
(vlax-property-available-p Obj 'Path)
(wcmatch (strcase (vla-get-ObjectName Obj)) "*BLOCK*")
 ) ;_ end of and
  (vla-put-Color Obj Color)
) ;_ end of if
  	) ;_ end of vlax-for
     )
     (t nil)
   ) ;_cond
 ) ;_ end of vlax-for
 (vl-cmdf "_redrawall")
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ChangeAllObjectsColor (Doc Color / txtstr tmp txt count)
;;; (setq ss (ssget))
;;; (sssetfirst nil ss)
;;;;(setq doc (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object))))
;;; (setq i 0)
;;; (repeat (vla-get-count doc)
 ;;;   (setq blk (vla-item doc i) )      
(vlax-for Blk (vla-get-blocks Doc)
   (if (= (vla-get-IsXref Blk) :vlax-false)
     (progn
(setq count 0 txt (strcat "Changed " (vla-get-name Blk)))
(grtext -1 txt)
                 (vlax-for Obj Blk
(setq count (1+ count))
(if (zerop(rem count 10))(grtext -1 (strcat txt " : " (itoa count))))
(if (and (vlax-write-enabled-p Obj) (vlax-property-available-p Obj 'Color) ) ;_ end of and
  (vla-put-Color Obj Color)
) ;_ end of if
(if (and (vlax-write-enabled-p Obj) (vlax-property-available-p Obj 'TextString) ) ;_ end of and
  (progn
		(setq txtstr
 (if (vlax-method-applicable-p Obj 'FieldCode)
     (vla-FieldCode Obj)
     (vlax-get-property Obj 'TextString)
                              		)
		)
		(setq tmp 0)
		(while (setq tmp (VL-STRING-SEARCH "\\C" txtstr tmp))
                   (setq txtstr
                    		(vl-string-subst (strcat (substr txtstr (1+ tmp) 2) (itoa Color) ";")
              		(substr txtstr (1+ tmp) (- (1+ (VL-STRING-SEARCH ";" txtstr tmp)) tmp))
              		txtstr tmp)
 )
                   (setq tmp (+ tmp 3))
		)
		(vla-put-Textstring Obj txtstr)
   )
) ;_ end of if
(if (and (vlax-write-enabled-p Obj)
           (= (vla-get-ObjectName obj) "AcDbBlockReference")
           (= (vla-get-HasAttributes obj) :vlax-true)
   ) ;_ end of and
  (foreach att (vlax-safearray->list
(vlax-variant-value (vla-GetAttributes obj))
     ) ;_ end of vlax-safearray->list
       (if (and (vlax-write-enabled-p att) (vlax-property-available-p att 'Color) ) ;_ end of and
        		(vla-put-Color att Color)
       ) ;_ end of if
   ) ;_ end of foreach
) ;_ end of if
          		(if (and (vlax-write-enabled-p Obj) (wcmatch (vla-get-Objectname Obj) "*Dimension*,AcDb*Leader")) ;_ end of and
   (progn
   (vl-catch-all-apply 'vla-put-ExtensionLineColor (list Obj Color))
   (vl-catch-all-apply 'vla-put-TextColor (list Obj Color))
   (vl-catch-all-apply 'vla-put-DimensionLineColor (list Obj Color))
   (if (vlax-property-available-p Obj 'LeaderLineColor)
       (progn
        		(setq tmp (vla-getinterfaceobject (vlax-get-acad-object) (strcat "AutoCAD.AcCmColor."
               (substr (getvar "ACADVER") 1 2)))  )
(vla-put-colorindex  tmp  Color)
        		(vl-catch-all-apply 'vla-put-LeaderLineColor (list Obj tmp))
                         )
  )
  ) ;_ end of progn
) ;_ end of if
        		) ;_ end of vlax-for
		)
   ) ;_ end of if
   ;;;(setq i (1+ i))
 ) ;_ end of vlax-for
(vl-cmdf "_redrawall")
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ChangeAllObjectsColor1 (Doc Color / txtstr tmp txt count)
 (setq ss (ssget))
(sssetfirst nil ss)
(setq doc (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object))))
(sssetfirst nil)
(setq i 0)
(repeat (vla-get-count doc)
   (setq blk (vla-item doc i) )      
;;;;(vlax-for Blk (vla-get-blocks Doc)
   (if (/= (vla-get-TrueColor Blk) nil)
     (progn
;;;;(setq count 0 txt (strcat "Changed " (vla-get-name Blk)))
;;;(grtext -1 txt)
                 ;;;(vlax-for Obj Blk
;;;(setq count (1+ count))
;;;(if (zerop(rem count 10))(grtext -1 (strcat txt " : " (itoa count))))
(if (and (vlax-write-enabled-p blk) (vlax-property-available-p blk 'Color) ) ;_ end of and
  (vla-put-Color blk Color)
) ;_ end of if
(if (and (vlax-write-enabled-p blk) (vlax-property-available-p blk 'TextString) ) ;_ end of and
  (progn
		(setq txtstr
 (if (vlax-method-applicable-p blk 'FieldCode)
     (vla-FieldCode blk)
     (vlax-get-property blk 'TextString)
                              		)
		)
		(setq tmp 0)
		(while (setq tmp (VL-STRING-SEARCH "\\C" txtstr tmp))
                   (setq txtstr
                    		(vl-string-subst (strcat (substr txtstr (1+ tmp) 2) (itoa Color) ";")
              		(substr txtstr (1+ tmp) (- (1+ (VL-STRING-SEARCH ";" txtstr tmp)) tmp))
              		txtstr tmp)
 )
                   (setq tmp (+ tmp 3))
		)
		(vla-put-Textstring blk txtstr)
   )
) ;_ end of if
(if (and (vlax-write-enabled-p blk)
           (= (vla-get-ObjectName blk) "AcDbBlockReference")
           (= (vla-get-HasAttributes blk) :vlax-true)
   ) ;_ end of and
  (foreach att (vlax-safearray->list
(vlax-variant-value (vla-GetAttributes blk))
     ) ;_ end of vlax-safearray->list
       (if (and (vlax-write-enabled-p att) (vlax-property-available-p att 'Color) ) ;_ end of and
        		(vla-put-Color att Color)
       ) ;_ end of if
   ) ;_ end of foreach
) ;_ end of if
          		(if (and (vlax-write-enabled-p blk) (wcmatch (vla-get-Objectname blk) "*Dimension*,AcDb*Leader")) ;_ end of and
   (progn
   (vl-catch-all-apply 'vla-put-ExtensionLineColor (list blk Color))
   (vl-catch-all-apply 'vla-put-TextColor (list blk Color))
   (vl-catch-all-apply 'vla-put-DimensionLineColor (list blk Color))
   (if (vlax-property-available-p blk 'LeaderLineColor)
       (progn
        		(setq tmp (vla-getinterfaceobject (vlax-get-acad-object) (strcat "AutoCAD.AcCmColor."
               (substr (getvar "ACADVER") 1 2)))  )
(vla-put-colorindex  tmp  Color)
        		(vl-catch-all-apply 'vla-put-LeaderLineColor (list blk tmp))
                         )
  )
  ) ;_ end of progn
) ;_ end of if
               ;;; ) ;_ end of vlax-for
		)
   ) ;_ end of if
   (setq i (1+ i))
 ) ;_ end of vlax-for
(vl-cmdf "_redrawall")
) ;_ end of defun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(princ
 "\nType ColorX, COLORXREF, ColorXL, COLORXREFL  in command line"
) ;_ end of princ

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Nhờ chỉnh sửa] Lisp doi mau. Xem đúng bài viết #204362 để đọc hướng dẫn và trao đổi thêm.

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