Previous Document Next Document

Understanding the CorelDRAW object model : Working with shapes : Creating shapes


Creating shapes

Every document is made up of shapes, or Shape objects, which are created by using the drawing tools. The shapes on a document page are stored on layers, so the various shape-creation methods belong to the Layer class.

For information on creating specific types of shapes, see the following subtopics:

 
 
 
 
 

 
Supported shapes not discussed in this section include polygons (or Polygon objects) and customized shapes (or CustomShape objects).
 
Customized shapes that are supported include tables (or TableShape objects).

 
Shapes are measured in document units. You can specify the unit of measurement for a document by using the Document.Unit property (see Setting document properties).

If you want, you can use event handlers to respond to events that are triggered by creating a shape:

 
AddinHook.ShapeCreated
 
Document.ShapeCreate
Creating rectangles

You can add rectangles (or Rectangle objects) to your documents by using one of the following methods:

 
Layer.CreateRectangle
 
Layer.CreateRectangle2
 
Layer.CreateRectangleRect

These methods return a reference to the new Shape object. They differ only in the parameters that they take, so you can choose the method that best suits your macro solution.

 
You can also use these rectangle-creation methods to create squares.

The CreateRectangle method creates a rectangle by using four parameters that specify the following:

 
the distance between the left, top, right, and bottom sides of the rectangle (in that order)
 
the corresponding edges of the page frame

The following VBA example uses the CreateRectangle method to create a 2" × 1" rectangle that is positioned 6" up from the bottom of the page frame and 3" in from the left side of the page frame:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
Set sh = ActiveLayer.CreateRectangle(3, 7, 6, 5)

The CreateRectangle2 method creates a rectangle based on the coordinates of its lower-left corner, its width, and its height.

The following VBA example uses the CreateRectangle2 method to create the same rectangle as the previous example:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
Set sh = ActiveLayer.CreateRectangle2(3, 6, 2, 1)

Finally, the CreateRectangleRect method creates a rectangle based on its bounding box (or Rect object).

These three rectangle-creation methods provide optional parameters for specifying corner roundness.

The CreateRectangle method specifies corner roundness by using parameters for the upper-left, upper-right, lower-left, and lower-right corners (in that order). These parameters take integer values (which range from the default 0 to 100) that define the radius of the four corners as a whole-number percentage of half of the length of the shortest side.

The following VBA example re-creates the 2" × 1" rectangle from the previous examples. However, this time, the four corner radii are set to 100%, 75%, 50%, and 0% of half of the length of the shortest side (that is, to 0.5", 0.375", 0.25", and a cusp):

Dim sh As Shape
ActiveDocument.Unit = cdrInch
Set sh = ActiveLayer.CreateRectangle(3, 7, 6, 5, 100, 75, 50, 0)

The CreateRectangle2 method and the CreateRectangleRect method define the corner radii in the same order as the CreateRectangle method (that is, upper-left, upper-right, lower-left, and lower-right). However, CreateRectangle2 and CreateRectangleRect take double (floating-point) values that measure the corner radii in document units.

 
When using CreateRectangle2 or CreateRectangleRect, you must limit the size of the corner radii to less than half of the length of the shortest side of the rectangle.

The following VBA example uses the CreateRectangle2 method to create the same round-cornered rectangle as the previous example:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
ActiveDocument.ReferencePoint = cdrBottomLeft
Set sh = ActiveLayer.CreateRectangle2(3, 6, 2, 1, 0.5, 0.375, 0.25, _
0)
Creating ellipses

You can add ellipses (or Ellipse objects) to your documents by using one of the following methods:

 
Layer.CreateEllipse
 
Layer.CreateEllipse2
 
Layer.CreateEllipseRect

These methods return a reference to the new Shape object. They differ only in the parameters that they take, so you can choose the method that best suits your macro solution.

 
You can also use the ellipse-creation methods to create circles, arcs, and pie shapes.

The CreateEllipse method creates an ellipse by using four parameters that specify the following:

 
the distance between the left, top, right, and bottom sides of the ellipse (in that order)
 
the corresponding edges of the page frame

The following VBA example creates a 50-millimeter circle:

Dim sh As Shape
ActiveDocument.Unit = cdrMillimeter
Set sh = ActiveLayer.CreateEllipse(75, 150, 125, 100)

The CreateEllipse2 method creates an ellipse based on its center point, its horizontal radius, and its vertical radius. (If only one radius is given, a circle is created.)

The following VBA example uses the CreateEllipse2 method to create an ellipse:

Dim sh As Shape
ActiveDocument.Unit = cdrMillimeter
Set sh = ActiveLayer.CreateEllipse2(100, 125, 50, 25)

The following VBA example uses the CreateEllipse2 method to create the same 50-millimeter circle as the CreateEllipse example:

Dim sh As Shape
ActiveDocument.Unit = cdrMillimeter
Set sh = ActiveLayer.CreateEllipse2(100, 125, 25)

Finally, the CreateEllipseRect method creates an ellipse based on its bounding box (or Rect object).

These three ellipse-creation methods provide three optional parameters for creating an arc or a pie shape. The StartAngle and EndAngle parameters — which are double values that are measured with zero being horizontally right on the page and with positive values being degrees from zero and moving counterclockwise — are used to define the start angle and end angle of the shape (respectively). In addition, the Pie parameter — which is a Boolean value — defines whether the shape is an arc (False) or a pie shape (True).

The following VBA code uses the CreateEllipse method to create a “C” shape:

Dim sh As Shape
ActiveDocument.Unit = cdrMillimeter
Set sh = ActiveLayer.CreateEllipse(75, 150, 125, 100, 60, 290, False)
Creating lines and curves

You can add lines and curves (or Curve objects) to your documents. To create a line or a curve, you must first create a Curve object “in memory” by using the Application.CreateCurve method.

Each Curve object has at least one subpath (or SubPath object). You can add a subpath to a line or a curve by using the Curve.CreateSubPath method.

Each SubPath object has at least one segment (or Segment object), which can be line-type or curve-type. You can add a line-type segment to the end of a subpath by using the SubPath.AppendLineSegment method; you can add a curve-type segment by using the SubPath.AppendCurveSegment method or the SubPath.AppendCurveSegment2 method.

 
The SubPath.AppendLineSegment method requires one set of Cartesian coordinates, which defines the end of the new segment.
 
The SubPath.AppendCurveSegment method requires one set of Cartesian coordinates, which defines the end of the new segment. Optionally, you can specify two sets of polar coordinates if you want to define the lengths and angles of the starting and ending control handles for the segment.
 
The SubPath.AppendCurveSegment2 method requires three sets of Cartesian coordinates: one to define the end of the new segment, and two to define the positions of the starting and ending control handles for the segment.

 
You can add a segment to the beginning of a subpath by setting the AppendAtBeginning parameter for the segment-creation method to True.

Finally, each Segment object has at least one node (or Node object). You can add a node to a segment by using the Segment.AddNodeAt method.

 
You can close a Curve object by setting its Closed property to True.

After creating a curve “in memory,” you can apply it to a layer by using the Layer.CreateCurve method. A reference to the new Shape object is returned.

The following VBA code creates a D-shaped curve that is closed:

Dim sh As Shape, spath As SubPath, crv As Curve
ActiveDocument.Unit = cdrCentimeter
Set crv = Application.CreateCurve(ActiveDocument)
'Create Curve object
Set spath = crv.CreateSubPath(6, 6) ' Create a SubPath
spath.AppendLineSegment 6, 3 ' Add the short vertical segment
spath.AppendCurveSegment 3, 0, 2, 270, 2, 0 ' Lower curve
spath.AppendLineSegment 0, 0 ' Bottom straight edge
spath.AppendLineSegment 0, 9 ' Left straight edge
spath.AppendLineSegment 3, 9 ' Top straight edge
spath.AppendCurveSegment 6, 6, 2, 0, 2, 90 ' Upper curve
spath.Closed = True ' Close the curve
Set sh = ActiveLayer.CreateCurve(crv) ' Create curve shape

The Layer class provides three additional methods that act as shortcuts for creating a basic line or basic curve that has a single segment on a single subpath:

 
Layer.CreateLineSegment — creates a basic line based on the given starting point and ending point
 
Layer.CreateCurveSegment — creates a basic curve based on the given starting point and ending point and, optionally, on the lengths and angles of the starting and ending control handles for the curve
 
Layer.CreateCurveSegment2 — creates a basic curve based on the given starting point and ending point and on the given positions of the starting and ending control handles for the curve

 
These three methods return a reference to the new Shape object.
Creating text objects

You can add text (or Text objects) to your documents. Two types of text are supported: artistic text and paragraph text. An artistic-text object is a short line of text to which you can apply graphical effects. In contrast, a paragraph-text object is a large block of text — stored in a rectangular container called a “frame” — to which you can apply more complex formatting.

To create an artistic-text object, you can use one of the following methods:

 
Layer.CreateArtisticText — creates basic artistic text
 
Layer.CreateArtisticTextWide — creates artistic text that is in Unicode format

 
Both of these methods require you to specify the position and content of the artistic-text object. Optionally, both of these methods let you set such text attributes as font style, font size, formatting, and alignment. In addition, both of these methods return a reference to the new Shape object.

The following VBA code uses the CreateArtisticText method to create a basic artistic-text object that places the words “Hello World” at the specified position:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
Set sh = ActiveLayer.CreateArtisticText(1, 4, "Hello World")

You can fit artistic text to a path by using the Text.FitTextToPath method, which simply attaches the text to the outline of a shape such that the text flows along the path of that shape.

The following VBA code creates a new artistic-text object and attaches it to the selected shape:

Dim sh As Shape, sPath As Shape
ActiveDocument.Unit = cdrInch
Set sPath = ActiveShape
Set sh = ActiveLayer.CreateArtisticText(1, 4, "Hello World")
sh.Text.FitToPath sPath

To create a paragraph-text object, you can use one of the following methods:

 
Layer.CreateParagraphText — creates basic paragraph text
 
Layer.CreateParagraphTextWide — creates paragraph text that is in Unicode format

 
Both of these methods require you to specify the size of the paragraph-text frame by setting its position from the left, top, right, and bottom sides of the page frame (in that order). Optionally, both of these methods let you specify the desired text and set such text attributes as font style, font size, formatting, and alignment. In addition, both of these methods return a reference to the new Shape object.

The following VBA code uses the CreateParagraphText method to create a basic paragraph-text object that centers the words “Hi There” in a frame of the specified size:

Dim sh As Shape
ActiveDocument.Unit = cdrInch
Set sh = ActiveLayer.CreateParagraphText(1, 4, 5, 2, "Hi There", _
Alignment := cdrCenterAlignment)

You can format existing paragraph text by using text ranges (or TextRange objects). Text ranges are handled in two ways, both of which involve frames (or TextFrame objects):

 
“frames” method — The Text.Frames property represents a series of text frames, each of which has its own text range.
 
“story” method — The Text.Story property represents a text range that includes all text in a series of text frames.

A text range can be treated as a single block of text, such that any changes to text properties (such as font style and font size) are applied to all text in that text range. Alternatively, a text range can be broken down into the following smaller text ranges:

 
columns (or TextColumns objects)
 
paragraphs (or TextParagraphs objects)
 
lines (or TextLines objects)
 
words (or TextWords objects)
 
characters (or TextCharacters objects)

 
The object model supports all paragraph-formatting options and character-formatting options that are offered by the application.

The following VBA code formats the specified text range by using the Text.Story property. The first paragraph of the story is changed to a heading style and the second and third paragraphs into a body-text style:

Dim txt As TextRange
' Format the first paragraph
Set txt = ActiveShape.Text.Story.Paragraphs(1)
txt.ChangeCase cdrTextUpperCase
txt.Font = "Verdana"
txt.Size = 18
txt.Bold = True
' Format the second and third paragraphs
Set txt = ActiveShape.Text.Story.Paragraphs(2, 2)
txt.Font = "Times New Roman"
txt.Size = 12
txt.Style = cdrNormalFontStyle

You can place selected text inside closed shapes by using the Shape.PlaceTextInside method.

The following VBA code creates a 5" × 2" ellipse and places the selected text inside it:

Dim txt As Shape, sh As Shape
ActiveDocument.Unit = cdrInch
Set txt = ActiveShape
Set sh = ActiveLayer.CreateEllipse(0, 2, 5, 0)
sh.PlaceTextInside txt
Creating symbols

A symbol (or Symbol object) is a reusable graphic element that is defined in a symbol library. Using symbols in your documents provides the following benefits:

 
lower file-size — Each symbol is defined only once, regardless of how many actual instances of that symbol appear in the document.
 
increased productivity — Any changes made to a symbol definition are automatically propagated to all instances of that symbol in the document.
 
improved workflow — Symbol libraries are a convenient way to store and reuse common graphic elements.

Symbol libraries come in two varieties: external and internal.

External symbol libraries use the filename extension CSL and contain symbol definitions that must be manually added to the workspace at the application level. You cannot modify a symbol that is defined in an external library unless you open the associated external library (CSL) file; simply importing the file as a library does not let you modify its contents.

 
External symbol libraries must be published to a location that all users can access. A common mapped drive is a good solution, but a corporate intranet is a better one. However, if the security of the symbols is not important, the best solution is a corporate Internet site.

Internal symbol libraries exist at the document level. Defining a new symbol in a document — or adding an instance of an external-library symbol to a document — automatically adds that symbol to the internal library for that document. For this reason, each document has its own unique internal symbol library.

 
Inserting an instance of a symbol from an external symbol library creates a link to the definition for that symbol in that external symbol library. If, at any point, the document cannot access the external symbol library, the symbol definition in the internal symbol library for that document is used instead.

The Application.SymbolLibraries property contains the collection of all external symbol libraries (or SymbolLibrary objects) that are available to the application; the Document.SymbolLibrary property returns just the internal symbol library for that document. The SymbolLibrary.Symbols property contains the collection of all symbol definitions (or SymbolDefinition objects) in that symbol library. A SymbolDefinition object is also returned by the Symbol.Defintion property; therefore, you can modify the defintion of a symbol by using the various properties and methods of the SymbolDefinition class.

 
To remove a symbol definition from the internal symbol library for a document, you must delete all instances of the symbol from the document and then run the SymbolLibrary.PurgeUnusedSymbols method. Simply removing all instances of a symbol from a document does not automatically remove its symbol definition from the internal symbol library for that document.

The following VBA code demonstrates the basics of using symbols:

Sub AddRemoveSymbols()
Dim objSymLibSwitchA As SymbolLibrary
Dim shpSymBreaker2 As Shape, shpSymBreaker2A As Shape
ActiveDocument.Unit = cdrMillimeter
'Add the switchesA external symbol library to the global
'workspace.
Set objSymLibSwitchA = SymbolLibraries.Add _
("C:\libs\switches\switchesA.csl")
'Add the breaker2 symbol to the active layer.
'NOTE: This automatically adds the symbol definition to the
'internal symbol library for the document.
Set shpSymBreaker2 = ActiveLayer.CreateSymbol(15, 20, _
"breaker2", SymbolLibraries("switchesA"))
'Add another instance of the breaker2 symbol, this time from the
'internal symbol library. NOTE: We did not specify a library, so
'the library for the local document is used by default.
Set shpSymBreaker2A = ActiveLayer.CreateSymbol(30, 20, _
"breaker2")
'Remove the switchesA library from the global workspace.
SymbolLibraries("switchesA").Delete
'Delete the two breaker2 symbols.
shpSymBreaker2.Delete
shpSymBreaker2A.Delete
'At this point, the internal symbol library for the document
'still has the definition of breaker2 stored. To remove this
'definition, we must purge the unused symbols from the library.
'The definition is unused because there are no instances that
'reference it.
ActiveDocument.SymbolLibrary.PurgeUnusedSymbols
End Sub

A symbol can contain (or “nest”) other symbols. A top-level symbol can contain symbols, and each of those symbols can contain a symbol, and so forth. In the object model, the SymbolDefinition.NestedSymbols property returns (as a SymbolDefinitions object) the collection of nested symbols for a symbol definition. While there is no restriction on how many nesting levels can be created, the symbol cannot be rendered without access to its symbol definition (whether external or internal). In addition, even if the first and second nesting layers of a symbol are rendered correctly, a symbol on the third nesting layer may not be rendered correctly without access to its required symbol definition.

Symbols and nested symbols

The following VBA code demonstrates the basics of using nested symbols:

Sub MakeNestedSymbol()
Dim shp1 As Shape, shp2 As Shape, shp3 As Shape, shpSym As Shape
Dim shpRng As New ShapeRange
'Create a pair of rectangles and a circle.
Set shp1 = ActiveLayer.CreateRectangle2(0, 0, 10, 20)
Set shp2 = ActiveLayer.CreateRectangle2(50, 50, 20 ,10)
Set shp3 = ActiveLayer.CreateEllipse(10, 10, 20)
'Make a symbol out of the circle. NOTE: This circle is
'automatically added to the internal symbol library for the
'document.
Set shpSym = shp3.ConvertToSymbol("circle")
'Add the rectangles and the circle symbol to a shape range.
shpRng.Add shp1
shpRng.Add shp2
shpRng.Add shpSym
'Convert the shape range into a symbol. NOTE: This symbol is
'added to the internal symbol library for the document. It is
'also is a nested symbol because it contains the symbol circle.
shpRng.ConvertToSymbol "shapes"
End Sub

Previous Document Next Document Back to Top

Copyright 2013 Corel Corporation. All rights reserved.