object to json output [Solved]

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
arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

object to json output [Solved]

Post by arun » Thu May 11, 2023 11:56 am

Hi,
I have an object that I would need to write to a json file.

code is mentioned below

Code: Select all

const doc = getDocument();

var layerName = getText()
var entitiesIds = doc.queryLayerEntities(doc.getLayerId(layerName));

const entitiesBBox = []
for (var i = 0; i<entitiesIds.length; ++i) {
    const entityId = entitiesIds[i];
    const bbox = {}
    bbox.id = entityId
    const entity = doc.queryEntity(entityId);
    const box = entity.getBoundingBox()
    bbox.props = {x: box.getMinimum().x, y: box.getMinimum().y, width: box.getWidth(), height: box.getHeight()}
    entitiesBBox.push(bbox)
}
How do I export/write entitiesBBox to a json file

I am using windows/linux and on qcad 3.28.1

Thanks,
Arun
Last edited by arun on Tue Jun 06, 2023 10:35 am, edited 1 time in total.

CVH
Premier Member
Posts: 3475
Joined: Wed Sep 27, 2017 4:17 pm

Re: object to json output

Post by CVH » Thu May 11, 2023 5:07 pm

Arun,

First, I don't think you need constants, mostly variables are created with 'var'.

How to create a JSON string from an object:
var exportString = JSON.stringify(object);
https://www.w3schools.com/js/js_json_intro.asp

How to export textual information to a file is something else.
For one, you probably need a file dialog to indicate how the file should be called and where to put it.

Maybe this example can show you how:
https://github.com/qcad/qcad/blob/maste ... lExport.js

Regards,
CVH

arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

Re: object to json output

Post by arun » Fri May 12, 2023 1:45 am

Thanks CVH for pointing out on json creation and export.
First, I don't think you need constants, mostly variables are created with 'var'
I guess I have only come across variable declaration using only 'var' in qcad. But, isn't 'const' and 'let' a preferred way of declaring variables than 'var' since ECMA6.

I am still new to js and excuse my questions if they are stupid.

Thanks,
Arun

CVH
Premier Member
Posts: 3475
Joined: Wed Sep 27, 2017 4:17 pm

Re: object to json output

Post by CVH » Fri May 12, 2023 4:20 am

arun wrote:
Fri May 12, 2023 1:45 am
But, isn't 'const' and 'let' a preferred way of declaring variables than 'var' since ECMA6.
I am convinced that the ECMAScript under QCAD is an early adoption.
On GitHub 'const' returns but 3 hits if we filter on 'JavaScript', all 3 are user contributions.
And none in code for the search term 'let'.

This is another example to export to a text file:
https://github.com/qcad/qcad/blob/611a1 ... tExport.js

Regards,
CVH

arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

Re: object to json output

Post by arun » Fri May 12, 2023 1:59 pm

Got it CVH
Thank you

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”