Editing Drawing Objects

From Apache OpenOffice Wiki
Jump to: navigation, search


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 the pages in the drawing document.


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