How to get a Layer Id quickly?

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
matthiaswm
Junior Member
Posts: 24
Joined: Mon Apr 29, 2013 4:34 pm

How to get a Layer Id quickly?

Post by matthiaswm » Thu May 23, 2013 11:12 pm

I have the following code inside an RImporter:
// what is the best practice to get the Document Interface inside an RImporter?
var di = EAction.getDocumentInterface();
var di = RMainWindow.getMainWindow().getDocumentInterface();

var lt = document.getLinetypeId("CONTINUOUS");
var clr = new RColor(128,0,0);
var layer = new RLayer(document, "01_Top", false, false, clr, lt);
var operation = new RAddObjectOperation(layer);
di.applyOperation(operation);
//this.layerID[1] = layer.getId();  // <--- This code fails. Why?
this.layerID[1] = document.getLayerId("01_Top"); // <---- This code works, but seems slow
What is the right way to get the Document Interface?

What is the right way to remember the Layer Id for later?

Thanks,

- Matthias

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: How to get a Layer Id quickly?

Post by andrew » Fri May 24, 2013 12:20 am

matthiaswm wrote:What is the right way to get the Document Interface?
The document interface connects a document with user interface elements (graphics scenes, graphics views, mouse events, etc.).

Importers and exporters should generally not make use of the document interface, since they can also be used in a context in which there is no GUI, for example command line tools.

You could use operation.apply(document); instead to apply the operation to the document.
However, inside an importer, it is preferable to call this.importObject(object);. In your code snippet that is: this.importObject(layer);.
importObject adds all objects of the entire import to one transaction which is faster and can be undone in one step (in case you are importing into an existing drawing).
matthiaswm wrote:What is the right way to remember the Layer Id for later?
The second last line of your code snippet is actually correct but fails because of a limitation of the script wrappers.
I've implemented a workaround for this particular problem since this will be quite a common issue. The workaround is available in the latest master of the git repository or the next release (>=3.1.0.3).

For now, I cannot think of anything better than your solution (last line).

matthiaswm
Junior Member
Posts: 24
Joined: Mon Apr 29, 2013 4:34 pm

Re: How to get a Layer Id quickly?

Post by matthiaswm » Fri May 24, 2013 7:16 am

I used this.importObject(object); for a block, but it did not add the block to the Blocks Toolbox. Going the DocumentInterfcae route fixed that. Or is there a call after the import that would update the Layer and Blocks Toolbox?

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: How to get a Layer Id quickly?

Post by andrew » Fri May 24, 2013 8:08 am

Yes, at the end of the import, you can notify all layer listeners, block listeners, etc that data might have changed:
var appWin = EAction.getMainWindow();
if (!isNull(appWin)) {
    appWin.notifyListeners();
}
This is usually done in an action that shows a file dialog, instantiates the importer and calls importFile:
MyImport.prototype.beginEvent = function() {
    var fileName = QFileDialog.getOpenFileName(...);
    ...

    var myImporter = new MyImporter(this.getDocument());
    svgImporter.importFile(fileName);

    // regenerate all entities in all views:
    var di = this.getDocumentInterface();
    if (!isNull(di)) {
        di.regenerateScenes();
    }

    // notify listeners (updates layer list, block list, etc):
    var appWin = EAction.getMainWindow();
    if (!isNull(appWin)) {
        appWin.notifyListeners();
    }
};

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”