乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 78|回复: 5

[编程交流] 组合两个lisp fun时出现问题

[复制链接]

76

主题

312

帖子

254

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
390
发表于 2022-7-5 16:49:48 | 显示全部楼层 |阅读模式
大家好,
 
我成功地构建了这个lisp:
 
  1. (defun c:HBKLICMelding (/ adoc item afstandbegin afstandeind truecol aci R G B)
  2. (vl-load-com)
  3. (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  4. (vla-startundomark adoc)
  5. (vlax-for item (vla-get-layers adoc)
  6.         (vl-catch-all-apply 'vla-put-name
  7.                 (list item
  8.                         (cond
  9.                                 ((= (substr(vla-get-name item)1 5) "B-OI-")
  10.                                 (setq afstandbegin (-(strlen (vla-get-name item))28))
  11.                                 (setq afstandeind (-(strlen (vla-get-name item))1))
  12.                                 (strcat (substr(vla-get-name item)1 afstandbegin)(substr(vla-get-name item) afstandeind))
  13.                                 )
  14.                                 (t
  15.                                 (princ)
  16.                                 )
  17.                         )
  18.                 )
  19.         )
  20. )
  21. (vlax-for item (vla-get-layers adoc)
  22. (setq truecol (vla-get-truecolor item))
  23.         (if (= (vla-get-ColorMethod truecol) acColorMethodByACI)
  24.                 (progn
  25.                         (setq aci (vla-get-ColorIndex truecol))            
  26.                         (vla-put-ColorMethod truecol acColorMethodByRGB)
  27.                         (vla-put-ColorIndex truecol aci)
  28.                         (setq R (vla-get-red truecol))
  29.                         (setq G (vla-get-green truecol))
  30.                         (setq B (vla-get-blue truecol))
  31.                         (vla-setRGB truecol R G B)
  32.                         (vla-put-truecolor item truecol)
  33.                         )
  34.                 )
  35.         )  
  36. )
  37. (vla-endundomark adoc)
  38. (princ)
  39. )
  40. (princ)

 
第一部分删除所有以“B-OI-”开头的层名称中的一部分。
第二部分将所有层的颜色更改为真彩色。
 
但是。。。我只希望符合第一个标准(“B-OI-”)的图层更改为颜色。
当我将代码更改为下面的代码时,更改颜色部分可以工作,但它不再重命名我的层。。。
 
  1. (defun c:HBKLICMelding (/ adoc item afstandbegin afstandeind truecol aci R G B)
  2. (vl-load-com)
  3. (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  4. (vla-startundomark adoc)
  5. (vlax-for item (vla-get-layers adoc)
  6.         (vl-catch-all-apply 'vla-put-name
  7.                 (list item
  8.                         (cond
  9.                                 ((= (substr(vla-get-name item)1 5) "B-OI-")
  10.                                 (setq afstandbegin (-(strlen (vla-get-name item))28))
  11.                                 (setq afstandeind (-(strlen (vla-get-name item))1))
  12.                                 (strcat (substr(vla-get-name item)1 afstandbegin)(substr(vla-get-name item) afstandeind))
  13.                                 (setq truecol (vla-get-truecolor item))
  14.                                 (setq aci (vla-get-ColorIndex truecol))            
  15.                                 (vla-put-ColorMethod truecol acColorMethodByRGB)
  16.                                 (vla-put-ColorIndex truecol aci)
  17.                                 (setq R (vla-get-red truecol))
  18.                                 (setq G (vla-get-green truecol))
  19.                                 (setq B (vla-get-blue truecol))
  20.                                 (vla-setRGB truecol R G B)
  21.                                 (vla-put-truecolor item truecol)
  22.                                 )
  23.                                 (t
  24.                                 (princ)
  25.                                 )
  26.                         )
  27.                 )
  28.         )
  29. )
  30. (vla-endundomark adoc)
  31. (princ)
  32. )
  33. (princ)

 
有什么建议吗??
回复

使用道具 举报

18

主题

1529

帖子

973

银币

中流砥柱

Rank: 25

铜币
649
发表于 2022-7-5 17:12:42 | 显示全部楼层
帮自己一个大忙,正确地缩进代码。
在第一个代码块中,函数在(vla endundomark adoc)之前结束。
 
  1. (if (= (substr (vla-get-name item) 1 5) "B-OI-")
  2. (progn
  3.    (setq newName ...)
  4.    (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-name (list item newName))))
  5.      (progn
  6.        ...
  7.        (vla-put-truecolor item truecol)
  8.      )
  9.    )
  10. )
  11. )
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 17:16:04 | 显示全部楼层
也许这种解剖将有助于:
  1. [color=GREEN];; Define function, declare local variables[/color]
  2. ([color=BLUE]defun[/color] c:HBKLICMelding ( [color=BLUE]/[/color] col doc lay lst )
  3.    [color=GREEN];; Assign the Active Document object to a local variable[/color]
  4.    ([color=BLUE]setq[/color] doc ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color])))
  5.    [color=GREEN];; If an undo group is already open[/color]
  6.    ([color=BLUE]if[/color] ([color=BLUE]=[/color] 8 ([color=BLUE]logand[/color] 8 ([color=BLUE]getvar[/color] 'undoctl)))
  7.        ([color=BLUE]vla-endundomark[/color] doc) [color=GREEN];; close it[/color]
  8.    ) [color=GREEN];; end if[/color]
  9.    
  10.    [color=GREEN];; Open a new undo group[/color]
  11.    ([color=BLUE]vla-startundomark[/color] doc)
  12.    [color=GREEN];; For each Layer object in the Layers Collection[/color]
  13.    ([color=BLUE]vlax-for[/color] obj ([color=BLUE]vla-get-layers[/color] doc)
  14.        [color=GREEN];; Retrieve the layer name[/color]
  15.        ([color=BLUE]setq[/color] lay ([color=BLUE]vla-get-name[/color] obj))
  16.       
  17.        [color=GREEN];; if the layer name starts with "B-OI-"[/color]
  18.        ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] lay) [color=MAROON]"B-OI-*"[/color])
  19.            [color=GREEN];; Then do the following:[/color]
  20.            ([color=BLUE]progn[/color]
  21.                [color=GREEN];; If the layer name has more than 28 characters[/color]
  22.                ([color=BLUE]if[/color] ([color=BLUE]<[/color] 28 ([color=BLUE]setq[/color] len ([color=BLUE]strlen[/color] lay)))
  23.                   
  24.                    [color=GREEN];; Then rename the layer[/color]
  25.                    [color=GREEN];; (wrap in vl-catch-all-apply in case another layer exists with the same name - sledgehammer approach)[/color]
  26.                    ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-put-name[/color] ([color=BLUE]list[/color] obj ([color=BLUE]strcat[/color] ([color=BLUE]substr[/color] lay 1 ([color=BLUE]-[/color] len 28)) ([color=BLUE]substr[/color] lay ([color=BLUE]1-[/color] len)))))
  27.                   
  28.                ) [color=GREEN];; end if[/color]
  29.                [color=GREEN];; Retrieve current layer color (color object)[/color]
  30.                ([color=BLUE]setq[/color] col ([color=BLUE]vla-get-truecolor[/color] obj))
  31.                [color=GREEN];; If the color object is set to use an ACI color[/color]
  32.                ([color=BLUE]if[/color] ([color=BLUE]=[/color] [color=BLUE]accolormethodbyaci[/color] ([color=BLUE]vla-get-colormethod[/color] col))
  33.                    [color=GREEN];; Then do the following:[/color]
  34.                    ([color=BLUE]progn[/color]
  35.                        [color=GREEN];; Retrieve the current RGB values[/color]
  36.                        ([color=BLUE]setq[/color] lst ([color=BLUE]mapcar[/color] '[color=BLUE]vlax-get-property[/color] ([color=BLUE]list[/color] col col col) '(red green blue)))
  37.                        [color=GREEN];; Change the color method to RGB[/color]
  38.                        ([color=BLUE]vla-put-colormethod[/color] col [color=BLUE]accolormethodbyrgb[/color])
  39.                        [color=GREEN];; Configure the RGB values accordingly[/color]
  40.                        ([color=BLUE]apply[/color] '[color=BLUE]vla-setrgb[/color] ([color=BLUE]cons[/color] col lst))
  41.                        [color=GREEN];; Apply the color to the layer[/color]
  42.                        ([color=BLUE]vla-put-truecolor[/color] obj col)
  43.                        
  44.                    ) [color=GREEN];; end progn[/color]
  45.                   
  46.                ) [color=GREEN];; end if[/color]
  47.                
  48.            ) [color=GREEN];; end progn[/color]
  49.            
  50.        ) [color=GREEN];; end if[/color]
  51.       
  52.    ) [color=GREEN];; end vlax-for[/color]
  53.    [color=GREEN];; Close the open undo group[/color]
  54.    ([color=BLUE]vla-endundomark[/color] doc)
  55.    
  56.    [color=GREEN];; Suppress the value returned by the last evaluated expression[/color]
  57.    ([color=BLUE]princ[/color])
  58. ) [color=GREEN];; end defun[/color]
  59. [color=green];; Load the ActiveX component of Visual LISP and suppress the value returned on load[/color]
  60. ([color=blue]vl-load-com[/color]) ([color=blue]princ[/color])
回复

使用道具 举报

18

主题

1529

帖子

973

银币

中流砥柱

Rank: 25

铜币
649
发表于 2022-7-5 17:26:04 | 显示全部楼层
你说得很对,罗伊-以下内容在AutoCAD中也适用:
  1. (defun KGA_Sys_ObjectColor_Aci_To_Rgb (obj / clr rgb)
  2. (if (= accolormethodbyaci (vla-get-colormethod (setq clr (vla-get-truecolor obj))))
  3.    (progn
  4.      (setq rgb (list (vla-get-red clr) (vla-get-green clr) (vla-get-blue clr)))
  5.      (apply 'vla-setrgb (cons clr rgb)) ; Also changes the color method.
  6.      (vla-put-truecolor obj clr)
  7.      rgb
  8.    )
  9. )
  10. )
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 17:46:28 | 显示全部楼层
谢谢李。你的建议(又一次)奏效了
回复

使用道具 举报

76

主题

312

帖子

254

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
390
发表于 2022-7-5 18:01:48 | 显示全部楼层
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-8-24 15:41 , Processed in 2.245606 second(s), 65 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表