乐筑天下

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

[编程交流] Creating layers with VLA, Auto

[复制链接]

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 17:36:51 | 显示全部楼层 |阅读模式
Hi guys,
I have like 100 copies of this:
  1. (setq WILDCARD-VLD "VLD_")                                           ;WILDCARD for the layer(setq LAYNAME1 "КОЛОНИ")                                            ;Name for the layer(setq LAYNM1-WILDCARD (strcat WILDCARD-VLD LAYNAME1))         ;Total name (with wildcard)(if (not (tblsearch "LAYER" LAYNM1-WILDCARD))  (progn                                                         (setq aNewLayer (vla-add LayerTable LAYNM1-WILDCARD))    (setq color (vlax-create-object "AutoCAD.AcCmColor.20")) ; for truecolor RGB    (vla-SetRGB color 80 100 244)                              ; for truecolor RGB;     (vla-put-TrueColor aNewLayer color)                      ; for truecolor RGB ; put semicoma here    (vla-put-color aNewLayer 154)                            ; for table color  ; or put semicoma here    (vla-put-linetype aNewLayer "Continuous")    (vla-put-LineWeight aNewLayer 50)    (vla-put-plottable aNewLayer :vlax-true)    (princ (strcat "\nCreated layer named " LAYNM1-WILDCARD " !"))  );then    (princ (strcat "\n " LAYNM1-WILDCARD " layer already exists!" ));else);if
I define each layer like this, and load them within command definition.
But the problem I have is that for my first attempt to load the layers - I get this:
  1. Error: Automation Error. Key not found
Then few of my layers are loaded, but I have to run the command 2nd time to load them.
And I'm not sure if I do load all the layers in the 2nd attempt, or most of them.
 
I know that the reason might be that I didn't construct a list and apply it with vl-catch-all-error.
My list skills are limited, so does anyone have suggestions how to correct this such type of code?
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 17:42:49 | 显示全部楼层
Hi,
 
No error here with AutoCAD 2014. Is this the whole codes you are using into your program ?
 
I think you have received this error because you have another LineType that is not already loaded into your Ltype table that you are trying to use!
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 17:47:46 | 显示全部楼层
That was an old code, with the skills I've gained from this forum I managed to write this:
  1. (defun C:test ( / MyLayerList )(setq MyLayerList        '(                ("Layername1" "RGB" nil 40 120 160 "Continuous" 50 1)                ("Layername2" "RGB" nil 40 120 160 "Continuous" 50 1)                ("Layername3" "RGB" nil 40 120 160 "Continuous" 50 1)                ("Layername4" "TableCol" 150 nil nil nil "Continuous" 50 0)                ("Layername5" "TableCol" 150 nil nil nil "Continuous" 50 0)                ("Layername6" "TableCol" 150 nil nil nil "Continuous" 50 0)        )); vl-catch-all-apply(foreach L MyLayerList        (VLA:CreateLayer (nth 0 L) (nth 1 L) (nth 2 L) (nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L) (nth 7 L) (nth 8 L) ))(princ)); Subfunction to create new layer using VLA method; Example1: (VLA:CreateLayer "Layername1" "RGB" nil 40 120 160 "Continuous" 50 1); Example1: (VLA:CreateLayer "Layername1" "TableCol" 150 nil nil nil "Continuous" 50 0)(defun VLA:CreateLayer ( LAYNAME1 ColMethod TableCol R G B LinT LinW Plt / )(vl-load-com)(setq acadobj (vlax-get-Acad-Object))(setq activedoc (vla-get-activedocument acadobj));(setq extdic (vla-GetExtensionDictionary))(setq LayerTable (vla-get-layers activedoc))(setq WILDCARD-VLD "VLD_")                                           ;WILDCARD for the layer(setq LAYNM1-WILDCARD (strcat WILDCARD-VLD LAYNAME1))         ;Total name (with wildcard)(if (not (tblsearch "LAYER" LAYNM1-WILDCARD))        (progn                                                                     (setq aNewLayer (vla-add LayerTable LAYNM1-WILDCARD))                (cond                        ( (= ColMethod "RGB")                                (setq color (vlax-create-object "AutoCAD.AcCmColor.20")) ; for truecolor RGB                                (vla-SetRGB color R G B)                              ; for truecolor RGB                                (vla-put-TrueColor aNewLayer color)                      ; for truecolor RGB ; put semicoma here                        )                        ( (= ColMethod "TableCol")                                (vla-put-color aNewLayer TableCol)                            ; for table color  ; or put semicoma here                        )                )                (vla-put-linetype aNewLayer LinT)                (vla-put-LineWeight aNewLayer LinW)                (cond                        ( (= plt 1)                                (vla-put-plottable aNewLayer :vlax-true)                        )                        ( (= plt 0)                                (vla-put-plottable aNewLayer :vlax-false)                        )                )                (princ (strcat "\nCreated layer named " LAYNM1-WILDCARD " !"))        )        (princ (strcat "\n " LAYNM1-WILDCARD " layer already exists!" )));if)                               
 
But still my question remains - how to avoid this Automation Error. Key not found ?
Is it possible to get it in my 2nd consideration (code) ?
EDIT:
Hi Tharwat, I just saw your post
I think that might be the problem - I'm trying to load some ACAD_ISO03W100 linetypes that are not loaded in the drawing. Thanks!
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 17:50:46 | 显示全部楼层
 
So you need to load that Ltype first before you append it to layer object.
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 17:56:52 | 显示全部楼层
 
Yes, now I realized that I had troubles with some loadlinetypes routine and that would be the reason for this problem.
Later I'll post a more interesting question, regarding coding.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 17:59:01 | 显示全部楼层
Generally speaking - it is better to post codes you are having a problem with than just posting a general codes.
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:05:44 | 显示全部楼层
 
I know, but in this case I avoided using lists and used instead like 100 copies of the part I posted, so I ended up with 72 KB .lsp file.
Its an old code, so I consider rewriting it now.
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-5 18:09:10 | 显示全部楼层
Grrr... Firstly, I love the user name! Haha
 
As for the layer creation, Tharwat has already addressed the need for some error checking on the Linetype issue. Rather than repeating myriad code blocks, just use a sub-function to create the desired layer(s), based on arguments passed.
 
Here's an old example of what I mean:  
 
http://www.cadtutor.net/forum/showthread.php?57570-Layer-Creater-Lisp-Routine-Issue&p=390543&viewfull=1#post390543
 
 
Cheers
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

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

铜币
325
发表于 2022-7-5 18:15:02 | 显示全部楼层
 
Hi BlackBox,
I went the same way you mentioned (if you check my #3 post), unfortunately today I'm spread on exploring different branches from the LISP world (from dumping/entget'ing tableobjects, attempts on mleaderstyle creation, to reactors). Yes I have time to waste!
Your username is nice aswell, I associate it with some deep coding stuff (from reading your posts on different lisp forums).
 
I'm surprised that you replied here and actually I had a question about one of your codes known as "Publish Reactor".
I was scratching my head few months about it, trying to figure out how it works just for doing a slight modification.
Not sure should I start a new thread about it or ask right here, since I don't know if its too complicated/impossible or easy fix.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:16:20 | 显示全部楼层
Haven't read the whole thread in detail, but this jumps out:
  1. (foreach L MyLayerList   (VLA:CreateLayer (nth 0 L) (nth 1 L) (nth 2 L) (nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L) (nth 7 L) (nth 8 L) ))
Note that the above could become:
  1. (foreach L MyLayerList (apply 'VLA:CreateLayer l))
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 03:14 , Processed in 0.369617 second(s), 72 queries .

© 2020-2025 乐筑天下

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