Nesting API

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

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

Nesting API

Post by andrew » Mon Apr 20, 2020 10:04 am

From a QCAD/CAM user:
Can I use the nesting functionality of QCAD/CAM from a script?

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

Re: Nesting API

Post by andrew » Mon Apr 20, 2020 10:12 am

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

fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Mon Jan 25, 2021 9:44 am

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

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

Re: Nesting API

Post by andrew » Mon Jan 25, 2021 11:12 am

fab wrote:
Mon Jan 25, 2021 9:44 am
where can i find the complete documentataion from the Nesting? Espacially RNester?
Nesting is not part of the public QCAD API.
fab wrote:
Mon Jan 25, 2021 9:44 am
Can i also download the source code for QCAD CAM like for the QCAD project from github?
No, the nesting plugin is proprietary.
fab wrote:
Mon Jan 25, 2021 9:44 am
Where can i find "the self-contained example script"?
Right above (green code part).

fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Mon Jan 25, 2021 3:37 pm

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:

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);
...
Is there an way to driectly convert the dxf-file to an RPolyline?

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

Re: Nesting API

Post by andrew » Mon Jan 25, 2021 9:56 pm

That depends on the contents of your DXF file.

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
    }

fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Wed Jan 27, 2021 2:50 pm

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.

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


fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Thu Jan 28, 2021 10:04 am

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

...

fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Wed Apr 14, 2021 2:58 pm

Hi Andrew,

the command:

Code: Select all

RNester.setAlignment(RNester.LeftBottom);
has no impact of my nester, respectively with

Code: Select all

RNester.setAlignment(RNester.LeftBottom);
RNester.setAlignment(RNester.RightTop);
RNester.setAlignment(3);
doesn´t change anything.

When i change the alignment without a script, it works without a problem.

Tanks and kind regards

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

Re: Nesting API

Post by andrew » Thu Apr 15, 2021 6:41 am

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.

fab
Newbie Member
Posts: 8
Joined: Mon Jan 25, 2021 9:23 am

Re: Nesting API

Post by fab » Thu Apr 29, 2021 7:32 am

Thank you very much.
I forgot to set the strategy to "align"

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”