How to get intersection points between polyline with another

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
duccoder
Active Member
Posts: 30
Joined: Thu Jun 25, 2015 10:11 am

How to get intersection points between polyline with another

Post by duccoder » Mon Apr 11, 2016 10:57 am

I try with lasted QCAD Pro (3.13.1) but can not get intersection points between polyline with another entity. Help me, please
var drawPolyline = function (rPolyline) {
  var op = new RAddObjectsOperation();
  var entity = new RPolylineEntity(document, new RPolylineData(rPolyline));
  op.addObject(entity);
  op.apply(document);
  return document.queryEntity(entity.getId());
};

var drawSpline = function (rSpline) {
  var op = new RAddObjectsOperation();
  var spline = new RSplineEntity(document, new RSplineData(rSpline));
  op.addObject(spline, false);
  op.apply(document);
  return document.queryEntity(spline.getId());
};

var n1 = new RVector(0, 0);
var n2 = new RVector(50, 50);
var n3 = new RVector(60, 30);
var n4 = new RVector(40, 20);

var r1Shape = drawPolyline(new RPolyline([n1, n2, n3, n4], false)).getData().castToShape();

var n5 = new RVector(20, 0);
var n6 = new RVector(30, 60);
var n7 = new RVector(40, 30);

var r2 = new RSpline();
r2.appendFitPoint(n5);
r2.appendFitPoint(n6);
r2.appendFitPoint(n7);
var r2Shape = drawSpline(r2).getData().castToShape();

var r3 = new RSpline();
r3.appendFitPoint(n1);
r3.appendFitPoint(n2);
r3.appendFitPoint(n3);
r3.appendFitPoint(n4);
var r3Shape = drawSpline(r3).getData().castToShape();

print(r1Shape.getIntersectionPoints(r2Shape, false)); // not show intersection points
print(r3Shape.getIntersectionPoints(r2Shape, false)); // RVector(23.5319, 41.9233, 0, true),RVector(33.7983, 51.6832, 0, true)
intersection_polyline_spline.png
intersection_polyline_spline.png (8.69 KiB) Viewed 6458 times

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

Re: How to get intersection points between polyline with ano

Post by andrew » Mon Apr 11, 2016 11:30 am

Can you attach the drawing as DXF file please? Thanks.

duccoder
Active Member
Posts: 30
Joined: Thu Jun 25, 2015 10:11 am

Re: How to get intersection points between polyline with ano

Post by duccoder » Mon Apr 11, 2016 11:38 am

Here is dxf file generated from above code
Attachments
test.dxf
(182.45 KiB) Downloaded 393 times

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

Re: How to get intersection points between polyline with ano

Post by andrew » Mon Apr 11, 2016 1:25 pm

The .getData().castToShape() construct is generally not a good idea since a data object is created (but not used) and a pointer to it's shape is retrieved which is well used. The data object will get deleted by the garbage collector and the pointer will become invalid.

I'd refactor as follows:
var r1Shape = new RPolyline([n1, n2, n3, n4], false);
drawPolyline(r1Shape);
...
var r2Shape = new RSpline();
r2Shape.appendFitPoint(n5);
r2Shape.appendFitPoint(n6);
r2Shape.appendFitPoint(n7);
drawSpline(r2Shape);

duccoder
Active Member
Posts: 30
Joined: Thu Jun 25, 2015 10:11 am

Re: How to get intersection points between polyline with ano

Post by duccoder » Mon Apr 11, 2016 2:42 pm

Thank Andrew, I understand, can easy get intersection point from 2 shapes: RSpline & RPolyline

But, if I only have entities (not have RShape),
r1 = RPolylineEntity(...) // this is RPolylineEntityPointer
r2 = RSpline(...) // this is RSplineEntityPointer

// From here, how to get intersection of 2 entities ?

var getIntersectionPoints = function (limitEntity, trimEntity, limited) {
  limited = limited || false;

  var limitShape = limitEntity.getData().castToShape();
  var trimShape = trimEntity.getData().castToShape();

  return trimShape.getIntersectionPoints(limitShape, limited);
};

print(getIntersectionPoints(r1, r2));
Can you help me solve getIntersectionPoints function? I attach snippet autostart file below
Attachments
intersection_polyline.js
(1.46 KiB) Downloaded 363 times

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

Re: How to get intersection points between polyline with ano

Post by andrew » Mon Apr 11, 2016 6:45 pm

REntity based classes also have a getIntersectionPoints method:
limitEntity.getIntersectionPoints(trimEntity, limited);

duccoder
Active Member
Posts: 30
Joined: Thu Jun 25, 2015 10:11 am

Re: How to get intersection points between polyline with ano

Post by duccoder » Tue Apr 12, 2016 2:35 am

Done. Thank you so much

Post Reply

Return to “QCAD 'How Do I' Questions”