mozman / ezdxf

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

Problems with hidden objects when converting png #992

Closed cococosusu closed 8 months ago

cococosusu commented 8 months ago

Describe the bug The result of converting to png is different from the CAD file! I've confirmed that the conversion works well by referring to the drawing-related documentation. However, the output seems to be different.

When I exploded something in AutoCAD, something came out. (Like the converted image below) Your code seems to draw even the hidden objects inside.

How do I draw only the objects that are clearly visible in CAD?

Result Screenshots

CAD image

converted image image

Code

import matplotlib.pyplot as plt
from ezdxf.addons import odafc
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
from ezdxf.addons.drawing.config import Configuration

dpi = 300
file = "sample.dwg"
out_file = "image.png"
doc = odafc.readfile(file)

auditor = doc.audit()
if auditor.has_errors:
    # But is most likely good enough for rendering.
    print(f"Found {len(auditor.errors)} unrecoverable errors.")
if auditor.has_fixes:
    print(f"Fixed {len(auditor.fixes)} errors.")

layout = doc.layouts.get("Model")

# setup drawing add-on configuration
config = Configuration()
fig: plt.Figure = plt.figure(dpi=dpi)
ax: plt.Axes = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
out = MatplotlibBackend(ax)
Frontend(ctx, out, config=config).draw_layout(layout, finalize=True)

if out_file is not None:
    print(f'saving to "{out_file}"')
    fig.savefig(out_file, dpi=dpi)
    plt.close(fig)

DWG File link

mozman commented 8 months ago

There is a template for creating an issue - the important part is data or code to reproduce the error, without this information I can't help.

cococosusu commented 8 months ago

@mozman Oh, I'm sorry. I have attached the DWG file and included the code.

mozman commented 8 months ago

Hi!

This is a huge DXF file 59MB when converted from DWG.

Ezdxf is maybe not the right kind of tool to handle such huge and complex drawing.

I have no idea which application created this drawing (it's not AutoCAD) but my computer still tries to render this file while writing this comment.

I already condensed the file to 29MB by using the PURGE and WBLOCK commands in BricsCAD.

-- still rendering

I don't see any chance to fix this. I don't have the time and the will to debug a 29MB DXF file. Sorry.

-- still rendering ...

The process uses all available memory which means heavy page swapping and a massive slowdown...

-- after 25 min. I stopped the process...

I can and will only help if you can create a small example to reproduce the error.

cococosusu commented 8 months ago

@mozman I really appreciate your hard work in answering!

I reorganized a specific block in autoCAD and made it smaller. Here is the download link for the DWG file.

I also ran the code again and the results are as follows.

AutoCAD screen image

Export to PDF from AutoCAD image

code result image2

mozman commented 8 months ago

Which ezdxf version do you use, because the current version renders nothing for "sample_small.dwg" converted by BricsCAD to DXF R2013.

cococosusu commented 8 months ago

I use version '1.1.3' image

and

I modified some of the attached code. Frontend(ctx, out, config=config).draw_layout(layout, finalize=True) # finalize False -> True

I made a mistake

mozman commented 8 months ago

OK, I can reproduce the error and get the same result as you.

cococosusu commented 8 months ago

That's good to hear!

As far as I've checked, it seems that ezdxf draws all possible information when creating a drawing. This implies that the issue isn't related to problems arising from PNG conversion. It appears that ezdxf draws even hidden information.

When checking the CAD screen shown by the 'ezdxf view' command, it's similar to the PNG conversion results.

ezdxf view image

mozman commented 8 months ago

A quick fix in AutoCAD is to save the visible content via WBLOCK in a new DXF file.

The file size goes down to 3.5MB from 20MB and ezdxf renders only the visible part:

wblock2

mozman commented 8 months ago

It seems this file uses SPATIAL_FILTER objects to clip parts of the modelspace. This is not supported by ezdxf - I don't even know how this works.

cococosusu commented 8 months ago

Wow! Thank you so much! You're a lifesaver! 😄 As you mentioned, I was able to get information about the cropped area by checking the spatial filter. I really appreciate your help!!