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
ECMA Script for automatic Polyline offset
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Re: ECMA Script for automatic Polyline offset
Yes.IBN wrote:1. Would that be possible at all?
None. Any QCAD Professional package will do.IBN wrote:2. What prerequisites do i need to make a script ? (compiler?)
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.
Getting all entities on a layer:IBN wrote:3. How can i iterate through all Polylines on one specific 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
Re: ECMA Script for automatic Polyline offset
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 :
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!
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 :
How does the offset function tell which side of the line is in or out?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 );
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!
Re: ECMA Script for automatic Polyline offset
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:How does the offset function tell which side of the line is in or out?
Does it do the necessary calculations internally?
The offset goes on the current layer. You can set the current layer with di.setCurrentLayer("My Layer") or di.setCurrentLayer(layerId).IBN wrote:Is it possible to define on which layer the offset line is placed, or does it always go to the same layer ?
Re: ECMA Script for automatic Polyline offset
Thanks a lot again!
I will try to get it work this weekend if i have the time, and report back here.
I will try to get it work this weekend if i have the time, and report back here.
Re: ECMA Script for automatic Polyline offset
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
- 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