Advice regarding rendered width/height of text #450
-
Hi, thanks for a great library! I have a question on how should I approach the below problem:
So, for example: I understand that this includes font-specific stuff, to keep things simple (simpler?) I can assume that the default MatplotlibBackend settings are ok for me (the PNG I generated with default settings looks very good for my use case). Any ideas? I'll be honest, I'm not really fluent in all this DWG/DXF stuff. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
(disclaimer: I'm not exactly sure what your requirements are here and with more context, a simpler solution might be preferable but I'm answering the question as-given) Making matplotlib use a fixed size canvas is a little tricky but is possible if you set the figure dpi to a reasonable value (the value doesn't matter so long as it's not extreme) then set the figure size (in inches) such that Once you have done that you will be able to render your text to a fixed-size canvas. To get the size of the text once rendered there are several things you could do, but probably the easiest would be to just iterate over the patches/artists in the axes after you have finished rendering and query the bounding boxes there. You can then use the as you can see, matplotlib doesn't have the best usability for doing pixel-perfect rendering. An additional backend which draws directly to a fixed canvas might be a nice addition but isn't something I personally require so haven't looked into much. |
Beta Was this translation helpful? Give feedback.
-
The DXF format is not designed to preserve the text layout across CAD applications. If you've ever imported a DXF drawing into any other CAD application, you will know that no other CAD application renders the text and dimensions in the same way as AutoCAD, although BricsCAD comes very close. Perhaps the DXF format and ezdxf are not the right tools for this task. You can use the import ezdxf.path
from ezdxf.addons import text2path
doc = ezdxf.readfile("your.dxf")
msp = doc.modelspace()
for text in msp.query("TEXT"):
bbox = ezdxf.path.bbox(text2path.make_paths_from_entity(text))
print(f"width={bbox.size.x}, height={bbox.size.y}") Converting this drawing units to pixels is up to you. |
Beta Was this translation helpful? Give feedback.
-
Thank you both for your suggestions. I ended up using what @mozman proposed. I agree with the mentioned drawbacks. However, this snippet is precisely what I needed for my use case. |
Beta Was this translation helpful? Give feedback.
The DXF format is not designed to preserve the text layout across CAD applications. If you've ever imported a DXF drawing into any other CAD application, you will know that no other CAD application renders the text and dimensions in the same way as AutoCAD, although BricsCAD comes very close. Perhaps the DXF format and ezdxf are not the right tools for this task.
You can use the
text2path
add-on to get the text bounding box in drawing units for a single TEXT entity: