Tutorial: Writing a plugin that draws a dovetail
Posted: Thu May 23, 2013 11:53 pm
OK, so this is how I write plugins. I write C++ code for thirtysome years, learned a few other languages and do some Reverse Engineering. That may or may not explain my approach here. I had no clue about Java Script until a few weeks ago. It's easy and powerful. You can learn it!
When extending functionality, I like to base my code on a working script and then change it the way I need, verifying my steps. So first we need to find an existing script that does 90% of what we want. Drawing a rectangle from point to point is good. After searching a bit, I found a directory: .../scripts/Draw/Line/LineRectangle/ . So lets duplicate the entire directory just where it is and name the new one "LineDove".
Go into the directory and rename the following files:
LineDove.svg is the vector graphic for the icon in the toolbar. Leave it alone for now, or edit it, if you have time to spare.
LineDoveInit.js registers the script with the user interface. Open the file in a text editor (not MSWord! Just a plain and simple text editor) and replace all occurrences of LineRectangle with LineDove. Replace the word "&Rectangle" with "Dovetail", and change the setDefaulCommands. The code looks now like this:
Let's check if we did everything right!
Launch QCad and select Draw/Line. You should find *two* icons for drawing a rectangle. Try the first one and draw a rectangle. If you did everything right, you will now see a very quirky shape drawn. No dovetail yet, but at least we "wrote" our first plugin. To be continued... .
If anything went wrong and QCad is not launching anymore, or crashing or anything else, simply remove the directory we created earlier (".../scripts/Draw/Line/LineDove/"). Everything should be back to normal. Now try over
When extending functionality, I like to base my code on a working script and then change it the way I need, verifying my steps. So first we need to find an existing script that does 90% of what we want. Drawing a rectangle from point to point is good. After searching a bit, I found a directory: .../scripts/Draw/Line/LineRectangle/ . So lets duplicate the entire directory just where it is and name the new one "LineDove".
Go into the directory and rename the following files:
Code: Select all
LineRectangle.svg, LineRectangleInit.js, LineRectangle.js
to
LineDove.svg, LineDoveInit.js, LineDove.js
LineDoveInit.js registers the script with the user interface. Open the file in a text editor (not MSWord! Just a plain and simple text editor) and replace all occurrences of LineRectangle with LineDove. Replace the word "&Rectangle" with "Dovetail", and change the setDefaulCommands. The code looks now like this:
function init(basePath) { var action = new RGuiAction(qsTranslate("LineDove", "&Rectangle"), RMainWindowQt.getMainWindow()); action.setRequiresDocument(true); action.setScriptFile(basePath + "/LineDove.js"); action.setIcon(basePath + "/LineDove.svg"); action.setDefaultShortcut(new QKeySequence("r,v")); action.setStatusTip(qsTranslate("LineDove", "Draw a dovetail joint")); action.setDefaultCommands(["dovetail"]); action.setSortOrder(500); EAction.addGuiActionTo(action, Line, true, true, true, true); }LineDove.js contains the actual script. But let's first fix the names so that the original script may run. In the entire file, replace LineRectangle with LineDove. Now find the first line that contains new RVector. This line of code draws the first side of the rectangle. Replace the code snippet this.corner1.y with this.corner1.y/2 (divide the y coordinate of your first click by two).
Let's check if we did everything right!
Launch QCad and select Draw/Line. You should find *two* icons for drawing a rectangle. Try the first one and draw a rectangle. If you did everything right, you will now see a very quirky shape drawn. No dovetail yet, but at least we "wrote" our first plugin. To be continued... .
If anything went wrong and QCad is not launching anymore, or crashing or anything else, simply remove the directory we created earlier (".../scripts/Draw/Line/LineDove/"). Everything should be back to normal. Now try over