OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Draw: Introduction to draw and basic shapes
Goto page 1, 2  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Jan 30, 2004 12:41 pm    Post subject: Draw: Introduction to draw and basic shapes Reply with quote

This article is about how to draw simple shapes in OOo Draw. This article will give you working examples of...
1. Createing or opening drawings.
2. Drawing Rectangles, Ellipses (or circles, including pie-shapes), Lines, and Text.
3. Setting a number of useful properties of the shapes (color, outline style/color, etc., fill color).
4. How to save your drawing.

Of all of the parts of OpenOffice.org, the Draw module has been my favorite. The first part of OOo's API that I started learning was how to program drawings. My earliest work was a Maze Generator (written in Java), followed by a Turtle Graphics Tutorial, and then soon followed by the first version of Danny's Draw Power Tools.

If you want to learn how to write macros that produce drawings, then this article might be a good place to begin.


Creating an empty drawing

Creating a new empty drawing is as easy as...
Code:
oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )

The variable oDrawDoc ends up with the document model of the drawing we are to work with. A new empty drawing.

You could also laod a drawing from a file like this...
Code:
cFile = "C:\MyDrawing.sxd"
cUrl = ConvertToURL( cFile )
oDrawDoc = StarDesktop.loadComponentFromURL( cUrl, "_blank", 0, Array() )

The variable oDrawDoc ends up with the document model of the drawing we are to work with. A drawing that was loaded from a file.

If the macro code you are writing is in a drawing document, you can get the document model of the macro document like this...
Code:
oDrawDoc = ThisComponent

The variable oDrawDoc ends up with the document model of the drawing we are to work with. The drawing which contains the code being executed.

In all of the examples that follow, I will assume that we are always creating a new empty drawing using the code...
Code:
oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )

and I will assume that the variable oDrawDoc contains the drawing document we are working on.

I've written an article here that describes everything you want to know about using loadComponentFromURL().
Open and Create documents, various prog. languages
http://www.oooforum.org/forum/viewtopic.php?t=5252


Getting a draw page

Everything you draw must be drawn onto a draw page. A drawing document has one or more draw pages. The first page is always page zero. To get the first draw page, use a line of code like this...
Code:
oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )

Throughout the rest of this article, I will assume that oDrawPage variable contains the page that we are currently drawing to.

If you are curious about what the above line actually means, here is a brief explanation. The drawing document has a method getDrawPages(). This returns a thing called a "draw pages supplier". The draw pages supplier has a few useful methods such as...
getCount()
getByIndex()
as well as methods to insert new draw pages, delete draw pages, etc. The getByIndex() method returns a certian page from the collection of draw pages represented by the "draw pages supplier".


Drawing shapes

In a moment I will give you the definition of some convenient subroutines. First I'm going to show you how those subroutines are used.
Code:
oShape = MakeRectangleShape( oDrawDoc, MakePoint( 1000, 1000 ), MakeSize( 2000, 3000 ) )

This routine demonstrates three new functions.

MakePoint( X, Y )
MakeSize( width, height )
MakeRectangleShape( drawDoc, position, size )

The MakePoint() function takes two integers and returns a com.sun.star.awt.Point structure. Examples in the Developer's Guide, OOo SDK, and Basic programmer's manual typically use several lines of code to construct a point. In order to make our code short and easy to read, we want to use but a single, concise function call to allocate, initialize, and return a Point structure.

The MakeSize() function is similar. It takes a width and height and returns a com.sun.star.awt.Size structure.

The MakeRectangleShape() function is similar. It takes parameters, and returns an initialized shape. You can create a Shape in one line by calling oDrawDoc.createInstance( "com.sun.star.drawing.RectangleShape" ). But then it takes several lines of code to initialize it. Every shape needs at a minimum a location and a size. So I've standardized my own drawing technique to use the above three functions. I can thus write one line of code (above) that creates a specific type of Shape with position and size already initialized.

After a shape is created, it must be added to the drawing page.
Code:
oDrawPage.add( oShape )


So the total code so far, to create a new drawing with a shape would be...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   oShape = MakeRectangleShape( oDrawDoc, MakePoint( 1000, 1000 ), MakeSize( 2000, 3000 ) )
   oDrawPage.add( oShape )
End Sub

If you executed this code, you could get a new drawing with a shape. (Assuming, of course, you also include the subroutines given below.)

In addition to MakeRectangleShape() there are other subroutines to create shapes...

MakeEllipseShape( drawDoc, position, size )
MakeLineShape( drawDoc, position, size )
MakeTextShape( drawDoc, position, size )



Setting Shape Properties

It is as easy as this to manipulate the properties of a shape once you have created it.
Code:
oShape.FillColor = RGB( 255, 0, 0 )

This would change the shape's color to a bright red.

Besides FillColor, other useful properties of a shape are LineStyle and FillStyle.

LineStyle can be one of...
com.sun.star.drawing.LineStyle.NONE
com.sun.star.drawing.LineStyle.SOLID
com.sun.star.drawing.LineStyle.DASH

FillStyle can be one of...
com.sun.star.drawing.FillStyle.NONE
com.sun.star.drawing.FillStyle.SOLID
com.sun.star.drawing.FillStyle.GRADIENT
com.sun.star.drawing.FillStyle.HATCH
com.sun.star.drawing.FillStyle.BITMAP

So for example, you could write...
Code:
' Remove the outline of the shape.
oShape.LineStyle = com.sun.star.drawing.LineStyle.NONE
' Make the shape fill be solid.
oShape.FillStyle = com.sun.star.drawing.FillStyle.SOLID
' Make the shape Blue.
oShape.FillColor = RGB( 220, 220, 255 )



The FillProperties for shapes that support it are described here...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/FillProperties.html

The LineProperties for shapes that support it are described here...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/LineProperties.html


When you look up the description of a particular shape, such as RectangleShape...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/RectangleShape.html
you can see that it supports both FillProperties and LineProperties.


Here is another code example to draw a shape with a dashed line...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = MakeRectangleShape( oDrawDoc, MakePoint( 1000, 1000 ), MakeSize( 5000, 5000 ) )
   oDrawPage.add( oShape )
   
   oShape.FillColor = RGB( 220, 255, 240 )
   
   oShape.LineStyle = com.sun.star.drawing.LineStyle.DASH
   oShape.LineDash = MakeLineDash( com.sun.star.drawing.DashStyle.ROUND,_
                           3, 200, 4, 400, 300 )
   oShape.LineWidth = 200
   oShape.LineColor = RGB( 190, 190, 240 )
End Sub



Here is an example of drawing an Ellipse...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = MakeEllipseShape( oDrawDoc, MakePoint( 5000, 8000 ), MakeSize( 5000, 5000 ) )
   oDrawPage.add( oShape )
   
   oShape.FillColor = RGB( 255, 220, 220 )
   
   oShape.LineStyle = com.sun.star.drawing.LineStyle.DASH
   oShape.LineDash = MakeLineDash( com.sun.star.drawing.DashStyle.ROUND,_
                           3, 200, 4, 400, 300 )
   oShape.LineWidth = 400
   oShape.LineColor = RGB( 190, 240, 240 )
End Sub


Now try adding this code to the Ellipse example, to draw part of a pie chart...
Code:
   oShape.CircleKind = com.sun.star.drawing.CircleKind.SECTION
   oShape.CircleStartAngle = 60 * 100 ' 60 degrees
   oShape.CircleEndAngle = 320 * 100 ' 320 degrees


CircleKind can be one of...
com.sun.star.drawing.CircleKind.FULL
com.sun.star.drawing.CircleKind.SECTION ' a circle with a cut connected by two lines
com.sun.star.drawing.CircleKind.CUT ' a circle with a cut connected by a line
com.sun.star.drawing.CircleKind.ARC ' a circle with an open cut

The EllipseShape is described here...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/EllipseShape.html
As you see it supports both LineProperties and FillProperties.



Here is an example of drawing a line...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = MakeLineShape( oDrawDoc, MakePoint( 5000, 8000 ), MakeSize( 1000, 15000 ) )
   oDrawPage.add( oShape )
   
   oShape.LineStyle = com.sun.star.drawing.LineStyle.DASH
   oShape.LineDash = MakeLineDash( com.sun.star.drawing.DashStyle.ROUND,_
                           3, 200, 4, 400, 300 )
   oShape.LineWidth = 400
   oShape.LineColor = RGB( 150, 240, 200 )
End Sub



Here is an example of drawing several lines....
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   For i = 1 To 15
      oShape = MakeLineShape( oDrawDoc, MakePoint( 1000, 8000 ), MakeSize( 1000 + i * 1200, 15000 ) )
      oDrawPage.add( oShape )
      
      oShape.LineStyle = com.sun.star.drawing.LineStyle.DASH
      oShape.LineDash = MakeLineDash( com.sun.star.drawing.DashStyle.ROUND,_
                              3, 200, 4, 400, 300 )
      oShape.LineWidth = 400
      oShape.LineColor = RGB( 150 * (i/15), 240 * ((15-i)/15), 200 )
   Next
End Sub



Here is an example of drawing some text...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = MakeTextShape( oDrawDoc, MakePoint( 1000, 3000 ), MakeSize( 15000, 500 ) )
   oDrawPage.add( oShape )
   oShape.setString( "This is a text shape" )
End Sub


The TextShape is described here...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/TextShape.html

Please note that the TextShape DOES have LineProperties and FillProperties!

The TextShape has TextProperties described here...
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/TextProperties.html
CharacterProperties...
http://api.openoffice.org/docs/common/ref/com/sun/star/style/CharacterProperties.html
ParagraphProperties...
http://api.openoffice.org/docs/common/ref/com/sun/star/style/ParagraphProperties.html


Here is a more elaborate example of text, using more properties...
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = MakeTextShape( oDrawDoc, MakePoint( 1000, 3000 ), MakeSize( 15000, 3000 ) )
   oDrawPage.add( oShape )
   oShape.setString( "This is a text shape" )
   
   oShape.CornerRadius = 1000
   
   oShape.FillStyle = com.sun.star.drawing.FillStyle.SOLID
   oShape.FillColor = RGB( 190, 240, 180 )
   
   oShape.LineStyle = com.sun.star.drawing.LineStyle.SOLID
   oShape.LineWidth = 300
   oShape.LineColor = RGB( 240, 200, 200 )
   
   oShape.CharColor = RGB( 80, 80, 150 )
   
   oShape.TextVerticalAdjust = com.sun.star.drawing.TextVerticalAdjust.CENTER
   oShape.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.CENTER
End Sub




About color

See in the above examples how the RGB() function is used to set both the line color and fill color of shapes.

Instead of calling RGB(); my HSB() function could be used instead. I introduce the HSB() function here...

Color conversions: HSB to RGB and back again
http://www.oooforum.org/forum/viewtopic.php?t=4945

Here is an experiment...
1. Grab the HSB() function and its supporting functions from the above linked article. The only functions you need to swipe are HSB(), __HSBtoRGB(), and Min2().
2. Try replacing the RGB() calls in the above examples with HSB( 0.1, 0.5, 1.0 ).
3. Now try changing the 0.1 (the hue) to the following values: 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, and 1.0.
4. Try setting the hue to 0.2 and 1.2? See how they are exactly the same. Notice that 2.2, 3.2, 4.2, are exactly the same hue as 0.2.


Here is an experiment. You will need the HSB(), __HSBtoRGB(), and Min2() functions from the above linked article, and the functions from this article.
Code:
Sub Main()
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   ' Draw some colored rectangles
   oSize = MakeSize( 1000, 2000 )
   For i = 0 To 15
      oShape = MakeRectangleShape( oDrawDoc, MakePoint( 1000 + (i * 1200), 1000 ), oSize )
      oDrawPage.add( oShape )

      oShape.FillColor = HSB( i / 15, 1.0, 1.0 )
      oShape.CornerRadius = 1000 * (i/15)
   Next
End Sub



Here is an interesting example. You will need the HSB(), __HSBtoRGB(), and Min2() functions from the above linked article, and the functions from this article.
This one is really worth trying!
Code:
Sub ColorWackoTest()

   ' Create a new Draw document
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   
   ' Get the first page
   oPage = oDrawDoc.drawPages( 0 )
   
   ' The size and spacing between each individual block in a group.
   Const nBlockWidth = 700
   Const nBlockHeight = 700
   Const nBlockHMargin = 80
   Const nBlockVMargin = 80

   ' Make size object for the size of each block.   
   oSize = MakeSize( nBlockWidth, nBlockHeight )
   
   ' Number blocks across and down
   Const nNumBlocksAcross = 10
   Const nNumBlocksDown = 10
   
   
   '----------------------------------------
   ' Draw a group of blocks.
   ' The saturation increases from 0.0 to 1.0 as the blocks go down.
   ' The hue changes from 0.0 to 1.0 as the blocks go across.
   ' The brightness remains constant on all blocks -- full brightness.
   '
   ' Since hue 0.0 and 1.0 are both red, the leftmost and rightmost
   '  blocks will be red.
   ' Since the first (top) row has saturation zero, the entire top
   '  row of blocks will be white.
   
   ' Location of the *group* of color blocks
   nBlockFirstX = 1000
   nBlockFirstY = 1000
   
   nBrightness = 1.0
   For row = 0 To nNumBlocksDown-1
      nSaturation = row / (nNumBlocksDown-1)
      For col = 0 To nNumBlocksAcross-1
         nHue = col / (nNumBlocksAcross-1)
         
         ' Calculate Position of next rectangle to create
         nX = nBlockFirstX + (col * (nBlockWidth + nBlockHMargin))
         nY = nBlockFirstY + (row * (nBlockHeight + nBlockVMargin))
         
         ' Create next rectangle shape, set its color.
         oShape = MakeRectangleShape( oDrawDoc, MakePoint( nX, nY ), oSize )
         oShape.FillColor = HSB( nHue, nSaturation, nBrightness )
         oPage.add( oShape )
      Next
   Next
   '
   '----------------------------------------
   
   
   '----------------------------------------
   ' Draw a group of blocks.
   ' The brightness increases from 0.0 to 1.0 as the blocks go down.
   ' The hue changes from 0.0 to 1.0 as the blocks go across.
   ' The saturation remains constant on all blocks -- fully saturated colors.
   '
   ' Since hue 0.0 and 1.0 are both red, the leftmost and rightmost
   '  blocks will be red.
   ' Since the first (top) row has brightness zero, the entire top
   '  row of blocks will be black.
   
   ' Location of the *group* of color blocks
   nBlockFirstX = 10000
   nBlockFirstY = 1000

   nSaturation = 1.0
   For row = 0 To nNumBlocksDown-1
      nBrightness = row / (nNumBlocksDown-1)
      For col = 0 To nNumBlocksAcross-1
         nHue = col / (nNumBlocksAcross-1)
         
         ' Calculate Position of next rectangle to create
         nX = nBlockFirstX + (col * (nBlockWidth + nBlockHMargin))
         nY = nBlockFirstY + (row * (nBlockHeight + nBlockVMargin))
         
         ' Create next rectangle shape, set its color.
         oShape = MakeRectangleShape( oDrawDoc, MakePoint( nX, nY ), oSize )
         oShape.FillColor = HSB( nHue, nSaturation, nBrightness )
         oPage.add( oShape )
      Next
   Next
   '
   '----------------------------------------
   
   
   '----------------------------------------
   ' Draw a group of blocks.
   ' The brightness increases from 0.0 to 1.0 as the blocks go down.
   ' The saturation increases from 0.0 to 1.0 as the blocks go across.
   ' The hue remains constant on all blocks -- yellow color.
   
   ' Location of the *group* of color blocks
   nBlockFirstX = 1000
   nBlockFirstY = 10000

   nHue = 0.166666666   ' Yellow
   For row = 0 To nNumBlocksDown-1
      nBrightness = row / (nNumBlocksDown-1)
      For col = 0 To nNumBlocksAcross-1
         nSaturation = col / (nNumBlocksAcross-1)
         
         ' Calculate Position of next rectangle to create
         nX = nBlockFirstX + (col * (nBlockWidth + nBlockHMargin))
         nY = nBlockFirstY + (row * (nBlockHeight + nBlockVMargin))
         
         ' Create next rectangle shape, set its color.
         oShape = MakeRectangleShape( oDrawDoc, MakePoint( nX, nY ), oSize )
         oShape.FillColor = HSB( nHue, nSaturation, nBrightness )
         oPage.add( oShape )
      Next
   Next
   '
   '----------------------------------------
   
   
   '----------------------------------------
   ' Draw a group of blocks.
   ' The hue changes from 0.0 to 1.0 as the blocks go down.
   ' The brightness increases from 0.0 to 1.0 as the blocks go across.
   ' The saturation remains constant on all blocks -- low saturation, i.e. "pastels".
   '
   ' Since hue 0.0 and 1.0 are both red, the topmost and bottommost
   '  blocks will be red.
   ' The brightness varies from 0.4 to 1.0.
   
   ' Location of the *group* of color blocks
   nBlockFirstX = 10000
   nBlockFirstY = 10000

   nSaturation = 0.4
   For row = 0 To nNumBlocksDown-1
      nHue = row / (nNumBlocksAcross-1)
      For col = 0 To nNumBlocksAcross-1
         nBrightness = (col / (nNumBlocksDown-1)) * 0.6 + 0.4 ' vary brightness from 0.4 to 1.0
         
         ' Calculate Position of next rectangle to create
         nX = nBlockFirstX + (col * (nBlockWidth + nBlockHMargin))
         nY = nBlockFirstY + (row * (nBlockHeight + nBlockVMargin))
         
         ' Create next rectangle shape, set its color.
'         oShape = MakeRectangleShape( oDrawDoc, MakePoint( nX, nY ), oSize )
         oShape = MakeEllipseShape( oDrawDoc, MakePoint( nX, nY ), oSize )
         oShape.FillColor = HSB( nHue, nSaturation, nBrightness )
         oPage.add( oShape )
      Next
   Next
   '
   '----------------------------------------
End Sub

This example program is really worth trying. Please do.





Saving your drawing

You can save your drawing like this....
Code:
cFile = "/home/danny/Desktop/MyDrawing.sxd"
cUrl = ConvertToURL( cFile )
oDrawDoc.storeAsURL( cUrl, Array() )

If you are on Windows, then change the first line to...
Code:
cFile = "C:\Documents and Settings\dbrewer\Desktop\MyDrawing.sxd"


I wrote the above code in three statements.
1. Assign filename to cFile.
2. Convert file pathname to a url.
3. Save the document.
I did this simply to illustrate the steps. It could be rewritten as a single statement like this...
Code:
oDrawDoc.storeAsUrl( ConvertToURL( "C:\MyDrawing.sxd" ), Array() )





The subroutines

Here are the subroutines that are necessary to make all of the above examples work.

Code:
'----------
'   Create and return a new Point object.
'
'   This is syntax sugar to make it easy to
'    create a com.sun.star.awt.Point object.
'
Function MakePoint( x As Long, y As Long ) As com.sun.star.awt.Point
   Dim aPoint As New com.sun.star.awt.Point
   aPoint.x = x
   aPoint.y = y
   MakePoint() = aPoint
End Function


'----------
'   Create and return a new Size object.
'
'   This is syntax sugar to make it easy to
'    create a com.sun.star.awt.Size object.
'
Function MakeSize( width As Long, height As Long ) As com.sun.star.awt.Size
   Dim aSize As New com.sun.star.awt.Size
   aSize.width = width
   aSize.height = height
   MakeSize() = aSize
End Function



'----------
'   Create and return a new LineDash object.
'   See...
'   http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/LineDash.html
'
Function MakeLineDash( nDashStyle As Long, nDots As Integer, nDotLen As Long,_
                  nDashes As Integer, nDashLen As Long, nDistance As Long ) As com.sun.star.drawing.LineDash
   Dim oLineDash As New com.sun.star.drawing.LineDash
   oLineDash.Style = nDashStyle
   oLineDash.Dots = nDots
   oLineDash.DotLen = nDotLen
   oLineDash.Dashes = nDashes
   oLineDash.DashLen = nDashLen
   oLineDash.Distance = nDistance
   MakeLineDash() = oLineDash
End Function


'----------
'   Create and return a RectangleShape object.
'
'   After you create the shape object, you can manipulate
'    its properties to alter its appearance, including
'    its size and location.
'
'   You must pass the document object.
'   Optionally, you may also pass in a location and size.
'   If you do not, then the new object has size (0,0)
'    and location (0,0), which is the extreme upper left.
'    In other words, the object is squished way up in the
'    upper left corner, and has no size, therefore cannot be seen.
'
Function MakeRectangleShape( oDrawDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.RectangleShape
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.RectangleShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeRectangleShape() = oShape
End Function


'----------
'   Create and return a EllipseShape object.
'
'   After you create the shape object, you can manipulate
'    its properties to alter its appearance, including
'    its size and location.
'
'   You must pass the document object.
'   Optionally, you may also pass in a location and size.
'   If you do not, then the new object has size (0,0)
'    and location (0,0), which is the extreme upper left.
'    In other words, the object is squished way up in the
'    upper left corner, and has no size, therefore cannot be seen.
'
Function MakeEllipseShape( oDrawDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.EllipseShape
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.EllipseShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeEllipseShape() = oShape
End Function


'----------
'   Create and return a TextShape object.
'
'   After you create the shape object, you can manipulate
'    its properties to alter its appearance, including
'    its size and location.
'
'   You must pass the document object.
'   Optionally, you may also pass in a location and size.
'   If you do not, then the new object has size (0,0)
'    and location (0,0), which is the extreme upper left.
'    In other words, the object is squished way up in the
'    upper left corner, and has no size, therefore cannot be seen.
'
Function MakeTextShape( oDrawDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.TextShape
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.TextShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeTextShape() = oShape
End Function


'----------
'   Create and return a LineShape object.
'
'   After you create the shape object, you can manipulate
'    its properties to alter its appearance, including
'    its size and location.
'
'   You must pass the document object.
'   Optionally, you may also pass in a location and size.
'   If you do not, then the new object has size (0,0)
'    and location (0,0), which is the extreme upper left.
'    In other words, the object is squished way up in the
'    upper left corner, and has no size, therefore cannot be seen.
'
Function MakeLineShape( oDrawDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.LineShape
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.LineShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeLineShape() = oShape
End Function



Everything in this article applies to Impress as well as draw. Where you see "private:factory/sdraw" to create a new drawing, just replace it with "private:factory/simpress".

I hope you enjoyed this article as much as I enjoyed writing it.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Sat Jan 31, 2004 7:17 am    Post subject: Reply with quote

Danny, I want to add some shapes but after working for two (or more) hours without success I don't see where to go ?.
I start from Bernard Marcelly code :
See http://fr.openoffice.org/Documentation/How-to/indexht.html
under the title : "l'API d'OO (presque) sans peine"

Here is the code :
Code:

Sub Main

Dim MonDocument As Object
Dim MaForme As Object
Dim UnePage As Object
Dim CoordPt(7) As New com.sun.star.awt.Point
Const Y1 = 6000, Y2 = 8000, Ecart = 1000 ' unité 1/100 de mm

MonDocument = ThisComponent
UnePage = MonDocument.DrawPages(0)
MaForme = MonDocument.createInstance("com.sun.star.drawing.PolyLineShape")

CoordPt(0).X = 11000 ' position par rapport ŕ la page
CoordPt(0).Y = Y2
CoordPt(1).X = CoordPt(0).X + Ecart
CoordPt(1).Y = Y1
CoordPt(2).X = CoordPt(1).X + Ecart
CoordPt(2).Y = Y2
CoordPt(3).X = CoordPt(2).X + Ecart
CoordPt(3).Y = Y1
CoordPt(4).X = CoordPt(3).X + Ecart
CoordPt(4).Y = Y2
CoordPt(5).X = CoordPt(4).X + Ecart
CoordPt(5).Y = Y1
CoordPt(6).X = CoordPt(5).X + Ecart
CoordPt(6).Y = Y2
CoordPt(7).X = CoordPt(6).X + Ecart
CoordPt(7).Y = Y1

UnePage.add(MaForme)
MaForme.PolyPolygon = Array(CoordPt())
MaForme.LineColor = RGB(200, 220, 20)
MaForme.LineWidth = 100 ' 1 mm

End Sub


which works and draw a polyline shape.

Then I want to modify this code to have the same programming style as you give in this thread. This gives me :
Code:

Sub Main
 Dim oCoord(3) As New com.sun.star.awt.Point
  oCoord(0)=MakePoint(200,200)
  oCoord(1)=MakePoint(400,400)
  oCoord(2)=MakePoint(600,200)
  oShape=MakePolyLineShape(oDocument,array(oCoord()))
   oPage.add(oShape)
   oShape.LineWidth = 100
  oShape.LineColor = RGB( 200, 200, 200 )   
End Sub

Function MakePolyLineShape(oDrawDoc As Object,_
                           positions as com.sun.star.awt.PolyPolygon) As com.sun.star.drawing.PolyLineShape
  oShape = oDrawDoc.createInstance( "com.sun.star.drawing.PolyLineShape" )
  oShape.PolyPolygon = positions
  MakePolyLineShape() = oShape
End Function 


which has no syntax fault but draw nothing on the screen.
And I don't see why ! I have tried different solutions for this com.sun.star.awt.PolyPolygon which is a sequence sequence of points but whitout success ?
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Jan 31, 2004 11:15 am    Post subject: Reply with quote

I recently had written some messages regarding drawing Bezier shapes.

Drawing Bezier shapes
http://www.oooforum.org/forum/viewtopic.php?t=4927

FYI....Example use of Bezier curves in Draw macro
http://www.oooforum.org/forum/viewtopic.php?t=4745

There is one thing you'll notice in both of these examples.

The Shape is added to the page BEFORE assigning the coordinates. This is also true of Bernard Marcelly based code you posted. This might be an issue with polygons as well.

Try first adding the polygon to the page. Then assigning the coordinates.

This means to do EITHER.

1. Change your MakePolyLineShape() to not take an array of coordinates. Call the MakePolyLineShape() to make the shape. Then add it to the page. Then assign coordinates.

OR

2. Change your MakePolyLineShape() to additionally require the oDrawPage parameter so that it can add the shape to the page prior to assigning the array of coordinates.


For example...
Code:
Sub Main
   oDocument = StarDesktop.loadComponentFromUrl( "private:factory/sdraw", "_blank", 0, Array() )
   oPage = oDocument.getDrawPages().getByIndex( 0 )
   
   Dim oCoord(3) As New com.sun.star.awt.Point
   oCoord(0)=MakePoint(2000,2000)
   oCoord(1)=MakePoint(4000,4000)
   oCoord(2)=MakePoint(6000,2000)
   
   oShape=MakePolyLineShape(oDocument)
   oPage.add(oShape)
   oShape.PolyPolygon = array(oCoord())
   oShape.LineWidth = 100
   oShape.LineColor = RGB( 200, 200, 200 )
   
   ' Move the shape to a better position
   oShape.Position = MakePoint( 3000, 5000 )
End Sub

Function MakePolyLineShape( oDrawDoc As Object ) As com.sun.star.drawing.PolyLineShape
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.PolyLineShape" )
   MakePolyLineShape() = oShape
End Function 

Function MakePoint( x As Long, y As Long ) As com.sun.star.awt.Point
   Dim aPoint As New com.sun.star.awt.Point
   aPoint.x = x
   aPoint.y = y
   MakePoint() = aPoint
End Function


I multiplied all your coordinates by 10 to make the shape larger. Then I move the shape to a better position on the page. (It was a 0,0)

You will also note that the array was dimensioned as oCoord(3), but the element (3) was never assigned. Therefore it is implicitly a 0,0 point. You could replace the whole multi-line array construction with this single line...
Code:
oCoord = Array( MakePoint(2000,2000), MakePoint(4000,4000), MakePoint(6000,2000), MakePoint(0,0) )

By doing so, you no longer need parenthesis when you write
Code:
oShape.PolyPolygon = array(oCoord())

you could instead write...
Code:
oShape.PolyPolygon = array(oCoord)



Hope this helps.
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Thu Mar 25, 2004 7:23 am; edited 2 times in total
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Jan 31, 2004 11:57 am    Post subject: Reply with quote

You'll notice that I used approach 1.
1. Change your MakePolyLineShape() to not take an array of coordinates.

If I had taken approach 2 instead
2. Change your MakePolyLineShape() to additionally require the oDrawPage parameter

I would have called it DrawPolyLineShape() instead of MakePolyLineShape().

In later posts, you'll see where I will introduce a number of powerful routines with names like....

DrawFooooBarrrrr( ... )

You can see this pattern in the post
http://www.oooforum.org/forum/viewtopic.php?t=4745
that draws random bezier shapes.

Some of my DrawXxx() functions have names like.... DrawMonthShape() or DrawYearShape(), etc.

Here is one example....
Code:
Sub Main
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   For i = 1000 To 20000 Step 1000
      DrawLine( oDrawDoc, oDrawPage, 1000, i+2000, 18000, (20000-i)+2000, RGB(255*(i/20000),150,255*((20000-i)/20000)), 400 )
   Next
End Sub




'----------
'   Draw a line from x1,y1 to x2,y2.
'   This adds the line to the page.
'   The line shape is returned as the function result.
'
Function DrawLine( oDrawDoc As Object, oDrawPage As Object,_
         x1 As Long, y1 As Long, x2 As Long, y2 As Long,_
         Optional nLineColor As Long,_
         Optional nLineWidth As Long )
   ' make sure size is non-zero
'   If y2 = y1 Then y2 = y1 + 1
'   If x2 = x1 Then x2 = x1 + 1
   
   Dim oPosition As New com.sun.star.awt.Point
   Dim oSize As New com.sun.star.awt.Size
   oPosition.X = x1
   oPosition.Y = y1
   oSize.Width = x2-x1
   oSize.Height = y2-y1

   ' Create a line shape
   oLineShape = MakeLineShape( oDrawDoc, oPosition, oSize )
   ' Set line shape's properties
   If Not IsMissing( nLineColor ) Then
      oLineShape.LineColor = nLineColor
   EndIf
   If Not IsMissing( nLineWidth ) Then
      oLineShape.LineWidth = nLineWidth
   EndIf

   ' Add the line shape to the page
   oDrawPage.add( oLineShape )

   DrawLine() = oLineShape
End Function



Here is another example that builds upon the previous example....
Code:
Sub Main
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   nCenterX = oDrawPage.Width / 2
   nCenterY = oDrawPage.Height / 2
   
   nSegments = 30
   For i = 1 to nSegments
      DrawLineAngleRadians( oDrawDoc, oDrawPage, nCenterX, nCenterY,_
            (i / nSegments) * 2*PI,_
            6000,_
            RGB(255*(i/nSegments), 255*((nSegments-i)/nSegments), 190 ), 400 )
   Next
End Sub



'----------
'   Draw a line from x1,y1 in the direction of nAngle, for
'    a distance of nDistance.
'   nAngle is measured clockwise from the 3 O'Clock (east) position,
'    in radians.
'   This adds the line to the page.
'   The line shape is returned as the function result.
'
Function DrawLineAngleRadians( oDrawDoc As Object, oDrawPage As Object,_
         x1 As Long, y1 As Long, nAngle As Double, nDistance As Long,_
         Optional nLineColor As Long,_
         Optional nLineWidth As Long )
   If IsMissing( nLineColor ) Then
      nLineColor = 0 ' black
   EndIf
   If IsMissing( nLineWidth ) Then
      nLineWidth = 0
   EndIf
   
   nDX = cos( nAngle ) * nDistance
   nDY = sin( nAngle ) * nDistance
   
   DrawLineAngleRadians() = DrawLine( oDrawDoc, oDrawPage, x1, y1, x1+nDX, y1+nDY, nLineColor, nLineWidth )
End Function



Need more? Get my standard library. An outdated version is on OOoMacros.org, or get the latest from...
http://kosh.datateamsys.com/~danny/OOo/
and get the document named DannysLibrary with the latest date. I can hardly expect Russ to keep the latest version on OOoMacros.org, since DannysLibrary is updated more frequently than the front page of Slashdot. (Well, not really, but you get the idea. Smile ) If you download DannysLibrary, then try running the various "test" programs in each module. Wink Don't overlook the seperate TurtleGraphics library which is in the same document (and its test routines).
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Jan 31, 2004 1:08 pm    Post subject: Reply with quote

One more observation. The function MakePolyLineShape() has very little purpose. The line...
Code:
oShape = MakePolyLineShape(oDrawDoc )

can be trivially replaced with...
Code:
oShape = oDrawDoc.createInstance( "com.sun.star.drawing.PolyLineShape" )



One more complete example...
Code:
Sub Main
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
   oDrawPage = oDrawDoc.getDrawPages().getByIndex( 0 )
   
   oShape = oDrawDoc.createInstance( "com.sun.star.drawing.PolyLineShape" )
   oDrawPage.add( oShape )
   
   oCoord = Array(_
            MakePoint( 1000, 1000 ),_
            MakePoint( 2000, 2000 ),_
            MakePoint( 3000, 1000 ),_
            MakePoint( 0, 0 ),_
            MakePoint( 500, 2000 ),_
            MakePoint( 0, 3000 ),_
            MakePoint( 1000, 2000 ),_
            MakePoint( 2000, 3000 ),_
            MakePoint( 3000, 2000 ) )
   oShape.PolyPolygon = Array( oCoord )
   
   oShape.LineWidth = 100
   oShape.LineColor = RGB( 120, 200, 180 )
   
   oShape.Position = MakePoint( 5500, 12000 )
End Sub



Function MakePoint( x As Long, y As Long ) As com.sun.star.awt.Point
   Dim aPoint As New com.sun.star.awt.Point
   aPoint.x = x
   aPoint.y = y
   MakePoint() = aPoint
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Sun Feb 01, 2004 11:31 am    Post subject: Reply with quote

Thank you Danny, I will try all of your advices to morrow.
I have note effectively in your Bezier Shape your comment on the fact that the Shape is added to the page before assigning the coordinates but have forget it. Embarassed
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Mar 25, 2004 7:30 am    Post subject: Reply with quote

Here are some additional articles I've written on Draw, all these examples work equally well with Impress.

Here is an example of switching which page is shown on a drawing, adding shapes to layers, and making layers visible/invisible, printable/unprintable.
http://www.oooforum.org/forum/viewtopic.php?p=26524#26524

Drawing Bezier shapes
http://www.oooforum.org/forum/viewtopic.php?t=4927

FYI....Example use of Bezier curves in Draw macro
http://www.oooforum.org/forum/viewtopic.php?t=4745

You can copy and paste example code from the above articles and try running the amusing examples.



Other related articles.....

Open and Create documents, various prog. languages
http://www.oooforum.org/forum/viewtopic.php?t=5252

MakePropertyValue function
http://www.oooforum.org/forum/viewtopic.php?t=5108

Other articles....

Calc Examples
http://www.oooforum.org/forum/viewtopic.php?t=4996

Writer Examples
http://www.oooforum.org/forum/viewtopic.php?t=6049
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Mar 31, 2004 9:55 am    Post subject: Reply with quote

Here is another simple drawing example.

http://www.oooforum.org/forum/viewtopic.php?p=27227#27227

This program draws a small pie-chart.

It is simple. Easy to extend. Demonstrates basic techniques of creating drawings in a macro.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Apr 08, 2004 10:22 am    Post subject: Reply with quote

Here is a simple, self-contained example. It creates an Impress document, but the result could be a Draw document just as easily by changing the first line from "private:factory/simpress" to "private:factory/sdraw".

What this example does....
1. Create new Presentation (or drawing)
2. Print a message telling me how many pages there are.
3. Create two new pages "Slide 2" and "Slide 3"
4. Put a text shape onto each of the three pages.
5. Draw some color rectangles on Slide 2.
6. Draw some color lines on Slide 3.
7. Change the name of "Slide 2" to "John".
8. Print a message telling me if the document is in "Master Page Mode".
9. Switch the view to slide 2. (Technique 1)
10. Switch the view to slide 3. (Different technique 2)
11. (commented out) delete Slide 1.

Code:

Sub Main
   ' Create presentation
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/simpress", "_blank", 0, Array() )
   
   ' Get draw pages.
   oDrawPages = oDoc.getDrawPages()

   ' Tell me how many pages there currently are.
   Print oDrawPages.getCount(), "total pages"
   
   oPage1 = oDrawPages.getByIndex( 0 ) ' 0 means page 1
   
   ' Create a new page.
   oNewPage2 = oDrawPages.insertNewByIndex( 1 ) ' 1 means page 2
   oNewPage3 = oDrawPages.insertNewByIndex( 2 ) ' 2 means page 3   
   
   ' Put a text on page 1.
   oText = MakeTextShape( oDoc, MakePoint( 3000, 10000 ), MakeSize( 15000, 1000 ) )
   oPage1.add( oText )
   oText.getText().setString( "This is page 1." )
   
   ' Put a text on page 2.
   oText = MakeTextShape( oDoc, MakePoint( 3000, 10000 ), MakeSize( 15000, 1000 ) )
   oNewPage2.add( oText )
   oText.getText().setString( "This is page 2." )
   
   ' Put a text on page 3.
   oText = MakeTextShape( oDoc, MakePoint( 3000, 10000 ), MakeSize( 15000, 1000 ) )
   oNewPage3.add( oText )
   oText.getText().setString( "This is page 3." )
   
   ' Draw some colored rectangles on page 2.
   oSize = MakeSize( 1000, 2000 )
   For i = 0 To 15
      oShape = MakeRectangleShape( oDoc, MakePoint( 1000 + (i * 1200), 1000 ), oSize )
      oShape.FillColor = RGB( 100 + (i * 10), 220, 255 - (i * 10) )
      oNewPage2.add( oShape )
   Next

   ' Draw some lines on page 3.
   nSegments = 16
   For i = 1 to nSegments
      DrawLine( oDoc, oNewPage3,_
            1000, i*250+11500, 5000, (nSegments-i)*250+11500,_
            RGB(255*(i/nSegments),150,255*((nSegments-i)/nSegments)), 150 )
   Next
   
   ' Change the name of "Slide 2"
   oNewPage2.Name = "John"


   ' Get document controller.
   oDocCtrl = oDoc.getCurrentController()
   
   ' Tell me whether the window is in "master page" display mode.
   Print "IsMasterPageMode: ", oDocCtrl.IsMasterPageMode
   
   ' Switch the view to page 2
   oDocCtrl.CurrentPage = oNewPage2
   
   ' Switch the view to page 3
   oDocCtrl.CurrentPage = oDrawPages.getByIndex( 2 ) ' 2 means page 3
   
   ' Delete the first page.
'   oDrawPages.remove( oPage1 )
End Sub


Function MakePoint( ByVal x As Long, ByVal y As Long ) As com.sun.star.awt.Point
   oPoint = createUnoStruct( "com.sun.star.awt.Point" )
   oPoint.X = x
   oPoint.Y = y
   MakePoint() = oPoint
End Function


Function MakeSize( ByVal width As Long, ByVal height As Long ) As com.sun.star.awt.Size
   oSize = createUnoStruct( "com.sun.star.awt.Size" )
   oSize.Width = width
   oSize.Height = height
   MakeSize() = oSize
End Function


Function MakeTextShape( oDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.TextShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.TextShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeTextShape() = oShape
End Function


Function MakeRectangleShape( oDoc As Object,_
               Optional oPosition As com.sun.star.awt.Point,_
               Optional oSize As com.sun.star.awt.Size ) As com.sun.star.drawing.RectangleShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.RectangleShape" )
   If Not IsMissing( oPosition ) Then
      oShape.Position = oPosition
   EndIf
   If Not IsMissing( oSize ) Then
      oShape.Size = oSize
   EndIf
   MakeRectangleShape() = oShape
End Function


Function MakeLineShape( oDoc As Object,_
               Optional position As com.sun.star.awt.Point,_
               Optional size As com.sun.star.awt.Size ) As com.sun.star.drawing.LineShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.LineShape" )
   If Not IsMissing( position ) Then
      oShape.Position = position
   EndIf
   If Not IsMissing( size ) Then
      oShape.Size = size
   EndIf
   MakeLineShape() = oShape
End Function

'----------
'   Draw a line from x1,y1 to x2,y2.
'   This adds the line to the page.
'   The line shape is returned as the function result.
'
Function DrawLine( oDoc As Object, oDrawPage As Object,_
         ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long,_
         Optional nLineColor As Long,_
         Optional nLineWidth As Long )
   ' make sure size is non-zero
'   If y2 = y1 Then y2 = y1 + 1
'   If x2 = x1 Then x2 = x1 + 1
   
   oPosition = MakePoint( x1, y1 )
   oSize = MakeSize( x2-x1, y2-y1 )

   ' Create a line shape
   oLineShape = MakeLineShape( oDoc, oPosition, oSize )
   ' Set line shape's properties
   If Not IsMissing( nLineColor ) Then
      oLineShape.LineColor = nLineColor
   EndIf
   If Not IsMissing( nLineWidth ) Then
      oLineShape.LineWidth = nLineWidth
   EndIf

   ' Add the line shape to the page
   oDrawPage.add( oLineShape )

   DrawLine() = oLineShape
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jun 01, 2004 11:16 am    Post subject: Reply with quote

This example program demonstrates how to draw a cylinder.

Most importantly, this shows how to create a new gradient and assign it to the fill of a shape.

Code:
Sub Main
   ' Get document.
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )
'   oDoc = ThisComponent
   ' Get first page of drawing.
   oDrawPage = oDoc.getDrawPages().getByIndex( 0 )
   
   oCylinderShape = DrawCylinder( oDoc, oDrawPage )
End Sub



' This draws a cylinder, and returns the shape.
Function DrawCylinder( oDoc, oDrawPage )
   ' Cylinder parameters
   nCylLeft = 5000
   nCylTop = 6000
   nCylTotalHeight = 14000
   nCylEllipseHeight = 2000
   nCylTotalWidth = 4000
   
   ' Convenience variables, calculated from parameters
   nCylEllipseHalfHeight = Int( nCylEllipseHeight / 2 )
   nCylRectTop = nCylTop + nCylEllipseHalfHeight
   nCylRectHeight = nCylTotalHeight - nCylEllipseHeight
   nCylBotEllipseTop = nCylTop + nCylTotalHeight - nCylEllipseHeight
   
   ' Create shapes.
   oRectShape = _
         MakeRectangleShape( oDoc, _
            MakePoint( nCylLeft, nCylRectTop ), _
            MakeSize( nCylTotalWidth, nCylRectHeight ) )
   
   oTopEllipseShape = _
         MakeEllipseShape( oDoc, _
            MakePoint( nCylLeft, nCylTop ), _
            MakeSize( nCylTotalWidth, nCylEllipseHeight ) )
   
   oBotEllipseShape = _
         MakeEllipseShape( oDoc, _
            MakePoint( nCylLeft, nCylBotEllipseTop ), _
            MakeSize( nCylTotalWidth, nCylEllipseHeight ) )
   
   ' Add shapes to drawing page.
   oDrawPage.add( oRectShape )
   oDrawPage.add( oTopEllipseShape )
   oDrawPage.add( oBotEllipseShape )
   
   SetCylinderGradient( oRectShape )
   SetCylinderGradient( oBotEllipseShape )
   oTopEllipseShape.FillStyle = com.sun.star.drawing.FillStyle.SOLID
   oTopEllipseShape.FillColor = RGB( 150, 150, 150 )
   
   ' Now we need to select two shapes.
   ' Create a shape collection.
   oShapeCollection = createUnoService( "com.sun.star.drawing.ShapeCollection" )
   ' Add the shapes we want to select to the collection.
   oShapeCollection.add( oRectShape )
   oShapeCollection.add( oBotEllipseShape )
   ' Now select all of the shapes in the shape collection.
   ' (This makes the two shapes in the collection have little
   '  green handles around them.)
   DrawingSelectShapes( oDoc, oShapeCollection )
   
   ' Now merge the two currently selected shapes.
   DocumentDispatch( oDoc, ".uno:Merge" )
   ' At this point, the two shapes oRectShape and oBotEllipseShape
   '  no longer exist.
   
   ' Get the currently selected shape.
   ' (The currently selected shapes are whatever has the little green handles.)
   oSelectedShapes = DrawingGetSelection( oDoc )
   ' Since only one shape is in the selection, just get it, as shape subscript zero.
   oCylPartShape = oSelectedShapes.getByIndex( 0 )
   
   ' Move the currently selected shape Back (behind the top ellipse)
   DocumentDispatch( oDoc, ".uno:Backward" )
   
   ' Group the top ellipse and the bottom part of the cylinder
   '  to form a single group shape.
   oShapesToGroup = createUnoService( "com.sun.star.drawing.ShapeCollection" )
   oShapesToGroup.add( oCylPartShape )
   oShapesToGroup.add( oTopEllipseShape )
   oCylinderShape = oDrawPage.group( oShapesToGroup )
      
   DrawCylinder = oCylinderShape
End Function


Sub SetCylinderGradient( oShape )
   oGradient = createUnoStruct( "com.sun.star.awt.Gradient" )
   With oGradient
      .Style = com.sun.star.awt.GradientStyle.AXIAL
      .StartColor = RGB( 255, 255, 255 )
      .EndColor = RGB( 0, 0, 0 )
      .Angle = 900
      .Border = 0
      .XOffset = 0
      .YOffset = 0
      .StartIntensity = 100
      .EndIntensity = 100
      .StepCount = 128
   End With
   oShape.FillStyle = com.sun.star.drawing.FillStyle.GRADIENT
   oShape.FillGradient = oGradient
End Sub


In order to make the above work, you need some additional subroutines.

Go to this thread....
Draw: Introduction to draw and basic shapes
http://www.oooforum.org/forum/viewtopic.php?t=5383
and get these subroutines....
Function MakePoint( x As Long, y As Long ) As com.sun.star.awt.Point
Function MakeSize( width As Long, height As Long ) As com.sun.star.awt.Size
Function MakeRectangleShape( oDoc, oPosition, oSize ) As com.sun.star.drawing.RectangleShape
Function MakeEllipseShape( oDoc, oPosition, oSize ) As com.sun.star.drawing.EllipseShape

Go to this thread....
Making the Dispatcher easier to use
http://www.oooforum.org/forum/viewtopic.php?t=5058
and get these subroutines....
Sub DocumentDispatch( oDocumentFrame, cURL, cTargetFrameName, nSearchFlags, aDispatchArgs )
Sub ClipboardPaste( oDocumentFrame )
Sub ClipboardCopy( oDocumentFrame )
Sub ClipboardCut( oDocumentFrame )
Sub SelectAll( oDocumentFrame )

Go to this thread....
Document model, controller and frame
http://www.oooforum.org/forum/viewtopic.php?t=5057
and get these subroutines....
Function GetDocumentController( oDoc ) As Object
Function GetDocumentFrame( oDoc ) As Object
Function GetDocumentModel( oDoc ) As Object

Go to this thread...
Draw: duplicate selected drawing shape
http://www.oooforum.org/forum/viewtopic.php?t=5089
and get these subroutines....
Sub DrawingSelectNothing( oDocCtrl )
Sub DrawingSelectShapes( oDocCtrl, oShapes )
Function DrawingGetSelection( oDocCtrl )
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Jul 14, 2004 6:49 am    Post subject: Reply with quote

If you liked this thread, then see this for more....

Draw Examples
http://www.oooforum.org/forum/viewtopic.php?t=10795
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Thu Jul 22, 2004 4:48 am    Post subject: Export graphics Reply with quote

Danny, thanks for your examples. But how can I export graphic in another formats, i.e. Gif or WMF.

I have tried StoreAsUrl method, but produced files are corrupted.
Code:
    VariantArr := VarArrayCreate([0, 0], varVariant);
    VariantArr[0] := MakePropertyValue('FilterName', 'GIF - Graphics Interchange');
    DrawDoc.StoreAsURL('file:///'+AnsiReplaceText(SaveDialog.FileName,'\','/'), VariantArr);

_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Jul 23, 2004 9:25 am    Post subject: Reply with quote

You have the right idea for exporting. You must use a valid filter name.

Find a list here....
http://www.oooforum.org/forum/viewtopic.php?t=3549

It looks like you are using a good filtername. I see no obvious reason this should not work.

Oh, try using storeToUrl instead of storeAsUrl.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
pasha_golub
General User
General User


Joined: 29 Jun 2004
Posts: 24
Location: Ukraine

PostPosted: Tue Jul 27, 2004 2:10 am    Post subject: Reply with quote

Well, I tried StoreToUrl and StoreAsUrl but produced file is still corrupted. I found that in OOo Draw menu there is Export item, may be I should use smth like ExportToUrl?

And more, when in Draw I choose Save menu item, I can't save drawing in external formats, only native OOo draw formats. For external I should use Export menu item, as I said before.
_________________
Nullus est in vitae sensus, ipsa vera est sensus!
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jul 27, 2004 5:59 am    Post subject: Reply with quote

pasha_golub wrote:
Well, I tried StoreToUrl and StoreAsUrl but produced file is still corrupted.


In order not to add a protracted discussion of storeToUrl and storeAsUrl to this thread, I'm replying in a new thread here....
http://www.oooforum.org/forum/viewtopic.php?t=11196

Maybe we can work out what the problem is. My goal in this thread is to present as much working code as possible, rather than solve problems.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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