Draw: set layer property "IsVisible"

The base is a drawing with several layers. Some are visible, some are printable and some have elements.
The task is, to copy and paste (at different place) the elements of specific layers and leaf the elements of other layers as the are.
As far as I know it is not possible to select the elements of an layer directly. So I will try to set all other layers invisible and select all visible elements.
But how can I set layers invisible? Following code dose nothing at all.

Sub SetLayerNotVisible()

Dim oController As Object : oController = ThisComponent.getCurrentController()
Dim oLayerManager As Object : oLayerManager = ThisComponent.getLayerManager()
Dim myLayer As Object : myLayer = oLayerManager.getByName("myLayerName")
Dim oActiveLayer As Object

oController.setPropertyValue("ActiveLayer", myLayer)
oActiveLayer = oController.ActiveLayer
oActiveLayer.setPropertyValue("IsVisible", False)

End sub

No Action also with this code!

...
code as above
...
oActiveLayer.IsVisible = False
...

The property is not marked as “read only” in MRI. So why doesn’t happen anything?

The interesting thing is, that this lines dose change the properties.

...
code as above
...
oActiveLayer.Title = "MyNewTitle"
oActiveLayer.Name= "MyNewName"
...

or

...
code as above
...
oActiveLayer.setPropertyValue("Title ", "MyNewTitle")
oActiveLayer.setPropertyValue("Name", "MyNewName")
...

Here is my sample file with a custom menu MyDrawMenu (near the Help menu), and three assigned Basic subroutine.

Update: Fixed macro.
DrawLayers.odg (19.5 KB)

Thank you for your sample. Not the sample but the use of the custom menu brought me to the solution.
The properties “IsVisible”, “IsPrintable” and “IsLocked” will be set with your sample as well as with the code I wrote above. But only if the macro is started from the Draw Application via custom menu or menu → Tools-> Macros → Run Macro… or Button_Click or…
If it is started in the Macro Editor App with the green triangle, the properties will not be set.
Is that a bug or a feature?

My macro is written for launching it from a Draw Document. You can modify it, if you want other usage…

That approach will not work. A layer has no information about the shapes. Only the other way round, the object knows on which layer it is.

Travers all objects of the draw page and inspect for each its property LayerID or its LayerName.

Do you want to copy and paste the objects to a different draw page or different document, or do you only want to move them to a different layer?

I want to copy the objects, change one or two of them (a number or a name), and paste them into the same document on the same layer next to the original objects. The objects include a rectangle as a frame object that specifies the width for moving before pasting.
All of this already works in a document with only one layer. The new task is to make this work in a document with multiple layers and objects on layers that must not be copied.

I did that. But in the first try the LayerID and LayerName where for all objects the same (all of the last layer) and totally wrong. But now I got it run with this code:


Sub GetLayerIdAndName()

Dim oController As Object : oController = ThisComponent.getCurrentController()
Dim oPage As Object : oPage = oController.getCurrentPage()
Dim numberOfShapes As integer : numberOfShapes = oPage.getCount()
Dim layerInfo As String
Dim shape As Object

Dim iShape As integer
For iShape = 0 To numberOfShapes - 1
	shape = oPage.getByIndex(iShape)
	layerInfo = layerInfo & iShape & " - "  & shape.LayerID & " - " & shape.LayerName & Chr(10)
Next iShape

MsgBox(layerInfo)

End sub

And I think it will bring me a big step forward. Thank you for your tip.

Can you tag your post as “Macro” ? this is usefull when searching posts

  • Thanks

Sorry, I can’t find a way to set a additional tag to the post, now it is published.
Is it possible and if, how?

If you can see the pencil icon next to the title, you can click it to edit the title and/or tags

Sorry again, there is no pencil icon nor a change of the mouse icon in this area.
I use the latest Firefox version on Win10 without any extensions or filters, if this matter.

screenShot

Why? IMO it will. At least if we add silently “for the selected page”

That’s true - and essential.
However, Draw has a tool to find all the shapes having assigned a specific layer, and the tool is triggered by "MouseOver" the layer’s tab in the view. For visible layers the result is shown by highlighting the found shapes which are placed in the current page.
See also the new attachment to my answer below.

That is implemented in LayerTabBar.cxx (line 167). It loops over all shapes of the page as well, see while in line 191.

There is a (eternal?) terminological mess about layers.

In fact there isn’t a thing like an “element of a layer”, and the Layer as an object (service) is only accessible via the LayerManager of the drawing document.
The graphical objects of a drawing aren’t “assigned”, “moved”, “inserted in”, ... to a layer, but the layer is assigned to the shape in two ways: Integer LayerID and String LayerName.
= = = Edit 2026-02-20 = = =
DrawLayerExperiments.odg (30.0 KB)