Script to read blocks positions and store in a csv file

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.

Post Reply
rodmaru
Junior Member
Posts: 13
Joined: Fri Mar 11, 2022 5:21 am

Script to read blocks positions and store in a csv file

Post by rodmaru » Sun Mar 13, 2022 8:51 pm

Hi there,

I'm new to QCAD. Before I was trying Freecad because of the Python scripting possibility. But QCAD is more CAD user-friendly.

I want to get blocks positions and store them in a CSV file.

1. get the list of all blocks
2. for each block get the position and write in the CSV file
3. save CSV file

Anybody can help me with that

Thanks in advance

OS: Debian 11
Version: 3.26.4.0 (3.26.4)
Internet: QCAD.org
Build Date: Sep 14 2021
Qt Version: 5.13.2
Architecture: x86_64
Compiler: gcc 10.2.1

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

Re: Script to read blocks positions and store in a csv file

Post by andrew » Sun Mar 13, 2022 9:12 pm

You might want to have a look at the existing block list export script accessible under Misc > Block > Export Block List. You can find the source on github at:
https://github.com/qcad/qcad/blob/maste ... tExport.js

It only exports the block count and block name but could easily be extended to export positions. Something like:

Code: Select all

        var blockRefIds = doc.queryBlockReferences(id);

        for (var k=0; k<blockRefIds.length; k++) {
            var blockRef = doc.queryEntity(blockRefIds[k]);
            ts.writeString("\n%1\t%2\t%3".arg(block.getName()).arg(blockRef.getPosition().x).arg(blockRef.getPosition().y));
        }

rodmaru
Junior Member
Posts: 13
Joined: Fri Mar 11, 2022 5:21 am

Re: Script to read blocks positions and store in a csv file

Post by rodmaru » Tue Mar 15, 2022 8:03 pm

Hi Andrew,

Thank you for your answer.
I have tried the following code:

Code: Select all

var file = new QFile('/home/maru/Documents/test.text');
var flags = new QIODevice.OpenMode(QIODevice.WriteOnly | QIODevice.Text);

if (!file.open(flags)) {
	this.terminate();
	return;
}

var ts = new QTextStream(file);
ts.setCodec("UTF-8");
ts.writeString("Reference Count\tBlock Name");


var doc = this.getDocument();
var result = doc.queryAllBlocks();
for (var k=0; k<result.length; k++) {
	var block = doc.queryBlock(result[k]);
	var name = block.getName();
	if (name == 'tomada') {
		var blockRefIds = doc.queryBlockReferences(result[k]);
		var blockRef = doc.queryEntity(blockRefIds);
		var x = blockRef.getPosition().x
    		var y = blockRef.getPosition().y
    		ts.writeString("\n%1\t%2\t%3".arg(block.getName()).arg(blockRef.getPosition().x).arg(blockRef.getPosition().y));
	}
}

file.close();
The file was created but with nothing inside.

In the script shell in Qcad I got the result running:

Code: Select all

ecma> var result = doc.queryAllBlocks(); 
undefined
ecma> result
5,51
ecma> var blockRefIds = doc.queryBlockReferences(51);
undefined
ecma> blockRefIds
56
ecma> var blockRef = doc.queryEntity(56);
undefined
ecma> blockRef.getPosition().x
70
ecma> blockRef.getPosition().x
70
ecma> var blockRef = doc.queryEntity(56);
undefined
ecma> blockRef.getPosition().x
80
Image

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

Re: Script to read blocks positions and store in a csv file

Post by andrew » Tue Mar 15, 2022 9:18 pm

Is this your complete script? If not, please post the complete script.
How do you run your script?

rodmaru
Junior Member
Posts: 13
Joined: Fri Mar 11, 2022 5:21 am

Re: Script to read blocks positions and store in a csv file

Post by rodmaru » Wed Mar 16, 2022 11:14 am

Yes, this is my own script, and I run it Misc>Development>Run Script.
The file was created but with nothing inside.

The second I run in the script shell inside Qcad in the order I posted early.

Is that make sense?

Thank you for your help.

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

Re: Script to read blocks positions and store in a csv file

Post by andrew » Wed Mar 16, 2022 11:23 am

There's a lot of code that cannot work in the context of Misc > Development > Run Script. For example everything with "this" cannot work as you are not working with a complete script class but only fragments.

I've added a simple example script that exports block references with block name and position (X,Y):

https://github.com/qcad/qcad/blob/maste ... fExport.js

Create directory scripts/Misc/MiscBlock/BlockRefExport and download the file BlockRefExport.js into that directory.

You can then call the script in QCAD through menu Misc > Block > Export Block References List.

Feel free to modify the script to your needs.

rodmaru
Junior Member
Posts: 13
Joined: Fri Mar 11, 2022 5:21 am

Re: Script to read blocks positions and store in a csv file

Post by rodmaru » Tue Mar 22, 2022 6:19 am

Hi Andrew,

Thank you!
That worked pretty well!

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”