ECMA Script for automatic Polyline offset

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
IBN
Newbie Member
Posts: 3
Joined: Wed Jun 22, 2016 9:03 am

ECMA Script for automatic Polyline offset

Post by IBN » Wed Jun 22, 2016 9:14 am

Hello.

I just received my copy of QCAD Pro and so i'm fairly new to it's scripting concept.

I want to make a script that automatically offsets all polylines on a layer by a certain amount and all in the same direction,
(all outwards or all inwards) to make kerf compensation for Laser cutting.

1. Would that be possible at all?
2. What prerequisites do i need to make a script ? (compiler?)
3. How can i iterate through all Polylines on one specific layer?
4. How to get the inward/outward side of a polyline entitiy?

Any help gratly appreciated.

cheers
Christian

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

Re: ECMA Script for automatic Polyline offset

Post by andrew » Wed Jun 22, 2016 10:32 am

IBN wrote:1. Would that be possible at all?
Yes.
IBN wrote:2. What prerequisites do i need to make a script ? (compiler?)
None. Any QCAD Professional package will do.
Have a look at the example scripts in scripts/Misc/Examples of your QCAD installation to get an idea of how scripts are structured in QCAD. For example scripts/Misc/Examples/MathExamples/ExMathSpiral/ExMathSpiral.js.
IBN wrote:3. How can i iterate through all Polylines on one specific layer?
Getting all entities on a layer:
var ids = doc.queryLayerEntities(layer.getId());
Iteration through polylines:
for (var i=0; i<ids.length; i++) {
    var id = ids;
    var entity = document.queryEntity(id);

    if (isPolylineEntity(entity)) {
        // do something with the polyline entity
    }
}

IBN wrote:4. How to get the inward/outward side of a polyline entitiy?

var shape = polylineEntity.castToShape();

var worker = new OffsetProWorker(
    shape,                     // polyline shape (RPolyline)
    distance,                  // offset distance
    1,                         // number of offset polylines to create
    RVector.invalid,           // irrelevant for this use case
    true,                      // true for inside / false for outside
    joinType,                  // * 
    endType,                   // *
    false,                     // true to interpolate arcs with line segments
    false,                     // true to verify result
    false                      // true to indicate we're doing a preview only
);

var offsetPolylines = worker.getOffsetShapes();


* Join types and end types are defined in RS.h:
RS::JoinType
RS::EndType

IBN
Newbie Member
Posts: 3
Joined: Wed Jun 22, 2016 9:03 am

Re: ECMA Script for automatic Polyline offset

Post by IBN » Thu Jun 23, 2016 8:17 am

Hi Andrew.

thanks a lot for you very detailed reply.
I think that will at least get me started.

What i'm not sure about is what you meant here :
andrew wrote:
var shape = polylineEntity.castToShape();

var worker = new OffsetProWorker(
    shape,                     // polyline shape (RPolyline)
    distance,                  // offset distance
    1,                         // number of offset polylines to create
    RVector.invalid,           // irrelevant for this use case
    true,                      // true for inside / false for outside
    joinType,                  // * 
    endType,                   // *
    false,                     // true to interpolate arcs with line segments
    false,                     // true to verify result
    false                      // true to indicate we're doing a preview only
);
How does the offset function tell which side of the line is in or out?
Does it do the necessary calculations internally?

If so, it would be quite easy to use.

Is it possible to define on which layer the offset line is placed, or does it always go to the same layer ?
If so i would have so "sort" the offsets out afterwards. (i think should be doably by script)

I will have a look at the demo scripts as soon as i got a little spare time and make a first try.

Thanks a lot again!

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

Re: ECMA Script for automatic Polyline offset

Post by andrew » Thu Jun 23, 2016 12:27 pm

IBN wrote:How does the offset function tell which side of the line is in or out?
Does it do the necessary calculations internally?
Yes (provided the that polyline is closed). For open polylines, a point can be passed to OffsetProWorker to indicate the side instead of RVector.invalid in the example.
IBN wrote:Is it possible to define on which layer the offset line is placed, or does it always go to the same layer ?
The offset goes on the current layer. You can set the current layer with di.setCurrentLayer("My Layer") or di.setCurrentLayer(layerId).

IBN
Newbie Member
Posts: 3
Joined: Wed Jun 22, 2016 9:03 am

Re: ECMA Script for automatic Polyline offset

Post by IBN » Fri Jun 24, 2016 9:56 am

Thanks a lot again!

I will try to get it work this weekend if i have the time, and report back here.

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

Re: ECMA Script for automatic Polyline offset

Post by CVH » Thu Dec 07, 2023 10:47 am

This appears to be outdated information:
- worker returns nothing with an RPolyline shape.

A QCAD Pro GUI offset may be calculated of a collection of polylines among other shapes ...
- With an RPolyline shape in an array the worker throws an exception in ShapeAlgorithms.js ... ShapeAlgorithms.getOffsetShapes( ... )
calling shape.getOffsetShapes(distance, number, side, pos); where side is undefined and pos is RVector.invalid

Same outcomes if we throw in an REntity queried from the document.
A collection of RShapes from the polyline exploded is a no go.

Refer to newer art (2018): https://www.qcad.org/rsforum/viewtopic. ... 742#p20676

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”