Modify text and save result from script

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
ouilogique
Active Member
Posts: 39
Joined: Sun Feb 21, 2021 2:43 pm

Modify text and save result from script

Post by ouilogique » Tue Oct 19, 2021 4:15 pm

System: Ubuntu 18 & macOS Big Sur
QCAD: 3.26.2.0 (3.26.2)

I would like to create a script that reads an existing drawing, modifies the content of two text fields and saves the updated result in DXF format.

Is this possible?

CVH
Premier Member
Posts: 3365
Joined: Wed Sep 27, 2017 4:17 pm

Re: Modify text and save result from script

Post by CVH » Tue Oct 19, 2021 5:20 pm

Hi,

Sure that is possible.
There is also Find/Replace under the Edit but that is not automated.

What to look for and with what to replace?

Regards,
CVH

ouilogique
Active Member
Posts: 39
Joined: Sun Feb 21, 2021 2:43 pm

Re: Modify text and save result from script

Post by ouilogique » Wed Oct 20, 2021 3:54 pm

OK, nice to know that it is possible. What I want to replace is the content of some text fields. I have 5 of them and I need to change their contents and save a copy of the file.

I join three files as examples. The file “text-replace.dxf” is a simplified copy of my drawing with 5 text fields. The file “TextReplace.js” is a skeleton of the script I want. The file “PlateValues.js” contains the texts that I want to display.

What I don’t know is how to select the text fields to change their contents and how to save a copy of the file with the updated text fields.
Attachments
TextReplace.js
(921 Bytes) Downloaded 370 times
text-replace.dxf
(100.18 KiB) Downloaded 347 times
PlateValues.js
(327 Bytes) Downloaded 362 times

CVH
Premier Member
Posts: 3365
Joined: Wed Sep 27, 2017 4:17 pm

Re: Modify text and save result from script

Post by CVH » Thu Oct 21, 2021 9:01 am

Hi,

"skeleton" is the right word choice ... :wink:
I am going to ask to move this topic to the 'QCAD Programming, Script Programming and Contributing' forum. :wink:

You definitely need some validated paths: These should be absolute paths.

Code: Select all

var source = "/Users/nico/Desktop/PlateValues.js";
var fi = new QFileInfo(source);
if(fi.exists()) {
    include(source);
}
else {
    EAction.handleUserWarning(qsTr("Can't find text replace source."));
    return;    // Failed
}

var drawingSource = "/Users/nico/Desktop";
var dir = new QDir(drawingSource);
if(!dir.exists()) {
    EAction.handleUserWarning(qsTr("Can't locate drawing folder."));
    return;    // Failed
}
You only require the qsTr( ... ) if you are going to create an Addon or so and provided in translations. :wink:

At this point there should be data in a variable called 'plateValues'. Remark the lower case for variables.

Code: Select all

if(isNull(plateValues) || plateValues.length < 1) {
    EAction.handleUserWarning(qsTr("Nothing to replace."));
    return;    // Missing or empty plateValues
}
You definitely need 3 nested loops: i) drawings, j=prop) text to replace and k) for cycling all text entities.
It is a bit more complex there we need to look for the plateValues[..] object property names ...

Code: Select all

var i, k, iMax, kMax;
iMax = plateValues.length;

for (i=0; i<iMax; i++) {
    var drawing = drawingSource + QDir.separator + plateValues[i].file_name;
    // import the drawing file to a new off-screen document
    // skipping with warning
    // query for a list of drawing text entities IDs
    // start a modification operation

    for (var prop in plateValues[i]) {
        var searchText = prop.toUpperCase();
        if (searchText === "FILE_NAME") continue;    // Skip file name property
        var newText = plateValues[i][prop];
         
        for (k=0; k<kMax; k++) {
            // Verifying each text entity its plain text and replacing text, adding an operation
        }
    }
    // apply all the operations
    // overwriting the drawing
    // destroy the off-screen document
}
How to handle documents can be sourced from this example (line 55 and on):
https://github.com/qcad/qcad/blob/maste ... /ExTool.js
All the QCAD open source script files can be found there too. :wink:

With a document (and document interface) one can list up text entities id's with:
var ids = doc.queryAllEntities(false, false, RS.EntityText); ... undone=false, allBlock=false
kMax = ids.length;

Retrieving a single text entity and its plain text in upper case:
var entity = doc.queryEntity(ids[k]);
var textU = entity.getPlainText().toUpperCase();

Setting new text: entity.setText(newText);

The modifying operation: var op = new RModifyObjectsOperation();
Adding op's: op.addObject(entity, false, false); ... useCurrentAttributes=false, forceNew=false
Applying all op's: di.applyOperation(op);

I think this covers most of your needs.
Ensure that you have a drawing, a text entity, that texts are valid and so on.
Handle exceptions.

Please share what is functional (or not functional).
You can reach me by PM too.

Regards,
CVH

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Modify text and save result from script

Post by Husky » Thu Oct 21, 2021 9:10 pm

Topic moved to "QCAD Programming, Script Programming and Contributing".
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

ouilogique
Active Member
Posts: 39
Joined: Sun Feb 21, 2021 2:43 pm

Re: Modify text and save result from script

Post by ouilogique » Sun Nov 21, 2021 7:33 pm

A very big thank you to you CVH.

I originally asked this question because I am in charge of designing the manufacturer’s plate for a machine called ARA¹. This plate has text fields that are common to all machines in the same series and other text fields that are unique to a given machine. The idea was to generate a DXF file for each plate, and send it to the plate manufacturer.

Today we have chosen to make only one DXF file per series and to use punch letters to engrave the texts that are unique. So I won’t be using the script for production. But since it can be very useful for other projects, I decided to make a prototype anyway and post my files here. This way you will also know that you didn’t waste your time answering my question.

¹ https://www.ecorobotix.com/en/ara_mounted_sprayer/
Attachments
text_replace.zip
(379.64 KiB) Downloaded 324 times

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”