How to get line coordinates

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
gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

How to get line coordinates

Post by gialanza » Thu Sep 16, 2021 9:59 pm

Hi, I'm new to Qcad Programming. I searched through examples but I didn't find anything.
How is possible to get the coordinates of starting and ending point of a line?

Thanks in advance
Gianfranco

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

Re: How to get line coordinates

Post by CVH » Fri Sep 17, 2021 6:39 am

Hi,

For a variable line that is a line REntity or a line RShape:

Code: Select all

var endpoint1 = line.getStartPoint();
var endpoint2 = line.getEndPoint();
What also can be used for RArc, RPolyline, RSpline ... and that as entity or as shape. :wink:

Have a look in the resource documentation:

https://qcad.org/doc/qcad/3.0/developer ... f16a98a8e6

https://qcad.org/doc/qcad/3.0/developer ... 2d225698a5

There is also getEndPoints() what returns a list (array) of RVectors of the endpoints.


Regards,
CVH

gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

Re: How to get line coordinates

Post by gialanza » Fri Sep 24, 2021 5:43 pm

@CVH Thanks for the help!

May I ask maybe another trivial question for you.

I trimmed a line and I'm trying to get the new trimmed endpoint but I get the original point ( before trim ).
What I'm missing?

Thanks again in advance.
Gianfranco

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

Re: How to get line coordinates

Post by CVH » Fri Sep 24, 2021 6:40 pm

gialanza wrote:
Fri Sep 24, 2021 5:43 pm
I trimmed a line and I'm trying to get the new trimmed endpoint but I get the original point ( before trim ).
What I'm missing?
Without an example script I wouldn't know. :wink:
Can you post a snippet?

Regards,
CVH

gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

Re: How to get line coordinates

Post by gialanza » Fri Sep 24, 2021 9:49 pm

This is part of a test script:

var v=[];

v.push([10,10]);
v.push([20,20]);
v.push([30,50]);
v.push([40,40]);
v.push([50,20]);
v.push([60,10]);


// add a spline to the drawing, using
// the computed coordinates as fit points:
SplineObj=addSpline(v, false);

// Add a vertical line intersecting the spline

var LineaStartPoint = new RVector(25,0);
var LineaEndPoint = new RVector(25,60);

var LineaObj = addLine(LineaStartPoint,LineaEndPoint);

// Trim the line to the intersection point with the spline

trim(LineaObj,25,0,SplineObj,25,0,false);

// Retrieve the starting and ending point

var startpoint1 = LineaObj.getStartPoint();
var endpoint2 = LineaObj.getEndPoint();

// Draw to new lines from (0,0) to the two points

var PuntoZero = new RVector(0,0);
var linea2 = addLine(PuntoZero,startpoint1);
var linea3 = addLine(PuntoZero,endpoint2);

// auto zoom:
autoZoom();

The linea3 shoud be ending touching the spline but instead it finish to the original endpoint.

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

Re: How to get line coordinates

Post by CVH » Sat Sep 25, 2021 2:00 am

Thanks for the snippet.

Running your code it is clear that the vertical line entity is trimmed to the spline entity.
With trimBoth = false only the first entity gets trimmed.

What happens here is that you add a line LineaObj and trim it later to the spline ...
... but you don't retrieve the new shape of the line after trimming.
The variable LineaObj still holds a clone of the shape you created with addLine().
See simple_create.js: function addLine() >> function addShape() >> function addEntity() >> function addObject()

Updated code below :wink:
Can be run in the Script Shell (GE) as is.
In a running GUI script the document is usually known and retrieved with e.g.: var doc = this.getDocument();

Regards,
CVH

Code: Select all

 // Using the simple API (if required to include it)
 // include("scripts/simple.js");

 // Spline coordinates as fitpoints:
 var v = [];
 v.push([10, 10]);
 v.push([20, 20]);
 v.push([30, 50]);
 v.push([40, 40]);
 v.push([50, 20]);
 v.push([60, 10]);

 // Add a fitpoint spline to the drawing:
 SplineObj = addSpline(v, false);    // NOTclosed periodic

 // Add a vertical line intersecting the spline:
 var LineaStartPoint = new RVector(25, 0);
 var LineaEndPoint = new RVector(25, 60);
 var LineaObj = addLine(LineaStartPoint, LineaEndPoint);

 // Trim the line to the intersection point with the spline:
 trim(LineaObj, 25, 0, SplineObj, 25, 0, false);    // NOTtrimBoth

 // Retrieve the trimmed shape:
 var doc = getTransactionDocument();
 var trimmedObj = doc.queryEntity(LineaObj.getId());

 // Retrieve the trimmed shape its starting and ending point:
 var startpoint1 = trimmedObj.getStartPoint();
 var endpoint2 = trimmedObj.getEndPoint();

 // Draw two new lines from the origin to the two endpoints:
 var PuntoZero = new RVector(0, 0);
 var linea2 = addLine(PuntoZero, startpoint1);
 var linea3 = addLine(PuntoZero, endpoint2);

 // Auto zoom:
 autoZoom();

gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

Re: How to get line coordinates

Post by gialanza » Sat Sep 25, 2021 1:09 pm

Great!

As I wrote I'm a rookie with Qcad and you gave me a big help.
I need to study.

Thanks a lot!
Gianfranco

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”