[Solved] Access to document objects at zOrder sequence

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

[Solved] Access to document objects at zOrder sequence

Post by Evgeniy »

Im want to save objects in zOrder sequence.
Is there any simple way for get access to document objects in zOrder sequence?

For example a can get all images or frame by this methods

Code: Select all

	oFrames = Doc.getTextFrames()
	oImages = Doc.GraphicObjects()
and i can get zOrder of object

Code: Select all

" zOrd="+Frame.ZOrder
But it possible get all object in one array and sort by zOrder and enumerate it?

My idea:

Code: Select all

Object pool=get all objects from list
Sort poll by zOrder if it possible

For i=0 to i<pool array bound
    if pool.type image 
          ... some action
    endif
    if pool type textfield
          ... some action
    endif
next i
Last edited by Evgeniy on Sat Jan 18, 2020 8:24 pm, edited 1 time in total.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: access to document objects at zOrder sequence

Post by JeJe »

Create an array the size of the number of textframes.
Go through the textframes putting the index or name of the text frame in the appropriate place in the array according to the zorder of the item.
Then you have a list of indexes or names sorted by zorder.

Edit: you can use an enumeration or for indexes = 0 to thecount -1 to get the zorder of each item and put the index or name in the appropriate place in the array.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: access to document objects at zOrder sequence

Post by Evgeniy »

Ok my version with Z ordering support:

Code: Select all

	Doc = ThisComponent
	oFrames = Doc.getTextFrames()
	oImages = Doc.GraphicObjects()

	size=oFrames.Count+oImages.Count
	Dim zorder(size-1) As Long
	Dim typ(size-1) As String
	Dim p,ind As Long
	p=0
	
	For i = 0 to oFrames.Count-1
		zorder(p) = oFrames.getByIndex(i).ZOrder
		typ(p) = "frame"
		p=p+1
	Next i
	
	For i = 0 to oImages.Count-1
		zorder(p) = oImages.getByIndex(i).ZOrder
		typ(p) = "image"
		p=p+1
	Next i
	
	Dim debstr as String
next_ind:
	p=-1	' when minimal zorder is zero
	ind=-1
	For i=0 to size-1
		if zorder(i)>p then
			p=zorder(i)
			ind=i
		endif
	Next i
	if ind<>-1 then
		debstr=typ(ind)+":z="+zorder(ind)+"; "+debstr
		zorder(ind)=-1
		goto next_ind
	endif	
		
	msgbox debstr
Or is there a better way?
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: access to document objects at zOrder sequence

Post by JeJe »

Code: Select all

 For i = 0 to oFrames.Count-1
      zorder(oFrames.getByIndex(i).ZOrder) = i 
   Next i
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: access to document objects at zOrder sequence

Post by Villeroy »

Document objects may be frames, media, images, OLE objects, drawings, charts, form controls among other things. They are attached to a draw page. Presentations and drawings consist of DrawPages, spreadsheets have one DrawPage per sheet, a text document has one DrawPage:

Code: Select all

REM Text Document
dp = ThisComponent.DrawPage
for i = 0 to dp.getCount() -1
  obj = dp.getByIndex(i)
  print obj.getImplementationName()
next
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
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: access to document objects at zOrder sequence

Post by Lupp »

For (not too old) versions of LibreOffice the striked-out part of the post should still be valid.

No need. All the objects having a property ZOrder are inserted into the respective container DrawPage. The elements of a drawpage (in case of a Writer document exactly one) are accessible by enumeration, in Basic with the construct

Code: Select all

For Each object In myDrawPage
  <statement block>
Next object
and also by IndexAccess.

Enumarating accesses them top down by ZOrder, access by increasing index does it bottom up. The index is the ZOrder value actually.

Concerning the Z-order I did not take this from a specification I knew, but from experience and "research". Please tell me (us) if I am wrong.

To study the effects of editing and anchoring on the Z-order you may use the attached example doc which contains a fe lines of related Basic code.[/strike]

It's a mess. My experiences are from my usage of LibreOffice.

AOO does it differently.
-1- A Writer document does not allow access to its DrawPage via a virtual DrawPages container which makes it possible in LibreOffice only to use the same code in Writer and in AOO.
(This is easily fixed by changing a few lines in the contained code.)
-2- AOO neither enumerates nor indexes the contents of the CodePage in Z-order.
Attachments
aoo100794zOrder.odt
(26.55 KiB) Downloaded 163 times
Last edited by Lupp on Sat Jan 18, 2020 1:37 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: access to document objects at zOrder sequence

Post by JeJe »

Lupp - there seem to be problems with the roamDrawPage sub in that file, I think dps = doc.DrawPages should be doc.DrawPage, and there are errors beyond that.

(Edit... two better solutions for the OP though...)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: access to document objects at zOrder sequence

Post by Lupp »

@JeJe: I already addressed the problem by editing my post above.
The differences between LibO and AOO get really annoying in very many cases now. Remembering previous problems of this kind I eventually tried my example also in AOO 4.1.5 and my experiences made me strike out the original post.
But it's not only about the container thing, but also about the kernel of the question concerning Z-order.

Since I use AOO only for testing for a few years now, I have to expect increasing impact of the differences on my "timekeeping". I will therefore cease to attempt to answer questions by AOO users suspectable of being afflicted by the problem.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: access to document objects at zOrder sequence

Post by Villeroy »

Code: Select all

Sub roamDrawPage()
REM Text Document
view = ThisComponent.getCurrentController()
dp = ThisComponent.DrawPage
for i = 0 to dp.getCount() -1
  obj = dp.getByIndex(i)
  view.select(obj)
  print obj.getImplementationName(), obj.ZOrder
next
End Sub
reports 10 Objects and their ZOrder with LibreOffice and OpenOffice.
There is no collection of DrawPages in a text document.
A spreadsheet has a collection of DrawPages (one per sheet) and a collection of Sheets having one DrawPage each.
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
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: access to document objects at zOrder sequence

Post by JeJe »

Lupp... we'll reach a point of divergence where it isn't practicable to write code that works for both and people will have to choose... if we're not there already. LibreOffice has a lot of advancements - but some things like having a Writer window in a dialog (which I wanted recently) turn out to run incredibly slowly by comparison with OO... so its OO if you want that... (which I do...)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: access to document objects at zOrder sequence

Post by Lupp »

Villeroy wrote:... There is no collection of DrawPages in a text document. ...
In LibO there is - since LibO V 6.1.2 (at least).
The DrawPages property of a Writer document always has .Count = 1.

Being on the experimental tour I had created -and tested of course- the attached example with V6.4.0.2RC. I missed to test it also with AOO or older versions of LibO.

I came accidentally about the "feature", used it in a few cases where I wanted to get shape-related code working for more than one document model, and missed to check for the history. Just did a bit of homework and can now tell that the change in LibO Writer was between versions 6.0.4(has not) and 6.1.2(has). I wrongly assumed the enhancement was much older.

Sorry for my mistake.

Concerning the Z-order: The enumeration in LibO also was in order of indices, and by that in ascending ZOrder. This was changed more recently. I did no resarch insofar.

I attach the adapted example running as expected in AOO 4.1.5 for me.
Attachments
aoo100794zOrderExamplForAOO.odt
(26.04 KiB) Downloaded 181 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: access to document objects at zOrder sequence

Post by Evgeniy »

JeJe wrote:

Code: Select all

 For i = 0 to oFrames.Count-1
      zorder(oFrames.getByIndex(i).ZOrder) = i 
   Next i
Thanks JeJe, but I think this can lead to an "out of index array exception" if you add an object other than frame or image but having a zorder.
Last edited by Evgeniy on Sat Jan 18, 2020 8:42 pm, edited 1 time in total.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: access to document objects at zOrder sequence

Post by Evgeniy »

Villeroy wrote:

Code: Select all

Sub roamDrawPage()
REM Text Document
view = ThisComponent.getCurrentController()
dp = ThisComponent.DrawPage
for i = 0 to dp.getCount() -1
  obj = dp.getByIndex(i)
  view.select(obj)
  print obj.getImplementationName(), obj.ZOrder
next
End Sub
reports 10 Objects and their ZOrder with LibreOffice and OpenOffice.
There is no collection of DrawPages in a text document.
A spreadsheet has a collection of DrawPages (one per sheet) and a collection of Sheets having one DrawPage each.
Yes, this is what I was looking for. Returns objects in ZOrder sequence!
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Access to document objects at zOrder sequence

Post by Villeroy »

Code written by MRI:

Code: Select all

Sub Snippet
  Dim oDrawPage As Variant
  Dim oObj1 As Variant
  Dim nZOrder As Long

  oDrawPage = ThisComponent.getDrawPage()
  oObj1 = oDrawPage.getByIndex(0)
  nZOrder = oObj1.ZOrder
  
End Sub
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
Evgeniy
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: Access to document objects at zOrder sequence

Post by Evgeniy »

To Villeroy. Youre next example have true formatting but excludes for cycle (and pretty debug features view.select and info printing).
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Access to document objects at zOrder sequence

Post by Villeroy »

Of course, the easiest method is to let others do the work.
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
Post Reply