Setting layers of texts and dimensions

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

Post Reply
User avatar
dfriasb
Senior Member
Posts: 119
Joined: Thu Mar 10, 2016 1:08 pm
Location: Calafell, Tarragona, Spain

Setting layers of texts and dimensions

Post by dfriasb » Sun Mar 14, 2021 9:23 pm

Hello all,

I'm writing a script for changing layers of all texts and dimensions simultaneously:

Code: Select all

var di = this.getDocumentInterface();
var document = this.getDocument();

var op = new RModifyObjectsOperation();

var entitiesIds = document.queryAllEntities(false, false);
for (var i=0; i<entitiesIds.length; i++) {
    entityId = entitiesIds[i];
    entity = document.queryEntity(entityId);
    if (isDimensionEntity(entity)) {
        entity.setLayerName("dim.");
        entity.update();
        op.addObject(entity)
        }
    else if (isTextEntity(entity)) {
        entity.setLayerName("ret.");
        entity.update();
        op.addObject(entity)
        }
    }   // looping ends
di.applyOperation(op);
Texts should result to be in layer called "ret." and dimensions in layer "dim.". But it's not working like this. It seems it is changing all entities to layer "dim". I guess something is not working as I want in the loop.

Any help will be very welcomed. Best regards!

David
David Frías Barranco | architect
[email protected] | davidfriasarquitecto.es

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

Re: Setting layers of texts and dimensions

Post by CVH » Mon Mar 15, 2021 5:48 am

Hi,
The addObject signature is found here:
https://www.qcad.org/doc/qcad/latest/de ... 57f845ec3a

useCurrentAttributes defaults to true.
forceNew defaults to false.

Meaning you don't add new entities but you update them all with the current attributes.
The current active layer was 'dim.' when you ran the script :!:

I don't see any reason to mark them as dirty with entity.update();
As you apply the update yourself.

Code: Select all

var di = this.getDocumentInterface();
var document = this.getDocument();

var op = new RModifyObjectsOperation();

var entitiesIds = document.queryAllEntities(false, false);
for (var i=0; i<entitiesIds.length; i++) {
    var entity = document.queryEntity(entitiesIds[i]);

    if (isDimensionEntity(entity)) {
        entity.setLayerName("dim.");
        op.addObject(entity, false)
    }
    else if (isTextEntity(entity)) {
        entity.setLayerName("ret.");
        op.addObject(entity, false)
    }
}   // looping ends
di.applyOperation(op);
Now, document.queryAllEntities(false, false); can be slow, returns all entities but doesn't query in Blocks.
https://www.qcad.org/doc/qcad/latest/de ... 3c488b7f9d

One can query all of a kind by:
document.queryAllEntities(false, false, RS.EntityDimension);
document.queryAllEntities(false, false, RS.EntityText);

Or use a list of types, more here:
https://www.qcad.org/doc/qcad/latest/de ... 2885832771

Also remark that a Dim or Text General Property 'byLayer' would mean 'by new layer'. :wink:
Declaring your variables is best practice (entityId, entity). :wink:

Regards,
CVH

Dacicusan
Active Member
Posts: 49
Joined: Sat Oct 14, 2017 1:44 am

Re: Setting layers of texts and dimensions

Post by Dacicusan » Mon Jan 17, 2022 12:52 pm

How can I use this script?

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

Re: Setting layers of texts and dimensions

Post by CVH » Mon Jan 17, 2022 5:32 pm

Dacicusan,
A finished script was not shared.
Basically this was Q&A about scripting it.

Regards,
CVH

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”