1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
# Copyright (c) 2021-2022 Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf
from ezdxf.enums import MTextParagraphAlignment
from ezdxf.tools.text import (
MTextEditor,
ParagraphProperties,
)
from ezdxf.tools.text_layout import lorem_ipsum
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
CWD = pathlib.Path(".")
# ------------------------------------------------------------------------------
# This example shows how to use the MTextEditor to compose MTEXT content.
#
# docs: https://ezdxf.mozman.at/docs/dxfentities/mtext.html
# tutorial: https://ezdxf.mozman.at/docs/tutorials/mtext.html
# ------------------------------------------------------------------------------
ATTRIBS = {
"char_height": 0.7,
"style": "OpenSans",
}
# use constants defined in MTextEditor:
NP = MTextEditor.NEW_PARAGRAPH
def recreate_mtext_py_example(msp, location):
# replicate example "mtext.py":
attribs = dict(ATTRIBS)
attribs["width"] = 15.0
editor = (
MTextEditor(f"recreate mtext.py result:{NP}normal ")
.overline("over line")
.append(" normal" + NP + "normal ")
.strike_through("strike through")
.append(" normal" + NP)
.underline("under line")
.append(" normal")
)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def using_colors(msp, location):
attribs = dict(ATTRIBS)
attribs["width"] = 10.0
editor = MTextEditor("using colors:" + NP)
# Change colors by name: red, green, blue, yellow, cyan, magenta, white
editor.color("red").append("RED" + NP)
# The color stays the same until changed
editor.append("also RED" + NP)
# Change color by ACI (AutoCAD Color Index)
editor.aci(3).append("GREEN" + NP)
# Change color by RGB tuples
editor.rgb((0, 0, 255)).append("BLUE" + NP)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_text_height_absolute(msp, location):
attribs = dict(ATTRIBS)
attribs["width"] = 40.0 # need mor space to avoid text wrapping
editor = MTextEditor(
"changing text height absolute: default height is 0.7" + NP
)
# doubling the default height = 1.4
editor.height(1.4)
editor.append("text height: 1.4" + NP)
editor.height(3.5).append("text height: 3.5" + NP)
editor.height(0.7).append("back to default height: 0.7" + NP)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_text_height_relative(msp, location):
attribs = dict(ATTRIBS)
attribs["width"] = 40.0 # need mor space to avoid text wrapping
editor = MTextEditor(
"changing text height relative: default height is 0.7" + NP
)
# this is the default text height in the beginning:
current_height = attribs["char_height"]
# The text height can only be changed by a factor:
editor.scale_height(2) # scale by 2 = 1.4
# keep track of the actual height:
current_height *= 2
editor.append("text height: 1.4" + NP)
# to set an absolute height, calculate the required factor:
desired_height = 3.5
factor = desired_height / current_height
editor.scale_height(factor).append("text height: 3.5" + NP)
current_height = desired_height
# and back to 0.7
editor.scale_height(0.7 / current_height).append(
"back to default height: 0.7" + NP
)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def changing_fonts(msp, location):
attribs = dict(ATTRIBS)
attribs["width"] = 15.0
editor = MTextEditor("changing fonts:" + NP)
editor.append("Default: Hello World!" + NP)
editor.append("SimSun: ")
# The font name for changing MTEXT fonts inline is the font family name!
# The font family name is the name shown in font selection widgets in
# desktop applications: "Arial", "Times New Roman", "Comic Sans MS"
#
# change font in a group to revert back to the default font at the end:
simsun_editor = MTextEditor().font("SimSun").append("你好,世界" + NP)
# reverts the font back at the end of the group:
editor.group(str(simsun_editor))
# back to default font OpenSans:
editor.append("Times New Roman: ")
# change font outside of a group until next font change:
editor.font("Times New Roman").append("Привет мир!" + NP)
# If the font does not exist, a replacement font will be used:
editor.font("Does not exist").append("This is the replacement font!")
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def indent_first_line(msp, location):
# Indentation is a multiple of the default text height (MTEXT char_height)
attribs = dict(ATTRIBS)
attribs["char_height"] = 0.25
attribs["width"] = 7.5
editor = MTextEditor("Indent the first line:" + NP)
props = ParagraphProperties(
indent=1, # indent first line = 1x0.25 drawing units
align=MTextParagraphAlignment.JUSTIFIED,
)
editor.paragraph(props)
editor.append(" ".join(lorem_ipsum(100)))
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def indent_except_fist_line(msp, location):
# Indentation is a multiple of the default text height (MTEXT char_height)
attribs = dict(ATTRIBS)
attribs["char_height"] = 0.25
attribs["width"] = 7.5
editor = MTextEditor("Indent left paragraph side:" + NP)
indent = 0.7 # 0.7 * 0.25 = 0.175 drawing units
props = ParagraphProperties(
# first line indentation is relative to "left", this reverses the
# left indentation:
indent=-indent, # first line
# indent left paragraph side:
left=indent,
align=MTextParagraphAlignment.JUSTIFIED,
)
editor.paragraph(props)
editor.append(" ".join(lorem_ipsum(100)))
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def bullet_list(msp, location):
attribs = dict(ATTRIBS)
attribs["char_height"] = 0.25
attribs["width"] = 7.5
# There are no special commands to build bullet list, the list is build of
# indentation and a tabulator stop. Each list item needs a marker as an
# arbitrary string.
bullet = "•" # alt + numpad 7
editor = MTextEditor("Bullet List:" + NP)
editor.bullet_list(
indent=1,
bullets=[bullet] * 3, # each list item needs a marker
content=[
"First item",
"Second item",
" ".join(lorem_ipsum(30)),
],
)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def numbered_list(msp, location):
attribs = dict(ATTRIBS)
attribs["char_height"] = 0.25
attribs["width"] = 7.5
# There are no special commands to build numbered list, the list is build of
# indentation and a tabulator stop. There is no automatic numbering,
# but therefore the absolute freedom for using any string as list marker:
editor = MTextEditor("Numbered List:" + NP)
editor.bullet_list(
indent=1,
bullets=["1.", "2.", "3."],
content=[
"First item",
"Second item",
" ".join(lorem_ipsum(30)),
],
)
# Indentation and tab stops are multiples of the default text height (MTEXT
# char_height)!
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def stacking(msp, location):
attribs = dict(ATTRIBS)
attribs["char_height"] = 0.25
attribs["width"] = 4
editor = MTextEditor("Stacked text:" + NP)
# place fraction with down scaled text height in a group:
stack = MTextEditor().scale_height(0.6).stack("1", "2", "^")
editor.append("over: ").group(str(stack)).append(NP)
stack = MTextEditor().scale_height(0.6).stack("1", "2", "/")
editor.append("fraction: ").group(str(stack)).append(NP)
stack = MTextEditor().scale_height(0.6).stack("1", "2", "#")
editor.append("slanted: ").group(str(stack)).append(NP)
# additional formatting in numerator and denominator is not supported
# by AutoCAD or BricsCAD.
# switching colors inside the fraction to red does not work:
numerator = MTextEditor().color("red").append("1")
stack = MTextEditor().scale_height(0.6).stack(str(numerator), "2", "#")
editor.append("color red: ").group(str(stack)).append(NP)
msp.add_mtext(str(editor), attribs).set_location(insert=location)
def create(dxfversion):
"""
Important:
MTEXT FORMATTING IS NOT PORTABLE ACROSS CAD APPLICATIONS!
Inline MTEXT codes are not supported by every CAD application and even
if inline codes are supported the final rendering may vary.
Inline codes are well-supported by AutoCAD (of course!) and BricsCAD,
but don't expect the same rendering in other CAD applications.
The drawing add-on of ezdxf support some of these features, but with a
different rendering result than AutoCAD/BricsCAD.
"""
doc = ezdxf.new(dxfversion, setup=True)
msp = doc.modelspace()
recreate_mtext_py_example(msp, location=(0, 0))
using_colors(msp, location=(0, 10))
changing_text_height_absolute(msp, location=(0, 25))
changing_text_height_relative(msp, location=(0, 40))
changing_fonts(msp, location=(15, 14))
indent_first_line(msp, location=(15, 6))
indent_except_fist_line(msp, location=(24, 6))
bullet_list(msp, location=(33, 6))
numbered_list(msp, location=(33, 2))
stacking(msp, location=(33, 14))
doc.set_modelspace_vport(height=60, center=(15, 15))
return doc
for dxfversion in ["R2000", "R2004", "R2007", "R2010", "R2013", "R2018"]:
doc = create(dxfversion)
filename = CWD / f"mtext_editor_{dxfversion}.dxf"
doc.saveas(filename)
print(f"saved {filename}")
|