| View previous topic :: View next topic |
| Author |
Message |
emil kasulzke Guest
|
Posted: Fri Mar 12, 2004 11:31 am Post subject: code: draw: export current selection to eps |
|
|
Hi all,
I've been trying for a while to write a macro that exports the current selection of a draw page to eps. I've read through a bunch of postings in this forum and through the ooo dev guide, but didn't get the thing really running (had difficulties assigning the selection to the filter and setting the filter properties correctly), until I stumbled over an ooo bug report (7918) about eps that contains a small sample script which solved most of my export problems.
I thought some of you might be interested in the macro.
It does the following:
Get the selection of the current draw page and export it to eps. The name of the eps is constructed from the name of the original sxd and the name of the current draw page.
If there is no current selection, all shapes in the current page are selected.
It is easy to change the macro to export the whole page or to export to another format.
Any comments or tips for improvement are welcome
Cheers,
emil
Here is the code:
| Code: |
' gets all shapes in the current draw page
' @param page: A XDrawPage. If page is not a XDrawPage, an empty shapes collection is returned.
' @return: a com.sun.star.drawing.ShapeCollection containing all shapes, may be empty.
function getAllShapesInDrawPage(thePage as Object)
' create empty collection of shapes
theShapes= createUnoService("com.sun.star.drawing.ShapeCollection")
if not hasUnoInterfaces(thePage, "com.sun.star.drawing.XDrawPage") then
getAllShapesInDrawPage = theShapes
exit function
end if
' add all shapes in the page to the collection
for i=0 to thePage.getCount() - 1
theShapes.add(thePage.getByIndex(i))
next i
getAllShapesInDrawPage = theShapes
end function
' From a Draw page, export the current selection to eps. If the current selection is empty, select all
' shapes on the page and export them. If there are no shapes on the page, export the empty page.
' Parts of this code are taken from the code in ooo bug 7918.
sub exportSelectionOrPageToEps
Dim currentPageName as String
theDoc = StarDesktop.getCurrentComponent()
if not theDoc.SupportsService( "com.sun.star.drawing.DrawingDocument" ) Then
Msgbox("The current document must be a Draw document", 0)
exit sub
end if
theController = theDoc.currentController
thePage = theController.currentPage
theSelection = theController.selection
' if nothing is selected, select all shapes in the current page
if isEmpty(theSelection) then
MsgBox("selection is empty, will select everything in current page", 0)
theController.select(getAllShapesInDrawPage(thePage))
theSelection = theDoc.currentController.selection
end if
'create url for storing
Dim origUrl as String
Dim exportUrl as new com.sun.star.util.URL
origUrl = theDoc.url
if isnull(thePage.name) then
exportUrl.complete = Left( origUrl, Len(origUrl) - 4 ) + "_Export" + ".eps"
else
exportUrl.complete = Left( origUrl, Len(origUrl) - 4 ) + "_" + thePage.name + ".eps"
endif
' Create the export filter.
' A GraphicExportFilter uses a page, shape or collection of shapes as source.
theExporter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
' Export selection if something is selected, otherwise whole page.
if not isEmpty(theSelection) then
theExporter.SetSourceDocument(theSelection)
else
msgbox("no selection, exporting whole page", 0)
theExporter.SetSourceDocument(thePage)
endif
' Set the filter data
Dim aFilterData(5) as new com.sun.star.beans.PropertyValue
aFilterData(0).Name = "Level" '1=PS level 1, 2=PS level 2
aFilterData(0).Value = 2
aFilterData(1).Name = "ColorFormat" '1=color, 2=grayscale
aFilterData(1).Value = 1
aFilterData(2).Name = "TextMode" '0=glyph outlines, 1=no glyph outlines, see ooo bug 7918
aFilterData(2).Value = 1
aFilterData(3).Name = "Preview" '0=none,1=TIFF,2=EPSI,3=TIFF+EPSI
aFilterData(3).Value = 0
aFilterData(4).Name = "CompressionMode" ' 1=LZW, 2=none
aFilterData(4).Value = 2
Dim aArgs (2) as new com.sun.star.beans.PropertyValue
aArgs(0).Name = "MediaType"
aArgs(0).Value = "application/postscript"
aArgs(1).Name = "URL"
aArgs(1).Value = exportUrl
aArgs(2).Name = "FilterData"
aArgs(2).Value = aFilterData()
theExporter.filter(aArgs())
MsgBox("exported " + origUrl + " to " + exportUrl.complete, 0)
End Sub
|
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Fri Mar 12, 2004 5:48 pm Post subject: |
|
|
That is a very interesting example. A technique I was unaware of.
emil, would you mind if this very interesting thread were moved to the Code Snippets forum? Please reply if that is okay (or not okay) with you.
To see the MIME types supported, use this....
| Code: | Sub ShowExportMimeTypes()
oDoc = ThisComponent
oExportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
aMimeTypeNames = oExportFilter.getSupportedMimeTypeNames()
' Display result in a MsgBox...
MsgBox Join( aMimeTypeNames, Chr(13) )
' Display result in a Writer doc.
oOutput = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
Writer_PrintLn( oOutput, Join( aMimeTypeNames, Chr(13) ) )
End Sub
|
To only see the output in a MsgBox, you must comment the last couple statements.
If you leave the last couple statements uncommented, then output appears in a new Writer document. In that case, you also need the following additional code ripped from my library....
| Code: | ' Sugar Coated way to Print into a Writer document.
' The oOutput parameter can be any of....
' com.sun.star.text.TextDocument
' com.sun.star.text.Text
' com.sun.star.drawing.Text
' com.sun.star.text.TextCursor
Sub Writer_Print( oOutput, cString )
If oOutput.SupportsService( "com.sun.star.text.TextDocument" ) Then
oText = oOutput.getText()
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.drawing.Text" ) Then
oText = oOutput
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.text.Text" ) Then
oText = oOutput
oCursor = oText.createTextCursor()
ElseIf oOutput.SupportsService( "com.sun.star.text.TextCursor" ) Then
oCursor = oOutput
oText = oCursor.getText()
Else
Exit Sub
EndIf
oCursor.gotoEnd( False )
nLen = Len( cString )
nStart = 1
Do
nPos = Instr( nStart, cString, Chr(13) )
bCRFound = (nPos > 0)
If Not bCRFound Then
nPos = nLen + 1
EndIf
cSegment = Mid( cString, nStart, nPos-nStart )
nStart = nPos + 1
oText.insertString( oCursor, cSegment, False )
If bCRFound Then
oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
EndIf
Loop While bCRFound
End Sub
' Same as Writer_Print(), just adds a line ending.
Sub Writer_PrintLn( oOutput, Optional cString )
If IsMissing( cString ) Then
cString = ""
EndIf
Writer_Print( oOutput, cString + Chr(13) )
End Sub
|
Here is the output produced by the above.....
image/x-MS-bmp
application/postscript
image/gif
image/jpeg
image/x-portable-bitmap
image/x-pict
image/x-portable-graymap
image/png
image/x-portable-pixmap
image/x-cmu-raster
image/svg+xml
image/tiff
image/x-xpixmap
Here is a variation on the previous example that exports all shapes on the page, or only the selected shapes as a PNG file...
| Code: | Sub ExportSelectedShapesToPNG()
oDoc = ThisComponent
oDocCtrl = oDoc.getCurrentController()
oDrawPage = oDocCtrl.getCurrentPage()
oSelection = oDocCtrl.getSelection()
oExportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
If IsEmpty( oSelection ) Then
oExportFilter.setSourceDocument( oDrawPage )
Else
oExportFilter.setSourceDocument( oSelection )
EndIf
cExportUrl = oDoc.getURL()
cExportUrl = Left( cExportUrl, Len(cExportUrl)-4 ) + "_" + oDrawPage.getName() + ".png"
oExportFilter.filter( _
Array( _
MakePropertyValue( "MediaType", "image/png" ),_
MakePropertyValue( "URL", cExportUrl ) ) )
End Sub
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
|
This capability was originally requested in this thread.
http://www.oooforum.org/forum/viewtopic.php?t=6053 _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
emil kasulzke Guest
|
Posted: Sun Mar 14, 2004 12:27 am Post subject: |
|
|
Good morning,
| Quote: |
emil, would you mind if this very interesting thread were moved to the Code Snippets forum? Please reply if that is okay (or not okay) with you.
|
If you think Code Snippets is the right place, no problem, move it
Btw, for finding out which parameters a given filter supports , I analyzed $OO_INSTALL_DIR/share/registry/res/en-US/org/openoffice/Office/Common.properties.
I am sure that is not the intended way to do it, but does anyone know how to get these infos programatically? Neither the filter description page (http://framework.openoffice.org/files/documents/25/897/filter_description.html) explains this, nor does the oo dev guide. I am not interested in the mimetypes supported (or the filter names), but in the FilterData parameters (like TextMode, CompressionMode, PreviewFormat, etc).
Best regards,
emil
[/quote] |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Mon Mar 15, 2004 2:34 pm Post subject: Re: code: draw: export current selection to eps |
|
|
emil,
are you sure your FilterData options are actually having an effect?
I'm trying the following experiments, and I am not at all convinced that my filter options have any effect whatsoever....
| Code: | Sub Export1()
cFile = "C:\Documents and Settings\dbrewer\Desktop\test.bmp"
cUrl = ConvertToURL( cFile )
oDoc = ThisComponent
oDoc.storeToURL( cUrl, _
Array( _
MakePropertyValue( "FilterName", "draw_bmp_Export" ), _
MakePropertyValue( "FilterData",_
Array( _
MakePropertyValue( "ExportMode", 1 ), _
MakePropertyValue( "Resolution", 300 ), _
MakePropertyValue( "Color", 3 ) _
) ) _
) )
End Sub
Sub Export2()
cFile = "C:\Documents and Settings\dbrewer\Desktop\test.bmp"
cUrl = ConvertToURL( cFile )
oDoc = ThisComponent
oDocCtrl = oDoc.getCurrentController()
oDrawPage = oDocCtrl.getCurrentPage()
oSelection = oDocCtrl.getSelection()
oExportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
If IsEmpty( oSelection ) Then
oExportFilter.setSourceDocument( oDrawPage )
Else
oExportFilter.setSourceDocument( oSelection )
EndIf
oExportFilter.filter( _
Array( _
MakePropertyValue( "MediaType", "image/x-MS-bmp" ),_
MakePropertyValue( "URL", cUrl ), _
MakePropertyValue( "FilterData",_
Array( _
MakePropertyValue( "ExportMode", 1 ), _
MakePropertyValue( "Resolution", 300 ), _
MakePropertyValue( "Color", 3 ) _
) ) _
) )
End Sub
Sub Export3()
cFile = "C:\Documents and Settings\dbrewer\Desktop\test.png"
cUrl = ConvertToURL( cFile )
oDoc = ThisComponent
oDocCtrl = oDoc.getCurrentController()
oDrawPage = oDocCtrl.getCurrentPage()
oSelection = oDocCtrl.getSelection()
oExportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
If IsEmpty( oSelection ) Then
oExportFilter.setSourceDocument( oDrawPage )
Else
oExportFilter.setSourceDocument( oSelection )
EndIf
oExportFilter.filter( _
Array( _
MakePropertyValue( "MediaType", "image/png" ),_
MakePropertyValue( "URL", cUrl ), _
MakePropertyValue( "FilterData",_
Array( _
MakePropertyValue( "Compression", 9 ), _
MakePropertyValue( "Interlaced", 0 ) _
) ) _
) )
End Sub
|
_________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Mar 20, 2004 8:20 am Post subject: |
|
|
Hi Danny,
| Quote: | | are you sure your FilterData options are actually having an effect? |
I use ColorMode and Level with different values, both Parameters have the desired effect.
Using CompressionMode does not work at all, the eps contains binary garbage in front of the otherwise valid PS instructions (the garbage can easily be removed with a text editor), no matter how I set compression mode. Therefore I removed the CompressionMode parameter from my script. This seems to be a known OOo bug (don't remember the bug id).
I had been toying around with storeToUrl + FilterData, but this did not work at all, I don't know why.
emil[/quote] |
|
| Back to top |
|
 |
Ikkyo Newbie

Joined: 21 May 2007 Posts: 1
|
Posted: Mon May 21, 2007 12:18 am Post subject: |
|
|
The script of the original poster still works in OpenOffice 2.0, after the following modification(s):
use | Code: | | aArgs(0).Value = "image/x-eps" |
instead of | Code: | | aArgs(0).Value = "application/postscript" |
I also had to use | Code: | | aFilterData(2).Value = 0 |
instead of | Code: | | aFilterData(2).Value = 1 |
to get the formulae and special fonts exported correctly. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|