Renaming all layers in a drawing

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules
Always indicate Operating system, QCAD version
Attach Drawing files, Screenshots
Post one question per topic.
Post Reply
User avatar
dfriasb
Senior Member
Posts: 112
Joined: Thu Mar 10, 2016 1:08 pm
Location: Calafell, Tarragona, Spain

Renaming all layers in a drawing

Post by dfriasb » Sat Jan 27, 2018 5:52 pm

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
David Frías Barranco | architect
[email protected] | davidfriasarquitecto.es

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

Re: Renaming all layers in a drawing

Post by andrew » Mon Jan 29, 2018 10:09 am

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.

Post Reply

Return to “QCAD 'How Do I' Questions”