How do I run "Detect Duplicates MD" programatically?

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
daniel_frac
Junior Member
Posts: 10
Joined: Thu Nov 14, 2019 4:15 pm

How do I run "Detect Duplicates MD" programatically?

Post by daniel_frac » Tue Dec 10, 2019 12:22 pm

Dear Support

I'm trying to create a script that cleans DXF files automatically, for our internal process.

Part of our cleaning operations require to delete duplicate entities, I would like to do this automatically on a jvscript but I cannot find any reference of "Detect Duplicates" on https://www.ribbonsoft.com/doc/qcad/3.0/developer/ or in https://github.com/qcad/qcad/tree/v3.23.0.9/scripts

Can you point me out to the right documentation or if this is possible to do in JS?

Best Regards

Daniel Hung

System Information:
OS: Windows 10 Pro 64bit
QCAD Version: QCAD Professional 3.23.0.0 (3.23.0)

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

Re: How do I run "Detect Duplicates MD" programatically?

Post by andrew » Tue Dec 10, 2019 1:25 pm

It's part of QCAD Professional.

You can invoke it like this:

Code: Select all

var cadFileName = "in.dxf";
var outFileName = "out.dxf";
var tolerance = 0.0001;

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

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

Duplicates.findDuplicates(di, true, tolerance, 0.0, true);

// delete duplicates:
var counter = doc.countSelectedEntities();
var op = new RDeleteSelectionOperation();
di.applyOperation(op);

if (!di.exportFile(outFileName, "DXF R27")) {
    qWarning("Export to file failed: ", outFileName);
}
else {
    print("Deleted: " + counter);
}

di.destroy();
The function signature is:

Code: Select all

/**
 * Find duplicate entities.
 *
 * \param di RDocumentInterface
 * \param allLayers True: compare entities across different layers
 * \param distanceTolerance Tolerance (maximum distance between two points to be regarded as identical)
 * \param angleTolerance Angle tolerance (maximum angle between two lines to be regarded as identical)
 * \param reversedIsSame True: ignore direction of entities when comparing
 */
Duplicates.findDuplicates = function(di, allLayers, distanceTolerance, angleTolerance, reversedIsSame) { ... }

daniel_frac
Junior Member
Posts: 10
Joined: Thu Nov 14, 2019 4:15 pm

Re: How do I run "Detect Duplicates MD" programatically?

Post by daniel_frac » Tue Dec 17, 2019 3:01 pm

Which module library Duplicates is?

in my script I have

include("scripts/library.js");
include("scripts/simple.js");

and I get an error when running:

Duplicates.findDuplicates(di, true, 0.001, 0.001, true);

Warning: RScriptHandlerEcma::eval: script engine exception: "ReferenceError: Can't find variable: Duplicates"
Warning: "main() at C:\\qcadModules\\main.js:51\n<global>() at C:\\qcadModules\\main.js:63"
Warning: At least one uncaught exception:
Warning: "main() at C:\\qcadModules\\main.js:51\n<global>() at C:\\qcadModules\\main.js:63"
13:58:09: Debug: "<global>() at 51"

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

Re: How do I run "Detect Duplicates MD" programatically?

Post by andrew » Tue Dec 17, 2019 4:44 pm

You can find a list of all scripts and their location under Help > About > Scripts.

You may want to copy that list and paste it into a text editor to make it searchable.

Duplicates scripts/Pro/Modify/Detection/Duplicates

This entry means that you you have to include scripts/Pro/Modify/Detection/Duplicates/Duplicates.js

Post Reply

Return to “QCAD 'How Do I' Questions”