Creating Custom Commands

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
fratink
Registered Member
Posts: 1
Joined: Mon Dec 31, 2018 3:04 am

Creating Custom Commands

Post by fratink » Mon Dec 31, 2018 3:09 am

Hello,

Is there a way to add scripts that use the command line in the qcad GUI? Essentially I'm hoping to be able to run the a script, say createCirclePattern(var foo, var bar, var etc..) within the command prompt of qcad, extending the existing commands you can run.

Something like:

Code: Select all

function circlePattern(x_start, y_start, x_spacing, y_spacing, , xnum, ynum, radius){
  i = 0;
  j = 0;
  for ( i = 0 ; i < xnum ; i++)
  {
    for ( j = 0 ; j < ynum ; j++)
    {
      addCircle(x_start + x_spacing * i, y_start + y_spacing * j, radius);
    }
  }
}
Which is run with:

Code: Select all

circlePattern(30, 30, 30, 30, 4, 4, 5);

Thanks!

riverbuoy
Senior Member
Posts: 121
Joined: Thu Oct 03, 2013 5:37 pm

Re: Creating Custom Commands

Post by riverbuoy » Mon Dec 31, 2018 10:50 am

Hi fratink,
fratink wrote:
Mon Dec 31, 2018 3:09 am
Is there a way to add scripts that use the command line in the qcad GUI?
Simple answer: No

More involved answer: Yes

The simplest way to achieve this is to use the 'script shell' (Misc->Development->Script Shell). This allows you to load scripts containing functions, which you can then run in script shell's command line. (see the 'simple.js' file and others to see how this is done). You could even add your functions to the 'simple_create.js' file, so when the 'script shell' is started your functions will be loaded. However, you have to use script shell's command line rather than the standard command line.

If you need to use the standard command line, you could alter the 'commandline.js' file, which is in 'scipts/Widgets/commandLine'.
If you add the following code to 'commandLine.js' (line 253, just before the '// Handle direct distance entry;' line), you can then run functions by prefixing your function name with a 'greater than' symbol (>).

Code: Select all

            // treat command as ecmascript
            if (command.startsWith(">")) {
                // remove leading '>'
                command = command.slice(1);
                if (eval(command)) {
                    // success
                    leCommand.clear();
                    return;
                } else {
                    // failure
                }
            }
This is very simple, so you can alter it to suit your needs. You could also change the prefix to any other character. (I don't think this breaks anything, but you may know better).

So you could run your function like

Code: Select all

>circlePattern(30, 30, etc...)
I hope this helps to answer your question.

Regards

riverbuoy

Post Reply

Return to “QCAD 'How Do I' Questions”