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