Creating layers with VLA, Auto
Hi guys,I have like 100 copies of this:
(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:
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? 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! That was an old code, with the skills I've gained from this forum I managed to write this:
(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!
So you need to load that Ltype first before you append it to layer object.
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. Generally speaking - it is better to post codes you are having a problem with than just posting a general codes.
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. 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
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. Haven't read the whole thread in detail, but this jumps out:
(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:
(foreach L MyLayerList (apply 'VLA:CreateLayer l))
页:
[1]
2