HowTo .setCurrentLayer() without an extra undo/redo step?

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
CVH
Premier Member
Posts: 3365
Joined: Wed Sep 27, 2017 4:17 pm

HowTo .setCurrentLayer() without an extra undo/redo step?

Post by CVH » Sun Mar 21, 2021 12:17 pm

Andrew,

Similar as in AddLayer.js is setting the current layer to the newly added an extra operation.

Adding a new layer to the QCAD Layer List requires two undo steps to fully undo.
The first undo step is called [Change Document Settings]
The second step is [Add Layer YA]

Snippet AddLayer.js:

Code: Select all

AddLayer.prototype.beginEvent = function() {
...
..
.
    var di = this.getDocumentInterface();
    di.setCurrentLayer(this.layer);

    // TODO: make new item the active, selected item

    this.terminate();
};
In the DrawFromCSV script new entities are casted with one global new RAddObjectsOperation()
But the layers for them are casted on the fly under the same setTransactionGroup(this.doc.getTransactionGroup())

On the fly because these layers must exist before initiating any new document entity for them.

The document is stored in 'this.doc' ... The document interface in 'this.di' ...
And the current layer at script level is stored in 'this.currentLayerName'.

Among the very few I found this example what does About the trick :

Code: Select all

    var layerOp = new RAddObjectOperation();
    layerOp.setText(qsTr("Cast CSV"));    // translated tool title
    layerOp.setTransactionGroup(this.doc.getTransactionGroup());
    
    var docVars = this.doc.queryDocumentVariables();
    docVars.setCurrentLayerId(this.doc.getLayerId(this.currentLayerName));
    layerOp.addObject(docVars);
    this.di.applyOperation(layerOp);
About, except that the Layer List isn't updated instantly :!:
  • It gets updated when swapping to another drawing tab and back. :?
    Or
    After 'Undo' and then 'Redo'. :shock:
Synchronizing the document current layer with that at script level is one of the last things I do before terminating the command script.

Maybe the only thing that is missing is refreshing the Layer List itself ... :oops:

That solved, I can update DrawFromCSV with Layers / Colors / Lineweights / Linetypes /.... :P and much more.
This also uses a method what is the solution for the FlexPainter group casting BUG. 8)

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”