[Solved] Get all contour lengths

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
SM_MaGa
Junior Member
Posts: 10
Joined: Tue Apr 13, 2021 3:20 pm

[Solved] Get all contour lengths

Post by SM_MaGa » Fri Jun 11, 2021 9:53 am

Hello,

i am currently looking for a way to export the lenghts of every contour in a .dxf file.
While using the GUI i can use the function "(De)-Select Contour" to manually select a single contour.
The length is then shown in the property window.

Is there a way to simulate that behaviour that function in a script? Just iterating through every contour would be fine.
I intend to use the .csv export sample to export the contour lengths.

Thanks for your support.
Last edited by SM_MaGa on Mon Aug 02, 2021 2:11 pm, edited 1 time in total.

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

Re: Get all contour lengths

Post by andrew » Fri Jun 11, 2021 9:57 am

Are you using QCAD Professional of the QCAD Community Edtion?

SM_MaGa
Junior Member
Posts: 10
Joined: Tue Apr 13, 2021 3:20 pm

Re: Get all contour lengths

Post by SM_MaGa » Fri Jun 11, 2021 9:59 am

I am using QCAD/CAM.

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

Re: Get all contour lengths

Post by CVH » Sat Jun 12, 2021 6:41 am

Hi,
Your question is rather ambiguous.

(De)-Select Contour (TC) doesn't select contours in the meaning of closed CNC contours.
In selection mode the tool selects all entities somehow connected to the entity reffered to.
Test it for yourself:
Test_TC.dxf
(101.22 KiB) Downloaded 414 times

The answer would be easier if your individual closed contours would be polylines.

One can select all connected entities to a given entity ID with the same result as TC by:

Code: Select all

var doc = this.getDocument();
var di = this.getDocumentInterface();
// var entityId = given entity ID 
// var tolerance = specific value or nothing for RS.PointTolerance
// var layerId = restricted to be on the given layer ID or nothing for any layer

var connectedEntities = doc.queryConnectedEntities(entityId, tolerance, layerId);
di.selectEntities(connectedEntities, false)

What returns a list of ID's that can be set selected as in the above.
Details:
https://github.com/qcad/qcad/blob/maste ... Contour.js
https://qcad.org/doc/qcad/3.0/developer ... c737669802
https://qcad.org/doc/qcad/3.0/developer ... a1be59fbe4

The idea is then to query all entities of your drawing.
Cycle the ID's one by one
Exclude some like Points .. Texts .. Dims ....
Retrieve the connected ID's.
Sum the individual lengths of the connected ID's for CSV export. (Here selections isn't required)
Exclude connected ID's from further cycling.

Regards,
CVH

SM_MaGa
Junior Member
Posts: 10
Joined: Tue Apr 13, 2021 3:20 pm

Re: Get all contour lengths

Post by SM_MaGa » Fri Jun 18, 2021 3:27 pm

Hi @CVH,

thanks for your detailed reply.
A script that gives the same output as the UI function is totally sufficient.

I tried to create a script using your sample and guide.
Now i am able to iterate through every entity and for each entity get the
connect entities. For each connected entity i write the id and draworder in a text file.

Here i noticed that the parameter of the function "queryConnectedEntities" are completely ignored.
I just outputs every entity. Do you have a working sample that uses this function?

Besides that i am missing a way to get the length of an entity. Using entity.length just writes "undefined" in the text file.

Thanks for you help.

greetings
Max

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

Re: Get all contour lengths

Post by CVH » Fri Jun 18, 2021 4:38 pm

SM_MaGa wrote:
Fri Jun 18, 2021 3:27 pm
For each connected entity i write the id and draworder in a text file.
Draw order serves for ... ?
I think you have more specific info with handle than with order.
The order of things may change after file close revert.
(Also ... id's may change between queries)
SM_MaGa wrote:
Fri Jun 18, 2021 3:27 pm
Here i noticed that the parameter of the function "queryConnectedEntities" are completely ignored.
tolerance can be ignored and then RS.PointTolerance (=1e-9) is used OR provide in a more suited tolerance ... A value.
layerId can be ignored for allowing all layers OR should be a valid layer id to restrick.

e.g. the entity layer itself:

Code: Select all

var doc         // a reference to the document in question
var e_id        // the entity id by any means
var entity = doc.queryEntity(e_id);  // query the entity by id
var l_id = entity.getLayerId(); // returns the layer id of the entity
( Remark here: 'RS::PointTolerance' -> RS.PointTolerance ; 'RLayer::INVALID_ID' -> RObject.INVALID_ID )
SM_MaGa wrote:
Fri Jun 18, 2021 3:27 pm
Do you have a working sample that uses this function?
Nope, surely Andrew has a working method, presumed a recursive one.
I only gave my idea on how one could implement it ... Rather coarse ... All depends on what the expected contours are.
... How many and what shapes there are involved ....
Besides that, I provided the related sources for documentation. 8)
SM_MaGa wrote:
Fri Jun 18, 2021 3:27 pm
Using entity.length just writes "undefined" in the text file.
.length is something assosiated with strings ... arrays ... the lenght of an array. :wink:
https://www.w3schools.com/jsref/jsref_length_string.asp
https://www.w3schools.com/jsref/jsref_length_array.asp

.getLength() is in general the actual length of an entity, shape, ....
Enter 'getLength' in the search here: https://qcad.org/doc/qcad/3.0/developer/index.html
'length' would not return many hits :roll: :wink:

Regards,
CVH
Last edited by CVH on Fri Aug 27, 2021 12:16 pm, edited 1 time in total.

SM_MaGa
Junior Member
Posts: 10
Joined: Tue Apr 13, 2021 3:20 pm

Re: Get all contour lengths

Post by SM_MaGa » Mon Aug 02, 2021 2:11 pm

Hello,

thanks for the information. I finally got it running.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”