Page 1 of 1

entity color to lineweight

Posted: Sun Apr 12, 2020 6:01 pm
by dfriasb
Hello all,

I'm writing a script in order to set lineweight of all entities that have specific color. I started with "Dark Green" entities; they should be converted to 0.13mm weight:

Code: Select all

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

var entities = document.queryAllEntities();
var op = new RModifyObjectsOperation();

for (i=0; i<entities.lentgh; i++) {

var entityId = entities[i];
var entity = document.queryEntity(entityId);
var entityColor = entity.getColor();
var entityColorName = entityColor.getName();  // OK til here

if (entityColorName === "Dark Green") {
// set lineweight to 0.13 mm.
entity.setLineweight(-1);

di.applyOperation(op);      } }  // NOT WORKING.
Something is not working in QCAD interface, even in Shell entity lineweight is changed. I tested with

Code: Select all

entity.getLineweight()
Any help will be appreciated. Regards,

David

Re: entity color to lineweight

Posted: Tue Apr 14, 2020 9:13 am
by andrew
Some issues:
- entities.length is misspelled (undefined is casted to 0 in JS)
- the modified entity is never actually added to the operation (op.addOperation)
- you'd want the applyOperation after the for-loop

Code: Select all

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

var entities = document.queryAllEntities();
var op = new RModifyObjectsOperation();

for (i=0; i<entities.length; i++) {
    var entityId = entities[i];
    var entity = document.queryEntity(entityId);
    var entityColor = entity.getColor();
    var entityColorName = entityColor.name();  // Use name (Qt API, always english), not getName (localised)

    if (entityColorName === "#008000") {  // name / code returned by QColor::name() for dark green 
        // set lineweight to ByLayer (?)
        entity.setLineweight(RLineweight.WeightByLayer);
    }
    op.addObject(entity, false);

}

di.applyOperation(op);