Friday, 29 July 2011

Skeeper - keep you selection (little macro)

In this post I would like to demonstrate a little macro which allows you to remember certain group of shapes without grouping them.


SKeeper creates groups of shapes/objects (not grouped shapes in CorelDraw meaning).
It put a registry to shape's name attribute (you can see it in Object Manager), when you click on the list with groups names it finds all shapes with the same record and adds them to selection.

Registry/records looks like this :    SK:groupName
where groupName will appear in the SKeeper list box

You can put anything in the object's name property, just keep SKeeper record at the end.

The limit is that the groups cannot overlap, in other words object can belong to only one group,
therefore if you try to assign object to group-2 which is already assigned to group-1 the first record will be replaced by second.

There is one thing I'd like to say about code, in this macro I use CQL (Corel Query Language) to search for shapes.
for example:
ActiveDocument.SelectableShapes.FindShapes(, , , "@name.Contains('SK:')")
 first 3 parameters are omitted (notice empty spaces between commas) and last parameter is a String type variable (notice quotes) and it represent the CQL syntax (very powerful tool).
See this article for more details:
Using CQL

Feel free to use this macro (in your company or home), look at the code if you like, leave comment, ask a question.

Gms file to download:

You might be interested in more featured  similar macro, see:

Tuesday, 12 July 2011

Selection tips

Object selection

To select or deselect objects you use Pick Tool.
To activate Pick Tool:

Click the icon on a toolbox:

Or hit Space-bar (if you are currently editing text hit Ctrl+ Space-bar).

The basic is that you get object.shape selected by clicking on it or drag mouse until desired object is surrounded by selection rectangle (blue dashed line).

To add/remove objects to/from the selection hold Shift.

While holding Alt key and dragging mouse the object(s) get selected/deselected if selection rectangle just cross it,
or when clicking can select objects being behind the clicked one.

Ctrl key makes selection rectangle a square,
or when clicking you can select objects within the group.

You can use any combination of modifier keys (Shift, Alt, Ctrl) to get desired effect.

When draging mouse to create selection press and hold Right Mouse Button before release Left one. You can now move the selection rectangle to different place.

You can use Object manager to select shapes as well.
On the main menu click window >> Dockers >> Object Manager  to open it.

This time it works like in a file manager ,
Hold Ctrl to add objects one by one.
Hold Shift key to add object between two selected.
This will not work within the grouped shape object.

Another way is to use Find Objects tool
Go to main menu >> Edit >> Find and Replace >> Find Objects...

This is a tool to search for objects that have specified properties. Result of searching is selected object(s).
When there is selected object on screen the option "Find objects that match the currently selected object" become available.

With selected object on screen.

Without selected object.

Node selection

Selecting nodes is pretty much the same like selecting objects.
By clicking on it surround it with selection rectangle.
Shift & Ctrl modifier keys works the same.
Alt activate 'Freehand' mode, you compare it to something known from other graphic packages as 'lasso selection'

On the images above you can see that actually I haven't pressed Alt key but change selection mode in left-upper corner to Freehand. Holding Alt key give the same result.

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
    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
'ASSIGNING VARIABLES START (you can modify this section to your needs)
    unit = cdrMillimeter
    tUnit = " mm"
    font = "Arial"
    fontSize = 12
    precision = 0
    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

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).

Tuesday, 12 April 2011

How to create macro (creating project)

As a short introduction, a macro is series of commands that automate some task.
In case of Corel Draw it is program written in visual basic programming language.

How to create a macro?
Macros are stored in files with extension  '.gms '. There is one file provided by Corel called globalMacroStorage.gms.
This is first (1) place where you can save your macro.

Second (2) option is to create new file which represents new macro project.

Very basic (simplified) structure of  macro project is as follows:
  • Project contain: modules
  • modules contain:  subs (subroutines) in many cases these are  actual macros
(You will see later that the structure of project is much more complex)

1) GlobalMacroStorage.gms

Go to:    main menu >> Tools >> Macros >> and choose Macro Manager

You should see new pop-up window or docker on the right hand side with the list of all macro projects in it.

Find the 'GlobalMacroStorage' and click the little cross next to see modules.

On the top of this window click 'New' button and then module.
While newly created module is selected repeat operation but this time select macro.

It opens the macro editor where you can write or edit your macro.

Sub nameOfMacro()  'Sub states for subroutine.

'Code here

End Sub

2) Creating new macro project

The procedure is pretty much the same except your first choice after clicking the 'New' button is 'Macro Project'.

You will be prompt to name and save new .gms file. The dialog box will display right location, which is: 

Windows XP: C:\Documents and Settings\'user nick name'\Application Data\Corel\CorelDRAW Graphics Suite X5\Draw\GMS

Windows Vista: C:\Users\'user nick name'\AppData\Roaming\Corel\CorelDRAW Graphics Suite X5\User Draw\GMS

Windows 7: C:\Users\'user nick name'\AppData\Roaming\Corel\CorelDRAW Graphics Suite X5\Draw\GMS

Form there Corel Draw loads your project at start up.

Rest procedure of creating new macro is the same as for 1) GlobalMacroStorage.gms

For the summary see this short video:


The first macro code will be explained in the next post. 

Another way to create new macro project is to  create simple text file and change it's extension to '.gms', and place it in same location as previous plus alternatively:

Windows XP: C:\Program Files\Corel\CorelDRAW Graphics Suite X5\Draw\GMS

The file is loaded at Corel start up (you can load it manually via macro manager by clicking the 'Load' button next to 'New' and navigate to .gms file), and then you can edit it in macro editor - main menu >> Tools >> Macros >>  Macro Editor.