scripting, fallen at the first hurdle !

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

scripting, fallen at the first hurdle !

Post by petevick » Wed Mar 03, 2021 8:11 pm

I thought I'd delve into the witchcraft of scripting, but I've fallen at the first hurdle!. I thought I'd start with the Adding Interaction example here -> https://qcad.org/en/tutorial-interactive-script-actions, copied and pasted and saved as test.js. Ran it through Run Script command, aaaaand, nothing, no crosshair to pick a co-ordinate, not even an error. I tried running the code through the Script Shell, same lack of results. This is the code...

Code: Select all

include("scripts/EAction.js");

function ExMinimal(guiAction) {
    EAction.call(this, guiAction);

    this.pos = undefined;
}

ExMinimal.prototype = new EAction();

ExMinimal.prototype.beginEvent = function() {
    EAction.prototype.beginEvent.call(this);

    var di = this.getDocumentInterface();
    di.setClickMode(RAction.PickCoordinate);
};

ExMinimal.prototype.pickCoordinate = function(event, preview) {
    this.pos = event.getModelPosition();

    if (preview) {
        this.updatePreview();
    }
    else {
        this.applyOperation();
    }
};

ExMinimal.prototype.getOperation = function(preview) {
    var doc = this.getDocument();

    var op = new RAddObjectOperation();
    var circle = new RCircle(this.pos, 1);
    op.addObject(shapeToEntity(doc, circle));
    return op;
};

ExMinimal.init = function(basePath) {
    var action = new RGuiAction(qsTr("&Minimal Example"), RMainWindowQt.getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/ExMinimal.js");
    action.setGroupSortOrder(100000);
    action.setSortOrder(0);
    action.setWidgetNames(["ExamplesMenu"]);
};
So what be the problem ?
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: scripting, fallen at the first hurdle !

Post by CVH » Wed Mar 03, 2021 9:05 pm

petevick wrote:
Wed Mar 03, 2021 8:11 pm
saved as test.js
Pete,

This is a QCAD GUI script, one that will get listed as a button to click on in Menus, in Toolbars and/or in Panels.
An AddOn to the QCAD GUI.
It will not run in the shell as a simple script.

Then the Qcad script folder tree and the script names are like mirrors. :!:

In ExMinimal.init we read action.setScriptFile(basePath + "/ExMinimal.js");
So the script name should be 'ExMinimal.js'
And it should be in a folder called 'ExMinimal' (Without the extension).
That way it is found and then initiated on boot by the init section.

That folder should be a subfolder of the folder 'Examples'.
And that is a subfolder of ..QCAD/scripts/Misc

On top, it might already been included ...
... As it is present in compiled form.
Look at Menu Misc .. Script Examples .. Minimal Example :wink:
But this 'ExMinimal.js' script will send 'Hello World' to the command history.

If you want them both:
You might want to download ExMinimal2.js and ui (user interface widget) from Github.
https://github.com/qcad/qcad/tree/maste ... c/Examples
Here you see the same folder & script tree as mentioned above.
And a vast amount of open source scripts to peek in ... :P

Documentation doesn't always keep up with development. :wink:

Regards,
CVH

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: scripting, fallen at the first hurdle !

Post by andrew » Wed Mar 03, 2021 9:11 pm

Thanks for the hint. I've added the missing bit of information that caused the problem for you:

"Note that in order for the script to be found, the file name needs to match the class name, i.e. "ExMinimal.js" in this case. It also needs to reside inside a directory with the same name "ExMinimal", so this script can for example be put into scripts/Misc/ExMinimal/ExMinimal.js."

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: scripting, fallen at the first hurdle !

Post by andrew » Wed Mar 03, 2021 9:15 pm

Thanks CVH. I also changed the name of the example to not collide with the other ExMinimal example.

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: scripting, fallen at the first hurdle !

Post by petevick » Wed Mar 03, 2021 9:16 pm

Thanks for the reply Andrew and CVH. I was right about witchcraft then :lol:
I think I'll have second thoughts about delving into scripting, AutoLisp was never this difficult :roll: :lol:
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: scripting, fallen at the first hurdle !

Post by andrew » Wed Mar 03, 2021 9:43 pm

Always a question of perspective.

QCAD very much deliberately uses ECMAScript. Everyone with some programming under the hood can easily understand the basics of ECMAScript (JavaScript). Lisp is for most at best a challenge at worst an alphabet soup riddle with added brackets :wink:

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: scripting, fallen at the first hurdle !

Post by petevick » Wed Mar 03, 2021 9:56 pm

andrew wrote:
Wed Mar 03, 2021 9:43 pm
Lisp is for most at best a challenge at worst an alphabet soup riddle with added brackets :wink:
:lol: I like the analogy :lol: To be honest it must be over 25 years since I was AutoLisping in anger, so I'd probably think that was witchcraft now :lol:
I'll take another look at it tomorrow, and then I'll probably decide to stick with stylesheet coding e_geek
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: scripting, fallen at the first hurdle !

Post by CVH » Wed Mar 03, 2021 9:58 pm

petevick wrote:
Wed Mar 03, 2021 9:16 pm
I was right about witchcraft then :lol:
It only looked as witchcraft for those that were outsiders.
Witches were herbalists.

Remark that I don't consider myself as an insider, meanwhile I know were to look for the trees in the forrest :wink:

The basic layout of GUI script names, class names and folders is just a matter of convention.
I find EMCAScript readable and AutoLisp looks as witchcraft to me.
Too many unknowns and Lisp scripts can be made very hard to read. :lol:

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”