Scripting question

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.

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

Re: Scripting question

Post by andrew » Mon Jul 16, 2012 7:31 pm

Here's an example for writing to a file using QTextStream:

Code: Select all

var outFile = new QFile("myFile.txt");
var flags = new QIODevice.OpenMode(QIODevice.WriteOnly | QIODevice.Text);
if (!outFile.open(flags)) {
    // cannot open file...
    return;
}

var outStream = new QTextStream(outFile);
outStream.writeString("first line...");

outFile.close();

socool
Junior Member
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Post by socool » Tue Jul 17, 2012 7:06 pm

Thanks for your help Andrew.

socool
Junior Member
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Post by socool » Thu Jul 19, 2012 6:33 pm

How can I use function defined in other file.

I have function in file "FileWithFunction.js"

Code: Select all

function myFunction(varieble1, varieble2){
  ...
};
In file "Script.js" I want to us this funcion

Code: Select all

var result = myFunction(a, b);
I try include file with function at begin of script file:

Code: Select all

include("FileWithFunction.js")
But function not work. If I copy function definition to script file, it works fine.

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

Re: Scripting question

Post by andrew » Thu Jul 19, 2012 7:18 pm

It looks generally correct. Maybe there's another problem or a typo somewhere.

You might also want to try running QCAD with the script debugger enabled, to see if there's an error somewhere:
./qcad -enable-script-debugger

It's also a good idea to keep an eye on the output of QCAD to check for errors and warnings.

socool
Junior Member
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Post by socool » Thu Jul 26, 2012 3:14 pm

Hello,

I use some parameters for my commandline script. I load it by command:

Code: Select all

var parameter1 = args[1];
If parameter is number it is load as string, but I need load it as integer. I multiply it by 1 to create integer:

Code: Select all

var parameter1 = args[1] * 1;
But probably it isn't clear solution, how can I set type of variable for this arguments?

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

Re: Scripting question

Post by andrew » Thu Jul 26, 2012 3:18 pm

Arguments are always of type string. Use standard JavaScript to convert between types:

Code: Select all

var parameter1 = parseInt(args[1]) * 1;

socool
Junior Member
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Post by socool » Thu Jul 26, 2012 3:28 pm

Thanks for your help Andrew.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”