Jump to content

Creating layers with VLA, Automation Error. Key not found


Grrr

Recommended Posts

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :thumbsup:

 

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

Link to comment
Share on other sites

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. :thumbsup:

 

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.

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

Nice catch, Lee!

I'll need to work out more with this list manipulation thing.

 

By the way, it seems that the transparency of the layer can be set only with the command call:

(command "_LAYER" "_TR" transparency layerName "")

 

DXF Data:

((-1 . <Entity name: 7ff6a5104f10>) (0 . LAYER) (330 . <Entity name: 7ff6a5103820>) (5 . 1F1) (100 . AcDbSymbolTableRecord) (100 . AcDbLayerTableRecord) (2 . VLD_TEXT) (70 . 0) (62 . 50) (6 . Continuous) (290 . 1) (370 . 18) (390 . <Entity name: 7ff6a51038f0>) (347 . <Entity name: 7ff6a5103e00>) (348 . <Entity name: 0>))

 

Allowed properties and methods:

; IAcadLayer: A logical grouping of data, similar to transparent acetate overlays on a drawing
; Property values:
;   Application (RO) = #<VLA-OBJECT IAcadApplication 00007ff6a62e9110>
;   Description = ""
;   Document (RO) = #<VLA-OBJECT IAcadDocument 000000f8a4d2c5a8>
;   Freeze = 0
;   Handle (RO) = "1F1"
;   HasExtensionDictionary (RO) = 0
;   LayerOn = -1
;   Linetype = "Continuous"
;   Lineweight = 18
;   Lock = 0
;   Material = "Global"
;   Name = "VLD_TEXT"
;   ObjectID (RO) = 43
;   ObjectName (RO) = "AcDbLayerTableRecord"
;   OwnerID (RO) = 44
;   PlotStyleName = "Color_50"
;   Plottable = -1
;   TrueColor = #<VLA-OBJECT IAcadAcCmColor 000000f8c914ab00>
;   Used (RO) = -1
;   ViewportDefault = 0
; Methods supported:
;   Delete ()
;   GetExtensionDictionary ()
;   GetXData (3)
;   SetXData (2)

Link to comment
Share on other sites

Nice catch, Lee!

I'll need to work out more with this list manipulation thing.

 

You're welcome!

 

By the way, it seems that the transparency of the layer can be set only with the command call:

(command "_LAYER" "_TR" transparency layerName "")

 

Although the layer transparency is stored within the xdata of the layer table entity and may be used to obtain the transparency of a given layer, if I recall correctly, modifying this xdata has no effect on the transparency of the layer, and so the transparency must be modified using a command call (this may be different in BricsCAD). There is a thread at the Swamp which discusses this behaviour.

Link to comment
Share on other sites

You're welcome!

 

 

 

Although the layer transparency is stored within the xdata of the layer table entity and may be used to obtain the transparency of a given layer, if I recall correctly, modifying this xdata has no effect on the transparency of the layer, and so the transparency must be modified using a command call (this may be different in BricsCAD). There is a thread at the Swamp which discusses this behaviour.

 

Thanks for comfirning it Lee!

Sadly theswamp is still closed, first few days I thought the site was at maintenance process. Now alot of the answers for the lisp questions must be googled somewhere else. :(

Link to comment
Share on other sites

@ Lee:

I think we have discussed this before at http://www.theswamp.org. In BricsCAD you can use entmod to change a layer's transparency. There have been some update issues, but these have meanwhile been fixed. But you do (still) have to regenerate after changing a layer in this manner.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...