[SOLVED] addPolyline()

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
User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

[SOLVED] addPolyline()

Post by petevick » Fri Dec 30, 2022 12:41 pm

Is it possible to use addPolyline() within a FuncName.prototype.getOperation = function(preview) {} to add a polyline shape to the drawing ??
Last edited by petevick on Sat Dec 31, 2022 7:29 pm, edited 1 time in total.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

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

Re: addPolyline()

Post by CVH » Fri Dec 30, 2022 7:44 pm

Pete,
FuncName.prototype.getOperation() requires that you return an operation.
See: https://github.com/qcad/qcad/blob/maste ... n.js#L1659

You can not use the Simple API for this purpose.
addPolyline() calls addShape() with a polyline shape and that calls addEntity() with a polyline entity what calls addObject().
addObject() creates an operation and immediately applies it to the current document interface.

So you need to set up an operation:

Code: Select all

    var op = new RAddObjectOperation();
    op.setText(this.getToolTitle());
The first line is singular but there is also RAddObjectsOperation() for adding more than one object. :wink:
The second line sets a tool title for Undo/Redo and here this is picked from the tool name itself, not required and about any simple string will do.

Then you must create a Polyline shape. This is a pure mathematical shape with no vertices.

Code: Select all

    var poly = new RPolyline();
Now there are several ways to add shapes or vertices + bulge factors.
One could peek into Simple API for that: https://github.com/qcad/qcad/blob/maste ... te.js#L252
Or one can use any of the polyline methods explained here: https://qcad.org/doc/qcad/3.0/developer ... yline.html
For example:
>> Adding by shape (I can not promote this if the shape is not trimmed to what already exists)
poly.appendShape(RShape, prepend=false) or poly.appendShapeAuto(RShape)
(=false means that the default is false if you omit that)
>> Adding by vertex
poly.appendVertex(RVector, bulge=0.0, W1=0.0, W2=0.0)
(Again the 3 last parameters have a default value)
>> Setting a list of vertices
poly.setVertices(.. an array of RVector's ..)
>> And setting the bulge factors
poly.setBulges(.. an array of values ..)

Then you must create a Polyline entity of your shape for the drawing in question.

Code: Select all

    var doc = this.getDocument();
    var polyEntity = shapeToEntity(doc, poly);
The first line retrieves the current drawing document.

Now it is just the question of adding the entity to the operation and to return your operation.

Code: Select all

    op.addObject(polyEntity, false, false);
    return op;
The two extra parameters on the first line are useCurrentAttributes=true and forceNew=false

Regards,
CVH

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: addPolyline()

Post by petevick » Fri Dec 30, 2022 8:33 pm

I should have known really that it wasn't going to be easy.

Thanks for the explanation CVH. I do actually understand most of that, and I think the poly.setVertices() might be my best bet, is each array entry relative to the previous entry or are they all from a common point like 0, 0 ? Also, I take it the poly.setBulges() is an array of the start points where a bulge should be applied plus a bulge factor ?

Thanks again CVH.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

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

Re: addPolyline()

Post by CVH » Fri Dec 30, 2022 10:13 pm

Pete, it is not that hard ...
You can read up on how simple_create.js addPolyline(points, closed, relative) does the trick.
https://github.com/qcad/qcad/blob/maste ... te.js#L252
The local/global relative functionality is part of tukuyomi proposal.
Maybe this older version is easier to understand:
https://github.com/qcad/qcad/commit/dd7 ... 8d0cf3a3ad
(wayback May 30, 2016 ... You need to open up the view to read line 72-98)
petevick wrote:
Fri Dec 30, 2022 8:33 pm
is each array entry relative to the previous entry or are they all from a common point like 0, 0 ?
Each array entry is an RVector, an RVector defines an absolute point in space in 3D ... Z defaults to zero if omitted :wink:
petevick wrote:
Fri Dec 30, 2022 8:33 pm
I take it the poly.setBulges() is an array of the start points where a bulge should be applied plus a bulge factor ?
It is a list of bulging factors, one per vertex for the upcoming segment ... Just the values from tangent(sweep/4) or zero for a straight segment.

Remark that both these zero based lists have the same number of entries and element 0 is the startpoint of the polyline.
The bulge factor of the third segment is bulges[2] and the startpoint of this segment is stored in Vertices[2].

I think that poly.appendVertex() is best suited for recursively adding the next vertex and the next bulge factor.
Then summing each next vector with the previous makes it relative as per tukuyomi.

With both lists at hand .setVertices() and .setBulges() are best suited.

Regards,
CVH
Last edited by CVH on Sat Dec 31, 2022 10:49 am, edited 1 time in total.

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

Re: addPolyline()

Post by CVH » Sat Dec 31, 2022 10:45 am

petevick wrote:
Fri Dec 30, 2022 8:33 pm
Also, I take it the poly.setBulges() is an array of the start points where a bulge should be applied plus a bulge factor ?
If you dive a little deeper then what you want to accomplish here is also possible.
One of the polyline methods is .setBulgeAt(index, bulge): https://qcad.org/doc/qcad/3.0/developer ... 90329b8f18

With a polyline shape poly with only straight segments for example ...
... poly.setBulgeAt(2, Math.tan(Math.PI/8)) would only set the bulge factor for the third segment to resemble a 90 degrees arc.

Most methods also works for polyline entities: https://qcad.org/doc/qcad/3.0/developer ... b3e8621893 :wink:

poly.getBulgeAt(2) returns the bulge factor of the third segment, poly.getVertexAt(2) the RVector of the third node.
You can even change an individual vertex:
>> To a known RVector ... https://qcad.org/doc/qcad/3.0/developer ... 078029c328
>> Adding an offset (=relative) also stored in an RVector ... https://qcad.org/doc/qcad/3.0/developer ... a809afe156

Regards,
CVH

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: addPolyline()

Post by petevick » Sat Dec 31, 2022 10:52 am

CVH wrote:
Fri Dec 30, 2022 10:13 pm
Pete, it is not that hard ...
Maybe not for you. I just can't seem to grasp the language (based on Javascript I believe). I self taught myself Basic, Visual Basic, Lisp, AutoLisp (which I really liked) and most recently Pascal, from the start I could see logic and flow in the code for all those languages, but I can't see either logic or flow for Qcad scripting. I guess it doesn't help that there is very little in the way of reference material to look at, yes there are plenty of example scripts to look at, but if you don't get the code it's like staring at mud.

I'll give the above a go and hopefully something will click and I'll be a little clearer with the coding....don't hold your breath though.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: addPolyline()

Post by petevick » Sat Dec 31, 2022 7:28 pm

Well I never, I've managed to get it all working, including bulges e_surprised e_surprised Now to move on to the next stage :roll:
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

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

Re: [SOLVED] addPolyline()

Post by CVH » Sun Jan 01, 2023 1:18 pm

I didn't hold my breath for several hours :lol: , I was certain that it would work out somehow.

The hurdle is not the scripting language itself but the vast amount of resources to support all kinds of CAD functionality.
It wouldn't matter if it was written in Basic, Pascal or even in Python.
There is nothing in these languages that defines a CAD drawing, a polyline, vertices or vectors and thus everything related to CAD is new.

Pretty curious about what you're cooking up for us for New Year's. :lol: :wink:

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”