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
Script to read blocks positions and store in a csv file
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Re: Script to read blocks positions and store in a csv file
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:
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));
}
Re: Script to read blocks positions and store in a csv file
Hi Andrew,
Thank you for your answer.
I have tried the following code:
The file was created but with nothing inside.
In the script shell in Qcad I got the result running:
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();
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
Re: Script to read blocks positions and store in a csv file
Is this your complete script? If not, please post the complete script.
How do you run your script?
How do you run your script?
Re: Script to read blocks positions and store in a csv file
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.
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.
Re: Script to read blocks positions and store in a csv file
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.
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.
Re: Script to read blocks positions and store in a csv file
Hi Andrew,
Thank you!
That worked pretty well!
Thank you!
That worked pretty well!