CreateLayerFromSelection

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
pmakowski
Newbie Member
Posts: 5
Joined: Thu Jun 16, 2016 8:59 am

CreateLayerFromSelection

Post by pmakowski » Thu Jun 16, 2016 9:07 am

Hi,
can you help me to find the correct syntax to use "CreateLayerFromSelection" ?

I'm trying to select all Hatch and then create a layer from this selection

Code: Select all

    
    var entityIds = doc.queryAllEntities();
    doc.clearSelection()
    for (var i = 0; i < entityIds.length; i++) {
        var entityId = entityIds[i];
        var entity = doc.queryEntityDirect(entityId);
        if (isHatchEntity(entity)) {
            doc.selectEntity(entityId, true)
        }
    }
    var op = new CreateLayerFromSelection("MYNEWLAYER");
    di.applyOperation(op);
What is the correct syntax for "CreateLayerFromSelection" , where can I find it ?

Thanks

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

Re: CreateLayerFromSelection

Post by andrew » Thu Jun 16, 2016 10:03 am

I'd recommend to derive your action from class AddLayer (or AddLayerPro to support hierarchical layers). After the new layer has been created you can simply move all hatches to that layer:
var op = new RAddObjectsOperation();
var entityIds = doc.queryAllEntities(false, false, RS.EntityHatch);
for (var i = 0; i < entityIds.length; i++) {
    var entityId = entityIds;
    var entity = doc.queryEntity(entityId);
    if (!isHatchEntity(entity)) {
        continue;
    }
    entity.setLayerId(layer.getId());
    op.addObject(entity);
}
di.applyOperation(op);

pmakowski
Newbie Member
Posts: 5
Joined: Thu Jun 16, 2016 8:59 am

Re: CreateLayerFromSelection

Post by pmakowski » Thu Jun 16, 2016 11:07 am

Thanks

so now my question is "how to create a new layer ?"

Code: Select all

var layer = new ?????
I'm lost in the documentation

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

Re: CreateLayerFromSelection

Post by andrew » Thu Jun 16, 2016 11:35 am

If you derive your action from AddLayer, the layer will be created by that action in beginEvent.

I've checked in a full example for you at:
https://github.com/qcad/qcad/blob/maste ... Hatches.js

If you don't want to use the layer dialog and create a fixed layer instead, you can create a new layer like shown in ExLayerAdd:
https://github.com/qcad/qcad/blob/maste ... ayerAdd.js

pmakowski
Newbie Member
Posts: 5
Joined: Thu Jun 16, 2016 8:59 am

Re: CreateLayerFromSelection

Post by pmakowski » Thu Jun 16, 2016 3:55 pm

Thanks

Seems that I'm still in trouble because I get a Seg fault
/**
 * Add hatch. All entities in drawing are assumed to be part of the hatch boundary.
 * Create a "Vision" layer, set it the only one active.
 * Command line tool.
 */

include("scripts/Tools/arguments.js");
include("scripts/library.js");
include("scripts/Draw/Hatch/Hatch.js");
include("scripts/Pro/Modify/ZeroLengthDetection/ZeroLengthDetection.js")
include("scripts/Select/SelectContour/SelectContour.js")

function printHelp() {
    print("Usage: " + args[1] + " [OPTIONS]... <data file>");
    print();
    print("Add hatch, Create a 'VISION' layer to the given drawing file (DXF, DWG).");
    print();
    print("  -o, -output=FILE        Set CAD output file to FILE");
    print("  -f, -force              Overwrite existing output file");
    print("  -h, -help               Display this help");
    print();
}

function visionOnly(doc,di) {
    doc.clearSelection()
    var operation = new RModifyObjectsOperation();
    var layerIds = doc.queryAllLayers();
    for (var i = 0; i < layerIds.length; i++) {
        var layerId = layerIds;
        var layer = doc.queryLayer(layerId);
        var layerName = layer.getName();
        print(layerName);
        if (layerName == "VISION") {
           layer.setFrozen(false)
        } else {
           layer.setFrozen(true)
        }
        operation.addObject(layer);
    }
    di.applyOperation(operation);
}


function visionCreate(doc, di) {
    var op = new RModifyObjectsOperation();
    var linetypeId = doc.getLinetypeId("CONTINUOUS");
    var layer = new RLayer(doc, "VISION", false, false, new RColor("red"), linetypeId, RLineweight.Weight000);
    op.addObject(layer);
    di.applyOperation(op);
    var layerId = layer.getId();
    var entityIds = doc.queryAllEntities(false, false, RS.EntityHatch);
    for (var i = 0; i < entityIds.length; i++) {
        var entityId = entityIds;
        var entity = doc.queryEntity(entityId);
        if (!isHatchEntity(entity)) {
            continue;
        }
        entity.setLayerId(layerId);
        op.addObject(entity);
    }
    di.applyOperation(op);
}



function main() {
    if (testArgument(args, "-h", "-help")) {
        printHelp();
        return;
    }

    if (args.length < 3) {
        print("No input file given. Try -h for help.");
        return;
    }

    var cadFileName = args[args.length - 1];
    if (isNull(cadFileName) || cadFileName.indexOf("-") === 0) {
        print("No input file. Try -h for help.");
        return;
    }

    var fi = new QFileInfo(cadFileName);
    if (!fi.isAbsolute()) {
        cadFileName = RSettings.getLaunchPath() + QDir.separator + cadFileName;
        fi = new QFileInfo(cadFileName);
    }

    var outFileName = getArgument(args, "-o", "-output");
    if (outFileName!==undefined) {
        if (!new QFileInfo(outFileName).isAbsolute()) {
            outFileName = RSettings.getLaunchPath() + QDir.separator + outFileName;
        }
    }

    if (!isNull(outFileName)) {
        if (new QFileInfo(outFileName).exists() && !testArgument(args, "-f", "-force")) {
            print("Output file exists already, not overwriting: ", outFileName);
            print("Use -f to force overwrite");
            return;
        }
    }

    var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
    var di = new RDocumentInterface(doc);

    if (di.importFile(cadFileName) !== RDocumentInterface.IoErrorNoError) {
        di.destroy();
        qWarning("Cannot import file:", cadFileName);
        return;
    }


    doc.clearSelection()
    if (doc.hasLayer("VISION")) {
        visionOnly(doc,di)
        } else {
        var entityIds = doc.queryAllEntities();
        doc.clearSelection()
        for (var i = 0; i < entityIds.length; i++) {
            var entityId = entityIds;
            var entity = doc.queryEntityDirect(entityId);
            if (isHatchEntity(entity)) {
                entity.setSolid(true);
                doc.selectEntity(entityId, true)
               }
            }

        var ids = entityIds.difference(doc.querySelectedEntities());

        // create hatch data:
        var hatchData = Hatch.createHatchData(doc, ids, true);
        hatchData.setSolid(true);
        hatchData.setScale(1.0);
        hatchData.setAngle(0.0);
        hatchData.setPatternName("SOLID");

        var hatch = new RHatchEntity(doc, hatchData);
        var op = new RAddObjectOperation(hatch);
        di.applyOperation(op);

        visionCreate(doc,di);
        visionOnly(doc,di);
        }

    if (!di.exportFile(outFileName)) {
        di.destroy();
        qWarning("Export to file failed: ", outFileName);
        return;
    }

    di.destroy();
}

main();

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

Re: CreateLayerFromSelection

Post by andrew » Mon Jun 20, 2016 7:58 am

queryEntityDirect should not be used if the entity obtained is then modified. For any kind of modification, create an operation, query the entity, modify the obtained entity clone, add the entity to the operation and then apply the operation.

Post Reply

Return to “QCAD 'How Do I' Questions”