text size and position manipulation #1053
-
I'm trying to place text in an existing DXF file but in a specific position and with a specific size, that is, regardless of the number of characters and the font used, the text must occupy the same space, I've put below some codes that I tried do and what happened vs what should happen. Both texts have the same parameters (except for the position), but they do not occupy the same space, below are two examples of how it should look: Both example 2 and example 3 solve the problem, below is the code used to generate the first image main.py from functions.dxf import Dxf
def main():
info = {
"basefile":"files/simple_2.dxf",
"filename":"000AAA.dxf",
"quantity":"1",
"modelo":{
"tamanho":{
"largura":"80",
"altura":"127",
"espacamento":"1"
},
"text_1":{
"text":"Short Text",
"pos":{
"x":"-70",
"y":"80"
},
"size":{
"x":"8",
"y":"1"
},
},
"text_2":{
"text":"This is an example of long text",
"pos":{
"x":"-70",
"y":"60"
},
"size":{
"x":"8",
"y":"1"
},
},
}
}
Dxf(info)
if __name__ == "__main__":
main()
dxf.py def Dxf(data):
import ezdxf
from ezdxf.enums import TextEntityAlignment
model_path = data["basefile"]
save_path = data["filename"]
texto_1 = data["modelo"]["text_1"]
texto_2 = data["modelo"]["text_2"]
file = ezdxf.readfile(model_path)
msp = file.modelspace()
text_1 = msp.add_text(
texto_1["text"],
dxfattribs={
"height":float(texto_1["size"]["x"])
}
).set_placement(
p1=(float(texto_1["pos"]["x"]), float(texto_1["pos"]["y"])),
p2=(float(texto_1["pos"]["x"])+float(texto_1["size"]["x"]), float(texto_1["pos"]["y"])+float(texto_1["size"]["y"])),
align=TextEntityAlignment.FIT
)
text_2 = msp.add_text(
texto_2["text"],
dxfattribs={
"height":float(texto_2["size"]["x"])
}
).set_placement(
p1=(float(texto_2["pos"]["x"]), float(texto_2["pos"]["y"])),
p2=(float(texto_2["pos"]["x"])+float(texto_2["size"]["x"]), float(texto_2["pos"]["y"])+float(texto_2["size"]["y"])),
align=TextEntityAlignment.FIT
)
file.saveas(save_path) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
That is the best result you can get: import ezdxf
from ezdxf.enums import TextEntityAlignment
def Dxf(data):
save_path = data["filename"]
texto_1 = data["modelo"]["text_1"]
texto_2 = data["modelo"]["text_2"]
doc = ezdxf.new()
msp = doc.modelspace()
doc.styles.add("ARIAL", font="DejaVuSans.ttf") # EDIT: changed font for Linux
x1 = float(texto_1["pos"]["x"])
y1 = float(texto_1["pos"]["y"])
length = float(texto_1["size"]["x"])
height = float(texto_1["size"]["y"])
msp.add_text(
texto_1["text"], dxfattribs={"height": height, "style": "ARIAL"}
).set_placement(p1=(x1, y1), p2=(x1 + length, y1), align=TextEntityAlignment.FIT)
x2 = float(texto_2["pos"]["x"])
y2 = float(texto_2["pos"]["y"])
length = float(texto_2["size"]["x"])
height = float(texto_2["size"]["y"])
msp.add_text(
texto_2["text"], dxfattribs={"height": height, "style": "ARIAL"}
).set_placement(p1=(x2, y2), p2=(x2 + length, y2), align=TextEntityAlignment.FIT)
doc.saveas(save_path)
def main():
info = {
"basefile": "files/simple_2.dxf",
"filename": "000AAA.dxf",
"quantity": "1",
"modelo": {
"tamanho": {"largura": "80", "altura": "127", "espacamento": "1"},
"text_1": {
"text": "Short Text",
"pos": {"x": "-70", "y": "80"},
"size": {"x": "8", "y": "1"},
},
"text_2": {
"text": "This is an example of long text",
"pos": {"x": "-70", "y": "75"},
"size": {"x": "8", "y": "1"},
},
},
}
Dxf(info)
if __name__ == "__main__":
main() The red line shows the align-line for the FIT alignment. The rendering depends on the CAD application and is not defined by the DXF reference. BricsCAD: ezdxf view: LibreCAD: EDIT: changed font for Linux to "DejaVuSans.ttf" rendering TTF fonts is possible on Linux |
Beta Was this translation helpful? Give feedback.
-
You can get a more precise solution if you don't mind getting HATCH entities as a result: # additonal imports
from ezdxf.addons import text2path
from ezdxf import path
from ezdxf.fonts import fonts
from ezdxf.math import Matrix44
def as_hatches(data):
texto_1 = data["modelo"]["text_1"]
texto_2 = data["modelo"]["text_2"]
doc = ezdxf.new()
msp = doc.modelspace()
ff = fonts.FontFace(family="DejaVu Sans")
def fit_into_bbox(text: str, x: float, y: float, width: float, height: float):
# draw the expected target size and location
msp.add_lwpolyline(
[(x, y), (x + width, y), (x + width, y + height), (x, y + height)],
close=True,
dxfattribs={"color": 1},
)
# insert location = (0, 0)
# default height = 1.0
text_paths = [text2path.make_path_from_str(text, font=ff)]
text_paths = path.fit_paths_into_box(
text_paths, size=(width, height, 0), uniform=False
)
# detect real extents
extmin = path.bbox(text_paths).extmin
m = Matrix44.translate(x - extmin.x, y - extmin.y, 0)
path.render_hatches(msp, [tp.transform(m) for tp in text_paths])
fit_into_bbox(
texto_1["text"],
x=float(texto_1["pos"]["x"]),
y=float(texto_1["pos"]["y"]),
width=float(texto_1["size"]["x"]),
height=float(texto_1["size"]["y"]),
)
fit_into_bbox(
texto_2["text"],
x=float(texto_2["pos"]["x"]),
y=float(texto_2["pos"]["y"]),
width=float(texto_2["size"]["x"]),
height=float(texto_2["size"]["y"]),
)
doc.saveas("hatch-entity.dxf") |
Beta Was this translation helpful? Give feedback.
You can get a more precise solution if you don't mind getting HATCH entities as a result: