[Solved] Inserting boxes and shapes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

[Solved] Inserting boxes and shapes

Post by Cogito ergo sum »

Hi, does anyone have a simple Basic macro for inserting a text box in a writer document and tilting it to an angle?
Also one for inserting a geometric figure e.g. a star?
many thanks in anticipation
Last edited by Hagar Delest on Wed Jun 13, 2018 6:32 pm, edited 1 time in total.
Reason: tagged solved
OpenOffice 5.3.6.1 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: inserting boxes and shapes

Post by Villeroy »

There is no simple macro for this.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: inserting boxes and shapes

Post by Zizi64 »

Draw and adjust it by the Draw application, and put it into the Gallery. Then you can use it by a "drag&Drop" from the Gallery in all of applications.
MyOwnShapes.png
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: inserting boxes and shapes

Post by Cogito ergo sum »

No simple macro? A complicated one then perhaps?
OpenOffice 5.3.6.1 on Windows 7
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: inserting boxes and shapes

Post by Zizi64 »

A complicated one then perhaps?
You will write it.

Please explain: Why you need it?
You can use a template instead (if you often need same shape in same position of the document), or the gallery with your own shapes.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: inserting boxes and shapes

Post by Cogito ergo sum »

A template is of no interest to me because I want to write the macro for an unknown number of users on a network who subsequently would be required to copy the template to their own gallery to use it. Also I would simply like to know how to do this programatically if it can be done as part of a learning process. And finally a macro would open this possibility of a user customising the use of it to some extent - e.g. the text content of a text box. So yeah I'll have to write it myself from trial and error - I had just hoped tofind a quick shortcut here.
OpenOffice 5.3.6.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: inserting boxes and shapes

Post by RoryOF »

A little bit of searching often throws up helpful OO macros.

I found these
[Solved] Insert Image, Position and Resize via Macro in Calc
[Solved] Add Image using macro
[Solved] Inserting and positioning image

and many others

I also recommend Andrew Pitonyak's works on macros.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: inserting boxes and shapes

Post by Villeroy »

Don't you have a common templates folder on the network?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Inserting boxes and shapes

Post by Sébastien C »

If I may, some examples...

Insert a text box in Writer and tilting it to an angle is not a big deal...

Code: Select all

Sub textBoxTilting45degrees()
	Dim           myForm As Object
	Dim myDimensionsForm As New com.sun.star.awt.Size
	Dim   myPositionForm As New com.sun.star.awt.Point

	                 myForm = thisComponent.createInstance("com.sun.star.drawing.TextShape")

	myDimensionsForm.Width  = 7500                                          ' 75 mm.
	myDimensionsForm.Height = 2530                                          ' 25,3 mm.
	  myPositionForm.X      = 5000                                          ' 50 mm.
	  myPositionForm.Y      = 10000                                         ' 100 mm.

	With myForm
	 .AnchorType       = com.sun.star.text.TextContentAnchorType.AT_PAGE
	 .AnchorPosition.X = 0
	 .AnchorPosition.Y = 0
	 .Size             = myDimensionsForm
	 .Position         = myPositionForm
	 .rotateAngle      = 4500                                               ' 45 degrees
	End With

	thisComponent.drawPage.add(myForm)
	myForm.String = "Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
End Sub
A common star is a custom shape, and unfortunately, it is so bad documented.
BUT, in Draw, we can convert it in a polygon shape.
And paste the star in Writer, just for studying it points...

After that, drawing a polygon shape is not a big deal too...

Code: Select all

Sub aNewStarIsBorn()
	Dim           myForm As Object
	Dim myDimensionsForm As New com.sun.star.awt.Size
	Dim   myPositionForm As New com.sun.star.awt.Point
	Dim     thePoints(9) As New com.sun.star.awt.Point

    myForm = thisComponent.createInstance("com.sun.star.drawing.PolyPolygonShape")
	thisComponent.drawPage.add(myForm)

	thePoints(0).X = 10869
	thePoints(0).Y = 1512
	thePoints(1).X = 10592
	thePoints(1).Y = 2421
	thePoints(2).X = 9681
	thePoints(2).Y = 2421
	thePoints(3).X = 10420
	thePoints(3).Y = 2987
	thePoints(4).X = 10143
	thePoints(4).Y = 3889
	thePoints(5).X = 10869
	thePoints(5).Y = 3336
	thePoints(6).X = 11596
	thePoints(6).Y = 3889
	thePoints(7).X = 11318
	thePoints(7).Y = 2987
	thePoints(8).X = 12058
	thePoints(8).Y = 2421
	thePoints(9).X = 11147
	thePoints(9).Y = 2421

	myForm.PolyPolygon = Array( thePoints() )

	myDimensionsForm.Width  = 2000                                          ' 20 mm.
	myDimensionsForm.Height = 2000                                          ' 20 mm.
	  myPositionForm.X      = 1000                                          ' 10 mm.
	  myPositionForm.Y      = 1000                                          ' 10 mm.

	With myForm
	 .AnchorPosition.X = 0
	 .AnchorPosition.Y = 0
	 .AnchorType       = com.sun.star.text.TextContentAnchorType.AT_PAGE
	 .LineWidth        = 80
	 .Size             = myDimensionsForm
	 .Position         = myPositionForm
	 .rotateAngle      = 4500                                               ' 45 degrees
	End With
End Sub
A good way, I think, for beginning to learn code...
:lol:
Attachments
formAndShape.odt
(14.67 KiB) Downloaded 153 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Inserting boxes and shapes

Post by Cogito ergo sum »

OK many thanks everyone for your ideas and to Sébastian for the code which set me on a useful path. Here's as far as I've got with Sébastians code and some googling and a lot of trial and error:

Code: Select all

               Dim args(),aStruct

               Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
               Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
               Set oDsk= objServiceManager.createInstance("com.sun.star.frame.Desktop")
               Set oDoc= oDsk.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
               Set objSize = createStruct("com.sun.star.awt.Size")

               Set objText= oDoc.getText
               Set objCursor= objText.createTextCursor

               Set objTextFrame= oDoc.createInstance("com.sun.star.drawing.TextShape")

               objSize.Width  = 7500    
               objSize.Height = 2530 

               objTextFrame.setSize( objSize)
               objText.insertTextContent objCursor, objTextFrame, false
               objTextFrame.String = "Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
               objTextFrame.rotateAngle      = 4500 
               objTextFrame.AnchorType = AS_CHARACTER
               objTextFrame.IsFollowingTextFlow = False
               objTextFrame.CharHeight="14"
               objTextFrame.CharFontName="Trebuchet MS"
               objTextFrame.CharColor="1500"
               objTextFrame.HoriOrient = 0
               objTextFrame.VertOrient = 0
               objTextFrame.VertOrientPosition = 3500
               objTextFrame.HoriOrientPosition = 2000

               Function createStruct( strTypeName)
               Set classSize= objCoreReflection.forName( strTypeName)
               classSize.createObject aStruct
               Set createStruct= aStruct
               End Function
I wanted a standalone macro and this Works impeccably as a VB file. I just lack the technique for making the borders of the box visible for it to be complete. (It turned out to be not too complicated)
OpenOffice 5.3.6.1 on Windows 7
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Inserting boxes and shapes

Post by Sébastien C »

Cogito ergo sum wrote:I just lack the technique for making the borders of the box visible for it to be complete. (It turned out to be not too complicated)

Code: Select all

objTextFrame.LineStyle = com.sun.star.drawing.LineStyle.SOLID
objTextFrame.LineWidth = 100
objTextFrame.LineColor = rgb(255, 0, 0)
It is maybe time to discover Xray or MRI...

Imagine, just for the line, discover: “LineCap”, “LineColor”, “LineDash”, “LineDashName”, “LineEnd”, “LineEndCenter”, “LineEndName”, “LineEndWidth”, “LineJoint”, “LineStart”, “LineStartCenter”, “LineStartName”, “LineStartWidth”, “LineStyle”, “LineTransparence”, “LineWidth”...

And for the margin: “LeftMargin”, “RightMargin”, “TopMargin”, “BottomMargin”, “ParaLeftMargin”, “ParaRightMargin”, “ParaTopMargin”, “ParaBottomMargin”.

A dream isn’t it ?
;)
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Cogito ergo sum
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Inserting boxes and shapes

Post by Cogito ergo sum »

Well, thank you once Again Sébastian - although objTextFrame.LineStyle = com.sun.star.drawing.LineStyle.SOLID throws up an error, but if I change it quite simply to objTextFrame.LineStyle = 1 then it works fine. Like a dream as you say but the dream becomes a nightmare again if for example I try the same thing with LineDash in order to try and get a dotted border. I now have:

Code: Select all

               Dim args(),aStruct

               Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
               Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
               Set oDsk= objServiceManager.createInstance("com.sun.star.frame.Desktop")
               Set oDoc= oDsk.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
               Set objSize = createStruct("com.sun.star.awt.Size")

               Set objText= oDoc.getText
               Set objCursor= objText.createTextCursor

               Set objTextFrame= oDoc.createInstance("com.sun.star.drawing.TextShape")

               objSize.Width  = 7500    
               objSize.Height = 2530 

               objText.insertTextContent objCursor, objTextFrame, false

               objTextFrame.setSize(objSize)
               objTextFrame.String = VbLf & "Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
               objTextFrame.rotateAngle = 4500
               objTextFrame.paraleftmargin = 300 
               objTextFrame.topmargin = 700
               objTextFrame.LineStyle = 1
               objTextFrame.LineWidth = 10
               objTextFrame.LineColor = rgb(255, 0, 0)
               objTextFrame.AnchorType = AS_CHARACTER
               objTextFrame.IsFollowingTextFlow = False
               objTextFrame.CharHeight="14"
               objTextFrame.CharFontName="Trebuchet MS"
               objTextFrame.CharColor="1500"
               objTextFrame.HoriOrient = 0
               objTextFrame.VertOrient = 0
               objTextFrame.VertOrientPosition = 3500
               objTextFrame.HoriOrientPosition = 2000

               Function createStruct( strTypeName)
               Set classSize= objCoreReflection.forName( strTypeName)
               classSize.createObject aStruct
               Set createStruct= aStruct
               End Function
and as can be seen I put a VbLf & before the text because although paraleftmargin works paratopmargin doesn't although it doesn't throw up an error. It could be an idea to try Xray, but does it do more than simply list parameters, properties and methods? What I seem to be lacking is the basic understanding of why some things work and others don't when it would seem logical that when the one works the other would too. And yes I have tried to read Pitonyaks stuff - it's excellent but over my head a lot of the time, unfortunately.
OpenOffice 5.3.6.1 on Windows 7
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Inserting boxes and shapes

Post by Lupp »

There are only three LineStyle constants: NONE, SOLID, and DASH.
To use them you need to give the complete qualified names:

Code: Select all

com.sun.star.drawing.LineStyle.NONE
com.sun.star.drawing.LineStyle.SOLID
com.sun.star.drawing.LineStyle.DASH
A lot to type for 0, 1, and 2.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply