Page 1 of 1

Renaming all layers in a drawing

Posted: Sat Jan 27, 2018 5:52 pm
by dfriasb
Hello all,

I'm trying to write a script in order to rename all layers of a file, adding "z ... " at the beginning of their current name, so they'll be all inside a new superlayer called "z".

My script looks like this:

Code: Select all

var di = this.getDocumentInterface();
var document = this.getDocument();
var layerIds = document.queryAllLayers();

var op = new RModifyObjectsOperation();

for (var i=0; i<layerIds.length; i++)   {
	var layerId = layerIds[i];
	var layer = document.queryLayer(layerId);
	var prevLayerName = layer.getName();

//		void RLayer::setName 	( 	const QString &  	n	) 	

	layer.setName(new ("z ... " + prevLayerName));
    	op.addObject(layer);
					};
di.applyOperation(op);
this.terminate();
It's not working right now, but no error message found. Anyone has any idea of what is happening?
I was thinking I need to "update" layers or something similar...?

Thank you! Best regards,

David

Re: Renaming all layers in a drawing

Posted: Mon Jan 29, 2018 10:09 am
by andrew

Code: Select all

layer.setName(new ("z ... " + prevLayerName));
throws an exception:

Code: Select all

"TypeError: Result of expression '(\"z ... \" + prevLayerName)' [z ... abc] is not a constructor."
Start QCAD from a terminal to see exceptions or start QCAD with -enable-script-debugger to pop up the debugger when a script exception is thrown.

Change to:

Code: Select all

layer.setName("z ... " + prevLayerName);
or better:

Code: Select all

layer.setName("z" + RLayer.getHierarchySeparator() + prevLayerName);
BTW: you also need to create layer "z" as parent layer to show the layers as child layers of z.