mozman / ezdxf

Python interface to DXF
https://ezdxf.mozman.at
MIT License
937 stars 191 forks source link

Incorrect plotting of dxf files #1177

Closed stansmolders closed 1 month ago

stansmolders commented 1 month ago

Hi, I am trying to plot simple dxf files with a variety of shapes. For some reason (when dxf files contain the entities arcs or circles), some entities seem to be plotted wrong. For some dxf files this behaviour seems to be quite random as e.g. in the same plot, some arcs can be plotted correctly on the positive plane, while others seem to be mirrored (x=-x). Adding x=-x however would mess up the correctly plotted entities, so this would not be a solution. Also, when plotting some dxf files, there is no problem at all. The problem seems to occur due to the use of dxf.center. Is this a known problem or am I using it wrong? I added screenshots of this behaviour below and the code i use for this part is as follows:

       def _plot_dxf(self, doc):
    """Plot the DXF file using matplotlib."""
    print("Plotting entities from the DXF file...")
    msp = doc.modelspace()
    fig, ax = plt.subplots()

    for entity in msp:
        print(f"Processing entity: {entity.dxftype()}")
        if entity.dxftype() == 'LINE':
            start = entity.dxf.start
            end = entity.dxf.end
            print(f"Plotting LINE from {start} to {end}")
            ax.plot([start.x, end.x], [start.y, end.y], 'b')
        elif entity.dxftype() == 'CIRCLE':
            center = entity.dxf.center
            radius = entity.dxf.radius
            print(f"Plotting CIRCLE at {center} with radius {radius}")
            circle = plt.Circle((center.x, center.y), radius, color='r', fill=False)
            ax.add_artist(circle)
        elif entity.dxftype() == 'ARC':
            center = entity.dxf.center
            radius = entity.dxf.radius
            start_angle = entity.dxf.start_angle
            end_angle = entity.dxf.end_angle
            print(f"Plotting ARC at {center} with radius {radius}, from {start_angle}° to {end_angle}°")
            arc = Arc((center.x, center.y), radius*2, radius*2, angle=0, theta1=start_angle, theta2=end_angle, color='g')
            ax.add_patch(arc)
        elif entity.dxftype() == 'ELLIPSE':
            center = entity.dxf.center
            major_axis = entity.dxf.major_axis
            print(f"Plotting ELLIPSE at {center} with major axis {major_axis}")
            ellipse = plt.Circle((center.x, center.y), major_axis.magnitude, color='y', fill=False)
            ax.add_artist(ellipse)
    ax.set_aspect('equal', adjustable='datalim')
    plt.title("DXF Viewer")
    plt.xlabel("X-axis")
    plt.ylabel("Y-axis")
    plt.grid(True)
    print("Displaying plot...")
    plt.show()

dxf plot4 dxf plot3 dxf plot2 dxf plot

mbway commented 1 month ago

Are you aware of the ezdxf drawing add-on? Have you compared your implementation with that to see where the logic differs?

stansmolders commented 1 month ago

Are you aware of the ezdxf drawing add-on? Have you compared your implementation with that to see where the logic differs?

Could you provide a link to what you mean? I basically only need to plot dxf files correctly for this part, so if this already available somewhere, other logic would be fine as well.