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.

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

How to get coordinates according to step value

Post by WildWolfCJ » Wed Nov 22, 2023 2:26 am

WIN10 QCAD3.28 PRO

I have a DXF file that contains a polyline with a line width of 2.11mm. My requirement is how to obtain the coordinates of the points on this polyline. These points need to meet the following requirements.
1 The distance between points is a specified value
2 points must be above the polyline
Attached is a test DXF file
Attachments
test_line_width.dxf
(95.03 KiB) Downloaded 175 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 » Wed Nov 22, 2023 11:16 am

Hi,
I was expecting a drawing with sample points to disambiguate 'Above' from left or right regarding the local orientation.

Basically any line-art has no width, any point along the polyline will be on the vectored data.
And thus on the central line with Lineweights or custom widths.
Pen sizes are to render visible line-art, a vector has an infinite small thickness and that doesn't show on screen/paper. :wink:

One can explode polylines with custom widths to outer contours (A hybrid format and there are known flaws).
One can not explode Lineweights to contours right out the box. :(
There should be a way because Lineweights are rendered as solid filled contours where the contours are again vectors.

A polyline has an overall length (Also disregarding pen size).
It is then as simple as stepping the step-size and retrieving each next position until you are past the end of the entity.

Getting points along the polyline at a certain distance from the start point:

Code: Select all

var polyEntity;    // A drawing polyline entity queried from the document.
var distance;    // For example N times a step-size
var points = polyEntity.getPointsWithDistanceToEnd(distance, RS.FromStart);
Remind that this returns an array given that you can use RS.FromAny aka points from both ends.

Code: Select all

var point = points[0];
One should validate what is returned as being a valid RVector.


'Above the polyline' could be as simple as adding an offset to the Y coordinate.
But translating the points upwards can still be on the polyline for vertical segments. :wink:

'Above' could also mean left or right and this is according the polyline direction.
Your example polyline changes 2 times in main horizontal direction ...
... Remind that from our perspective left from the entity may be both up or down.
Left in perceptive of the polyline direction may be left or right.

The amount of steps is based on your polyline length and the step-size:

Code: Select all

var stepSize = 10;    // Given step-size
var steps = Math.floor(polyEntity.getLength() / stepSize);
for (var i=0; i<steps; i++) {
    distance = stepSize * i;

   // ...
   // Code for each point
   // ...
}
Remark that I don't use distance = distance + stepSize or distance += stepSize each iteration.
> You should avoid running sums in Floating Point arithmetic.
Also remark that I used for (var i=0 what includes the startpoint, for (var i=1 does not.
Due to Math.floor we may or may not include the endpoint but we will never traverse it.
Excluding the endpoint is by verifying the point at active distance against the entity endpoint with some degree of tolerance.

Code: Select all

// Don't include entity endpoint:
if (point.equalsFuzzy2D(polyEntity.getEndPoint()) {    // Fuzzy compare with RS.PointTolerance = 1e-9
    // Exit for loop:
    break;
}
To get the nearest segment related to the position and the orientation of this segment:

Code: Select all

var segmentId = polyEntity.getClosestSegment(point);
var segment = polyEntity.getSegmentAt(segmentId);
if (isLineShape(segment)) {
    var ori = segment.getAngle();
}
You then translate the point by an orthogonal vector to the left (ori + Math.PI/2) or to the right (ori - Math.PI/2).
Remark that I excluded arc segments, for these the offset vector is oriented to/from the arc shape center.
The offset vector magnitude is the required offset.
The offset should account for half the Lineweight:

Code: Select all

var halfPen = polyEntity.getLineweight() / 200;    // divided by 100, divided by 2
Remind that:
- Lineweights are in 1/100mm and custom widths are in document units, a further scaling on document units may be required for Lineweights.
- Lineweights can also refer to byLayer or byBlock and then you need to resolve that.
- For points (almost) coinciding with nodes the closest segment is the segment before the node.
> Can be tricky with closed polylines.
- Lineweights are rendered as a round pen, custom widths as a flat pen, the offset at nodes is thus quite different.
- The local custom width depends on A: a global width OR B: a local width depending the segment start and end widths.

For now this answer is not closed form, please elaborate on 'above' or give examples.
But maybe you just require the offset contour of line-art with a certain pen width.

Regards,
CVH
Last edited by CVH on Sat Nov 25, 2023 1:23 pm, edited 1 time in total.

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 Nov 24, 2023 2:38 am

HI CVH Thank you for your answer. I took a rough look at your post and there are still many things I don’t understand. Maybe I didn’t describe it clearly enough. I have more urgent tasks to deal with now. I’ll wait until I’m free. Study this issue carefully

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 » Sat Nov 25, 2023 9:57 pm

WildWolfCJ,
Reviewing your other topics it seems you are asking the hard to answer CAD questions and that is a bit like mine. :lol: :lol: :roll:
CVH wrote:
Wed Nov 22, 2023 11:16 am
But maybe you just require the offset contour of line-art with a certain pen width.
Here there is another tricky thing ....
There is no simple offset shape half the pen size away of an ellipse.
Nor are there splines that are an offset of quadratic/cubic (degree 2/3) splines with a reasonable polynomial degree.

On screen/paper this is rendered as a bunch of short straight lines for simplicity.
Arcs/circles are also handled segmented although offsets to these exists, mathematically it is much easier.
You may notice the segmentation when zooming in quite deep on curved line-art.

The left and right offset shape of a line segment is straightforward and always fairly correct.
At endpoints or at corners it is merely adding a circle with the diameter of the round pen to represent Lineweights and merging all the filled area.
QCAD has no concept of merging areas. :|
Connecting each set of offset shapes their start-points and end-points is similar as using a flat pen for polylines with custom widths.
Although the rendering of polylines with custom widths is a hybrid.
Where 2 line-segments meet, a miter join is used, otherwise but cap's are used.

The vector data of both examples is identical, the difference is the usage of a round or a flat pen.
RenderingOfPolyWeight-Widths.png
Poly Lineweight vs custom widths, vector data in red
RenderingOfPolyWeight-Widths.png (7.81 KiB) Viewed 58715 times

The right example polyline with custom widths can be exploded to its contours ... = Plural:
PolyWidthsXP.png
Explosion of Poly with custom widths
PolyWidthsXP.png (4.02 KiB) Viewed 58715 times

I think that you can notice one of the flaws of exploding a polyline with custom widths at first glance. :wink:
Only where miter join's were used the contour is continuously, otherwise the partial contours have a gap and overlap when not tangent.
At discontinuities one could imaging that the flat pen stand still and rotates around its center until closing any gap, until re-aligned.
I once presented an adapted XP tool mimicking this behavior, meanwhile correcting flaws, but that was rejected.
As engraver I need the contour to be singular and flawless for pocketing.

There is no method to explode line-art Lineweights to contours. :(
Something similar can be achieved with a uniform offset to vectorized shapes and nodes for a certain Lineweight.
Two continuous poly offsets with round join's and round cap's at endings works too ... If offsets at touching arcs would never fail. :roll:
OffsetHalfPenSize.png
Half a pen sized offset to segments and nodes
OffsetHalfPenSize.png (4.49 KiB) Viewed 58715 times

What remains is fixing the flaws (ifso) and merging the contours to a single outline.
Easier said than done.

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 » Wed Nov 29, 2023 9:01 am

HI CVH
Thank you very much for your patient and careful answer. I will describe my needs in detail. If there is a polyline with a certain pen width, I need to obtain the points located on the polyline according to a fixed step value. My current approach is as follows ( For now, only consider the case where the polylines are all straight lines):
1 If this polyline is a closed interval, then obtain the inner contour and outer contour of this polyline (I guess the inner contour and outer contour obtained are two polylines), and then obtain the containing rectangle according to the outer contour, according to the step The value loops through the rectangle from top to bottom and left to right. If the coordinate is located on the outer contour and not on the inner contour, the coordinate is valid, thereby obtaining a valid coordinate set.
2 If the polyline is not a closed interval, then obtain the outer contour of the polyline, then obtain the containing rectangle based on the outer contour, and loop through the rectangle from top to bottom from left to right according to the step value. If the coordinate is located in the outer contour, this The coordinates are valid, thus obtaining the valid coordinate set
The difficulty I am encountering now is that I cannot accurately obtain the inner and outer contours.
I read your answer carefully, and there are many parts that I don't understand very well. Is there a simple and quick way to achieve this function?

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 » Wed Nov 29, 2023 10:09 am

WildWolfCJ wrote:
Wed Nov 29, 2023 9:01 am
If there is a polyline with a certain pen width
Width or Lineweight?
A polyline with global or local widths can be exploded to contours as explained above.
(With flaws, discontinuities and so on).

The offset polylines to a polyline using QCAD Pro can be achieved by:
https://www.qcad.org/rsforum/viewtopic. ... pro#p12806

oriShape is an RPolyline shape:

Code: Select all

var polyEntity;    // A drawing polyline entity queried from the document.
var polyShape = polyEntity.getData().castToShape();
distance would be half the pen size.
number = 1
pos = undefined
forceInside would be true and then false for inner and outer offset.
joinType as required
No experience with endType
...


This returns RPolyline shapes.
If you need them as drawing entities then you need to convert them and cast them to the document.

Regards,
CVH
Last edited by CVH on Wed Nov 29, 2023 12:48 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 » Wed Nov 29, 2023 11:06 am

WildWolfCJ wrote:
Wed Nov 29, 2023 9:01 am
I guess the inner contour and outer contour obtained are two polylines
The offset of a polyline is indeed another polyline or a set of shapes (lines considering only straight polyline segments).
WildWolfCJ wrote:
Wed Nov 29, 2023 9:01 am
and then obtain the containing rectangle according to the outer contour,
The whole outer offset (and the inner offset) will only resemble a rectangle when the base polyline was also resembling a rectangle.
Or do you mean the rectangle around a single straight polyline segment according the pen size?
Remember that Lineweight pens are round. :wink: The pen offset to a line segment would have round ends.
WildWolfCJ wrote:
Wed Nov 29, 2023 9:01 am
and then obtain the containing rectangle according to the outer contour, according to the step The value loops through the rectangle from top to bottom and left to right. If the coordinate is located on the outer contour and not on the inner contour, the coordinate is valid, thereby obtaining a valid coordinate set.
Rather odd that you are stepping along the outer offset (=rectangle) and verify against positions on the inner offset.
You probably mean stepping in X/Y inside the outer offset.
And from the rest of the phrase I understand that on the outer is valid, on the inner in not valid.
But what with the majority of points in X/Y not on outer and not on inner?

2) Doesn't hold there an open ended polyline will hardly resemble a rectangle.
Here there is no inner nor outer offset but there are still a right and a left offset.
Remark that inner or outer is just a question of rephrasing right and left according the polyline orientation.
Why valid is then solely IN the so said outer offset eludes me.

Perhaps you need to elaborate this with a graphical representation ... For example a QCAD drawing. :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 » Thu Nov 30, 2023 2:36 am

Perhaps you need to elaborate this with a graphical representation ... For example a QCAD drawing.

HI CVH Thank you for your patient answer. The attachment is a simple DXF file containing a rectangle with a pen width of 2.11nn. I display this graphic in my program and get the coordinates and save them to the file. They are the coordinates of the red points discussed above. The method I am currently using to obtain coordinates is not very scientific.
Attachments
VeryCapture_20231130093411.jpg
VeryCapture_20231130093411.jpg (161.86 KiB) Viewed 58573 times
VeryCapture_20231130093449.jpg
VeryCapture_20231130093449.jpg (199.04 KiB) Viewed 58573 times
VeryCapture_20231130093509.jpg
VeryCapture_20231130093509.jpg (181.34 KiB) Viewed 58573 times
test.dxf
(95.07 KiB) Downloaded 150 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 Nov 30, 2023 1:10 pm

Right, rather obscure. :(
Recieved a drawing with a single logical closed polyline in the shape of an irregular quadrilateral.
The single entity on layer 0 has a custom set Lineweight of 2.11mm.

Red dots are not included ... Those are probably not even generated with QCAD.
The text file with CSV data is not attached.
While the bottom line segment is slanted for 179.72716502° I see an export for X from 46.37054 to 57.17054 where Y is stable at 0.34501201.
The first reference I have to work with is 1.05321157270302312mm from the center, the last is 1.00178358435491432 from the center.
Both within half a pen diameter of the lower quasi horizontal line segment.

In a way this looks like rasterization with a step value of 0.4mm.
First unknown is where the origin of the rasterization is.
The decimal fraction of your exported coordinates do not align with any of the 4 corners nor with an 1.055 offset to that.
From data I suspect that origin is at (0.37054000, 0.34501201) with 8 decimal digits.
This is not in respect with:
WildWolfCJ wrote:
Wed Nov 29, 2023 9:01 am
The value loops through the rectangle from top to bottom and left to right.
Because then I assume that the origin is left-high like for most bitmap formats.
Vector-wise left-high is (-80.174464, 99.79868533) and accounting for a pen that is (81.231345898119329, 100.85267348771695).

I then come to a first solution:
Export the geometry with QCAD as bmp with pixel size 0.4 by 0.4 units (What should be possible in newer releases of QCAD).
It is then a question of bit/byte banging in the binary data of the image file.
For all pixels that are actually set in the raster of X by Y pixels the so-called 'valid' coordinate is (X*0.4, Y*0.4).
X & Y not zero based and adding some sort of origin and we are done.

For the next solutions you need to construct the most outer contours of the vector art regarding a pen width.
I suppressed the original polyline on Layer 0.
Added the vector art in red.
Added 2 offsets half the pen size.
Done in the QCAD GUI but feasible with OffsetProWorker.js because it is so trivial.
WildWolfCJ-Contours-01.dxf
(97.94 KiB) Downloaded 156 times

Second solution:
To scan this we also need a limit from where to start and where to end. => Blue crosses (-90, 110) to (140, -10)
Then scanning coordinates each multiple of 0.4 in X and Y to be outside the inner contour but still inside the outer contour.
You could swap the tests depending on that you expect small shapes on a larger empty space.
This is probably what you are already doing.

Nifty solution:
I created a Hatch Pattern with dots 1 units spaced, used at scale 0.4 and a zero degree angle.
Hatched the area between the contours and set the hatch origin afterwards.
Exploded the hatch to points and exported these points coordinates to CSV.


And then now the main question:
Are you really interested in solutions based on QCAD resources or not?
When I see your screen grab, I'm starting to seriously doubt that.

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 Dec 01, 2023 4:02 am

HI CVH
Thank you for your patient answer,
In a way this looks like rasterization with a step value of 0.4mm.
It is indeed this step value
I then come to a first solution:
This plan is not suitable
For the next solutions you need to construct the most outer contours of the vector art regarding a pen width.
This plan is consistent with my idea. My idea is to find all the polylines in the DXF file with a pen width other than default, and then use half of the pen width to decompose them into inner and outer contours, and then find the coordinates based on the inner and outer contours. Can you teach me how to write this JS script?

Nifty solution:
I don't quite understand this solution, can you give me a demonstration example? Or can you describe it in more detail? Thank you.
Are you really interested in solutions based on QCAD resources or not?
Yes, I need to use some features in QCAD, but I can only run QCAD through the command line instead of the GUI. The DXF file preprocessed through the command line can be used in my own program.

Thank you again for your patient and detailed reply

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 Dec 01, 2023 4:06 am

Right, rather obscure. :(
I'm very sorry, because I use Google Translate from Chinese to English, so some translations may not be very accurate.

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 » Fri Dec 01, 2023 10:09 am

WildWolfCJ wrote:
Fri Dec 01, 2023 4:02 am
This plan is consistent with my idea. My idea is to find all the polylines in the DXF file with a pen width other than default, and then use half of the pen width to decompose them into inner and outer contours, and then find the coordinates based on the inner and outer contours. Can you teach me how to write this JS script?
First problem: To find all the polylines
- Not all polylines use a Lineweight, they may use a global or local custom widths.
- Not all polylines are trivial to offset.

Meaning that you want to replace polylines with a certain Lineweight in a dxf by their contour(s).
Any Lineweight there 0.00mm does not require an offset or better: The inner and outer contour would be duplicates.

Second problem:
- An inner and outer contour of a closed polyline in a Lineweight other than 0.00mm are unrelated polylines in a drawing.
Before rasterization you need to pair up any polyline with respectively its related inner or outer counterpart.
- A closed polyline in Lineweight 0.00mm can not be distinguished from a polyline representing a contour.
Your rasterization process would fill whole the enclosed area.
- The pen style contour of an open polyline is a single closed polyline and indistinguishable from the former.
Here raster-filling whole the enclosed area is the required method.

Third problem:
- Raster size 0.4 exceeds the size of at least 9 types of Lineweights for a single string of dots.
And exceeding at least 14 types for a double string.
Dots may even not be generated depending the raster origin.

Question:
Is rasterization done inside or outside QCAD?
While offsetting QCAD knows what contours pair up.
What would be the output? A drawing with dots, a list of coordinates, ...

Side note:
I am an engraver and to engrave I use conical cutters.
The surface width of a cut is related to the depth of a cut.
A pen style offset in arbitrary size is in fact the cleared area of my cutter following a vector style toolpath.
I have created QCAD tools to visualize the cleared area, filling the outer and inner contour with a colored hatch.
I use that to verify if a round cutter would remove all the required area, especially in corners.
Not hatched is not cut.
For pocketing I can then decrease or even increase the step size between toolpaths depending too lean or too coarse overcut.
WildWolfCJ wrote:
Fri Dec 01, 2023 4:02 am
Can you teach me how to write this JS script?
If the above would come out flawless each time then I would already have shared such a tool :wink:
The time invested is not at all free but since it is a basic necessity for me ... It is kinda payed for.
Or I could pay several 10k annual fee for a V-Carving package that still has its own major to minor flaws.

Question:
Please elaborate on what these coordinates are used for.
The required goals may influence the plan of attack.

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 » Mon Dec 04, 2023 4:30 am

Is rasterization done inside or outside QCAD?
While offsetting QCAD knows what contours pair up.
What would be the output? A drawing with dots, a list of coordinates, ...
I can find a way to complete the rasterization in an external program. I only need to consider polylines with pen width. For unclosed polylines, I can obtain the outer contour and the inner contour, and connect the inner and outer contours end to end to form a closed contour. Looking for coordinates again, I now want to obtain the inner and outer contours through OffsetProWorker.js, and know which polyline corresponds to which contour.

Question:
Please elaborate on what these coordinates are used for.
The required goals may influence the plan of attack.
I am an electron beam lithography machine host computer engineer. I need to obtain the graphic coordinates in the dxf file according to the step value, and then SEM carves the corresponding graphic based on the coordinates.

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 Dec 04, 2023 9:27 am

WildWolfCJ wrote:
Mon Dec 04, 2023 4:30 am
electron beam lithography ... and then SEM carves the corresponding graphic based on the coordinates.
The first was already clear to me but that you do dots not really.
I thought that a SEM usually followed a toolpath.
AKA for a rectangle from corner A to corner B to corner ...
And that it has a 'pen size' depending on the stylus shape.
And that the resolution is far better than your 0.4mm ... Rather more something as 0.4 micron.

We usually don't enter a shop and ask for just a rope ... We ask for a rope for some specific purpose.
The purpose will define more or less what kind of ropes we are offered. :wink:

Gathered from other topics I then come to the conclusion that you have line-art mixed with text-art.
And that you need to pocket the outer contours of all that art with dots.
As if a Drill toolpath but rather not for drilling, instead it is firing the beam at given coordinates.

Fully done with QCAD I think you are better of with the 'Nifty' solution.
There filling in the contours with a hatch is implemented right out the box.
You might also want to consider a hexagonal spaced point grid instead of a squared out type.

Please provide an example drawing with all kinds of drawing art that you may encounter.
Line art in different pen sizes, if so including arcs, text, logo's, ... and so on.
For text ensure that you include the font file(s).
For images (only linked in dxf) include the image file(s).
The latter may be the harder part.

With an example drawing we could do the conversion manually in QCAD.
If that is satisfactory then we can automate the GUI methods in a script.
A script at GUI level can be called as per OS Command Line resulting in a CSV file with coordinates.

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 » Tue Dec 05, 2023 2:17 am

Thank you very much for your reply and guidance
And that the resolution is far better than your 0.4mm ... Rather more something as 0.4 micron.
SEM does have a higher resolution than 0.4, this value can be set
Gathered from other topics I then come to the conclusion that you have line-art mixed with text-art.
And that you need to pocket the outer contours of all that art with dots.
Yes, this is the function I mainly develop,Obtain the outline coordinates of the solid graphics in DXF, or obtain the filling coordinates of the closed graphics, which is also optional.
As if a Drill toolpath but rather not for drilling, instead it is firing the beam at given coordinates.
Yes, absolutely correct
Fully done with QCAD I think you are better of with the 'Nifty' solution.
This is the first time I have heard of this plan. I will check the relevant information, or can you give me some guidance?
Please provide an example drawing with all kinds of drawing art that you may encounter.
Now I only deal with polylines, line segments, circles, arcs, ellipses, fonts, and these graphics with pen width (there are still bugs), and I can't handle complex graphics yet.
My requirement is that the customer gives me a DXF file, and I need to use SEM to engrave the graphics in the DXF file. Because SEM engraves using dots, I must first convert the graphics in the DXF into coordinates.
Because I am a newbie in this industry and I don’t understand many things, so it is very difficult for me to do it.
A script at GUI level can be called as per OS Command Line resulting in a CSV file with coordinates.
How should I do this? I have no idea at all

Thank you again from the Far East

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”