How to get coordinates according to step value

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.

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

Re: How to get coordinates according to step value

Post by CVH » Thu Dec 28, 2023 7:25 am

Replied in PM
worker2 does not receive a polyline RShape to work with.
offs2 is most probably empty.

Best practice is to verify what is returned and handle faults, report things or whatever.

Regards,
CVH

WildWolfCJ
Full Member
Posts: 84
Joined: Fri Oct 20, 2023 7:21 am

Re: How to get coordinates according to step value

Post by WildWolfCJ » Thu Jan 11, 2024 7:17 am

HI CVH
thanks for your guidance
We have made great progress and have been able to successfully obtain the internal and external contours, but now we encounter another problem. The 2 3 marked in red in the attachment are two polylines obtained by offset. How to combine 2 3 into one through JS What about closed polylines? I searched the QCAD API and found no relevant solutions
Attachments
VeryCapture_20240111141317.jpg
VeryCapture_20240111141317.jpg (349.06 KiB) Viewed 10813 times

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

Re: How to get coordinates according to step value

Post by CVH » Thu Jan 11, 2024 12:31 pm

Hi again, best wishes for the new year, sorry for your loss.

One needs to add round butt-endings.
Seeing the signature of the deprecated OffsetProWorker that was planned to be an option :(.

That are in fact two 180 degree arcs, start and end angle can be derived from the base shape end orientation.
But then you need to merge arc1 to poly1 ensuring correct arc orientation ...
... merge poly2 to poly1+arc1 ... The arc shape is converted to an arc segment ...
... merge arc2 to poly1+arc1+poly2 again ensuring correct arc orientation ...
... logically close the resulting outline.
Ensuring correct orientation of the 4 shapes requires that you test all that and do some clever Math for the arcs.
(There is an appending method auto arranging things but then it is out of your hands)

End orientations of a RPolyline shape (Implemented for most RShape types):
https://qcad.org/doc/qcad/3.0/developer ... 9c45b16642
https://qcad.org/doc/qcad/3.0/developer ... 3df9d078bf

Endings of a RPolyline shape (Implemented for most RShape types):
https://qcad.org/doc/qcad/3.0/developer ... e477a6e022
https://qcad.org/doc/qcad/3.0/developer ... 14aca6bbb8

Reversing the orientation of an existing RShape called 'shape' in this example:

Code: Select all

shape.reverse();
Creating a new RArc shape:
https://qcad.org/doc/qcad/3.0/developer ... 0a8e269e5a

Code: Select all

arc1 = new RArc(center, radius, startAngle, endAngle, reversed);
Where:
  • center = Base shape ending
    radius = half the pen size.
    startAngle = End orientation +/- PI/2 or also the angle from the center point to the endpoint of an offset shape.
    endAngle = End orientation -/+ PI/2 or also the angle from the center point to the endpoint of the other offset shape.
    reversed = true/false (Can be tricky :roll: )
Getting the angle between two positions, two RVectors is a method of the RVector class:
https://qcad.org/doc/qcad/3.0/developer ... 21ea53baab

Merging an RShape with an existent RPolyline:
As is: https://qcad.org/doc/qcad/3.0/developer ... f891e79c64
Auto: https://qcad.org/doc/qcad/3.0/developer ... 05ba3b7861

All that for the hard way .... :wink:
- = = = = = = = = = = -

If all went well then two offsets endings are one pen diameter apart from each other ...
... And the associated base shape ending is perfectly in the middle ... Test for these constrains to be sure.

Further I use twice a left-hand offset reversing the base shape in between so that the offset shapes already have an opposite orientation.

I can then simply set the last bulge of poly1 to value +/-1.0 and add an extra vertex = begin of poly2
This I can repeat at the end of pol1+poly2 and logically close the resulting outline.
Remark here that bulge = 1.0 equals an arc shape with a sweep of 180 degrees ...
... And if the endpoints of the offsets were one pen diameter apart then R = halfPen what is the perfect arc shaped segment for the job.
This means that you need much less error-prone tests and Math this way.

Modifying the last bulge factor of an RPolyline shape called 'shape' in this example:

Code: Select all

shape.setBulgeAt(shape.countVertices() - 1, value);
Remark here that the bulge factor is defined for the polyline segment after the vertex.
Without a next vertex there is nothing to display for this bulging segment.
Use a value of 1.0 or -1.0 depending the base shape orientation (CW/CCW)
https://qcad.org/doc/qcad/3.0/developer ... 3cb995d79c
(I should note that this property is not 100% proof for any possible polyline)

Adding an extra vertex:
https://qcad.org/doc/qcad/3.0/developer ... 3bdd518ec2
You only require to fill in the other offset matching endpoint, the parameters: bulge, w1 and w2 are by default zero and can be ommitted.
As with bulge the custom start- and end widths w1 and w2 are for the segment that follows.

:!: Polylines with custom widths can not be handled in the way we handle common polylines and these should be excluded from processing.
https://qcad.org/doc/qcad/3.0/developer ... d2a0bf304e
We can get the outline shapes (plural) for that type with a build in method of the Explode (XP) tool ... Not 100% proof.

Regards,
CVH
Last edited by CVH on Thu Jan 11, 2024 6:19 pm, edited 1 time in total.

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

Re: How to get coordinates according to step value

Post by CVH » Thu Jan 11, 2024 12:47 pm

When you tackled all that then the next challenge might be self-intersecting shapes ... Touching ... Intersecting
In a way you can't avoid that they would exist in a dxf nor can it be excluded that 2 shapes or the offsets touch or intersect each other.

At the moment I am still struggling with that.
Little steps forward followed by some major setbacks.
Learning from each failure. :wink:

Regards,
CVH

WildWolfCJ
Full Member
Posts: 84
Joined: Fri Oct 20, 2023 7:21 am

Re: How to get coordinates according to step value

Post by WildWolfCJ » Fri Jan 12, 2024 7:29 am

HICVH
I will refer to these links to find a way to combine it and will update with any progress, thanks again for your help

WildWolfCJ
Full Member
Posts: 84
Joined: Fri Oct 20, 2023 7:21 am

Re: How to get coordinates according to step value

Post by WildWolfCJ » Mon Jan 15, 2024 6:55 am

I have made some progress, but am having problems merging four entities into one polyline. Attached are the dxf file I used for testing (test_polyline - 5.dxf) and the converted dxf file (test123123.dxf),
It can perfectly combine p1 arc2 to form a polyline, but a strange figure will appear when combining p2 arc1. I have tried different combination codes, but they still failed. I have no idea about this problem.
Below is my combined code

Code: Select all

            // If it is an unclosed polyline, combine the offset polylines into a closed polyline.
            if (isOpenPolylineEntity(polyline)) {
                print("opened polyline");
                if (shapeEntity1 && shapeEntity2) {
             
                    var startPoint = shapeEntity1.getStartPoint();  
                    var startPoint2 = shapeEntity2.getStartPoint();  
                   
                    var center = new RVector((startPoint.getX() + startPoint2.getX()) / 2, (startPoint.getY() + startPoint2.getY()) / 2);                    
                    var startAngle = center.getAngleTo(startPoint);
                    var endAngle = center.getAngleTo(startPoint2);
                    var arc1 = new RArc(center,
                        offset,
                        startAngle,
                        endAngle,
                        false
                    );
                    var arcEntity = new RArcEntity(doc, new RArcData(arc1));
                    op = new RAddObjectsOperation();                    
                    op.addObject(arcEntity, false);
                    di.applyOperation(op);

                
                    var endPoint = shapeEntity1.getEndPoint();  
                    var endPoint2 = shapeEntity2.getEndPoint();  
                   
                    center = new RVector((endPoint.getX() + endPoint2.getX()) / 2, (endPoint.getY() + endPoint2.getY()) / 2);
                    startAngle = center.getAngleTo(endPoint);
                    endAngle = center.getAngleTo(endPoint2);
                    var arc2 = new RArc(center,
                        offset,
                        startAngle,
                        endAngle,
                        false
                    );
                    arcEntity = new RArcEntity(doc, new RArcData(arc2));
                    op = new RAddObjectsOperation();
                    op.addObject(arcEntity, true);
                    di.applyOperation(op);


                    var  p1 = shapeEntity1.getPolylineShape();
                    var  p2 = shapeEntity2.getPolylineShape();
                    p1.appendShape(arc2); 

                    // Logically close the RPolyline shape if appropriate:
                    if (!p1.isClosed() && p1.isGeometricallyClosed()) {
                        p1.toLogicallyClosed();
                    }

                    p2.appendShape(arc1);         

                    op = new RAddObjectsOperation();
                    op.addObject(new RPolylineEntity(doc, new RPolylineData(p1)), true);
                    op.addObject(new RPolylineEntity(doc, new RPolylineData(p2)), true);
                    di.applyOperation(op);
                }
            }
Attachments
test123123.dxf
(142.83 KiB) Downloaded 135 times
test_polyline - 5.dxf
(94.93 KiB) Downloaded 138 times
VeryCapture_20240115114523.jpg
VeryCapture_20240115114523.jpg (311.62 KiB) Viewed 10581 times
VeryCapture_20240115114620.jpg
VeryCapture_20240115114620.jpg (350.32 KiB) Viewed 10581 times

WildWolfCJ
Full Member
Posts: 84
Joined: Fri Oct 20, 2023 7:21 am

Re: How to get coordinates according to step value

Post by WildWolfCJ » Mon Jan 15, 2024 8:18 am

HI CVH
Made progress, changed the method of merging arcs to adding vertex and set Bulge to correctly combine polylines. It seems to be running well.
Thank you again for your help
Attachments
VeryCapture_20240115151718.jpg
VeryCapture_20240115151718.jpg (252.34 KiB) Viewed 10572 times

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

Re: How to get coordinates according to step value

Post by CVH » Mon Jan 15, 2024 7:08 pm

WildWolfCJ wrote:
Mon Jan 15, 2024 6:55 am
but a strange figure will appear when combining p2 arc1.
Odd because R = offset for both arcs.
I already expressed my reserves on flying blind by PM.

Code: Select all

var center = new RVector((startPoint.getX() + startPoint2.getX()) / 2, (startPoint.getY() + startPoint2.getY()) / 2); 
Or var center = RVector.getAverage(startPoint, startPoint2);
Does that coincide with the base shape startpoint? It should ... You don't know because it is untested!
Is the distance between these two start points twice the offset? ... Untested!

Next ... Ifso, then both the offsets have the same orientation as the base shape.
To be circular connected the four shapes should have endpoints matching another shape starting point.

Or you could use .appendShapeAuto(..);
The things to merge should at least be connected by an endpoint shared by both shapes ... :roll:

What I don't understand in your code is why casting the first offset as drawing entity, create an RArc, cast it as drawing entity ...
... And continue with merge shapes, casting each shape again as drawing entity.
Why not create the whole offset as one RPolyline and cast that once as drawing entity when fully constructed?
WildWolfCJ wrote:
Mon Jan 15, 2024 8:18 am
changed the method of merging arcs to adding vertex and set Bulge to correctly combine polylines.
Indeed, the second route is more straightforward but that is also the danger of this method.
You still have to verify things to ensure that you don't merge them blindfolded.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”