Can I use the nesting functionality of QCAD/CAM from a script?
Nesting API
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Nesting API
From a QCAD/CAM user:
Re: Nesting API
Nesting is not part of the public API. Nevertheless it's possible to use nesting from a script as shown in this self-contained example script:
Code: Select all
// global settings:
RSettings.setValue("Nesting/Threads", "auto"); // use as many threads as there are CPU cores
RSettings.setValue("Nesting/Iterations", 10); // more iterations means longer execution times and generally better results
RSettings.setValue("Nesting/AlignAngles", true); // align parts with longest side to X axis
// load document and select all entities for nesting:
var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
di.importFile("/path/to/input_file.dxf");
// select all entities as parts for nesting:
di.selectAll();
// init nester:
var nester = new RNester();
nester.setMargin(1); // margin between parts
nester.setSheetMargin(5); // margin between sheet and parts
nester.setRotations(4); // possible rotations per part, more can mean better results for non-rectangular parts
nester.setTolerance(0.001);
RNester.setDocumentInterface(di);
RNester.setAlignment(RNester.LeftBottom);
// create and add sheet:
var vertices = [
new RVector(0,0),
new RVector(100,0),
new RVector(100,100),
new RVector(0,100)
];
var sheet = new RPolyline(vertices, true);
nester.addSheet(sheet);
// add parts (all selected entities):
nester.addPartsFromSelection(doc);
// start nesting:
nester.start();
// apply the best result:
var op = new RModifyObjectsOperation();
nester.applyBestSolution(op);
di.applyOperation(op);
// save file under new name:
di.exportFile("/path/to/output_file.dxf");
Re: Nesting API
hi,
where can i find the complete documentataion from the Nesting? Espacially RNester?
Can i also download the source code for QCAD CAM like for the QCAD project from github?
Where can i find "the self-contained example script"?
thanks
where can i find the complete documentataion from the Nesting? Espacially RNester?
Can i also download the source code for QCAD CAM like for the QCAD project from github?
Where can i find "the self-contained example script"?
thanks
Re: Nesting API
Nesting is not part of the public QCAD API.
No, the nesting plugin is proprietary.
Right above (green code part).
Re: Nesting API
thanks for the fast response.
I want to use for the nesting sheet a RPolyline which comes from an dxf-file. How can i import it and convert it to an Rpolyline for using it?
I actually have:
Is there an way to driectly convert the dxf-file to an RPolyline?
I want to use for the nesting sheet a RPolyline which comes from an dxf-file. How can i import it and convert it to an Rpolyline for using it?
I actually have:
Code: Select all
...
var doc_1 = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di_1 = new RDocumentInterface(doc_1);
di_1.importFile(".....path_to.dxf");
di_1.selectAll();
?????
var pl = new RPolylineEntity(doc_1, new RPolylineData());
var sheet1= pl.getPolylineShape();
nester.addSheet(sheet1);
...
Re: Nesting API
That depends on the contents of your DXF file.
Assuming the file contains at least one polyline entity:
Assuming the file contains at least one polyline entity:
Code: Select all
var ids = doc.queryAllEntities();
for (var i=0; i<ids.length; i++) {
var id = ids[i];
var pl = doc.queryEntity(id);
if (!isPolylineEntity(pl)) {
continue;
}
// found a polyline:
// pl is of type RPolylineEntity
}
Re: Nesting API
Thanks andrew,
i still have a problem:
I have several polylines from the DXF file and want to add them to the nester as a sheet. This works fine when I do only one or one after the other.
For example in a loop. The Problem is, that some polylines can be an island in another.
How can i add them to the nester that it is clear there are island/ holes? When i do it manually in QCAD CAM it is no problem and it is recognized that it is an island.
i still have a problem:
I have several polylines from the DXF file and want to add them to the nester as a sheet. This works fine when I do only one or one after the other.
For example in a loop. The Problem is, that some polylines can be an island in another.
How can i add them to the nester that it is clear there are island/ holes? When i do it manually in QCAD CAM it is no problem and it is recognized that it is an island.
Code: Select all
var doc_1 = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di_1 = new RDocumentInterface(doc_1);
di_1.importFile(".....path_to.dxf");
di_1.selectAll();
var ids = doc_1.queryAllEntities();
var sheets = [];
for (var i=0; i<ids.length; i++) {
var id = ids[i];
var pl = doc_1.queryEntity(id);
if (!isPolylineEntity(pl)) {
continue;
}
sheets [i] = pl.getPolylineShape();
}
nester.addSheet(sheets[0]); // only one or in a loop
Re: Nesting API
i found a solution:
Code: Select all
...
var doc_1 = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di_1 = new RDocumentInterface(doc_1);
di_1.importFile(".....path_to.dxf");
di_1.selectAll();
var ids = doc_1.queryAllEntities();
nester.addSheetsFromCandidates(doc_1,ids);
...
Re: Nesting API
Hi Andrew,
the command:
has no impact of my nester, respectively with
doesn´t change anything.
When i change the alignment without a script, it works without a problem.
Tanks and kind regards
the command:
Code: Select all
RNester.setAlignment(RNester.LeftBottom);
Code: Select all
RNester.setAlignment(RNester.LeftBottom);
RNester.setAlignment(RNester.RightTop);
RNester.setAlignment(3);
When i change the alignment without a script, it works without a problem.
Tanks and kind regards
Re: Nesting API
Note that the alignment only has an effect for the align nest strategy (RNester.Align), not for the hull strategy.
QCAD/CAM uses the same API call when the user sets the alignment. This API should work. Perhaps, you can provide more context.
QCAD/CAM uses the same API call when the user sets the alignment. This API should work. Perhaps, you can provide more context.
Re: Nesting API
Thank you very much.
I forgot to set the strategy to "align"
I forgot to set the strategy to "align"