How to close corners of differtent splines in script

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
Kv12
Registered Member
Posts: 2
Joined: Sat Jan 22, 2022 10:47 pm

How to close corners of differtent splines in script

Post by Kv12 » Sat Jan 22, 2022 11:17 pm

Hello,

I have a problem closing the corners of a "square" of four splines in a script for Qcad. The coordinates of the hitpoints for the splines a the results of a measuring tool; they will become variable in a later stadium.

Code: Select all


//coordinaten voor de raakpunten van de splines
//links
var l=[];
l.push([100,100]);
l.push([105,500]);
l.push([102,1000]);
//boven
var b=[];
b.push([120,1050]);
b.push([500,1060]);
b.push([800,1040]);
//rechts
var r=[];
r.push([900,900]);
r.push([890,600]);
r.push([920,110]);
//onder
var o=[];
o.push([800,75]);
o.push([400,90]);
o.push([130,80]);

// teken fitpoint splines met bovenstaande coordinaten
SplineObjL = addSpline(l, false);    // NOTclosed periodic
SplineObjB = addSpline(b, false);    // NOTclosed periodic
SplineObjR = addSpline(r, false);    // NOTclosed periodic
SplineObjO = addSpline(o, false);    // NOTclosed periodic

Using Qcad professional 3.25.2.0 and Windows 10

Does anybody have an idea?
Attachments
closecorners.dxf
result
(104.18 KiB) Downloaded 184 times

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

Re: How to close corners of differtent splines in script

Post by CVH » Sun Jan 23, 2022 8:07 am

Hi,
not really ... :?

First: addSpline(...);
Adds a fitpoint spline entity by using the simple API.
You don't have to store the result in a variable called 'SplineObj...'
Here an undeclared one, remark that we use variable names starting with a lowercase.

Unless you need that as a reference of the created drawing entity for another purpose.
The reference would only be valid without using a transaction to group additions/modifications to one action.
If you want to do further modifications, you can do that on a lower mathematically level before entities are added.

Second:
The 4 splines are not really 'connected' so the corners are indeed open.
The left one ends at (102,1000).
The top one starts at (120,1050).
That ends at (800,1040).
The one on the right starts at (900,900).
And so on.

I know that splines with control points can be tweaked to have hard corners.
With 3 successive control points at the same spot.
But that won't serve your needs as it aren't fitpoints aka 'raakpunten'.
It is not good practice either.

QCAD can not trim your splines pairwise. :shock:
There is no natural defined way in how a spline should continue beyond its endpoint. e_geek

The attached dxf includes polylines. It seems L-T-R are trimmed pairwise.
The ending segments of polylines from exploding splines are approximations of the local curvature.
Those ending arcs depends primarily on your explosion (XP) preferences.
Both ending arc segments can indeed be trimmed pairwise.
Still, that isn't a natural continuation of the original splines themselves. :wink:

QCAD can trim fitpoint splines that actually cross another entity. 8)
Remind that when trimming fitpoints splines, control point splines are returned.
That is to keep the tangent of the spline in the intersection point. e_geek

Thinking outside the box:
Your splines are each defined by 3 points.
Maybe a 3-point arc would serve your needs. :?
The differences are very minute. :wink:

Regards,
CVH

Kv12
Registered Member
Posts: 2
Joined: Sat Jan 22, 2022 10:47 pm

Re: How to close corners of differtent splines in script

Post by Kv12 » Sun Jan 23, 2022 9:29 pm

Thx CVH for your reaction.

In this example the splines indeed are defined by 3 points, but they will be defined by 5 or more points later on, so a 3 point arc won't work.

The attached DXF does indeed include polylines. This was not the result of te script; I manually modified them afterwards and forgot to undo this action before saving. I discovered that when I changed them into polylines, I was able to close the corners by using "Trim Both" (manually). In a way this is a solution for my problem, but I would like to accomplish this in a script, but cannot find out how..

Hope you can help me further with this information?

Regards,
Kv12

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

Re: How to close corners of differtent splines in script

Post by CVH » Mon Jan 24, 2022 6:36 am

Kv12,
Kv12 wrote:
Sun Jan 23, 2022 9:29 pm
forgot to undo this action before saving ...
I was able to close the corners by using "Trim Both" (manually).
In a way this is a solution for my problem, but I would like to accomplish this in a script, but cannot find out how..
I understand, and by undoing you undo the trimming and the explosion and return to open corners ... :(

The easiest route:
- defining splines from intersection point to intersection point.
  • Using the simple API as in your example.
The only perfect solution ... e_geek

The simple_modify API also provides a method for trimming two entities:
https://qcad.org/doc/qcad/3.0/developer ... 37b8b53b93
Again, that will throw an exception with splines and open corners.
Getting polylines from splines is explained lower (approximateWithArcs).
Casting the polylines keeping a reference and then trimming them pairwise ...
... You need to provide 'clickpoints' as if your are doing it manually. :(

I would use the lower level mathematically approach. :wink:

The spline route:
- having points to define splines that actually cross.
  • Creating math level shapes RSplines.
    Get the intersection point pairwise.
    Trimming each spline shape twice at the returned intersections.
    Casting the resulting control point spline shapes as drawing entities.
This would be nearly perfect ... :P

var splineXX = new RSpline([collection of fitpoints], degree); For each spline shape.
https://qcad.org/doc/qcad/3.0/developer ... 725f28eff7
var intersectionPointsA = RShape.getIntersectionPointsSS(splineXX, splineYY); For each pair of splines (Always a list).
https://qcad.org/doc/qcad/3.0/developer ... 6aa5519660
splineXX.trimEndPoint(intersectionPointA); For each endpoint.
https://qcad.org/doc/qcad/3.0/developer ... c5afa23c4c
Remind: requires PRO, check if intersections points are returned, check if it is just one and that it is a valid RVector.

Code: Select all

if (intersectionPointsA.length === 1 && isValidVector(intersectionPointsA[0])) {
    // Trimming the shapes in question
}
else {
    // Error & skip or terminate
}
Casting resulting splines shapes to the document in one single operation.

Code: Select all

var di = this.getDocumentInterface(); // Valid if there is an active document
var doc = di. getDocument(); // A reference to the document
var op = new RAddObjectsOperation();
var splineData, splineEntity; // Declaring common variables

// For each final spline shape:
splineData = new RSplineData(splineXX);
splineEntity = new RSplineEntity(doc, splineData);
// Adding the new entity to the operation
op.addObject(splineEntity, true, false); // useCurrentAttributes, NOTforceNew

// Finally applying all additions:
di.applyOperation(op);
The polyline route:
This may look OK but you don't really extend the splines.
You only extend the last arc segment at each polyline ending. :!:

var polylineXX = splineXX.approximateWithArcs(tolerance); Requires PRO.
https://qcad.org/doc/qcad/3.0/developer ... d09aa2e319

One can opt for a fixed tolerance or:

Code: Select all

    // Retrieve approximation tolerances of the Explode method (XP), if any, or use defaults:
    var sTol = RSettings.getDoubleValue("Explode/SplineTolerance", 0.01);    // Default =0.01 deviation
var intersectionPointsA = RShape.getIntersectionPoints(polylineXX, polylineYY, false, false, false); Always a list.
https://qcad.org/doc/qcad/3.0/developer ... 9c209339e1
polylineXX.trimStartPoint(intersectionPointA, RVector.invalid, true);
https://qcad.org/doc/qcad/3.0/developer ... 357eb67c3f
polylineXX.trimEndPoint(intersectionPointB, RVector.invalid, true);
https://qcad.org/doc/qcad/3.0/developer ... 5957b8569d

Seems easy enough but remind that arcs can intersect once, twice or not at all.
I asume that the shortest solution is returned.
One can tweak that with using a specific so called 'clickpoint' other than RVector.invalid.

Casting the resulting polyline shapes to the document is analog to the above with splines.
Replace 'spline' with 'polyline' textual, mind the upper/lower case.

One could even merge the polylines, appending individual shapes from successive polylines.
Personally I am not fond on how QCAD prepend/append arc shapes to polylines. :(

All QCAD open source scripts:
https://github.com/qcad/qcad
All QCAD resources:
- Classes: https://qcad.org/doc/qcad/3.0/developer/annotated.html
- Modules: https://qcad.org/doc/qcad/3.0/developer/modules.html
- ...

Regards,
CVH

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

Re: How to close corners of differtent splines in script

Post by CVH » Mon Jan 24, 2022 9:09 am

Kv12,

Seems less straightforward to implement as how it is explained in the resourses ....
Had to tweak the spline shape creation a bit.
At first it returned no intersections at all.

You can run this script from its saved location with Misc menu .. Run Script (XC) ... :D
With at least an empty document, limited to 'links' and 'boven' as example.
A *.js script is plain text. :wink:
TrimmingSplinesEX.js
(2.67 KiB) Downloaded 192 times

You can reach me in Dutch by PM, I am from Belgium, Antwerp. :wink:

Regards,
CVH

Post Reply

Return to “QCAD 'How Do I' Questions”