LibreCAD / libdxfrw

Successor of https://sourceforge.net/projects/libdxfrw/, developed for LibreCAD, by LibreCAD Contributors, usable for all
GNU General Public License v2.0
193 stars 72 forks source link

How to determine if an ARC is ccw (counter clock wise) or not? #69

Closed tridiserkan closed 1 year ago

tridiserkan commented 1 year ago

Hi all, I have, stumbled across this feature. I am reading a DXF and converting all possible entities into lines or polylines. However some of the arcs are reversed. When I debug, I realised isccw is being set to 1 while creating an arc on reader.

Do you have an idea how I can determine if an arc is ccw or not?

rvt commented 1 year ago

Best is to check the DXF specification for it.

If libdxfrw 'fixes' it to 1 it will properly doing it wrong, order is important for some applications like like laser cutting and milling.

tridiserkan commented 1 year ago

I already checked. It can be found with extrusion direction maybe however its an optional field and most of the DXF files does not contain this information.

What I do not understand is DWG TrueView can show them correctly but I dont know how. Its happening with only +x side ARCs but not every one of them.

Anybody knows how TrueView does it correctly? Maybe it traverses entities with an order?

image image image

lordofbikes commented 1 year ago

Have you tried to open this file in LibreCAD? What are the results?

DWG TrueView is closed source and it is from Autodesk, the inventor of DWG/DXF format, so they must know how to interpret their format. Thus I'm afraid that nobody here can tell how TrueView interprets the DXF.

The referenced isccw in DRW_Arc class is always true for regular arcs. https://github.com/LibreCAD/libdxfrw/blob/072aecd49d835eb83e41572a4ab4d143fdba105a/src/drw_entities.h#L352 A regular arc is always CCW because the coordinate system is CCW. To revert an arc you simply swap start and end angle.

As the comment above states, isccw is used in hatches and I think in polylines too. The situation for hatch and polyline is different, start and end angles can't be swapped, because the vertices must be kept in correct order. When you search for isccw in source code, you'll see that it is red or written with code 73 in hatch boundary paths. Polylines have a different approach, they have a bulge parameter with code 42 to describe the radius of an arc. Depending on the bulge sign, a polyline arc segment is CW or CCW.

Have you located the arc in the DXF? This is the most important step to get behind this issue. Which codes describe the arc? Is it a polyline?

If it is an arc and it has 210, 220 and 230 codes, it is probably an export from 3D software. Then you can look at LibreCAD/LibreCAD#1237 and LibreCAD/LibreCAD#1084 which are about these codes. In short, those codes define a 3D vector representing the Object Coordinate System and they must be used to translate the object into World Coordinate System. Search for Arbitrary Axis Algorithm in DXF reference. It describes how to translate OCS to WCS.

tridiserkan commented 1 year ago

Hey @rvt , @lordofbikes

I made a mistake. I was converting arcs into polylines. Turns out all of them should be CCW but i made a mistake while I was making a linear space between start angle and end angle. It should be circular space. I fixed it and its all right now.

Thanks for your help, I am closing the issue.