Saving coordinates in csv

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

Moderator: andrew

Post Reply
mpele
Newbie Member
Posts: 7
Joined: Mon Jan 02, 2017 9:57 am

Saving coordinates in csv

Post by mpele » Wed Jan 04, 2017 2:51 pm

I am trying to modify tree point example to save coordinates of selected points in csv file.
I have added the code in the ExThreePoints.prototype.coordinateEvent and there are no errors during execution. The file is created, but it is empty.
Could anyone suggest me what is wrong with code, or point me to documentation where I could find answer?

Code: Select all

var fileName = "d:\\points.txt";

var file = new QFile(fileName);

var flags = new QIODevice.OpenMode(QIODevice.Append | QIODevice.Text);
if (!file.open(flags)) {
	EAction.handleUserWarning("cannot open file: " + fileName);
	this.terminate();
	return;
}

EAction.handleUserMessage("exporting: " + fileName);

var stream = new QTextStream(file);
stream << "Result: " << pos.x << pos.y;
file.close();

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

Re: Saving coordinates in csv

Post by andrew » Wed Jan 04, 2017 2:58 pm

ECMAScript (JavaScript) does not have stream operators (<<). Stream operators are mapped from C++ to ECMAScript through functions called writeXXX (writeString, writeInt, ...).

You can find an example for writing to a file under scripts/Misc/MiscBlock/BlockListExport/BlockListExport.js.

mpele
Newbie Member
Posts: 7
Joined: Mon Jan 02, 2017 9:57 am

Re: Saving coordinates in csv

Post by mpele » Thu Jan 05, 2017 8:14 am

Thank you andrew - it works now.
I have expected, if the stream operators are not supported to get some error or warning.

What documentation would you suggest to read if someone want to get familiar for ECMAScript for QCad? For example how to include script with relative path, like:

Code: Select all

include("scripts/EAction.js"); 
from standalone script.

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

Re: Saving coordinates in csv

Post by andrew » Thu Jan 05, 2017 9:56 am

mpele wrote:I have expected, if the stream operators are not supported to get some error or warning.
<< is a bitwise shift in ECMAScript. The code is valid in ECMAScript, but just does something completely different.
mpele wrote:What documentation would you suggest to read if someone want to get familiar for ECMAScript for QCad? For example how to include script with relative path, like:

Code: Select all

include("scripts/EAction.js"); 
from standalone script.
I'd suggest to look at examples. Always keep the scripts directory of QCAD ready and search through it for a class name when working with a particular class. Chances are you will find example code that does almost exactly what you need.

You can find the QCAD API reference at:
https://www.qcad.org/doc/qcad/latest/developer/

The QCAD Simple API provides some convenience functions for scripts:
https://www.qcad.org/doc/qcad/latest/de ... imple.html

Finally, all Qt classes are documented in the Qt API reference. With the exceptions of a few special cases (like streams or flags), almost the entire Qt API can be used in the same way as in C++:
https://doc.qt.io/qt-5/reference-overview.html

mpele
Newbie Member
Posts: 7
Joined: Mon Jan 02, 2017 9:57 am

Re: Saving coordinates in csv

Post by mpele » Mon Jan 09, 2017 10:33 am

Hi andrew,
Thank you for your information. I am going better, but still I need some help.
I am trying to modify example ExThreePoints to be started as standalone application, but with no results. Could you explain what should be changed in the mentioned example to start with execution of script. If the script (example) is moved from original folder and started through menu nothing is happening – neither it is added to the menu nor it the input of point is expected.

Thanks

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

Re: Saving coordinates in csv

Post by andrew » Mon Jan 09, 2017 12:17 pm

You can launch your script as standalone application using the -autostart command line option:

./qcad -autostart scripts/MyTool/MyScript/MyScript.js

The script is then simply executed and QCAD terminated. No special structure is required from your script, but it would usually define a main function which is launched at the end of the script (if the script is not being included).

It is possible for the same script to work when started from command line (standalone) or within QCAD from a menu. In that case it has to contain a class with the same name as the script and at least a beginEvent method. The beginEvent method could start the same function as the main of the command line version.

For a complete example of a command line tool (standalone application), please refer to the merge command line tool:

Launch script (Linux / macOS):
https://github.com/qcad/qcad/blob/master/merge

Launch script (Windows):
https://github.com/qcad/qcad/blob/master/merge.bat

Implementation:
https://github.com/qcad/qcad/tree/maste ... geDrawings

Post Reply

Return to “QCAD Developers and Contributors”