Pages

Monday 6 June 2011

How to create macro (first macro)


Today I'd like explain macro's code I have used in the video from previous post (see it).
Macros are written in Visual Basic and I'll try to indroduce VBA syntax as well.

Good starting point is to read 'Macro Programming Guide' shiped with Corel Draw.
Go to:  start >> All Programs >> CorelDRAW Graphics Suite X5 >> Documentation >> Macro programming Guide - PDF


Full code:

Sub putDetails()
    
    If ActiveSelection.Shapes.Count = 0 Then Exit Sub 'this is in case you try to run the macro without making selection before
    
'DECLARING VARIABLES START
    Dim unit As Long
    Dim tUnit As String
    Dim font As String
    Dim fontSize As Single
    Dim tSize As String
    Dim precision As Long

    Dim x As Double      ' x coordinate of lower left corner of selections bounding box
    Dim y As Double      ' y coordinate of lower left corner of selections bounding box
    Dim w As Double      ' width of selections bounding box
    Dim h As Double      ' height of selections bounding box
'DECLARING VARIABLES END
    
'ASSIGNING VARIABLES START (you can modify this section to your needs)
    unit = cdrMillimeter
    tUnit = " mm"
    font = "Arial"
    fontSize = 12
    precision = 0
'ASSIGNING VARIABLES END
    
'PROCEDURES
    ActiveDocument.unit = unit 'set up the unit for the current active document

    ActiveSelection.GetBoundingBox x, y, w, h 'get the bounding box of the selected objects. See DECLARING VARIABLES section
    
    tSize = Round(w, precision) & " x " & Round(h, precision) & tUnit 'set up details 'width and height' for our message. These are rounded to specified precision
    
    ActiveLayer.CreateArtisticText x, y - (1.6 * (fontSize / 4)), tSize, , , font, fontSize, 'finaly put the details as text below the selected objects
'PROCEDURES

End Sub

If ActiveSelection.Shapes.Count = 0 Then Exit Sub

The first line ensures to terminate the subroutin if there is nothing selected.
The ActiveSelection refers to curent selected object(s) of a document you are working on.

ActiveSelection can contain many shapes, so
ActiveSelection.Shapes refers to theose shapes.

To count how many shapes are in the selection put ActiveSelection.Shapes.Count .

If that statement gives you zero it means there is no selection in the current document.
and the rest code of our program will not be executed  - Exit Sub.


Declaring variables section

Dim unit As Long
....

'unit is variable name.
'long' is type of variable. Type 'long' represents whole numbers
there are another types in our example:

String - repesetnts text
Single -whole  numbers again
Double - numbers with decimal point

See VB help to read more about variables.

Next section is the variables assignment.
Note that to assign some text (or sequnce of characters) to string you use quotes.
To assign numbers just put value without quotes.

In the unit variable store the inforamtion about units you want use. In this example it is milimmeters

ActiveDocument.unit = unit

This action sets desired units for the document.

Coordinates of lower left corner of selection bounding box, width and height (in set units) are obtain by:


ActiveSelection.GetBoundingBox x, y, w, h

so this information are assigned now to x, y, w and h variables.
We've got everything we need.
Next procedrues are about formating and presenting the results on drawing area.

...
tSize = Round(w, precision) & " x " & Round(h, precision) & tUnit
...


This is to compose values to a line of text (string).
Round() function rounds the width and height of selection (w, h) to specified precision (precision) and return numeric value (double).

As you can see there are to types of variables
string and double.

string:     " x "    and     tUnit (defined at the beginning of code)

double:  Round(w, precision)    and    Round(h, precision)

and they are separated (or connected) by '&' .
In this situation the numeric values are automatically converted to string so it can be safely assigned to tSize variable.

And the last line:

...
ActiveLayer.CreateArtisticText x, y - (1.6 * (fontSize / 4)), tSize, , , font, fontSize
...

creates text object on the drawing area (or canvas) right below the selection.
To set the position of the text object I use x & y variables obtained from
ActiveSelection.GetBoundingBox  xy,  w,  h

The  y - (1,6 * (fontSize / 4)) expression positions the text below the selection. If only y is used the text appears on the selection.

The (fontSize / 4) expression gives current height of capital letter in millimeters.

Empty spaces between commas are for optional parameters, when omited default value is used.
(you can see default values in the hint message while typing code in editor).