Basic macro to insert a custom shape into a page ?

Hi all,

does someone has a basic macro snippet for to insert a custom shape (e.g. a smiley) into a page?

Background: For a unit test I need to insert a custom shape via code similar to as it is done via mouse. But I have no idea how to code it. A macro might give me an initial stage for a solution.

Kind regards
Regina

For Calc, for example:

Sub Main
Dim size As New com.sun.star.awt.Size
Dim args(0) As New com.sun.star.beans.PropertyValue

    doc = ThisComponent
    sheet = doc.getCurrentController.getActiveSheet()
    dp = sheet.getDrawPage()
  
    shape =
doc.createInstance("com.sun.star.drawing.CustomShape")
    dp.add(shape)

    size.Width = 5000
    size.Height = 5000
    shape.setSize(size)
  
    args(0).Name = "Type"
    args(0).Value = "smiley"
    shape.setPropertyValue("CustomShapeGeometry", args)
  
End Sub

This might seem obvious, but:

1. What document type?

2. What is a "custom" shape? Do you mean com.sun.star.drawing.CustomShape, or do you mean a Custom Shape that you have added to a Library?

Not that I have never used Cutom Shapes in any way in LibreOffice, but i have written a Macro at least once. :slight_smile:

I have written code that adds things such as lines (and other shapes) into documents.

I might have looked at something in LO that allows me to have a bunch of shapes. On my computer, I use

View > Gallery

Too open it. From there it looks like I can drag objects out of the "gallery" and onto a document.

I had forgotten you can do that in LO. Is this the type of graphic that you want to insert? Not that I know how, but I am trying to clarify in case it is something that I do know how to do.

This might seem obvious, but:

1. What document type?

Calc

2. What is a "custom" shape? Do you mean com.sun.star.drawing.CustomShape

Yes

Not that I have never used Cutom Shapes in any way in LibreOffice, but i have written a Macro at least once. :slight_smile:

I have written code that adds things such as lines (and other shapes) into documents.

I might have looked at something in LO that allows me to have a bunch of shapes. On my computer, I use

View > Gallery

Too open it. From there it looks like I can drag objects out of the "gallery" and onto a document.

That makes a kind of copy&paste. I need an object newly created with the mouse.

When you click on a shape icon in the toolbar and then click and drag with the mouse to create the shape, then the created custom shape is on a wrong layer. You notice in the "Drawing object properties"-toolbar, that both "To Foreground" and "To Background" are enabled. "To Foreground" should not be enabled, because when you create a custom shape with the mouse, you are working already in the "Foreground". I have a patch for it, but struggle with the unit test.

Kind regards
Regina

Hi Mauricio,

Hi all,

does someone has a basic macro snippet for to insert a custom shape
(e.g. a smiley) into a page?

Background: For a unit test I need to insert a custom shape via code
similar to as it is done via mouse. But I have no idea how to code
it. A
macro might give me an initial stage for a solution.

Kind regards
Regina

For Calc, for example:

Sub Main
Dim size As New com.sun.star.awt.Size
Dim args(0) As New com.sun.star.beans.PropertyValue

     doc = ThisComponent
     sheet = doc.getCurrentController.getActiveSheet()
     dp = sheet.getDrawPage()
  
     shape =
doc.createInstance("com.sun.star.drawing.CustomShape")
     dp.add(shape)

     size.Width = 5000
     size.Height = 5000
     shape.setSize(size)
  
     args(0).Name = "Type"
     args(0).Value = "smiley"
     shape.setPropertyValue("CustomShapeGeometry", args)
  
End Sub

Aha! That means, that Size and Type are set after the object is added to the draw page.

Unfortunately using this macro, does not show the error, which I'm going to fix. Nevertheless I will test in code, whether creating with a ctor with new ... (which corresponds to createInstance) and then applying the properties will work. But I will need some time for that.

Kind regards
Regina

So my guess that you were using the gallery was wrong. On the bright side, I did figure out how to access the gallery :slight_smile:

It looks like you were provided a solution with using a custom shape, another thing I have not done before!

Good Luck!

Andrew Pitonyak

This might seem obvious, but:

1. What document type?

Calc

2. What is a "custom" shape? Do you mean com.sun.star.drawing.CustomShape

Yes

Not that I have never used Cutom Shapes in any way in LibreOffice, but i have written a Macro at least once. :slight_smile:

I have written code that adds things such as lines (and other shapes) into documents.

I might have looked at something in LO that allows me to have a bunch of shapes. On my computer, I use

View > Gallery

Too open it. From there it looks like I can drag objects out of the "gallery" and onto a document.

That makes a kind of copy&paste. I need an object newly created with the
mouse.

When you click on a shape icon in the toolbar and then click and drag
with the mouse to create the shape, then the created custom shape is on
a wrong layer. You notice in the "Drawing object properties"-toolbar,
that both "To Foreground" and "To Background" are enabled. "To
Foreground" should not be enabled, because when you create a custom
shape with the mouse, you are working already in the "Foreground". I
have a patch for it, but struggle with the unit test.

Kind regards
Regina

Hi Mauricio,

I have translated the steps to C++ with createInstance => new
and keeping setPropertyValue or using method MergeDefaultAttributes.
The custom shape is generated, but the faulty method (for which I have the patch) is not touched.

It seems, it is needed, that the custom shape is generated with the mouse.

At least I now know, how to generate a custom shape in a Basic macro. :slight_smile:

Kind regards
Regina