Scripting question

Discussion forum for contributors and developers who are using the QCAD version 3 ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Re: Scripting question

Postby 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();
User avatar
andrew
Site Admin
 
Posts: 2528
Joined: Fri Mar 30, 2007 6:07 am

Re: Scripting question

Postby 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

Postby 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.
socool
Junior Member
 
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Postby 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.
User avatar
andrew
Site Admin
 
Posts: 2528
Joined: Fri Mar 30, 2007 6:07 am

Re: Scripting question

Postby 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?
socool
Junior Member
 
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Re: Scripting question

Postby 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;
User avatar
andrew
Site Admin
 
Posts: 2528
Joined: Fri Mar 30, 2007 6:07 am

Re: Scripting question

Postby socool » Thu Jul 26, 2012 3:28 pm

Thanks for your help Andrew.
socool
Junior Member
 
Posts: 13
Joined: Sun Jun 17, 2012 12:16 pm

Previous

Return to QCAD 3 Developers and Contributors

Who is online

Users browsing this forum: No registered users and 1 guest

cron