I am trying to change some of the layer names with Ruby. I wrote some code like:
layout_info = layout.get_info( found_layer )
if layout_info.layer == 10
layout_info.name = "DIFF"
Put I get an error Ruby error: 'Cannot call non-const method name=, class LayerInfo on a const reference'
This makes sense once I check the documentation, as layout.get_info does return a const. I was wondering if there is a good way of changing an existing layer name from ruby without having to first delete the layer and then add a new layer.
"layout_info" is a reference into the internal object (that's because in general, one tries to avoid creating copies).
To change the properties, you have to create a duplicate first (note the "dup"):
I admit, it does not make much sense keeping the reference in this case. From the performance point of view there is no reason to do so.
This is not a major issue but I thought I'd point it out.
I hope to use your code above to strip all layer names out. Just keep the layer number and datatype number and remove the string.
The following script works. However the "" string is not updated in the panel on the right. i.e. if it had a name before, it still has a string name in that panel.
I know the script is working because if I select a layer and choose Edit > Layer > Edit layer specification, the string is gone. Or if I save and re-open, the string is gone. But the change is not reflected on the layer list immediately after running the script. Is there a command to update that panel?
you're right. It's true that the list is not updated whenever you change something on the layer name, not just resetting the layer name.
The reason is a bit difficult to explain - basically the layer list entry is a "view" which means it's supposed to specify what is shown and how it's shown. So it's not reflecting the database, it's independent of it. For example, if a layer list entry say's "I'm showing layer 1, datatype 0", you can change the layer's number in the database to 2. The effect won't be that the view changes but simply nothing will be shown any more: the view still looks for layer 1, datatype 0, but it's gone now.
The same happens for the layer name. If you assign a different name, the view will still say "I'm looking for layer ABC". If there is a layer/datatype specification too, it will say "I'm looking for layer 17, datatype 0 and if there is no such layer I'll try to layer ABC.". Changing the name won't make it look elsewhere, but since layer 17, datatype 0 is still there, it knows what to display.
So there is no function call to update the view?
When I update the name using the menus Edit -> Layer -> Edit Layer Specification, the view gets updated on name change.
there is a function, but views and database layers are not synchronized automatically. You need to update both. This is essentially what the "edit layer properties" function does.
Here is some code: