CreateLayerFromSelection

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]… ”);
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();_