DXF Parser : Ellipses angle direction

2019-04-04 05:58发布

问题:

I wrote my own ASCII DXF file parser and I encounter a problem with ellipses in some particular documents.

It seems that there is no "Group Code" defining the angle direction (Clockwise or Counterclockwise) for Start and End Parameters. In most case, it's counterclockwise, but not in ALL cases...

To simplify, here is a visual exemple:

As you can see, the first one has a start angle of 135, and the second one a start angle of 45... But, the ellipses seems to be identical (half-circle, on the left)... This is because the first one's angles are clockwise, and the second one's are counter-clockwise...

Obviously, they appear correctly in AutoCAD, but with my parser, they appear like that:

Simply because I don't know if angles are represented CW or CCW...

Did I miss a group code or something ?

Info: In AutoCAD, I can see that the first ellipse "Minor Axis Endpoint" is [200, -200, 0], while the second one's is [-200, 200, 0], I suppose this is how AutoCAD knows if the angles are CW or CCW... But all I have in the DXF file is the "Major Axis Endpoint" and the "Major to Minor Axis Ratio" (a number)

Here is the DXF file corresponding to this example: http://www.woofiles.com/dl-279966-ZvoMjamr-c.dxf

回答1:

The ellipse might not lie in the 2D XY plane so just using the sign of the Z component of the extrusion direction isn't safe. Here's a more general approach for a 3D ellipse:

1) Create the ellipse in the XY plane with the major axis in the +X direction and going counter-clockwise from start parameter (group code 41) to end parameter (group code 42). First make sure the end parameter is greater than the start parameter and add 2pi if it's not. You can then calculate each point with:

X = [length of major radius] * cos(angle)
Y = [length of minor radius] * sin(angle)

2) Rotate it to this new coordinate system:

Direction of new X axis = endpoint of major axis
Direction of new Z axis = extrusion direction
Direction of new Y axis = [new Z axis] cross product [new X axis]

You can do this by normalizing these vectors and making a 3x3 transformation matrix where each column contains one of the vectors, then multiply this matrix by every point in the ellipse created in step 1.



回答2:

Solved: I forgot to take in account the extrusion vector

When it is negative (0,0,-1), the angles must be treated as CCW instead of CW (and vice-versa)