Page 2 of 2

Re: Scripting question

Posted: Mon Jul 16, 2012 7:31 pm
by andrew
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();

Re: Scripting question

Posted: Tue Jul 17, 2012 7:06 pm
by socool
Thanks for your help Andrew.

Re: Scripting question

Posted: Thu Jul 19, 2012 6:33 pm
by socool
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.

Re: Scripting question

Posted: Thu Jul 19, 2012 7:18 pm
by andrew
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.

Re: Scripting question

Posted: Thu Jul 26, 2012 3:14 pm
by socool
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?

Re: Scripting question

Posted: Thu Jul 26, 2012 3:18 pm
by andrew
Arguments are always of type string. Use standard JavaScript to convert between types:

Code: Select all

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

Re: Scripting question

Posted: Thu Jul 26, 2012 3:28 pm
by socool
Thanks for your help Andrew.