Replies: 6 comments 3 replies
-
tl;dr: it's not possible yet, but there are experimental features for it Shapes and vector fonts in AutoCAD are stored in SHX files and the source code for SHX The The big problem: Copyright I can not include the SHX/SHP fonts in If you have any Autodesk product installed you can add the font directory to the support
If you don't have any CAD software which includes SHX fonts, you have to search the LibreCAD circumvents this problem by using other replacement fonts with the ".lff" An example for using SHX fonts can be found here: examples/render/shapefile_rendering.py The following example renders the text "7410/A" by the "ISO.SHX" font: import pathlib
import ezdxf
from ezdxf import shapefile, path, zoom
from ezdxf.filemanagement import find_support_file
from ezdxf.document import Drawing
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
CWD = pathlib.Path(".")
# Add the directory containing the shx/shp files to your config file:
# 1. create a default config file in home directory at ~/.config/ezdxf/ezdxf.ini
#
# $ ezdxf config --home
#
# 2. add the font directory as support dir, this is my config file:
# [core]
# default_dimension_text_style = OpenSansCondensed-Light
# test_files = ~/src/dxftest
# font_cache_directory =
# support_dirs = ~/src/shx-fonts
# ~/src/ctb
#
# or
#
# set the support directories in the script:
# ezdxf.options.support_dirs = ["/absolut/path/to/shx/files"]
def render_txt(fontname: str, text: str) -> Drawing:
doc = ezdxf.new()
msp = doc.modelspace()
fontname = find_support_file(fontname, ezdxf.options.support_dirs)
# fontname should be the file path to the SHX font
font = shapefile.readfile(fontname)
# text_path is a ezdxf.path.Path instance!
text_path = font.render_text(text)
path.render_splines_and_polylines(msp, [text_path])
return doc
doc = render_txt("iso.shx", "7410/A")
zoom.extents(doc.modelspace())
doc.saveas(CWD / "iso_shx.dxf") If everything works, the result should look like this: Additional tips: The text style is stored in DXF attribute "style" of the TEXT entity and the font name is stored in for text in msp.query("TEXT"):
style_name = text.dxf.style
style = doc.styles.get(style_name)
print(style.dxf.font)
# same as:
print(text.font_name()) To render MTEXT entities, explode them by the The method wcs_path = text_path.transform(text.text_transformation_matrix()) |
Beta Was this translation helpful? Give feedback.
-
That's exactly what I needed. |
Beta Was this translation helpful? Give feedback.
-
Just a little update for version 1.1.0: one import changes from: from ezdxf import shapefile to: from ezdxf.fonts import shapefile and this code from the previous examples: text_path = font.render_text(text)
path.render_splines_and_polylines(msp, [text_path]) to: text_path = font.render_text(text).to_path()
path.render_splines_and_polylines(msp, [text_path]) Updating this since I couldn't find anything about the second one in the docs and this use is pretty uncommon. |
Beta Was this translation helpful? Give feedback.
-
ehm, I tested the code in the last example you posted and... the output is not what I need. I get: I need: Which is what I was getting when I opened this discussion; I guessI'm going to use: font = shapefile.readfile(fontname) |
Beta Was this translation helpful? Give feedback.
-
mmm, too much work, postponed. Scrap that, seems like I'm having issues with scaling. Will complete this when less sleepy. |
Beta Was this translation helpful? Give feedback.
-
In a less sleep deprived state, I found what I was doing wrong. I used to use: font = shapefile.readfile(fontpath) # this was the actual full path of the font But now: font = fonts.make_font(fontname, cap_height=3.5) # this is just the font name As you suggested I had to add: ezdxf.options.support_dirs = [fontdir] # my custom font path list to have the desired font loaded; turns out I was misinterpreting what the class was doing. It simplifies some of my code, getting cap_height from entity.dxf.height, along the other stuff I needed for insert. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to get a TEXT converted to ARC, LINEs, ... to mill it; this is the source text:
I see text2path does convert text to paths, and I can get this:
This is different from what I need:
This seems the same of the first image, but it's actually the result of exploding the text with AutoCAD express tools (or LibreCAD explode tool): those are arcs, lines, polilynes.
Basically I don't want the path around the font, but inside - obviously I'm not after fancy fonts as you can see.
Is this doable with text2path?
Beta Was this translation helpful? Give feedback.
All reactions