0% found this document useful (0 votes)
3 views2 pages

Mozman 2

The document contains a Python script that uses the ezdxf library to create a DXF file with two text entities fitted into specified bounding boxes. It defines a function 'as_hatches' that takes data about the text and its positioning, draws the bounding boxes, and renders the text within those boxes. The script is designed to save the output as 'hatch-entity.dxf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Mozman 2

The document contains a Python script that uses the ezdxf library to create a DXF file with two text entities fitted into specified bounding boxes. It defines a function 'as_hatches' that takes data about the text and its positioning, draws the bounding boxes, and renders the text within those boxes. The script is designed to save the output as 'hatch-entity.dxf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# additonal imports

import ezdxf
from ezdxf.enums import TextEntityAlignment
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])
path.render_lwpolylines(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")

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"},
},
},
}

as_hatches(info)

if __name__ == "__main__":
main()

You might also like