Page 1 of 1

How to get intersection points between polyline with another

Posted: Mon Apr 11, 2016 10:57 am
by duccoder
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 6459 times

Re: How to get intersection points between polyline with ano

Posted: Mon Apr 11, 2016 11:30 am
by andrew
Can you attach the drawing as DXF file please? Thanks.

Re: How to get intersection points between polyline with ano

Posted: Mon Apr 11, 2016 11:38 am
by duccoder
Here is dxf file generated from above code

Re: How to get intersection points between polyline with ano

Posted: Mon Apr 11, 2016 1:25 pm
by andrew
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);

Re: How to get intersection points between polyline with ano

Posted: Mon Apr 11, 2016 2:42 pm
by duccoder
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

Re: How to get intersection points between polyline with ano

Posted: Mon Apr 11, 2016 6:45 pm
by andrew
REntity based classes also have a getIntersectionPoints method:
limitEntity.getIntersectionPoints(trimEntity, limited);

Re: How to get intersection points between polyline with ano

Posted: Tue Apr 12, 2016 2:35 am
by duccoder
Done. Thank you so much