Macros/Draw/001

Context
We want to be able to show or hide objects by clicking a button.

The below screenshot shows an example where clicking on the buttons "Squares" or "Circles" shows or hides the respective objects.



Realization
LibreOffice Draw permits the defining of layers in a given draw page. The use of layers enable us to access objects exclusive to some particular layer. Think of layers like different available workspaces which you can handle individually, such as their visibility or whether they should be locked or left out when printing, etc. By default, Draw proposes three layers (which can be seen at the bottom toolbar of the interface):
 * Layout
 * Controls
 * Dimension Lines

We will create two more layers manually to be able to handle objects by layer:
 * The squares will be created in the layer called Squares.
 * The circles will be created in the layer called Circles.

Every layer has a Boolean property called IsVisible. The below macro toggles this property between True and False.

A unique macro is actuated by a the event Execute action for each button. The variable oEvent is passed as an argument to the macro which runs when button is clicked. This can be set by creating a Push Button, and right-click and select Control Properties, and finally in Events you can select a macro for the Execute action item.

From this we can trace the Label property of the button and find loop through the layer to see if we find an object of the same name. Another option is to use the Tag property to store the name of the layer in question.

The code also forsees a possible error by verifying that the document really has a layer by that name, by investigating the hasByName property.

Code
In LibreOffice Basic:

Example ODG file

 * [[Media:Example macro for manipulating object properties by layer.odg|Download example ODG file with this macro included]]