Page 1 of 1

Setting layers of texts and dimensions

Posted: Sun Mar 14, 2021 9:23 pm
by dfriasb
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

Re: Setting layers of texts and dimensions

Posted: Mon Mar 15, 2021 5:48 am
by CVH
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

Re: Setting layers of texts and dimensions

Posted: Mon Jan 17, 2022 12:52 pm
by Dacicusan
How can I use this script?

Re: Setting layers of texts and dimensions

Posted: Mon Jan 17, 2022 5:32 pm
by CVH
Dacicusan,
A finished script was not shared.
Basically this was Q&A about scripting it.

Regards,
CVH