How to create AutoCAD Layers if they don't exist yet

Hi all,

Here a part of my latest script that creates an AutoCAD Layer, but only if it doesn’t exist yet:

This isn’t the prettiest solution, but it works without showing any error messages if a layer already exists (at the Layer.Create node) or if it doesn’t exist yet (at the Document.LayerByName node).

Thoughts? Perhaps someone else has a cleaner, easier to follow solution :smiley:

1 Like

I solved this by deleting them before creating, using a wait for code block

Hola Amigo @Daan is in deed the way to do it!! :+1: I had posted a similar method for beginners in a previous post, take a look!!

Hi @kovacsv ,

Wouldn’t that remove all layer properties, like color, linetype etc?

It does, I’m sure they can be later adjusted in the script. I don’t know how many layers do you have, if you have many layers with large variety of settings this isn’t optimal.
For me it was convinient, because it made it possible to rerun the script in Player as it deletes all objects as well, so recreates layer and also the objects and then sets colors and maybe visibibility, if I remember correctly. But this is about 10 or so layers.

EDIT: Nevermind I’m stupid and I lied. I remembered deleting the layers, but I only delete the objects. So that way new layers are created if needed by the Object.ByGeometry node and I do the settings later. Sorry.

I’m pretty sure the Arkance Systems Node Library contains a node to create a layer or return the existing one. Maybe Camber too.

Keep in mind if you use the API in, for example, Python, a deleted layer still remain until the drawing is closed. A check for an existing layer will return a false positive. In that case you need to check if a layer is deleted if the API claims it exists.

2 Likes