Qcad Simple API :: Polylines

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
tukuyomi
Full Member
Posts: 50
Joined: Mon Aug 09, 2010 6:15 pm

Qcad Simple API :: Polylines

Post by tukuyomi » Fri Sep 09, 2016 7:56 am

Here is a little contribution in attempting to improve addPolyline() function :

Code: Select all

function addPolyline(p, closed, grel) { //p = Array of type [ [ x, y, bulge, rel ], ..., [ ... ] ]
                                     //  rel = true | false (this vector's coords are relative to previous')
                                     //closed = true | false
                                     //grel = true | false (Global relative)
  var pline=new RPolyline();  
  pline.setClosed(isNull(closed) ? true : closed);
  var v = new RVector(0,0);
  for (var i = 0; i < p.length; i++) {
    if( isVector( p[i][0] ) ) {
      var v0 = p[i][0] , b = def(p[i][1]) , rel = p[i][2];
    } else {
      var v0 = new RVector( def(p[i][0]) , def(p[i][1]) ) , b = def(p[i][2]) , rel = p[i][3];
    }
    if ( isNull(rel) ? grel : rel ) {
      v = v.operator_add(v0);
    } else {
      v = v0;
    }
    pline.appendVertex( v , b );
  }
  return addEntity(pline);
}
Differences with Qcad's addPolyline :
- Can use bulge
- Current vertex can be relative to the previous one.
-- 'relative' flag can also be set globally, next to the 'closed polyline' one

Usage :

Code: Select all

// Without Relative Coordinates --standard usage
addPolyline([
 [ 0 , 0 ],
 [ 20 , -10 , 1 ],
 [ 20 , 30 ],
 [ 0 , 20 , 1 ],
])

//With individual Relative Coordinates
addPolyline([
 [ 100 , 0 ],
 [ 20 , -10 , 1 ],
 [ 0 , 40 , 0 , true ],
 [ -20 , -10 , 1 , true ],
])

//With Global Relative Coordinates
addPolyline([
 [ 200 , 0 , 0 ],
 [ 20 , -10 , 1 ],
 [ 0 , 40 ],
 [ -20 , -10 , 1 ],
], true , true )

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

Re: Qcad Simple API :: Polylines

Post by andrew » Fri Sep 09, 2016 8:38 am

Excellent, thanks! I've merged your changes to the master branch:
https://github.com/qcad/qcad/commit/164 ... 96d9fc8458

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”