Hi again!
If you're into creating macros or addons for CorelDRAW, I'm about to blow your mind! New to CorelDRAW X7 Update 1 is the ability to create your own tools in VBA, C# or C++. We now expose a new interface that anyone can implement -- ToolState. We've also implemented many new functions and classes that are specifically geared towards creating tools.
While you can implement new tools in any language that supports COM, in this post, I'll be focusing on C#. I'll share some VBA and C++ samples in a future post.
First, fire up your copy of Visual Studio 2012 or 2013. If you don't have a Visual Studio, fret not, you can grab a copy of Visual Studio Express here. Once you've done so, install the CorelDRAW Tool Addon extension. In Visual Studio, open up Tools->Extensions and Updates, expand the Online tab and search for "CorelDRAW". Select CorelDRAW Tool Addon and install it!
Next, create a new project (File->New Project), and navigate to Installed->Templates->Other Languages->C#, and select CorelDRAW Tool Addon from the list. Give your tool any name (I'm using "MyTool" here) and create your project.
In the menu, select Build->Build Solution (or hit F7). Congrats, you've just created your first tool. Want to try it out? Press F5 to run it. It will launch CorelDRAW and your new tool will be added to the toolbox.
If you can't tell, that's a line and a gear. Sorry, it's hard to fit anything detailed in a 16x16 image.
The sample tool has on screen preview UI, snapping, constraints, a property bar, and everything else you would expect. It is a fully fledged tool. At this point, you're probably thinking that it was too easy.
Let's crack open the code, I'll do my best to explain how it all works.
The Code
The project has two .cs files. Addon.cs contains the entry point. This class will be instantiated by CorelDRAW when you start the application, and it's here where you can register your new tool, or data source, or other tasks you'd like to perform on startup.
///<summary> /// Constructor for the Addon. This is called by CorelDRAW when it is discovered. ///</summary> public Addon(Application Application) { // Create and register our tool ToolState toolState = new CGS.MyTool(Application); Application.RegisterToolState("b17f7ca0-0062-4dd8-adf6-1ea2192a3d35", "MyTool", toolState); }
The function RegisterToolState tells that application that the guid "b17f7ca0-0062-4dd8-adf6-1ea2192a3d35" identifies your tool. This guid must be different for every tool (don't worry the Visual Studio extension generates a new one for every project). You can create multiple tools within your project and register them all at this spot. If you create a second tool within the project, you'll need a GUID generator. I use https://gist.github.com/ijprest/3845947 which is an AutoHotKey script, but you can find other GUID generators online (google it).
MyTool.cs contains the guts of the tool. The tool is less than 200 lines long, not too bad. MyTool inherits the ToolState interface, when CorelDRAW is using your tool, it will call into the functions that you implement. For example, when the tool starts, your OnStartState() will be executed, allowing you to set up your tool for use. When you move the mouse across the view, OnMouseMove() will be called, which allows you to update the UI, or collect mouse positions for curve creation.
The code for the sample tool is documented well, please take a few moments to read through the code and the comments to gain a basic understanding of the functions. The pseudo code for the line tool is as follows:
OnLButtonDownLeaveGrace save the left mouse button down positionOnMouseMove if the left mouse is down update the xor'd on screen UI with a line from the saved location to the current positionOnLButtonUp Create a line from the saved position to the current position.
The UI
In your project you'll find two xslt files, AppUI.xslt and UserUI.xslt. Xslt is an XML transformation file. It's job is to update the users workspace to include your new tool. You can do much more complex changes to the workspace using XSLT, but in this post I'll be limiting it to adding a new tool Icon and a new Toolbar for your tool. If you're attempting to create any complicated UI, you may want to read up on xslt to better understand it.
AppUI.xslt is responsible for defining new items (e.g. buttons) and adding dockers or dialogs. Anything that isn't customizable. In the sample line tool, it adds one new item.
<!-- C# Test Line Tool --> <itemData guid="b17f7ca0-0062-4dd8-adf6-1ea2192a3d35" type="groupedRadioButton" currentActiveOfGroup="*Bind(DataSource=WAppDataSource;Path=ActiveTool;BindType=TwoWay)" enable="*Bind(DataSource=WAppDataSource;Path=ToolboxEnable)" identifier="b17f7ca0-0062-4dd8-adf6-1ea2192a3d35"/>
A guid (a unique identifier) is used to identify the tool button ("b17f7ca0-0062-4dd8-adf6-1ea2192a3d35"). This guid set for both the item and the identifier. This is also the same guid that we've registered MyTool with the application. This is important as it causes MyTool activate when the button is clicked.
UserUI.xslt is for modifying any UI that is customizable, for example, adding buttons to existing toolbars or menus, or creating new toolbars for the tools.
<xsl:template match="uiConfig/commandBars/commandBarData[@guid='74e03d83-404c-49f5-824a-fe0fd02ab29a']/toolbar"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> <modeData guid="fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480" captionRef="01965a34-ae38-01a0-4271-cd903b48e518"> <item guidRef="3148e122-4f73-6e8a-4dfb-b2f6a84090d5"/> <item guidRef="a41164ff-c9e9-4b2c-954a-095f4204538e"/> <item guidRef="266435b4-6e53-460f-9fa7-f45be187d400"/> <item guidRef="325d7c86-da3a-4610-bd25-5cf98cf66a41"/> <item guidRef="42bef211-16e5-4b4f-b9ee-52b7b5476232"/> <item guidRef="d289f32b-3808-4510-b892-fd2cb0820209"/> <item guidRef="266435b4-6e53-460f-9fa7-f45be187d400"/> <item guidRef="8723ad52-3e31-473c-8756-7ae85abcc483"/> <item guidRef="266435b4-6e53-460f-9fa7-f45be187d400"/> <item guidRef="e6644135-9dab-4935-8ab9-fc85527810ca"/> <item guidRef="6ae897fd-2eab-4dad-b172-f4fb768c273e"/> <item guidRef="266435b4-6e53-460f-9fa7-f45be187d400"/> <item guidRef="97e5c8b9-f7e2-453c-92a2-af7fb61e67b2"/> <item guidRef="266435b4-6e53-460f-9fa7-f45be187d400"/> <item guidRef="76dd9e0c-e9c2-42b6-b8bb-f7717482164e"/> <item guidRef="6bd00383-d132-4686-8a21-8d7052b6a81b"/> <item guidRef="40cc3adc-498a-4256-a98a-d1210a4019f7"/> </modeData> </xsl:copy> </xsl:template> <xsl:template match="uiConfig/commandBars/commandBarData[@guid='7c905e2a-cb64-4ba1-aff0-c306f392680c']/toolbar/item[@guidRef='af052244-1121-4a82-b0d2-194106db742a']"> <xsl:copy-of select="."/> <item guidRef="b17f7ca0-0062-4dd8-adf6-1ea2192a3d35"/> </xsl:template>
The first section defines a new property bar mode for the tool property bar. The guid "fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480" is unique for this tool as well, and in MyTool::StartAtState(), you'll find the following line:
currentAttribs.PropertyBarGuid = "fd9a53f7-05cc-4cdc-b2ad-0fd6a7ea3480";
This tells CorelDRAW that we want to use this property bar when the tool starts. The items on this property bar come from the application, mostly from the real line tool. I'll describe how to create your own items in a future post.
The second section adds the item to the toolbox. The code specifically places the new tool after the Smart Fill tool in the tool box.
The Resources
The three types of resources that we can expose to CorelDRAW are icons, cursors and strings. config.xml ties a guid to a particular resource. In this case we tie "b17f7ca0-0062-4dd8-adf6-1ea2192a3d35" to string "10223" and icon "104. You can find these resources in your project by double-clicking on resources.rct.
A note on adding icons or cursors: I find it easiest to create the icon in an external editor and then use "Add Resource", "Icon", Import, instead of using the built-in editor.
The APIs
Below is a list of all of the new functions added to CorelDRAW's object model in Update 1. Have fun playing with them!
Things to look out for
For more information on creating custom tools, please see: http://community.coreldraw.com/blogs/insider/archive/2014/06/06/tutorial-make-a-zig-zag-tool-for-coreldraw-x7-update-1.aspx
Building a WPF menu in the toolbar, and when floating, it should be an event, but fixed to the top toolbar event is not responding. I don't know why?
How to make it work on Corel Designer? (it doesn't work for me even if I add the Designer.addon in my pluggin folder)
Your list of utilities looks impressive. I would especially be interested in a replacement for CorelDraw's GetMatrix and SetMatrix routines, which don't behave in a way I can figure out. In particular, I can't get the tx and ty parameters (which are not explained in the help files, but presumably are for translation of a shape) to work as expected.
I followed your instructions and installed Visual Studio. However, a seach failed to show anything related to CorelDraw.
Is your Addon not available any more?
Best regards, Holger Nielsen