Page 1 of 1

[SOLVED] QCAD to start with custom code

Posted: Wed May 14, 2025 5:19 pm
by Jeffyu
Hello,
This may be very basic. Please show me a link if it is already answered.
I have win 10 and QCAD ver 3.32.2, installed in late March.
Following the scripting tutorials, trying to add new menu/toolbar and new action, but when start QCAD, new menu / action don't show, and there is no error messages anywhere.
Here is the folder I made the file structure to follow the tutorial, also tried the installation scripts folder, same result.
C:\Users\**design\AppData\Roaming\QCAD\scripts

even added this to QCAD3.ini
[Scripts]
ScriptPath=C:/Users/**design/AppData/Roaming/QCAD/scripts, C:/Program Files/QCAD/scripts
EnableScripting=1

[Debug]
LogFile=C:/temp/qcad_debug.log
LogLevel=Debug

Anything missed in the QCAD installation?
can somebody help on this? thanks a lot.
Jeff

Re: QCAD to start with custom code

Posted: Thu May 15, 2025 5:03 am
by CVH
Hi, and welcome to the QCAD forum.

No, scripting under QCAD is not really considered as basic for many users. :wink:
But many things are standardized and logically ordered.
The folder structure under 'scripts' is like a mirror of the menus.

It can simply be uppercase 'ENERGY' and Camel-case 'Energy.js' for the script name. :wink:

Most QCAD tools are configured as Addons and then it is logical that setting these up uses a standardized method.
On startup QCAD scans all sub folders of the folder called 'scripts' for a JS file with the same name as the sub folder.
If found it tries to call the init() function to initialize the Addon.

Energy.init is never called because the script resides in the folder 'ENERGY'.

There are two ways to define the init() function:
- As a function included in the tool script itself (Called 'MyCustomScript' as example):

Code: Select all

MyCustomScript.init = function(basePath) { ... }
- Or included in a separate JS script called 'MyCustomScriptInit.js':

Code: Select all

function init(basePath) { ... }
When the tool script has no init() function it textually adds 'Init' to the script base name and tries to locate that JS file.
If that exists in the same folder it calls the init() function there.


# You don't need to edit the QCAD3.ini at all.
Remove these unsupported and thus obsolete entries.

# After defining Energy.prototype it is common to set the base path:

Code: Select all

// Derive class Energy from class EAction:
Energy.prototype = new EAction();
Energy.includeBasePath = includeBasePath;
# It may be required to handle a call like this.getTitle().

Code: Select all

Energy.prototype.getTitle = function() {
    return Energy.getTitle();
};
# In DxfBlock.js it is better to include Energy.js in direct relative to 'scripts' instead of as 1 level back:

Code: Select all

// include base class
include("scripts/Energy/Energy.js");
# The beginEvent of DxfBlock.js calls Energy.prototype.beginEvent.call(this); and that is undefined and probably transferred to EAction.
Typically used to show the tools panel if any.

# Displaying a user message/info/warning can simply be handled by EAction:

Code: Select all

DxfBlock.prototype.beginEvent = function() {
    Energy.prototype.beginEvent.call(this);

    EAction.handleUserMessage("DxfBlock is running.");

    this.terminate();
};
# I would not rule out setting a default command.

# About sort orders ... In 2015 Andrew published a list and I think that this is still valid, see related topic.
I am about sure that your custom script is unrelated to the standard included 'Tutorials'. :wink:
Your custom menu should pop up far to the right when using a GroupSortOrder of 90000 for example.

The SortOrder is added to that, typically 100-200-300-... instead of 1-2-3-... within this menu.
That leaves room for later inserting a tool with order 150 for example.

Regards,
CVH

Re: QCAD to start with custom code

Posted: Thu May 15, 2025 8:57 am
by CVH
Also ...

You can add custom scripts under the 'scripts' folder of your QCAD installation.
On Windows and installed with the msi installer that is under the 'Programs File' folder but guarded by the UAC.
Then you probably need Admin rights to change anything.

Another option is using the local data location, see QCAD Changelog and scroll down to 3.26.2 (2021/04/15)
There is mentioned: Windows: 'C:/Users/[Username]/AppData/Local/QCAD/QCAD'

The complete path to the 'scripts' folder there would be: 'C:/Users/[Username]/AppData/Local/QCAD/QCAD/scripts/...'
Remark that 'QCAD' occurs twice. :wink:

Recently Andrew advised to verify this, see related topic.
Start Misc .. Development .. Script Shell (GE) and execute: RSettings.getDataLocation()

In my case that is under 'AppData/Local' and not under 'AppData/Roaming'.

Also remark the difference in folder separator.
Mostly Windows can cope but in JS it must be a normal slash '/'.

Regards,
CVH

Re: QCAD to start with custom code

Posted: Thu May 15, 2025 3:12 pm
by Jeffyu
Thank you very much for your time.
I am pretty new to this scripting part, will need to digest your information and post my progress later.

update:
so the most important first step is to put custom scripts in right location.

Another option is using the local data location, see QCAD Changelog and scroll down to 3.26.2 (2021/04/15)
There is mentioned: Windows: 'C:/Users/[Username]/AppData/Local/QCAD/QCAD'

my case is: 'C:/Users/[Username]/AppData/Local/QCAD/QCAD Professional', by running:
Start Misc .. Development .. Script Shell (GE) and execute: RSettings.getDataLocation()
then add scripts folder inside. Now I see my custom menu/action.

@CVH, I started from this tutorial: https://qcad.org/doc/qcad/3.0/developer/index.html, a bit confusion when installation scripts folder needs admin rights to add files.

Thanks again.

Re: [SOLVED] QCAD to start with custom code

Posted: Tue May 20, 2025 6:11 am
by CVH
Hi,

Updating a comment/reply is not notified.
Stumbled on it while reviewing.
Jeffyu wrote:
Thu May 15, 2025 3:12 pm
so the most important first step is to put custom scripts in right location.
Yes, sub-folders of scripts and menus are like mirrors.
Jeffyu wrote:
Thu May 15, 2025 3:12 pm
a bit confusion when installation scripts folder needs admin rights to add files.
That is Windows related when QCAD was installed using the msi.

You can also install QCAD from a provided ZIP into any location.
Manually adding a desktop shortcut and file associations.

Tutorials, guidelines and many other things are usually 'as in general' and OS independent.

Regards,
CVH