userzhl 发表于 2006-9-4 18:12:00

[求助]改块颜色。

(defun c:blk_col( / blk blkref blocks doc ent name ss n clo)<BR>&nbsp;(vl-load-com)<BR>&nbsp; (princ "\n选要改颜色的块: ")<BR>(setq ss (ssget '((0 . "INSERT")))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n&nbsp; (sslength ss)<BR>)<BR>&nbsp; (while (and (setq BLK&nbsp;&nbsp;&nbsp;&nbsp; (ssname ss (setq n (1- n))))<BR>&nbsp;&nbsp; (setq BLKREF&nbsp; (vlax-ename-&gt;vla-object BLK))<BR>&nbsp;&nbsp; (not(and(/= (vla-get-objectname BLKREF) "AcDbBlockReference")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (princ"\n不是块:"))<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq clo (acad_colordlg 7))
<P>&nbsp;&nbsp; (setq name(vla-get-name BLKREF))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; (progn<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command"undo""group")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq DOC&nbsp;&nbsp;&nbsp;&nbsp; (vla-get-activedocument (vlax-get-acad-object))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BLOCKS&nbsp; (vla-get-blocks doc)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blk&nbsp;&nbsp;&nbsp;&nbsp; (vla-item BLOCKS name)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vlax-for ENT blk<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-layer ent "图块")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-color ent clo)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-regen doc acActiveViewport)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vlax-release-object blk)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vlax-release-object BLOCKS)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vlax-release-object DOC)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (command"undo""end")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp; )<BR>&nbsp; (princ"\nUndo后请regen.")<BR>(princ))</P>
<P>这个程序只能单选,而且需要选色——可不可以改成这样:</P>
<P>不用多次选色(即:可同时选择若干个块,无论是多重块嵌套块还是属性块),颜色自动改为8号色(不需选色),并把其图层(块内的所有图元)自动归到"图块"层。</P>

HuaiYu 发表于 2006-9-5 22:11:00

<P>已改写成如下,请验收:</P>
<P>(Defun c:tt2 (/ ss color layer i *AcadDocument* blocks vn)<BR>&nbsp; ;;&nbsp;子程序&nbsp;&nbsp;.<BR>&nbsp; (Defun sub_Fun (vn)<BR>&nbsp;&nbsp;&nbsp; (vla-put-color vn color)<BR>&nbsp;&nbsp;&nbsp; (vla-put-layer vn layer)<BR>&nbsp;&nbsp;&nbsp; (vlax-for ent (vla-item blocks (vla-get-name vn))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-color ent color)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-layer ent layer)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (= (vla-get-objectname ent) "AcDbBlockReference")<BR>&nbsp;(sub_Fun ent)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp; )<BR>&nbsp; ;;&nbsp;程序开始&nbsp;.<BR>&nbsp; (vl-load-com)<BR>&nbsp; (princ "\n选要改颜色的块: ")<BR>&nbsp; (setq ss (vl-catch-all-apply 'ssget '(((0 . "INSERT")))))<BR>&nbsp; (cond ((or (vl-catch-all-error-p ss) (null ss)) (vl-exit-with-value 0)))<BR>&nbsp; ;;&nbsp;设定这个要改的 color=8,layer="图块"&nbsp; (假设 "图块"层一定存在)&nbsp;.<BR>&nbsp; (setq&nbsp;color 8<BR>&nbsp;layer "图块"<BR>&nbsp; )<BR>&nbsp; (setq&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<BR>&nbsp;*AcadDocument* (vla-get-activeDocument (vlax-get-acad-object))<BR>&nbsp;blocks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-get-blocks *AcadDocument*)<BR>&nbsp; )<BR>&nbsp; (repeat (sslength ss)<BR>&nbsp;&nbsp;&nbsp; (setq vn (vlax-ename-&gt;vla-object (ssname ss i))<BR>&nbsp;&nbsp; i&nbsp; (1+ i)<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; ;;&nbsp;防止出错&nbsp;.<BR>&nbsp;&nbsp;&nbsp; (sub_Fun vn)<BR>&nbsp; )<BR>&nbsp; (prin1)<BR>)<BR></P>

userzhl 发表于 2006-9-6 10:15:00

非常感谢!

HuaiYu 发表于 2006-9-6 20:34:00

<P>根据斑主的指示,已将 Attribute 改好,但标注的尺寸是改的不是很好的...</P>
<P>我看了一下,觉得应该要将 dimstyle 中的 textColor ExtensionLineColor DimensionLineColor 设成 0即 ByBlock才行的...</P>
<P>还请版主明示......</P>
<P>(Defun c:tt2 (/ ss color layer i *AcadDocument* blocks vn)<BR>&nbsp; ;; 子程序&nbsp; .<BR>&nbsp; (Defun sub_Fun (vn / atts)<BR>&nbsp;&nbsp;&nbsp; (vla-put-color vn color)<BR>&nbsp;&nbsp;&nbsp; (vla-put-layer vn layer)<BR>&nbsp;&nbsp;&nbsp; (cond<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((and (= (vla-get-objectname vn) "AcDbBlockReference") (= (vla-get-hasAttributes vn) :vlax-true))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (setq atts (vlax-safeArray-&gt;list (vlax-variant-value (vla-getAttributes vn))))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (foreach att atts (vla-put-color att color) (vla-put-layer att layer))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; (vlax-for ent (vla-item blocks (vla-get-name vn))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-color ent color)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-layer ent layer)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cond ((wcmatch (vla-get-objectName ent) "AcDb*Dimension")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-DimensionLineColor ent color)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-textColor ent color)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-put-ExtensionLineColor ent color)<BR>&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (= (vla-get-objectname ent) "AcDbBlockReference")<BR>&nbsp;(sub_Fun ent)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp; )<BR>&nbsp; ;; 程序开始 .<BR>&nbsp; (vl-load-com)<BR>&nbsp; (princ "\n选要改颜色的块: ")<BR>&nbsp; (setq ss (vl-catch-all-apply 'ssget '(((0 . "INSERT")))))<BR>&nbsp; (cond ((or (vl-catch-all-error-p ss) (null ss)) (vl-exit-with-value 0)))<BR>&nbsp; ;; 设定这个要改的 color=8,layer="图块"&nbsp; (假设 "图块"层一定存在) .<BR>&nbsp; (setq&nbsp;color 8<BR>&nbsp;layer "图块"<BR>&nbsp; )<BR>&nbsp; (setq&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<BR>&nbsp;*AcadDocument* (vla-get-activeDocument (vlax-get-acad-object))<BR>&nbsp;blocks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vla-get-blocks *AcadDocument*)<BR>&nbsp; )<BR>&nbsp; (repeat (sslength ss)<BR>&nbsp;&nbsp;&nbsp; (setq vn (vlax-ename-&gt;vla-object (ssname ss i))<BR>&nbsp;&nbsp; i&nbsp; (1+ i)<BR>&nbsp;&nbsp;&nbsp; )<BR>&nbsp;&nbsp;&nbsp; ;; 防止出错 .<BR>&nbsp;&nbsp;&nbsp; (sub_Fun vn)<BR>&nbsp; )<BR>&nbsp; (prin1)<BR>)</P>
<P>&nbsp;</P>

龙龙仔 发表于 2006-9-7 09:21:00

<P>;;实在还有一些改进的地方,附图供测试<BR>;;我U盘上有ch_color.vlx程序,是改全图顏色比较效果看看</P>
<P></P>

zhangrunze 发表于 2024-4-1 14:56:42

龙龙仔 发表于 2006-9-7 09:21
;;实在还有一些改进的地方,附图供测试;;我U盘上有ch_color.vlx程序,是改全图顏色比较效果看看

感谢分享~
ch_color.vlx可以分享下吗?
页: [1]
查看完整版本: [求助]改块颜色。