Fusion of shapes

Discussion forum for contributors and developers who are using the QCAD ECMAScript development platform or the C++ plugin interface or who are otherwise looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Post Reply
caramel
Newbie Member
Posts: 4
Joined: Mon Jan 08, 2018 7:01 pm

Fusion of shapes

Post by caramel » Mon Jan 08, 2018 7:05 pm

Hi,

Please, could you tell me if there is a way to construct a shape base on fusion of other shapes like the piture attached :
newShape = fusion([Shape1, Shape2, Shape3]);

Many thanks!
Caramel
Attachments
shapesFusion.PNG
shapesFusion.PNG (65.94 KiB) Viewed 1477 times

User avatar
J-J
Moderator
Posts: 396
Joined: Tue Mar 24, 2009 9:48 pm

Re: Fusion of shapes

Post by J-J » Mon Jan 08, 2018 9:39 pm

Hi Caramel,
Welcome to the forum.
Please, could you tell me if there is a way to construct a shape base on fusion of other shapes like the piture attached
That should be easy using the "break out segment" (D2) tool, just click on the segments you wish to remove.
JJ

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

Re: Fusion of shapes

Post by andrew » Tue Jan 09, 2018 10:40 am

Presumably, you are looking for a programmatic way (since you posted to the developer forum).

For polygon shapes (i.e. only straight segments), you can use clipper which comes with QCAD Professional:

Code: Select all

var clipper = new RPolygonClipper();

// add subject paths (islands: ccw, holes: cw):
clipper.addSubjectPath([new RVector(0,0), new RVector(100,0), new RVector(100,100), new RVector(0,100)]);
clipper.addSubjectPath([new RVector(50,50), new RVector(150,50), new RVector(150,150), new RVector(50,150)]);

// union:
clipper.execute(RS.Union, RS.NonZero);

// print result:
for (var c=0; c<clipper.getSolutionPathCount(); c++) {
    var vertices = clipper.getSolutionPath(c);
    qDebug("polygon:", vertices);
}

caramel
Newbie Member
Posts: 4
Joined: Mon Jan 08, 2018 7:01 pm

Re: Fusion of shapes

Post by caramel » Tue Jan 09, 2018 1:34 pm

Hi andrew,
Thanks for your response,

Yes I'm looking for a programmatic way and just for polygon shapes (with straight segments).

I tested your code with QCAD Professional and it works,
But when I test with other shapes :

Code: Select all

  var clipper = new RPolygonClipper();

  clipper.addSubjectPath([new RVector(0,0), new RVector(10,0), new RVector(10,10), new RVector(0,10)]);
  clipper.addSubjectPath([new RVector(-2,2), new RVector(12,2), new RVector(12,-2), new RVector(-2,-2)]);
  
  // union:
  clipper.execute(RS.Union, RS.NonZero);
  // print result:
  for (var c=0; c<clipper.getSolutionPathCount(); c++) {
      var vertices = clipper.getSolutionPath(c);
      qDebug("polygon:" , vertices)
      qDebug("area:" , Mod_Line2P.getArea(vertices))
  }

I get these results :
polygon: RVector(12, 2, 0, true),RVector(10, 2, 0, true),RVector(10, 10, 0, true),RVector(0, 10, 0, true),RVector(0, 2, 0, true),
RVector(10, 2, 0, true),RVector(10, 0, 0, true),RVector(0, 0, 0, true),RVector(0, 2, 0, true),
RVector(-2, 2, 0, true),RVector(-2, -2, 0, true),RVector(12, -2, 0, true)
area: 116

When I excpect these results :
polygon expected: RVector(12, 2, 0, true),RVector(10, 2, 0, true),RVector(10, 10, 0, true),RVector(0, 10, 0, true),RVector(0, 2, 0, true),
RVector(-2, 2, 0, true),RVector(-2, -2, 0, true),RVector(12, -2, 0, true)
area expected: 136

Is this a bug ?

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

Re: Fusion of shapes

Post by andrew » Tue Jan 09, 2018 2:18 pm

I think you might have missed the bit in the code about islands being CCW (counter-clockwise) and holes being CW (clockwise). Your second polygon is CW, hence treated as hole.

You might want to use RPolyline::getOrientation() to check for orientation CW or CCW, RPolyline::reverse() to reverse and RPolyline::getArea() to compute the area.

caramel
Newbie Member
Posts: 4
Joined: Mon Jan 08, 2018 7:01 pm

Re: Fusion of shapes

Post by caramel » Tue Jan 09, 2018 2:58 pm

You have absolutely right, I missed with orientation as you mentioned, now I get the exact expect results.

Thanks a lot !

Post Reply

Return to “QCAD Developers and Contributors”