编辑绘图对象
From Apache OpenOffice Wiki
组合对象
在很多情况下,可以将几个单独的绘图对象组合起来作为一个较大的对象进行处理,此操作非常有用。
以下示例将两个绘图对象组合在一起:
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 = 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
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
此代码创建了一个矩形和一个圆,并将其插入到页面中。然后,创建一个支持 com.sun.star.drawing.ShapeCollection 服务的对象,并使用 Add 方法将矩形和圆添加到此对象中。将使用 Group 方法在页面中添加 ShapeCollection,它返回可像单个 Shape 那样编辑的实际 Group 对象。
如果要为组中的单个对象设置格式,应在将其添加到组中之前应用格式。在将对象添加到组中后,将无法对其进行修改。
旋转和修剪绘图对象
也可以使用 com.sun.star.drawing.RotationDescriptor 服务旋转和修剪前面几节中介绍的所有绘图对象。
该服务提供了以下属性:
- RotateAngle (Long)
- 旋转角度,以百分之一度为单位
- ShearAngle (Long)
- 修剪角度,以百分之一度为单位
以下示例创建一个矩形,并使用 RotateAngle 属性将其旋转 30 度:
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 = StarDesktop.CurrentComponent
Page = Doc.drawPages(0)
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
RectangleShape.Position = Point
RectangleShape.RotateAngle = 3000
Page.add(RectangleShape)
接下来的示例创建与上一示例相同的矩形,但使用 ShearAngle 属性将其修剪 30 度。
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 = StarDesktop.CurrentComponent
Page = Doc.drawPages(0)
RectangleShape = Doc.createInstance("com.sun.star.drawing.RectangleShape")
RectangleShape.Size = Size
RectangleShape.Position = Point
RectangleShape.ShearAngle = 3000
Page.add(RectangleShape)
搜索和替换
与文本文档一样,绘图文档也提供了搜索和替换功能。此功能类似于文本文档中使用的功能,如文本文档中所述。不过,在绘图文档中,用于搜索和替换的描述符对象不是直接通过文档对象创建的,而是通过关联的字符级别创建的。以下示例简要说明绘图中的替换过程:
Dim Doc As Object
Dim Page As Object
Dim ReplaceDescriptor As Object
Dim I As Integer
Doc = StarDesktop.CurrentComponent
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
此代码使用文档的第一个 DrawPage 创建 ReplaceDescriptor,然后通过循环将此描述符应用于绘图文档中的所有页面。
Content on this page is licensed under the Public Documentation License (PDL). |