Page 1 of 1

How to get a Layer Id quickly?

Posted: Thu May 23, 2013 11:12 pm
by matthiaswm
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

Re: How to get a Layer Id quickly?

Posted: Fri May 24, 2013 12:20 am
by andrew
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).

Re: How to get a Layer Id quickly?

Posted: Fri May 24, 2013 7:16 am
by matthiaswm
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?

Re: How to get a Layer Id quickly?

Posted: Fri May 24, 2013 8:08 am
by andrew
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();
    }
};