entity color to lineweight

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

entity color to lineweight

Post by dfriasb » Sun Apr 12, 2020 6:01 pm

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
David Frías Barranco | architect
[email protected] | davidfriasarquitecto.es

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: entity color to lineweight

Post by andrew » Tue Apr 14, 2020 9:13 am

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

Post Reply

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