[Solved] Inserting boxes and shapes

Creating a macro - Writing a Script - Using the API

[Solved] Inserting boxes and shapes

Postby Cogito ergo sum » Sun Jun 10, 2018 10:01 pm

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
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: inserting boxes and shapes

Postby Villeroy » Sun Jun 10, 2018 10:18 pm

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 25847
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: inserting boxes and shapes

Postby Zizi64 » Mon Jun 11, 2018 7:37 am

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; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7373
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: inserting boxes and shapes

Postby Cogito ergo sum » Mon Jun 11, 2018 7:54 am

No simple macro? A complicated one then perhaps?
OpenOffice 5.3.6.1 on Windows 7
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: inserting boxes and shapes

Postby Zizi64 » Mon Jun 11, 2018 8:47 am

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; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7373
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: inserting boxes and shapes

Postby Cogito ergo sum » Mon Jun 11, 2018 10:10 am

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
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: inserting boxes and shapes

Postby RoryOF » Mon Jun 11, 2018 10:24 am

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.5 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 27488
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: inserting boxes and shapes

Postby Villeroy » Mon Jun 11, 2018 10:26 am

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 25847
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Inserting boxes and shapes

Postby Sébastien C » Mon Jun 11, 2018 2:23 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
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 21 times
LibreOffice 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 57
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Inserting boxes and shapes

Postby Cogito ergo sum » Mon Jun 11, 2018 11:45 pm

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   Expand viewCollapse view
               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
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Inserting boxes and shapes

Postby Sébastien C » Tue Jun 12, 2018 12:40 am

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   Expand viewCollapse view
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 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 57
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Inserting boxes and shapes

Postby Cogito ergo sum » Tue Jun 12, 2018 10:38 am

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   Expand viewCollapse view
               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
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Inserting boxes and shapes

Postby Lupp » Tue Jun 12, 2018 7:47 pm

There are only three LineStyle constants: NONE, SOLID, and DASH.
To use them you need to give the complete qualified names:
Code: Select all   Expand viewCollapse view
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 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Let's create a powerful UFO: United Free Office!
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2101
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 4 guests