Difference between revisions of "Documentation/BASIC Guide/Editing Drawing Objects"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Searching and Replacing)
(Corrected error in last line of the page)
(8 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
}}
 
}}
 
{{DISPLAYTITLE:Editing Drawing Objects}}
 
{{DISPLAYTITLE:Editing Drawing Objects}}
 +
 
== Grouping Objects ==
 
== Grouping Objects ==
  
Line 12: Line 13:
  
 
The following example combines two drawing objects:
 
The following example combines two drawing objects:
<source lang="vb">
+
 
Dim Doc As Object
+
<source lang="oobas">
Dim Page As Object
+
Dim Doc As Object
Dim Square As Object
+
Dim Page As Object
Dim Circle As Object
+
Dim Square As Object
Dim Shapes As Object
+
Dim Circle As Object
Dim Group As Object
+
Dim Shapes As Object
Dim Point As New com.sun.star.awt.Point
+
Dim Group As Object
Dim Size As New com.sun.star.awt.Size
+
Dim Point As New com.sun.star.awt.Point
Dim NewPos As New com.sun.star.awt.Point
+
Dim Size As New com.sun.star.awt.Size
Dim Height As Long
+
Dim NewPos As New com.sun.star.awt.Point
Dim Width As Long
+
Dim Height As Long
+
Dim Width As Long
Doc = StarDesktop.CurrentComponent
+
Page = Doc.drawPages(0)
+
Point.x = 3000
+
Point.y = 3000
+
Size.Width = 3000
+
Size.Height = 3000
+
' create square drawing element
+
Square = Doc.createInstance("com.sun.star.drawing.RectangleShape")
+
Square.Size = Size
+
Square.Position = Point
+
Square.FillColor = RGB(255,128,128)
+
Page.add(Square)
+
 
   
 
   
' create circle drawing element
+
Doc = ThisComponent
Circle = Doc.createInstance("com.sun.star.drawing.EllipseShape")
+
Page = Doc.DrawPages(0)
Circle.Size = Size
+
Point.x = 3000
Circle.Position = Point
+
Point.y = 3000
Circle.FillColor = RGB(255,128,128)  
+
Size.Width = 3000
Circle.FillColor = RGB(0,255,0)
+
Size.Height = 3000
Page.add(Circle)
+
' create square drawing element
+
Square = Doc.createInstance("com.sun.star.drawing.RectangleShape")
' combine square and circle drawing elements
+
Square.Size = Size
Shapes = createUnoService("com.sun.star.drawing.ShapeCollection")
+
Square.Position = Point
Shapes.add(Square)
+
Square.FillColor = RGB(255,128,128)
+
Page.add(Square)
Shapes.add(Circle)
+
 
Group = Page.group(Shapes)
+
' create circle drawing element
' centre combined drawing elements
+
Circle = Doc.createInstance("com.sun.star.drawing.EllipseShape")
Height = Page.Height
+
Circle.Size = Size
Width = Page.Width
+
Circle.Position = Point
NewPos.X = Width / 2
+
Circle.FillColor = RGB(255,128,128)  
NewPos.Y = Height / 2
+
Circle.FillColor = RGB(0,255,0)
Height = Group.Size.Height
+
Page.add(Circle)
Width = Group.Size.Width
+
 
NewPos.X = NewPos.X - Width / 2
+
' combine square and circle drawing elements
NewPos.Y = NewPos.Y - Height / 2
+
Shapes = createUnoService("com.sun.star.drawing.ShapeCollection")
Group.Position = NewPos
+
Shapes.add(Square)
 +
 
 +
Shapes.add(Circle)
 +
Group = Page.group(Shapes)
 +
' centre combined drawing elements
 +
Height = Page.Height
 +
Width = Page.Width
 +
NewPos.X = Width / 2
 +
NewPos.Y = Height / 2
 +
Height = Group.Size.Height
 +
Width = Group.Size.Width
 +
NewPos.X = NewPos.X - Width / 2
 +
NewPos.Y = NewPos.Y - Height / 2
 +
Group.Position = NewPos
 
</source>
 
</source>
  
Line 79: Line 81:
 
The following example creates a rectangle and rotates it by 30 degrees using the <tt>RotateAngle</tt> property:
 
The following example creates a rectangle and rotates it by 30 degrees using the <tt>RotateAngle</tt> property:
  
<source lang="vb">
+
<source lang="oobas">
Dim Doc As Object
+
Dim Doc As Object
Dim Page As Object
+
Dim Page As Object
Dim RectangleShape As Object
+
Dim RectangleShape As Object
Dim Point As New com.sun.star.awt.Point
+
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
+
Dim Size As New com.sun.star.awt.Size
+
 
Point.x = 1000
+
Point.x = 1000
Point.y = 1000
+
Point.y = 1000
Size.Width = 10000
+
Size.Width = 10000
Size.Height = 10000
+
Size.Height = 10000
+
 
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
Page = Doc.drawPages(0)
+
Page = Doc.DrawPages(0)
+
 
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
+
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
+
RectangleShape.Size = Size
RectangleShape.Position = Point
+
RectangleShape.Position = Point
+
 
RectangleShape.RotateAngle = 3000
+
RectangleShape.RotateAngle = 3000
+
 
Page.add(RectangleShape)
+
Page.add(RectangleShape)
 
</source>
 
</source>
  
 
The next example creates the same rectangle as in the previous example, but instead shears it through 30 degrees using the <tt>ShearAngle</tt> property.
 
The next example creates the same rectangle as in the previous example, but instead shears it through 30 degrees using the <tt>ShearAngle</tt> property.
  
<source lang="vb">
+
<source lang="oobas">
Dim Doc As Object
+
Dim Doc As Object
Dim Page As Object
+
Dim Page As Object
Dim RectangleShape As Object
+
Dim RectangleShape As Object
Dim Point As New com.sun.star.awt.Point
+
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
+
Dim Size As New com.sun.star.awt.Size
+
 
Point.x = 1000
+
Point.x = 1000
Point.y = 1000
+
Point.y = 1000
Size.Width = 10000
+
Size.Width = 10000
Size.Height = 10000
+
Size.Height = 10000
+
 
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
Page = Doc.drawPages(0)
+
Page = Doc.DrawPages(0)
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
+
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
+
RectangleShape.Size = Size
RectangleShape.Position = Point
+
RectangleShape.Position = Point
+
 
RectangleShape.ShearAngle = 3000
+
RectangleShape.ShearAngle = 3000
+
 
Page.add(RectangleShape)
+
Page.add(RectangleShape)
 
</source>
 
</source>
  
Line 132: Line 134:
 
As in text documents, drawing documents provide a function for searching and replace. This function is similar to the one that is used in text documents as described in [[Documentation/BASIC_Guide/Text_Documents|Text Documents]]. However, in drawing documents the descriptor objects for searching and replacing are not created directly through the document object, but rather through the associated character level. The following example outlines the replacement process within a drawing:
 
As in text documents, drawing documents provide a function for searching and replace. This function is similar to the one that is used in text documents as described in [[Documentation/BASIC_Guide/Text_Documents|Text Documents]]. However, in drawing documents the descriptor objects for searching and replacing are not created directly through the document object, but rather through the associated character level. The following example outlines the replacement process within a drawing:
  
<source lang="vb">
+
<source lang="oobas">
Dim Doc As Object
+
Dim Doc As Object
Dim Page As Object
+
Dim Page As Object
Dim ReplaceDescriptor As Object
+
Dim ReplaceDescriptor As Object
Dim I As Integer
+
Dim I As Integer
+
 
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
Page = Doc.drawPages(0)
+
Page = Doc.DrawPages(0)
+
 
ReplaceDescriptor = Page.createReplaceDescriptor()
+
ReplaceDescriptor = Page.createReplaceDescriptor()
ReplaceDescriptor.SearchString = "is"
+
ReplaceDescriptor.SearchString = "is"
ReplaceDescriptor.ReplaceString = "was"
+
ReplaceDescriptor.ReplaceString = "was"
+
 
For I = 0 to Doc.drawPages.Count - 1
+
For I = 0 to Doc.DrawPages.Count - 1
    Page = Doc.drawPages(I)
+
  Page = Doc.DrawPages(I)
    Page.ReplaceAll(ReplaceDescriptor)  
+
  Page.ReplaceAll(ReplaceDescriptor)  
Next I
+
Next I
 
</source>
 
</source>
  
This code uses the first <tt>DrawPage</tt> of the document to create a <tt>ReplaceDescriptor</tt> and then applies this descriptor in a loop to all of the pages in the drawing document.
+
This code uses the first page of the document to create a <tt>ReplaceDescriptor</tt> and then applies this descriptor in a loop to all of the pages in the drawing document.
  
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Editing Drawing Objects}}
 
{{PDL1}}
 
{{PDL1}}

Revision as of 18:42, 25 September 2009


Grouping Objects

In many situations, it is useful to group several individual drawing objects together so that they behave as a single large object.

The following example combines two drawing objects:

Dim Doc As Object
Dim Page As Object
Dim Square As Object
Dim Circle As Object
Dim Shapes As Object
Dim Group As Object
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
Dim NewPos As New com.sun.star.awt.Point
Dim Height As Long
Dim Width As Long
 
Doc = ThisComponent
Page = Doc.DrawPages(0)
Point.x = 3000
Point.y = 3000
Size.Width = 3000
Size.Height = 3000
' create square drawing element
Square = Doc.createInstance("com.sun.star.drawing.RectangleShape")
Square.Size = Size
Square.Position = Point
Square.FillColor = RGB(255,128,128) 
Page.add(Square)
 
' create circle drawing element
Circle = Doc.createInstance("com.sun.star.drawing.EllipseShape")
Circle.Size = Size
Circle.Position = Point
Circle.FillColor = RGB(255,128,128) 
Circle.FillColor = RGB(0,255,0)
Page.add(Circle)
 
' combine square and circle drawing elements
Shapes = createUnoService("com.sun.star.drawing.ShapeCollection")
Shapes.add(Square)
 
Shapes.add(Circle)
Group = Page.group(Shapes)
' centre combined drawing elements
Height = Page.Height
Width = Page.Width
NewPos.X = Width / 2
NewPos.Y = Height / 2
Height = Group.Size.Height
Width = Group.Size.Width
NewPos.X = NewPos.X - Width / 2
NewPos.Y = NewPos.Y - Height / 2
Group.Position = NewPos

This code creates a rectangle and a circle and inserts them into a page. It then creates an object that supports the com.sun.star.drawing.ShapeCollection service and uses the Add method to add the rectangle and the circle to this object. The ShapeCollection is added to the page using the Group method and returns the actual Group object that can be edited like an individual Shape.

If you want to format the individual objects of a group, apply the formatting before you add them to the group. You cannot modify the objects once they are in the group.

Rotating and Shearing Drawing Objects

All of the drawing objects that are described in the previous sections can also be rotated and sheared using the com.sun.star.drawing.RotationDescriptor service.

The service provides the following properties:

RotateAngle (Long)
rotary angle in hundredths of a degree
ShearAngle (Long)
shear angle in hundredths of a degree

The following example creates a rectangle and rotates it by 30 degrees using the RotateAngle property:

Dim Doc As Object
Dim Page As Object
Dim RectangleShape As Object
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
 
Point.x = 1000
Point.y = 1000
Size.Width = 10000
Size.Height = 10000
 
Doc = ThisComponent
Page = Doc.DrawPages(0)
 
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
RectangleShape.Position = Point
 
RectangleShape.RotateAngle = 3000
 
Page.add(RectangleShape)

The next example creates the same rectangle as in the previous example, but instead shears it through 30 degrees using the ShearAngle property.

Dim Doc As Object
Dim Page As Object
Dim RectangleShape As Object
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
 
Point.x = 1000
Point.y = 1000
Size.Width = 10000
Size.Height = 10000
 
Doc = ThisComponent
Page = Doc.DrawPages(0)
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
RectangleShape.Position = Point
 
RectangleShape.ShearAngle = 3000
 
Page.add(RectangleShape)

Searching and Replacing

As in text documents, drawing documents provide a function for searching and replace. This function is similar to the one that is used in text documents as described in Text Documents. However, in drawing documents the descriptor objects for searching and replacing are not created directly through the document object, but rather through the associated character level. The following example outlines the replacement process within a drawing:

Dim Doc As Object
Dim Page As Object
Dim ReplaceDescriptor As Object
Dim I As Integer
 
Doc = ThisComponent
Page = Doc.DrawPages(0)
 
ReplaceDescriptor = Page.createReplaceDescriptor()
ReplaceDescriptor.SearchString = "is"
ReplaceDescriptor.ReplaceString = "was"
 
For I = 0 to Doc.DrawPages.Count - 1
   Page = Doc.DrawPages(I)
   Page.ReplaceAll(ReplaceDescriptor) 
Next I

This code uses the first page of the document to create a ReplaceDescriptor and then applies this descriptor in a loop to all of the pages in the drawing document.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools