How to get a Layer Id quickly?

Discussion forum for contributors and developers who are using the QCAD version 3 ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

How to get a Layer Id quickly?

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

Re: How to get a Layer Id quickly?

Postby 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).
User avatar
andrew
Site Admin
 
Posts: 2522
Joined: Fri Mar 30, 2007 6:07 am

Re: How to get a Layer Id quickly?

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

Re: How to get a Layer Id quickly?

Postby 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();
}
};
User avatar
andrew
Site Admin
 
Posts: 2522
Joined: Fri Mar 30, 2007 6:07 am


Return to QCAD 3 Developers and Contributors

Who is online

Users browsing this forum: No registered users and 2 guests

cron