# How to separate entities into a specific layer in a script

**URL:** <http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839>\
**Category:** QCAD Script Programming & Contributions\
**Created:** [January 4, 2026, 12:52pm UTC](http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839 "2026-01-04T12:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolinger](http://forum.qcad.org/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@Rolinger](http://forum.qcad.org/u/Rolinger)\
**Post date:** [January 4, 2026, 12:52pm UTC](http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839/1 "2026-01-04T12:52:03Z")

</div>

Hi. I’ve created a script that creates certain layers and plots entities (lines, polylines, and text). The problem is that all the elements end up in layer 0

```plaintext
Create all layers first
addLayer("ARMATURA", "black", "Continuous", RLineweight.Weight000);
addLayer("DODATNA_ARMATURA", "black", "Continuous", RLineweight.Weight000);
addLayer("OKVIRI", "black", "Continuous", RLineweight.Weight000);
addLayer("OPLATA", "black", "Continuous", RLineweight.Weight000);
addLayer("TEXT", "black", "Continuous", RLineweight.Weight000);
addLayer("TEXT 0", "black", "Continuous", RLineweight.Weight000);

// Layer: OPLATA
setCurrentLayer("OPLATA");
addPolyline([[0.0, 0.0], [600.0, 0.0], [600.0, -640.0], [0.0, -640.0]], true, false);
addPolyline([[600.0, 0.0], [1200.0, 0.0], [1200.0, -640.0], [600.0, -640.0]], true, false);

// Layer: ARMATURA
setCurrentLayer("ARMATURA");
addLine([26000.0, -100.0], [26600.0, -100.0]);
addLine([26000.0, -462.0], [26600.0, -462.0]);
addLine([26100.0, 0.0], [26100.0, -640.0]);
addLine([26430.0, 0.0], [26430.0, -640.0]);
addLine([26600.0, -100.0], [27200.0, -100.0]);

```

//

I can send a script for inspection in PM

Best regards

P.S.  
Please note that I am not a programmer - please be careful when answering me 🙂

---

<div class="post-metadata">

**Author:** ![CVH](http://forum.qcad.org/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@CVH](http://forum.qcad.org/u/CVH)\
**Post date:** [January 4, 2026, 1:31pm UTC](http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839/2 "2026-01-04T13:31:10Z")

</div>

Hi,

You are using the QCAD Simple API.  
At first glance the code seems to be functional.

An attempt to run this code snippet using the Script Shell ( **GE** ) confirms that. 🙂

In a new file your script creates:

- 6 new layers.  
Adds 2 squares side by side on layer ‘OPLATA’.  
Adds 4 Lines on layer ‘ARMATURA’.  
You code snippet does not contain the creation of a text with addSimpleText(..)

I don’t see the problem but detected another one.  
Just a check to see what happens if such layers already exists and are locked for example.  
By default the layers are created as: On (not hidden), not frozen, not locked.  
If you delete the new entities, lock both used layers and run the code again … QCAD crashes. 😮

Adding a layer that exist is not an issue.  
Adding a layer that exist as locked is not an issue.  
Setting a locked layer the active one neither.  
It is adding an entity to an active locked layer that crashes QCAD.

Regards,  
CVH

---

<div class="post-metadata">

**Author:** ![CVH](http://forum.qcad.org/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@CVH](http://forum.qcad.org/u/CVH)\
**Post date:** [January 4, 2026, 3:04pm UTC](http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839/3 "2026-01-04T15:04:07Z")

</div>

For the full script by PM:

It seems to be related to the use of startTransaction(..) and endTransaction()

First: It is startTransaction(RDocument) or startTransaction(RDocumentInterface), see [current reference](https://github.com/qcad/qcad/blob/7ecc686c9c72897d19939ec36db1bb01a6642b0a/scripts/simple_transaction.js#L5-L20)  
Not startTransaction(.. Some string ..)

Second: When I disable both, the script is less efficient but everything doesn’t ends up on Layer ‘0’. 🙂  
Operations are handled one by one with each time a document update, a screen update and so on.

A possible cause: A layer must exists before you can cast drawing entities on it.  
By making it one complete transaction … The layers are not already present when switching to them …  
… Only layer ‘0’ exists and adding new drawing objects, defining new REntity objects reverts to this existing default layer. 😉

Solution:

- Use variable doc for startTransaction(doc) but this is not mandatory here.  
startTransaction() reverts to the current active document but the parameter must be nothing.
- Group the creation of the layers in one transaction and end that to create them.  
Ensuring that the layers exist before the creation of drawing entities.  
**#EDIT#**
- Group the rest of the code in an additional transaction per layer.

Still efficient but in **2N** steps and it are **2N** steps to undo/redo. 😉  
(2N because setting a current active layer is also a step to undo)  
**#END EDIT#**

Another note:

```plaintext
var doc = getDocument();
if (isNull(doc)) {
    createDocument();
    doc = getDocument();
}

```

How do you run this script without an open document?  
**XC** and **GE** are disabled without.

PS: I would avoid the use of Lineweight 0.00mm 😉

Regards,  
CVH

---

<div class="post-metadata">

**Author:** ![CVH](http://forum.qcad.org/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@CVH](http://forum.qcad.org/u/CVH)\
**Post date:** [January 6, 2026, 10:40am UTC](http://forum.qcad.org/t/how-to-separate-entities-into-a-specific-layer-in-a-script/9839/4 "2026-01-06T10:40:26Z")

</div>

Corrected in the above:

- One can not assign the layer attribute of a new entity using the QCAD Simple API.

It ends up on the current active layer.

One can set the current layer but if that is enclosed in a transaction for efficiency that won’t work.  
Probably because setting a current layer is itself an instantaneous transaction.  
Everything ends up in the last active layer that was set.

One can group the creation of entities for a layer in one transaction.  
These end up in the active layer that was set before this transaction.

Regards,  
CVH
