How to separate entities into a specific layer in a script

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
Rolinger
Junior Member
Posts: 22
Joined: Thu Aug 29, 2024 5:17 pm

How to separate entities into a specific layer in a script

Post by Rolinger » Sun Jan 04, 2026 1:52 pm

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

Code: Select all

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

CVH
Premier Member
Posts: 5054
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to separate entities into a specific layer in a script

Post by CVH » Sun Jan 04, 2026 2:31 pm

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

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

CVH
Premier Member
Posts: 5054
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to separate entities into a specific layer in a script

Post by CVH » Sun Jan 04, 2026 4:04 pm

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

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. :wink:
(2N because setting a current active layer is also a step to undo)
#END EDIT#

Another note:

Code: Select all

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 :wink:

Regards,
CVH
Last edited by CVH on Tue Jan 06, 2026 11:45 am, edited 1 time in total.

CVH
Premier Member
Posts: 5054
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to separate entities into a specific layer in a script

Post by CVH » Tue Jan 06, 2026 11:40 am

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 a 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.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”