[solved] Change document properties 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
jisaletter
Registered Member
Posts: 2
Joined: Sun Dec 01, 2024 4:25 am

[solved] Change document properties from script

Post by jisaletter » Sun Dec 01, 2024 5:45 am

Hello,

I would like to change some document properties from within a script. Specifically the dimension size scale factor and linear dimension precision.

It seems that the way to do this is using the get/setKnownProperty methods of the RDocument class. I can get the current value of these properties using getKnownProperty with the RS.DIMDEC and RS.DIMSCALE keys. I can then set them using setKnownProperty and get the updated values using getKnownProperty again - so far so good. However if I export the document and open it in qcad it seems nothing has changed!

I am on linux and qcad version 3.29.4.0 (3.29.4) CE

The following code demonstrates my problem. I put it into 'test.js' and run the script on the command line using:

Code: Select all

qcad -no-gui -allow-multiple-instances -autostart test.js
Any help would be much appreciated.

Code: Select all

const doc = new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
const di = new RDocumentInterface(doc);

// export to test1.dxf before attempting to change document properties
di.exportFile("/tmp/test1.dxf");

// first let's verify that the enums are evaluating to numbers:
qDebug(RS.DIMDEC);
qDebug(RS.DIMSCALE);

// then let's see what the current knownVariable states are:
qDebug(doc.getKnownVariable(RS.DIMDEC));
qDebug(doc.getKnownVariable(RS.DIMSCALE));

// verify that these are the same values as the document properties in
// '/tmp/test1.dxf' via Edit -> Drawing Preferences -> Dimension Settings
// (on my setup they are:
//  linear precision: 0.0000 (4) - YES same as above
//  size scale factor: 1 - YES same as above
// )

// now let's set the 2 document properties we just viewed:
doc.setKnownVariable(RS.DIMDEC, 1);
doc.setKnownVariable(RS.DIMSCALE, 300);

// output the property values again... SUCCESS they have changed!
qDebug(doc.getKnownVariable(RS.DIMDEC));
qDebug(doc.getKnownVariable(RS.DIMSCALE));

// export to test2.dxf to verify changed document properties in qcad:
di.exportFile("/tmp/test2.dxf");

// verify that the document property values are changed in
// '/tmp/test2.dxf' via Edit -> Drawing Preferences -> Dimension Settings

// unfortunately no change from test1.dxf!
PS. some of the things I've tried with no success.

1. wrap the setKnownVariable calls in start/endTransaction.

The only demonstration of using setKnownVariable that I can find is https://qcad.io/rsforum/viewtopic.php?t=9153
Here the poster wraps the setKnownVariable calls in 'startTransaction(di) ... endTransaction()'. I can add these calls (and add include('simple.js') and copy simple*.js from the git repo scripts folder) but the changes still are not reflected in the latter exported dxf file.

2. use RDocumentVariables class

Code: Select all

const doc = new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
const dvs = new RDocumentVariables(doc);

// these both give 'undefined':
qDebug(dvs.getKnownVariable(RS.DIMDEC));
qDebug(dvs.getKnownVariable(RS.DIMSCALE));
Last edited by jisaletter on Sun Dec 01, 2024 11:31 am, edited 1 time in total.

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

Re: Change document properties from script

Post by CVH » Sun Dec 01, 2024 9:00 am

Hi, and welcome to the QCAD forum.

I think that your problem is the enhancement to support dimension styles.
In DimensionSettings.js we can still find the traces of using get- setKnownVariable under DimensionSettings.savePreferences
Remark that the instructions of interest all use the provided transaction.
None given would mean that such is created on the fly but is that transaction executed?
See reference: RDocument::setKnownVariable

Typically we have to cast things back to the document when altered or when newly created.


The new syntax would then be:

Code: Select all

    // With a RDocument = doc and a RDocumentInterface = di
    // Get a copy of the current state:
    var dimStyle = doc.queryDimStyle();
 
    // Apply changes:
    dimStyle.setInt(RS.DIMDEC, 1);
    dimStyle.setDouble(RS.DIMSCALE, 300);
    
    // Create an operation and store the new state:
    var op = new RAddObjectsOperation();
    op.addObject(dimStyle);
    di.applyOperation(op);
Setting DIMSCALE is not literally included in DimensionSettings.js (part of DimensionSettings.dimx).
I took the example that we can find in ConvertUnit.js
There you can also see that setting the drawing unit is by docVars.

Remark that it is common to use var for declaring variables when coding under QCAD.

ODDITY :!: :
Styles were added on Sep 1, 2021 (commit) and later.
You will NOT find the queryDimStyle() method listed in the RDocument Class Reference
... https://github.com/qcad/qcad/blob/maste ... ent.h#L190
... https://github.com/qcad/qcad/blob/maste ... .cpp#L2191
All because the reference was generated on Thu Apr 15 2021, approximately 43 months ago. :roll:

It is not the first time that I encounter that the main QCAD reference is not up to date. :wink:

Regards,
CVH

jisaletter
Registered Member
Posts: 2
Joined: Sun Dec 01, 2024 4:25 am

Re: Change document properties from script

Post by jisaletter » Sun Dec 01, 2024 11:29 am

Thanks CVH for your prompt and informative reply!

Using the approach you suggested seems to have addressed my problem.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”