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?
Modify text and save result from script
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.
-
- Active Member
- Posts: 39
- Joined: Sun Feb 21, 2021 2:43 pm
Re: Modify text and save result from script
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
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
-
- Active Member
- Posts: 39
- Joined: Sun Feb 21, 2021 2:43 pm
Re: Modify text and save result from script
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.
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 377 times
-
- text-replace.dxf
- (100.18 KiB) Downloaded 352 times
-
- PlateValues.js
- (327 Bytes) Downloaded 368 times
Re: Modify text and save result from script
Hi,
"skeleton" is the right word choice ...
I am going to ask to move this topic to the 'QCAD Programming, Script Programming and Contributing' forum.
You definitely need some validated paths: These should be absolute paths.
You only require the qsTr( ... ) if you are going to create an Addon or so and provided in translations.
At this point there should be data in a variable called 'plateValues'. Remark the lower case for variables.
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 ...
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.
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
"skeleton" is the right word choice ...
I am going to ask to move this topic to the 'QCAD Programming, Script Programming and Contributing' forum.
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
}
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
}
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
}
https://github.com/qcad/qcad/blob/maste ... /ExTool.js
All the QCAD open source script files can be found there too.
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
Re: Modify text and save result from script
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
-
- Active Member
- Posts: 39
- Joined: Sun Feb 21, 2021 2:43 pm
Re: Modify text and save result from script
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/
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 330 times