Script language questions

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.

alf2006x
Active Member
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Post by alf2006x » Sun Jan 15, 2012 7:48 pm

Ok, thanks.

And can I send to my script some parameters through the command line command?
I need to send to my script the path to text file with different parameters.

I mean something like this:
alf@ubuntu:~$ /home/alf/qcad/qcad-trial -no-gui -autostart /home/alf/MyScripts/TestScript2.js -p'/home/alf/MyScripts/params.txt'

, where '-p' is key of parameter for the script

User avatar
hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

Post by hungerburg » Sat Jan 21, 2012 5:38 pm

alf2006x wrote:Ok, thanks.

And can I send to my script some parameters through the command line command?
I need to send to my script the path to text file with different parameters.
There may be better ways, I do it like that:

Code: Select all

$ DISPLAY="" /opt/qcad/qcad -no-gui -allow-multiple-instances -autostart Headless.js presets.txt 2> /dev/null
In Headless.js, the second argument "presets.txt" is available as the value of the global variable "args[1]". The name of the script "Headless.js" is in args[0]. As far as I know, there are no named parameters, only positional ones.

alf2006x
Active Member
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Post by alf2006x » Mon Jan 23, 2012 9:11 pm

Thanks a lot, hungerburg.

But that was one part of global question. The issue is to bring to my script an array of parameters from internet user form thru the php script. Our php programmer tells me that this task could be doing by Json format.
Is it good idea? And is it correct solution to begin working this way?

User avatar
hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

Post by hungerburg » Tue Jan 24, 2012 12:43 am

alf2006x wrote:Json format. Is it good idea? And is it correct solution to begin working this way?
JSON is great for transferring data objects. QT script has a built in parser, that is to be secure and robust. I use someting like below:

Code: Select all

/**
 * JSON Datei in Objekt laden.
 * @param {String} fn Datei Pfad und Name
 * @returns {Object} Objekt aus Datei
 */
function fromFile(fn) {
	var fh = new QFile(fn);
	var fi = new QFileInfo(fh);
	if (!fi.exists()) { throw new Error("Preset laden: " + fn + " gibt es nicht"); }
	try {
		var ts, res = "";
		fh.open(QIODevice.OpenMode(QIODevice.ReadOnly, QIODevice.Text));
		ts = new QTextStream(fh);
		ts.setCodec("UTF-8");
		while(!ts.atEnd()) { res += ts.readLine(); }
		return JSON.parse(res);
	} catch (e) {
		throw new Error("Preset laden: " + e.message);
	} finally {
		fh.close();
	}
}
var preset = fromFile("foo.json");

alf2006x
Active Member
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Post by alf2006x » Tue Jan 24, 2012 5:50 pm

hungerburg, what IDE (editor, debugger) do you use to write your QCAD scripts?

User avatar
hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

Post by hungerburg » Wed Jan 25, 2012 10:39 am

Alf, I use the free version of komodo edit: it helps with balancing braces and shows many typos, syntax errors etc. It can auto complete functions of my own project, but not the R functions of qcad. It even helps with jsdoc. I use the qcad built in debugger.

alf2006x
Active Member
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Post by alf2006x » Tue Jan 31, 2012 8:00 pm

hungerburg, please help!
I can't remove source Entities (lines)
My script is below.
What am I doing wrong? And is it correct way to remove start lines? Or I should use some way "UpdatePreview()" function?

Code: Select all

var Operation = new RAddObjectsOperation();
var DelOperation = new RDeleteObjectsOperation();

var p1 = new RVector(0, 0);
var p2 = new RVector(10, 0);
var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);

var lineentity1 = new RLineEntity(document, new RLineData(p1, p2));
var lineentity2 = new RLineEntity(document, new RLineData(p2, p3));

Operation.addObject(lineentity1);
Operation.addObject(lineentity2);

var success = Round.round(Operation,lineentity1,p1,lineentity2,p3,true,1.5,false);
DelOperation.deleteObject(lineentity1);
DelOperation.deleteObject(lineentity2);

qDebug("lines is rounded: %s", success);

Operation.apply(document);
DelOperation.apply(document);

User avatar
hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

Post by hungerburg » Fri Feb 03, 2012 6:38 pm

hello Alf, I hear you! Please be aware, that my thoughts will be strongly biased.

For various reasons, I advise against sourcing qcad UI related functions in your code, especially if you are developing a console only application. From reading your snippet, I suppose that you want to create two lines that are joined with a rounded corner. The easiest way I imagine is to draw the lines a little short and the joining arc in between. A more general approach probably was, to create a function, that can round any two lines. Probably lift the rounding code from the qcad UI function and wrap it into your own convenience function. Looks like that is what you are doing, though in the way you do it, I cant see, what the UI function does.

Anyway, without running your code, I cannot tell for sure, what is failing, you may have to be more specific.

alf2006x
Active Member
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Post by alf2006x » Fri Feb 03, 2012 7:59 pm

Hello, hungerburg. I'm very glad to hear you.

Have you any script that makes some difficult drawing? And if you have one can you show it to me? It is very tough to me to understand how to make the drawing in QCAD. There are very few examples in QCAD documentation.
If you send on [email protected] any examples I will be very grateful to you.

User avatar
hungerburg
Premier Member
Posts: 160
Joined: Fri May 28, 2010 7:35 pm

Post by hungerburg » Sun Feb 05, 2012 10:21 pm

Thank you Alf for asking, here to the most tricky piece in all of my qcad scripts, except of course the many spaghetti in some parts ;) See the other thread: http://www.ribbonsoft.com/rsforum/viewt ... =5642#5642

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”