How to separate entities into a specific layer in a script

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

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

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

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

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

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’. :slight_smile:
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:

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

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