Every document is made up of shapes, or Shape objects, which are created by using the drawing tools. Any changes that are made to the properties of a shape — such as by moving the shape, changing its size, or giving it a new fill — are immediately visible to the object model.
The shapes on a document page are stored on layers. Each shape is a member of the Layer.Shapes collection for the layer on which it appears. The shapes in a Layer.Shapes collection appear in the order in which they appear on that layer — the first shape is the one at the top of the "stack," and the last shape is the one at the bottom. If shapes are added, reordered, or deleted, the affected Layer.Shapes collection is immediately updated to reflect the new shape order of that layer.
In addition, each document page has a Shapes collection, which contains all Layer.Shapes collections for that page. The first shape in a Page.Shapes collection is the one at the very top of that page, and the last shape is the one at the very bottom.
Corel DESIGNER and CorelDRAW provide a number of properties, methods, and events for working with shapes, the most useful of which are listed in the following table.
For detailed information on any property, method, or event, see the API documentation.
For more information on shape-related activities, see the following subtopics:
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 includecallout shapes (or CalloutShape objects in Corel DESIGNER) and 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:
Creating rectangles
You can add rectangles (or Rectangle objects) to your documents by using one of the following methods:
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 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:
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 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:
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:
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:
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):
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:
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 dimension objects
You can add dimension objects (or Dimension objects) to your Corel DESIGNER documents. Dimension objects let you display the size of a shape or the distance or angle between multiple shapes. Dimension objects are widely used in both technical illustration and computer-aided design (CAD).
A dimension object consists of two parts:
Corel DESIGNER supports both linear dimension objects (or DimensionLinear objects) and angular dimension objects (or DimensionAngular objects). You can create dimension objects by using the following methods:
The properties of a dimension object are set upon its creation. However, you can adjust those properties at any time by accessing the various properties of the corresponding DimensionLinear object or DimensionAngular object.
You can link a dimension object to its target shape, such that the dimension line and its measurement are updated as the shape is manipulated. To create a linked dimension object, you must assign the end points of the dimension line to the snap points of the target shape.
The following VBA example illustrates the concept of a linked dimension object. A linear dimension object and an angular dimension object are created and then linked to the specified rectangle. If the rectangle is moved, the dimension lines continue to "stick" to the snap points; if the rectangle is resized, the dimension measurements change accordingly.
Sub Example1() Dim shp1 As Shape, shp2 As Shape, shp3 As Shape Dim Vertex As New SnapPoint ' Create a rectangle Application.Unit = cdrInch ActiveDocument.Unit = cdrInch Set shp1 = ActiveLayer.CreateRectangle2(0, 0, 5, 2.5) ' Create a linear dimension that is linked to snap points of the ' rectangle and get a reference to the Dimension object Set shp2 = _ ActiveLayer.CreateLinearDimension(cdrDimensionVertical, _ shp1.SnapPoints(2), shp1.SnapPoints(3), TextX:=2) ' Modify the properties of the dimension object With shp2.Dimension 'Set the dimension's units to inches and display them .Linear.Units = cdrInch .Linear.ShowUnits = True End With ' Create an angular dimension on the rectangle's other snap points ' and set its properties at creation time Vertex.PositionX = ActiveDocument.DrawingOriginX - 2 Vertex.PositionY = ActiveDocument.DrawingOriginY Set shp3 = ActiveLayer.CreateAngularDimension(Vertex, _ shp1.SnapPoints(2), shp1.SnapPoints(3), 0, 0, _ Precision:=3, ShowUnits:=True, _ Units:=cdrDimensionUnitDegrees, OutlineWidth:=0.014, _ Arrows:=ArrowHeads(1), TextSize:=10) End Sub
Alternatively, you can create a static dimension object that is not linked to any shape, such that the dimension line and its measurement do not change as the shapes in the document are manipulated. To create a static dimension object, you must create free snap points on the layer and then attach the dimension line to those snap points.
The following VBA example illustrates the concept of a static dimension object. The dimension object is not linked to any shape, so it measures a static distance. Only changing the location of the free snap points for the dimension line modifies the measurement of the dimension object.
Sub Example2() Dim Shape1 As Shape Dim Snap1 As New SnapPoint, Snap2 As New SnapPoint 'Initialize the snap points on the canvas Snap1.PositionX = 0 Snap1.PositionY = 0 Snap2.PositionX = 0 Snap2.PositionY = 10 'Create a linear dimension linked to the rectangle's snap points 'and get a reference to the dimension object Set Shape1 = _ ActiveLayer.CreateLinearDimension(cdrDimensionVertical, _ Snap1, Snap2) ' Modify the dimension object's properties With Shape1.Dimension 'Set the dimension's units to inches and display them .Linear.Units = cdrInch .Linear.ShowUnits = True End With End Sub
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:
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
Each Shape object has a read-only Type property, which returns the shape type (for example, rectangle, ellipse, curve, text, or group). The properties and methods that are available to a shape vary with shape type; therefore, it’s a good idea to determine the shape type before applying any properties or methods to that shape.
The following sample VBA code determines whether a shape is text. If the shape is text, the code determines whether it is artistic text or paragraph text. If the shape is artistic text, it is rotated by 10 degrees.
Dim sh As Shape Set sh = ActiveShape If sh.Type = cdrTextShape Then If sh.Text.IsArtisticText = True Then sh.Rotate 10 End If End If
Each shape is a member of the Layer.Shapes collection for the layer on which it appears. The shapes in a Layer.Shapes collection appear in the order in which they appear on that layer — the first shape is the one at the top of the "stack," and the last shape is the one at the bottom. If shapes are added, reordered, or deleted, the affected Layer.Shapes collection is immediately updated to reflect the new shape order of that layer.
If you want to access individual shapes, you can select them. When you select shapes, you create a "selection" that contains only those shapes.
The Shape.Selected property takes a Boolean value that indicates whether a shape is selected: True if the shape is selected, False otherwise. You can select a shape by changing the value of its Selected property to True; this technique adds the shape to the current selection — that is, rather than creating a new selection that contains only that shape.
If you want to create a new selection from a shape — that is, by selecting a specified shape and deselecting any other selected shapes — you can use the Shape.CreateSelection method, as in the following VBA example:
Dim sh As Shape Set sh = ActivePage.Shapes(1) If sh.Selected = False Then sh.CreateSelection
You can select multiple shapes by using the ShapeRange.CreateSelection method. The following VBA code uses this method — in combination with the Shapes.All method — to select all shapes on the active page (except those on locked or hidden layers):
ActivePage.Shapes.All.CreateSelection
You can access a selection in one of two ways:
To summarize, you can access a selection directly, or you can access a copy of that selection; alternatively, you can access a subset of the shapes in a selection. You can also reorder the shapes in a selection. When you no longer require a selection, you can deselect one or all of its shapes.
For more information on selecting shapes, see the following subtopics:
Accessing selections directly
As previously discussed, you can use the Document.Selection method to access the contents of a selection directly. A Shape object is returned, and this Shape object is updated to reflect any changes made to the selection.
The following VBA code returns the selection for the active document:
Dim sel As Shape Set sel = ActiveDocument.Selection
The shortcut for ActiveDocument.Selection is ActiveSelection, which returns a Shape object of subtype cdrSelectionShape. The Shape subtype has a member collection called Shapes, which represents a collection of all the selected shapes in the document. The shapes in the ActiveSelection.Shapes collection can be accessed as in the following VBA example:
ActiveDocument.Selection
ActiveSelection
Dim sh As Shape, shs As Shapes Set shs = ActiveSelection.Shapes For Each sh In shs sh.Rotate 15 'Rotate each shape by 15 degrees counterclockwise Next sh
After you use the ActiveSelection command to select shapes, you cannot subsequently use the command to access those shapes. Instead, you must create a copy of the selection by using one of the following methods:
As previously discussed, you can use the Document.SelectionRange property to make a copy of the shapes in a selection. However, the returned ShapeRange object is not refreshed when the selection is updated because it represents a "snapshot" of the selection at the moment when that ShapeRange object was created.
The following VBA code returns a copy of the selection for the active document:
Dim selRange As ShapeRange Set selRange = ActiveDocument.SelectionRange
The shortcut for the ActiveDocument.SelectionRange command is ActiveSelectionRange, which returns a ShapeRange object. This object contains a collection of references to the shapes that were selected at the moment when the property was invoked. The shapes in the ActiveSelectionRange collection can be accessed as in the following VBA example:
ActiveDocument.SelectionRange
ActiveSelectionRange
Dim sh As Shape, shRange As ShapeRange Set shRange = ActiveSelectionRange For Each sh In shRange sh.Skew 15 ' Skew each shape thru 15° counterclockwise Next sh
After you use the ActiveSelectionRange command to create a copy of the current document selection, you can subsequently access the returned ShapeRange object to access any of its shapes. You can even add shapes to or remove shapes from the returned ShapeRange object. You can then use the ShapeRange.CreateSelection method if you want to replace the current selection with the modified ShapeRange object.
The following VBA code creates a ShapeRange object from the current document selection, removes the first and second shapes from that shape range, and then replaces the original selection with this modified ShapeRange object:
Dim shRange As ShapeRange Set shRange = ActiveSelectionRange shRange.Remove 1 shRange.Remove 2 shRange.CreateSelection
If you want to add a specified ShapeRange object to the current selection (rather than use it to replace the current selection), you can use the ShapeRange.AddToSelection method.
Accessing the shapes in a selection
You can use a similar process for accessing the shapes in a selection as you can for accessing the shapes in a selection range.
Here is a a VBA code sample for accessing the shapes in a selection:
Dim shs As Shapes, sh As Shape Set shs = ActiveSelection.Shapes For Each sh In shs ' Do something with the shape, sh Next sh
Here is a VBA code sample for accessing the shapes in a selection range:
Dim sRange As ShapeRange, sh As Shape Set sRange = ActiveSelectionRange For Each sh In sRange ' Do something with the shape, sh Next sh
Remember that the ActiveSelection.Shapes command provides direct access to the current selection, while the ActiveSelectionRange command provides a copy of the current selection. Use ActiveSelection.Shapes if you want to access the current selection; use ActiveSelectionRange if you want to create a "snapshot" of the current selection that you can access later.
Reordering the shapes in a selection
The ActiveSelection.Shapes command and the ActiveSelectionRange command return shapes in the reverse order from which they were selected: the first shape is the last one selected, and the last shape is the first one selected. Please keep this fact in mind when reordering the shapes in a selection.
Deselecting shapes
You can deselect any shape by changing the value of its Shape.Selected property to False.
You can deselect all shapes by using the Document.ClearSelection method. The following VBA code uses the ClearSelection method to deselect all shapes in the active document:
ActiveDocument.ClearSelection
If you want, you can use event handlers to respond to events that are triggered by deseleting a shape:
You can also use event handlers to respond to events that are triggered by deactivating a selection:
You can use the Shape.Duplicate method to duplicate a shape, and you can use the ShapeRange.Duplicate method to duplicate a range of shapes.
ActiveSelection.Duplicate
The Duplicate method provides two optional parameters, OffsetX and OffsetY, which offset the duplicate from the original (horizontally and vertically, respectively). The following VBA code positions the duplicate two inches to the right and one inch above the original:
ActiveDocument.Unit = cdrInch ActiveSelection.Duplicate 2, 1
You can transform shapes in various ways, as explained in the following topics:
If you want, you can use event handlers to respond to events that are triggered by transforming a shape:
Sizing shapes
You can return the width and height of a shape (in document units) by using the Shape.SizeWidth and Shape.SizeHeight properties, as in the following VBA example:
Dim width As Double, height As Double ActiveDocument.Unit = cdrMillimeter width = ActiveShape.SizeWidth height = ActiveShape.SizeHeight
You can also use the Shape.SizeWidth and Shape.SizeHeight properties to resize an existing shape by specifying new values for those properties. The following VBA example uses these properties to set the size of the active shape to a width of 50 millimeters and a height of 70 millimeters:
ActiveDocument.Unit = cdrMillimeter ActiveShape.SizeWidth = 50 ActiveShape.SizeHeight = 70
You can return both the width and the height of a shape (in document units) by using the Shape.GetSize method, as in the following VBA example:
Dim width As Double, height As Double ActiveDocument.Unit = cdrMillimeter ActiveShape.GetSize width, height
You can resize a shape by using the Shape.SetSize method to specify a new width and new height for it, as in the following VBA example:
ActiveDocument.Unit = cdrMillimeter ActiveShape.SetSize 50, 70
You can also resize a shape by using the Shape.SetSizeEx method. Besides the new width and new height for the shape, this method takes a reference point for the resize (instead of using the center point of the shape). The following VBA code uses the SetSizeEx method to resize the current selection to 10 inches wide by 8 inches high about the reference point (6, 5) in the document:
ActiveDocument.Unit = cdrInch ActiveSelection.SetSizeEx 6, 5, 10, 8
If you want to take the outline of a shape into account when returning the size of that shape, you must use the Shape.GetBoundingBox method. The bounding box for a shape surrounds both the shape and its outline; however, the actual dimensions of a shape specify its width and height irrespective of the size of its outline. The following VBA example uses the GetBoundingBox method to return the size of the active shape:
Dim width As Double, height As Double Dim posX As Double, posY As Double ActiveDocument.Unit = cdrInch ActiveDocument.ReferencePoint = cdrBottomLeft ActiveShape.GetBoundingBox posX, posY, width, height, True
The Shape.GetBoundingBox method takes parameters that specify the position of the lower-left corner of the shape, the width of the shape, and the height of the shape. The final parameter is a Boolean value that indicates whether to include (True) or exclude (False) the outline of the shape. The Shape.SetBoundingBox method lets you set the size of a shape by specifying the size of its bounding box; however, this method lacks the parameter for specifying whether to include the outline in the new size. If you want to calculate the size and position of the bounding box of a shape without including its outline, you can use the GetBoundingBox method twice (once including the outline and once excluding it), as in the following VBA example:
Public Sub SetBoundingBoxEx(X As Double, Y As Double, _ Width As Double, Height As Double) Dim sh As Shape Dim nowX As Double, nowY As Double Dim nowWidth As Double, nowHeight As Double Dim nowXol As Double, nowYol As Double Dim nowWidthol As Double, nowHeightol As Double Dim newX As Double, newY As Double Dim newWidth As Double, newHeight As Double Dim ratioWidth As Double, ratioHeight As Double Set sh = ActiveSelection sh.GetBoundingBox nowX, nowY, nowWidth, nowHeight, False sh.GetBoundingBox nowXol, nowYol, nowWidthol, nowHeightol, True ratioWidth = Width / nowWidthol ratioHeight = Height / nowHeightol newWidth = nowWidth * ratioWidth newHeight = nowHeight * ratioHeight newX = X + (nowX - nowXol) newY = Y + (nowY - nowYol) sh.SetBoundingBox newX, newY, newWidth, newHeight, False, cdrBottomLeft End Sub
Stretching shapes
You can stretch a shape (or scale it by stretching is proportionately) by using the Shape.Stretch method or the Shape.StretchEx method. Both methods take a decimal value for the stretch, where 1 is 100% (or no change); you cannot use zero, so you must use a very small value instead.
The following VBA code uses the Shape.Stretch method to stretch the selection to half its current height and twice its width, about the midpoint of the bottom edge of its bounding box:
ActiveDocument.ReferencePoint = cdrBottomMiddle ActiveSelection.Stretch 2, 0.5
If you want to specify the reference point about which to perform a stretch, you can use the Shape.StretchEx method. The following VBA code performs the same stretch as the previous code, but it performs that stretch about the point (4, 5) on the page (in inches):
ActiveDocument.Unit = cdrInch ActiveSelection.StretchEx 4, 5, 2, 0.5
The Shape.Stretch and Shape.StretchEx methods provide an optional Boolean parameter that determines how to stretch paragraph text. A value of True stretches the characters, while False stretches the bounding box and re-flows the text within it.
Skewing shapes
You can skew a shape by using the Shape.Skew method or the Shape.SkewEx method. These methods let you specify the horizontal-skew angle (in degrees, where positive values move the top edge to the left and the bottom edge to the right) and the vertical-skew angle (in degrees, where positive values move the right edge upwards and the left edge downwards).
Skews of angles close to or greater than 90° are not allowed.
The horizontal skew is applied before the vertical skew.
The difference between the Shape.Skew and Shape.SkewEx methods is the point about which the skew is performed: Skew uses the center of rotation for the shape, while SkewEx uses the specified reference point.
You can determine the center of rotation for a shape by returning the values of its Shape.RotationCenterX and Shape.RotationCenterY properties. Changing these values moves the center of rotation for that shape.
The following VBA code uses the Shape.Skew method to skew the selection (about its center of rotation) by 30°horizontally and by 15° vertically:
ActiveSelection.Skew 30, 15
Rotating shapes
You can rotate a shape by using the Shape.Rotate method or the Shape.RotateEx method. These methods rotate the shape by the given angle (in degrees). However, the difference between these methods is the point about which they perform the rotation: Rotate uses the center of rotation for the shape, while RotateEx uses the specified reference point.
The following VBA code uses the Shape.Rotate method to rotate the selection (about its center of rotation) by 30°:
ActiveSelection.Rotate 30
The following VBA code uses the Shape.RotateEx method to rotate each selected shape by 15° clockwise about its lower-right corner:
Dim sh As Shape ActiveDocument.ReferencePoint = cdrBottomRight For Each sh In ActiveSelection.Shapes sh.RotateEx -15, sh.PositionX, sh.PositionY Next sh
Positioning shapes
You can return the horizontal and vertical position of a shape by using the Shape.PositionX and Shape.PositionY properties (respectively). Alternatively, you can use the Shape.GetPosition method to return both the horizontal position and the vertical position of a shape.
You can use the Shape.GetBoundingBox method if you want to return the position of a shape, including its outline. For more information on this method, see Sizing shapes.
The following VBA code uses the Shape.GetPosition method to return the position of the selection relative to the current reference point of the active document, which the code explicitly sets to the lower-left corner:
Dim posX As Double, posY As Double ActiveDocument.ReferencePoint = cdrBottomLeft ActiveSelection.GetPosition posX, posY
You can also use the Shape.PositionX and Shape.PositionY properties to set the horizontal and vertical position of a shape (respectively), thereby moving that shape to the specified position. Alternatively, you can use the Shape.SetPosition method to move a shape to specified horizontal and vertical position, or you can use the Shape.SetPositionEx method to move the shape to a specified point.
You can also use the Shape.SetSizeEx and Shape.SetBoundingBox methods to set the position of a shape. For more information on these methods, see Sizing shapes.
The following VBA code uses the Shape.SetPosition method to set the position of the lower-right corner of each selected shape in the active document to (3, 2) in inches:
Dim sh As Shape ActiveDocument.Unit = cdrInch ActiveDocument.ReferencePoint = cdrBottomRight For Each sh In ActiveSelection.Shapes sh.SetPosition 3, 2 Next sh
If you want, you can use event handlers to respond to events that are triggered by positioning a shape:
You can add color to a shape by applying a fill (or Fill object) to it. The fill type for a shape is recorded by the Fill.Type property as one of the following constants for the cdrFillType enumeration:
The following VBA code returns the type of fill that is applied to the active shape:
Dim fillType As cdrFillType fillType = ActiveShape.Fill.Type
You cannot change the fill type for a shape by modifying its Fill.Type property. Instead, you must use the appropriate Fill.Apply...Fill method, as described in the subsections that follow.
You can also add color to a shape by applying an outline (or Outline object) to it.
In addition, the object model provides a variety of properties and methods for working with the colors (or Color objects) that you apply to shapes.
For information on applying fills and outlines and on working with colors, see the following subtopics:
In your macros, you can include queries that search for shapes that have specific fill properties, outline properties, or color properties. For information, see "Including queries in macros" in the Macros Help file for the application.
Applying uniform fills
Uniform fills consist of a single, solid color. A uniform fill is represented by the Fill.UniformColor property as a Color object.
You can apply a uniform fill to a shape by using the Fill.ApplyUniformFill method. The following VBA example applies a red uniform fill to the active shape:
ActiveShape.Fill.ApplyUniformFill CreateRGBColor(255, 0, 0)
You can change the color of a uniform fill by modifying its Fill.UniformColor property. The following VBA example changes the uniform fill of the active shape to deep navy blue:
ActiveShape.Fill.UniformColor.RGBAssign 0, 0, 102
You can remove the uniform fill from a shape by using the Fill.ApplyNoFill method.
Applying fountain fills
Fountain fills display a progression between two colors. A fountain fill is represented by the Fill.Fountain property as a FountainFill object, which specifies the various properties for the fountain fill: start color, end color, angle, blend type, and so on. The colors in a fountain fill are represented by a FountainColors collection.
You can apply a fountain fill to a shape by using the Fill.ApplyFountainFill method. This method provides optional parameters for various fountain-fill settings, such as the midpoint and offset of the blend. The following VBA example creates a simple linear fountain fill, from red to yellow, at 30 degrees to the horizontal:
Dim startCol As New Color, endCol As New Color startCol.RGBAssign 255, 0, 0 endCol.RGBAssign 255, 255, 0 ActiveShape.Fill.ApplyFountainFill startCol, endCol, cdrLinearFountainFill, 30
You can add a color to a fountain fill by using the FountainColors.Add method. Color positions are integer values in percent, where 0% is the start-color position and 100% is the end-color position. The following VBA example adds a green color to the fountain fill at a position about one-third (33%) of the way from the existing red color:
Dim fFill As FountainFill Set fFill = ActiveShape.Fill.Fountain fFill.Colors.Add CreateRGBColor(0, 102, 0), 33
You can move a color in a fountain fill by using the FountainColor.Move method. The following VBA code moves the green color from the previous example to a position that is 60% of the way from the red (that is, more towards the yellow):
ActiveShape.Fill.Fountain.Colors(1).Move 60
You can use the FountainColors.Count property to determine the number of colors between the start color and end color of a fountain fill. (For the preceding example, this value is 1.) The first color in the collection is that start color, and its index number is 0; this color cannot be moved, but its color can be changed. The last color in the collection is the end color, and its index number is (Count + 1); this color cannot be moved, but its color can be changed. The following VBA code changes the end color from yellow to blue:
Dim cols As FountainColors Set cols = ActiveShape.Fill.Fountain.Colors cols(cols.Count + 1).Color.RGBAssign 0, 0, 102
You can remove the fountain fill from a shape by using the Fill.ApplyNoFill method.
Applying pattern fills
Pattern fills display a series of repeating vector objects or bitmap images. A pattern fill is represented by the Fill.Pattern property as a PatternFill object, which specifies the various properties for the pattern fill: foreground color, background color, tile offset, and so on.
The collection of available pattern fills is stored in the PatternCanvases collection.
You can apply a pattern fill to a shape by using the Fill.ApplyPatternFill method.
You can remove the pattern fill from a shape by using the Fill.ApplyNoFill method.
Applying texture fills
Texture fills are fractally generated and fill a shape with one image rather than a series of repeating images. A texture fill is represented by the Fill.Texture property as a TextureFill object, which specifies the various properties for the texture fill: origin, resolution, tile offset, and so on.
The properties for a texture fill are stored in a TextureFillProperties collection.
You can apply a texture fill to a shape by using the Fill.ApplyTextureFill method.
You can remove the texture fill from a shape by using the Fill.ApplyNoFill method.
Applying PostScript fills
PostScript fills are texture fills that are designed by using the PostScript language. A PostScript fill is represented by the Fill.PostScript property as a PostScriptFill object, which specifies the various properties for the PostScript fill.
You can apply a PostScript fill to a shape by using the Fill.ApplyPostScriptFill method.
You can remove the PostScript fill from a shape by using the Fill.ApplyNoFill method.
Applying hatch fills
Hatch fills are composed of vector-based lines and can be used to clearly distinguish the materials or object relationships in a drawing. A hatch fill is represented by the Fill.Hatch property as a HatchFill object, which specifies the various properties for the hatch fill.
The collection of available hatch-fill patterns is stored in the HatchPatterns collection, and each document stores its own library of hatch-fill patterns in a HatchLibraries collection.
You can apply a hatch fill to a shape by using the Fill.ApplyHatchFill method.
You can remove the hatch fill from a shape by using the Fill.ApplyNoFill method.
Applying outlines
You can use the various properties and methods of the Outline class to define the outline of a shape.
The Outline.Type property uses the following constants of the cdrOutlineType enumeration to record whether the specified shape has an outline:
If a shape has no outline, setting its Outline.Type property to cdrOutline applies the document-default outline style.
If a shape has an outline, setting its Outline.Type property to cdrNoOutline removes that outline.
The Outline.Width property for an outline sets its width in document units. In the following VBA example, the outline of the selected shapes is set to 1 millimeter:
ActiveDocument.Unit = cdrMillimeter ActiveSelection.Outline.Width = 1
If a shape does not have an outline, its Outline.Width value is 0. Changing this value applies an outline and automatically changes the value for the Outline.Type property from cdrNoOutline to cdrOutline.
Similarly, if a shape has an outline, its Outline.Width value is greater than 0. Changing this value to 0 removes the outline and automatically changes the value for the Outline.Type property from cdrOutline to cdrNoOutline.
The Outline.Color property for an outline defines its color, as in the following VBA example:
ActiveSelection.Outline.Color.GrayAssign 0 ' Set to black
Setting the color of an outline automatically sets the Outline.Type property of that outline to cdrOutline and applies the default outline width.
The Outline.Style property for an outline specifies the dash settings of that outline. These dash settings are defined by the following properties of the OutlineStyle class:
DashLength(1)
5
Outline objects have many other properties, including the following:
Outline objects also have methods, including the following:
Working with color
The Color class defines the fill colors and outline colors that you apply to shapes. This class provides a number of properties and methods for working with color.
You can determine the color model of a color by accessing its Color.Type property, as in the following VBA example:
Dim colType As cdrColorType colType = ActiveShape.Outline.Color.Type
The Color.Type property is defined by the cdrColorType enumeration, which provides the following constants (among many others) for supported color models:
The color components for each supported color model are defined by additional properties of the Color class, as demostrated by the following VBA examples:
The range of values that is supported by a color component depends on the color model for that component.
To create a color, you can use the automation keyword New, as in Dim col As New Color.
Dim col As New Color
To assign a color model to a new color, you can use the desired ...Assign method (such as Color.CMYKAssign, Color.RGBAssign, or Color.GrayAssign). Each of these methods provides one parameter for each color component in its respective color model. For example, col.RGBAssign 0, 0, 102 assigns a deep-blue RGB color to the new color that was created in the previous tip.
col.RGBAssign 0, 0, 102
To use the application’s color-management settings to change the color model that is assigned to a color, you can use the desired ConvertTo... method (such as Color.ConvertToCMYK, Color.ConvertToRGB, or Color.ConvertToGray). For example, ActiveShape.Fill.UniformColor.ConvertToRGB converts the fill of the active shape to the RGB color model.
ActiveShape.Fill.UniformColor.ConvertToRGB
You can copy the properties of one color to another color by using the Color.CopyAssign method, as in the following VBA example:
Dim sh As Shape Set sh = ActiveShape sh.Outline.Color.CopyAssign sh.Fill.UniformColor
The color "none" does not exist. To set a fill color or outline color to "none," you must instead set the fill type or outline type to "none."
The object model provides a number of methods for applying effects to shapes. For information on these methods, see the following subtopics:
Applying an effect returns an Effect object, which lets you access various properties and methods for the created effect. For example, you can use the Effect.Separate method to separate the shapes that are generated by an effect from the shape to which that effect is applied. In addition, you can use the Effect.Clear method to remove an effect from a shape.
Applying blends
The Shape.CreateBlend method creates a blend between the current shape and the shape that is specified as a parameter. This method provides optional parameters for various blend settings, such as the acceleration of the blend and the path along which the blend is created.
The following VBA code creates a basic ten-step blend:
Dim sh As Shapes, eff As Effect Set sh = ActiveSelection.Shapes Set eff = sh(1).CreateBlend(sh(2), 10)
In the preceding example, the number of shapes in the blend is twelve: the start and end shapes, plus the ten blend steps that are created.
The Shape.CreateBlend method returns an Effect object, the Effect.Blend property for which you can use to modify the created blend.
Applying contours
The Shape.CreateContour method applies a contour to a shape. This method provides optional parameters for various contour settings, such as the colors and acceleration of the contour.
The following VBA code creates a three-step contour at a five-millimeter spacing:
Dim eff As Effect ActiveDocument.Unit = cdrMillimeter Set eff = ActiveShape.CreateContour(cdrContourOutside, 5, 3)
The Shape.CreateContour method returns an Effect object, the Effect.Contour property for which you can use to modify the created contour.
Applying customized effects
The Shape.CreateCustomEffect method applies a customized effect to a shape. This method provides parameters for various effect settings.
The Shape.CreateCustomEffect method returns an Effect object, the Effect.Custom property for which you can use to modify the created effect.
Applying distortions
The following methods apply a distortion to a shape:
These methods provide parameters for various distortion settings.
The distortion-creation methods return an Effect object, the Effect.Distortion property for which you can use to modify the created distortion.
If you want, you can use event handlers to respond to events that are triggered by distorting a shape:
Applying drop shadows
The Shape.CreateDropShadow method applies a drop shadow to a shape. This method provides optional parameters for various drop-shadow settings, such as the feathering and offset of the drop shadow.
The Shape.CreateDropShadow method returns an Effect object, the Effect.DropShadow property for which you can use to modify the created drop shadow.
Applying envelopes
The following methods apply an envelope to a shape:
These methods provide parameters for various envelope settings.
The envelope-creation methods return an Effect object, the Effect.Envelope property for which you can use to modify the created envelope.
Applying extrusions
The Shape.CreateExtrude method applies an extrusion to a shape. This method provides optional parameters for various extrusion settings, such as the angle and color of the extrusion.
The Shape.CreateExtrude method returns an Effect object, the Effect.Extrude property for which you can use to modify the created extrusion.
Applying lenses
The Shape.CreateLens method applies a lens to a shape. This method provides optional parameters for various lens settings, such as the color and magnitude of the lens.
The Shape.CreateLens method returns an Effect object, the Effect.Lens property for which you can use to modify the created lens.
Applying perspective
The Shape.CreatePerspective method applies perspective to a shape. This method provides optional parameters for specifying horizontal and vertical vanishing points.
The Shape.CreatePerspective method returns an Effect object, the Effect.Perspective property for which you can use to modify the created perspective effect.
Isometric projections are two-dimensional objects that are converted into apparent three-dimensional objects. In Corel DESIGNER, you can project a single shape by using the Shape.Project method, or you can project a range of shapes by using the ShapeRange.Project method.
Shapes in a range are projected parallel to each other, so they do not converge to a vanishing point.
You can project shapes onto three isometric planes:
You can also originate the projection from a specified reference point:
Finally, you can apply a projection to a duplicate of the specified object or to the original object.
The following VBA code sample illustrates how to create a projection:
Sub MakeProjection() Dim shp1 As Shape, shp2 As Shape, shp3 As Shape Dim shpRng As ShapeRange 'Create a pair of rectangles and a circle. ActiveDocument.Unit = cdrMillimeter Set shp1 = ActiveLayer.CreateRectangle2(0, 0, 10, 20) Set shp2 = ActiveLayer.CreateRectangle2(50, 50, 20, 10) Set shp3 = ActiveLayer.CreateEllipse(10, 10, 20, 20) 'Add the rectangles to a ShapeRange. Set shpRng = ActiveLayer.FindShapes(Type:=cdrRectangleShape) 'Project the shapes onto the top isometric plane, 'using the upper-right corner of the ShapeRange bounding box 'as the anchor point. Modify the original object. shpRng.Project cdrProjectTop, cdrTopRight 'Project a duplicate of the circle onto the right isometric plane, 'using the default reference point. shp3.Project Plane:=cdrProjectRight, ApplyToDuplicate:=True End Sub
In your macros, you can include queries that search for shapes that have specific shape properties, fill properties, outline properties, or color properties. To do this, you use Corel Query Language (CQL) in conjunction with one of the following methods:
You can specify the shape properties for which to search. For example, the expression ActiveShape.Evaluate("@name") searches searches the Name property of all selected shapes.
Consider the following VBA code sample, in which the Type property and the Width property are used to select all rectangles that are wider than two inches:
ActivePage.Shapes.FindShapes(Query := "@type = 'rectangle' and _ @width > {2 in}").CreateSelection
For comprehensive information on using CQL, see "Including queries in macros" in the Macros Help file for the application.
If you want, you can use event handlers to respond to events that are triggered by deleting a shape: