DXF TEXT & MTEXT 实体完全参考DXF TEXT & MTEXT Entity Complete Reference
什么是基线(Baseline)What is the Baseline
在排版学和 CAD 绘图中,基线(Baseline)是文字字符所坐落的一条假想水平线。对于拉丁字母(如 a, b, c),字符的主体部分坐落在基线上,而下伸部分(如 g, p, y 的尾部)则会延伸到基线下方。In typography and CAD drawing, the baseline is an imaginary horizontal line on which text characters sit. For Latin letters (a, b, c), the main body rests on the baseline, while descenders (like g, p, y) extend below it.
在 DXF 的 TEXT 实体中,默认的插入点(组码 10/20/30)就位于文字的左端基线处。也就是说,当你输入一个 TEXT 实体而不指定任何对齐方式时,文字将从插入点开始向右侧延伸,字符底部坐落在基线上。这与 MTEXT 不同:MTEXT 默认的插入点位于文字的左上角。In DXF TEXT entities, the default insertion point (group codes 10/20/30) is at the left end of the baseline. When you create a TEXT entity without specifying alignment, text extends to the right from the insertion point with characters sitting on the baseline. This differs from MTEXT, whose default insertion point is at the top-left corner of the text.
TEXT 默认插入点 = 左下角基线处(近似左下角,但因基线非最底部,严格说是左端基线)。
MTEXT 默认插入点 = 左上角。 TEXT default insertion point = left end of baseline (approximately lower-left, but strictly speaking left end of baseline since baseline is not the very bottom).
MTEXT default insertion point = top-left corner.
TEXT 与 MTEXT 核心区别TEXT vs MTEXT Core Differences
| 特性Feature | TEXT(单行文字)TEXT (Single-line) | MTEXT(多行文字)MTEXT (Multiline) |
|---|---|---|
| 文字内容Content | 单行纯文本,无格式控制Single line of plain text, no formatting | 多行文本,支持富格式(字体、颜色、高度、堆叠等)Multiline text with rich formatting (font, color, height, stacking, etc.) |
| 换行支持Line Breaks | 不支持自动换行,需手动插入多个 TEXTNo automatic wrapping; multiple TEXT entities needed | 支持自动换行和段落Supports automatic wrapping and paragraphs |
| 默认插入点Default Insertion Point | 左端基线处(左下近似)Left end of baseline (approx. lower-left) | 左上角Top-left corner |
| 对齐控制Alignment Control | halign (72) + valign (73) 两个字段halign (72) + valign (73) two fields |
attachment_point (71) 单一字段attachment_point (71) single field |
| 文字高度Text Height | 组码 40,统一高度Group code 40, uniform height | 组码 40 为标称高度,每行可不同Group code 40 is nominal height; each line may differ |
| 宽度因子Width Factor | 组码 41Group code 41 | 组码 41 为参考矩形宽度Group code 41 is reference rectangle width |
| 旋转角度Rotation | 组码 50Group code 50 | 组码 50Group code 50 |
| 适用场景Use Case | 简单标签、标注文字、属性值Simple labels, dimension text, attribute values | 段落说明、技术注释、多行标注Paragraph descriptions, technical notes, multiline annotations |
在 DXF 文件中,组码 0 的值区分两种实体:
TEXT 表示单行文字,MTEXT 表示多行文字。两者不能混用,各自拥有独立的组码定义。
In DXF files, group code 0 distinguishes the two: TEXT for single-line text, MTEXT for multiline text. They are not interchangeable and each has its own group code definitions.
TEXT 实体详解TEXT Entity in Detail
TEXT 是 DXF 中最基本的文字实体,用于表示单行文字。它的插入点、高度、旋转角度和对齐方式都由独立的组码控制。TEXT is the most basic text entity in DXF, representing a single line of text. Its insertion point, height, rotation angle, and alignment are controlled by independent group codes.
TEXT 专属组码TEXT-Specific Group Codes
| 组码Code | 字段名Field Name | 中文说明Description | 类型Type |
|---|---|---|---|
| 1 | Text String | 文字内容字符串The text string itself | String |
| 10 | Insertion Point X | 插入点 X 坐标(默认在左端基线)Insertion point X (default at left baseline) | Double |
| 20 | Insertion Point Y | 插入点 Y 坐标Insertion point Y | Double |
| 30 | Insertion Point Z | 插入点 Z 坐标(可省略,默认 0)Insertion point Z (optional, default 0) | Double |
| 40 | Text Height | 文字高度(沿基线到 ascent 的距离)Text height (baseline to ascent distance) | Double |
| 41 | Relative X Scale Factor | X 方向宽度比例因子(默认 1.0)X scale factor (default 1.0) | Double |
| 50 | Rotation Angle | 旋转角度(度,绕插入点逆时针)Rotation angle in degrees (CCW around insert point) | Double |
| 51 | Oblique Angle | 倾斜角度(度,用于斜体字)Oblique angle in degrees (for italic effect) | Double |
| 7 | Text Style Name | 文字样式名称(如 “Standard”)Text style name (e.g. “Standard”) | String |
| 71 | Text Generation Flags | 文字生成标志(2=倒置, 4=反向)Text generation flags (2=upside-down, 4=backwards) | Int16 |
| 72 | Horizontal Alignment | 水平对齐方式(halign)Horizontal alignment (halign) | Int16 |
| 73 | Vertical Alignment | 垂直对齐方式(valign)Vertical alignment (valign) | Int16 |
| 11 | Alignment Point X | 对齐点 X(当 halign/valign 非默认时有效)Alignment point X (used when halign/valign non-default) | Double |
| 21 | Alignment Point Y | 对齐点 YAlignment point Y | Double |
| 31 | Alignment Point Z | 对齐点 ZAlignment point Z | Double |
| 210 | Extrusion X | 拉伸方向 X(定义文字所在平面)Extrusion direction X (defines text plane) | Double |
| 220 | Extrusion Y | 拉伸方向 YExtrusion direction Y | Double |
| 230 | Extrusion Z | 拉伸方向 Z(默认 0,0,1 即 XY 平面)Extrusion direction Z (default 0,0,1 = XY plane) | Double |
TEXT 水平对齐(halign,组码 72)TEXT Horizontal Alignment (halign, code 72)
| 取值Value | 中文名称Name | 插入点位置说明Insertion Point Description |
|---|---|---|
| 0 | 左对齐(默认)Left (default) | 文字左端位于插入点Left end of text at insertion point |
| 1 | 居中对齐Center | 文字水平中心位于插入点Horizontal center of text at insertion point |
| 2 | 右对齐Right | 文字右端位于插入点Right end of text at insertion point |
| 3 | 对齐(Aligned)Aligned | 文字两端分别对齐到两个点,高度自动调整Text stretched between two points, height auto-adjusted |
| 4 | 布满(Fit)Fit | 文字两端分别对齐到两个点,高度不变Text fitted between two points, height fixed |
| 5 | 基线居中对齐Baseline Center | 基线中点位于插入点(与 1 类似但定位在基线)Baseline midpoint at insertion point |
TEXT 垂直对齐(valign,组码 73)TEXT Vertical Alignment (valign, code 73)
| 取值Value | 中文名称Name | 插入点位置说明Insertion Point Description |
|---|---|---|
| 0 | 基线对齐(默认)Baseline (default) | 插入点位于文字基线Insertion point on text baseline |
| 1 | 底部对齐Bottom | 插入点位于文字最底部(含下伸部)Insertion point at text bottom (including descenders) |
| 2 | 居中对齐Middle | 插入点位于文字垂直中心Insertion point at vertical center |
| 3 | 顶部对齐Top | 插入点位于文字最顶部Insertion point at text top |
当 halign 或 valign 不为 0(默认值)时,组码 11/21/31 定义的文字对齐点生效,而组码 10/20/30 的插入点仅用于定位。若 halign=0 且 valign=0,则对齐点与插入点重合。 When halign or valign is non-zero, the alignment point (codes 11/21/31) takes effect, while codes 10/20/30 serve only for positioning. If both are 0, the alignment point coincides with the insertion point.
MTEXT 实体详解MTEXT Entity in Detail
MTEXT(多行文字)是功能更强大的文字实体,支持多行显示、内嵌格式代码、段落设置和分栏。它的对齐方式由单一的 attachment_point 字段控制。MTEXT (multiline text) is a more powerful entity supporting multiple lines, inline format codes, paragraph settings, and columns. Its alignment is controlled by a single attachment_point field.
MTEXT 专属组码MTEXT-Specific Group Codes
| 组码Code | 字段名Field Name | 中文说明Description | 类型Type |
|---|---|---|---|
| 1 | Text Content | 文字内容(含格式代码,如 \Hxxx; 控制高度)Text content (with format codes like \Hxxx; for height) | String |
| 3 | Additional Text | 附加文字(内容超长时续接,可多个组码 3)Additional text (continuation when content is long, multiple code 3 allowed) | String |
| 10 | Insertion Point X | 插入点 X(默认左上角)Insertion point X (default top-left) | Double |
| 20 | Insertion Point Y | 插入点 YInsertion point Y | Double |
| 30 | Insertion Point Z | 插入点 ZInsertion point Z | Double |
| 40 | Nominal Text Height | 标称文字高度(默认行高)Nominal text height (default line height) | Double |
| 41 | Reference Rectangle Width | 参考矩形宽度(自动换行的边界宽度,0=不换行)Reference rectangle width (wrap boundary, 0=no wrapping) | Double |
| 42 | Reference Rectangle Height | 参考矩形高度Reference rectangle height | Double |
| 43 | Actual Rectangle Height | 实际矩形高度(DXF 输出时填充)Actual rectangle height (filled on DXF output) | Double |
| 50 | Rotation Angle | 旋转角度(度)Rotation angle in degrees | Double |
| 71 | Attachment Point | 附着点 / 对齐方式(1~9,见下表)Attachment point / alignment (1-9, see table below) | Int16 |
| 72 | Drawing Direction | 文字绘制方向(1=左到右, 3=上到下, 5=右到左)Drawing direction (1=L-R, 3=T-B, 5=R-L) | Int16 |
| 73 | Line Spacing Style | 行间距样式(1=至少, 2=精确)Line spacing style (1=at least, 2=exact) | Int16 |
| 44 | Line Spacing Factor | 行间距比例因子(基于文字高度的倍数)Line spacing factor (multiple of text height) | Double |
| 7 | Text Style Name | 文字样式名称Text style name | String |
| 90 | Column Type | 分栏类型(0=无, 1=静态, 2=动态,R2018+)Column type (0=none, 1=static, 2=dynamic, R2018+) | Int32 |
| 210 | Extrusion X | 拉伸方向 XExtrusion direction X | Double |
| 220 | Extrusion Y | 拉伸方向 YExtrusion direction Y | Double |
| 230 | Extrusion Z | 拉伸方向 ZExtrusion direction Z | Double |
MTEXT 附着点(attachment_point,组码 71)MTEXT Attachment Point (code 71)
MTEXT 的 attachment_point 用单个数值 1~9 表示九宫格位置,同时控制水平和垂直对齐:MTEXT’s attachment_point uses a single value 1-9 to represent a 3×3 grid position, controlling both horizontal and vertical alignment simultaneously:
绿色边框 = 默认附着点(左上角在 DXF 中记为 1,但在上图中为了符合视觉习惯将 1 放在左上;注意:标准 DXF 文档中 1=左上, 2=上中, 3=右上, 4=左中, 5=正中, 6=右中, 7=左下, 8=下中, 9=右下)Green border = default attachment point. In standard DXF: 1=Top-Left, 2=Top-Center, 3=Top-Right, 4=Middle-Left, 5=Middle-Center, 6=Middle-Right, 7=Bottom-Left, 8=Bottom-Center, 9=Bottom-Right.
| 取值Value | 英文缩写Abbreviation | 中文名称Name | 插入点相对位置Relative Position |
|---|---|---|---|
| 1 | TL | 左上角(默认)Top-Left (default) | 文字框左上角位于插入点Text box top-left at insertion point |
| 2 | TC | 上中Top-Center | 文字框上边中点位于插入点Text box top-center at insertion point |
| 3 | TR | 右上角Top-Right | 文字框右上角位于插入点Text box top-right at insertion point |
| 4 | ML | 左中Middle-Left | 文字框左边中点位于插入点Text box middle-left at insertion point |
| 5 | MC | 正中心Middle-Center | 文字框几何中心位于插入点Text box geometric center at insertion point |
| 6 | MR | 右中Middle-Right | 文字框右边中点位于插入点Text box middle-right at insertion point |
| 7 | BL | 左下角Bottom-Left | 文字框左下角位于插入点Text box bottom-left at insertion point |
| 8 | BC | 下中Bottom-Center | 文字框下边中点位于插入点Text box bottom-center at insertion point |
| 9 | BR | 右下角Bottom-Right | 文字框右下角位于插入点Text box bottom-right at insertion point |
MTEXT 没有
halign 和 valign 属性。若你的代码同时遍历 TEXT 和 MTEXT,读取 entity.dxf.halign 时遇到 MTEXT 会抛出 AttributeError。必须先判断实体类型:entity.dxftype() == 'TEXT' 时才读取 halign/valign。
MTEXT does not have halign and valign attributes. If your code iterates over both TEXT and MTEXT, reading entity.dxf.halign on an MTEXT will raise AttributeError. Always check entity type first: only read halign/valign when entity.dxftype() == 'TEXT'.
对齐机制对比总表Alignment Mechanism Comparison
TEXT 对齐组合表TEXT Alignment Combination
| 水平对齐 halignhalign (code 72) | 取值Value | 垂直对齐 valignvalign (code 73) | 取值Value | 插入点实际位置Insertion Point Location |
|---|---|---|---|---|
| 左对齐(默认)Left (default) | 0 | 基线对齐(默认)Baseline (default) | 0 | 文字左端基线处(近似左下角)Left end of baseline (approx. lower-left) |
| 居中对齐Center | 1 | 底部对齐Bottom | 1 | 文字水平中心 × 最底部Horizontal center × bottom |
| 右对齐Right | 2 | 居中对齐Middle | 2 | 文字右端 × 垂直中心Right end × vertical center |
| 对齐(Aligned)Aligned | 3 | 顶部对齐Top | 3 | 两端拉伸至指定宽度,高度自动Stretched to width, height auto |
| 布满(Fit)Fit | 4 | – | – | 两端对齐至指定宽度,高度固定Fitted to width, height fixed |
默认基准点对比Default Anchor Point Comparison
TEXT
默认插入点:左端基线处Default insertion: Left end of baseline
文字向右延伸,字符坐落在基线上。下伸部(g, p, y)会向下超出插入点。Text extends to the right, characters sit on baseline. Descenders (g, p, y) extend below insertion point.
对齐字段:halign (72) + valign (73)Alignment fields: halign (72) + valign (73)
MTEXT
默认插入点:左上角Default insertion: Top-left corner
文字框向右下方扩展。插入点始终位于文字框的边界上(由 attachment_point 决定具体哪个角/边)。Text box expands right and down. Insertion point is always on the text box boundary (specific corner/edge determined by attachment_point).
对齐字段:attachment_point (71)Alignment field: attachment_point (71)
如何理解 BBox(边界框)Understanding BBox (Bounding Box)
在 CAD 编程中,BBox(Bounding Box,边界框)是指能够完全包围一个实体的最小轴对齐矩形。对于文字实体,BBox 是计算文字渲染后的实际占据空间所得到的矩形。In CAD programming, BBox (Bounding Box) is the minimum axis-aligned rectangle that fully encloses an entity. For text entities, the BBox is calculated from the actual rendered space occupied by the text.
TEXT 的 BBoxTEXT BBox
TEXT 的 BBox 由以下因素决定:TEXT’s BBox is determined by:
- 文字内容:字符数量决定宽度Text content: character count determines width
- 文字高度(组码 40):决定从基线到 ascent 的距离Text height (code 40): determines baseline-to-ascent distance
- 宽度因子(组码 41):水平缩放比例Width factor (code 41): horizontal scale ratio
- 倾斜角度(组码 51):使 BBox 倾斜Oblique angle (code 51): tilts the BBox
- 旋转角度(组码 50):使整个 BBox 绕插入点旋转Rotation angle (code 50): rotates entire BBox around insertion point
- 文字样式:不同字体的字符宽度不同Text style: different fonts have different character widths
MTEXT 的 BBoxMTEXT BBox
MTEXT 的 BBox 更为复杂,因为:MTEXT’s BBox is more complex because:
- 包含多行文字,高度由行数和行间距决定Contains multiple lines; height depends on line count and spacing
- 每行可能有不同格式(高度、字体、颜色),导致各行高度不同Each line may have different formatting (height, font, color), causing varying line heights
- 自动换行使宽度受限于组码 41(参考矩形宽度)Automatic wrapping limits width to group code 41 (reference rectangle width)
- 内边距和段落间距也会贡献到总高度Padding and paragraph spacing also contribute to total height
在 ezdxf 等库中,
entity.bbox() 或 entity.get_bbox() 通常需要渲染引擎支持(如 matplotlib 或自定义字体测量)。它并非直接读取 DXF 组码,而是根据字体文件动态计算每个字符的轮廓后取并集。因此,没有字体文件时 BBox 计算可能不准确。
In libraries like ezdxf, entity.bbox() or entity.get_bbox() typically requires a rendering engine (like matplotlib or custom font metrics). It does not directly read DXF group codes but dynamically calculates each character’s outline from the font file and takes the union. Without the font file, BBox calculation may be inaccurate.
BBox 的数学表达BBox Mathematical Expression
# 伪代码:BBox 结构
class BBox:
min_x: float # 包围盒左边界
min_y: float # 包围盒下边界
min_z: float # 包围盒前边界(通常文字在 XY 平面,Z=0)
max_x: float # 包围盒右边界
max_y: float # 包围盒上边界
max_z: float # 包围盒后边界
@property
def width(self) -> float:
return self.max_x - self.min_x
@property
def height(self) -> float:
return self.max_y - self.min_y
@property
def center(self) -> Point:
return Point(
(self.min_x + self.max_x) / 2,
(self.min_y + self.max_y) / 2,
(self.min_z + self.max_z) / 2
)
text_size 与 mtext_size 方法text_size and mtext_size Methods
在 CAD 编程库(如 ezdxf)中,text_size 和 mtext_size 是用于计算文字实体渲染尺寸的工具函数。它们帮助开发者预估文字在图纸上的实际占据空间。In CAD programming libraries (like ezdxf), text_size and mtext_size are utility functions for calculating the rendered size of text entities. They help developers estimate the actual space text occupies on the drawing.
text_size 方法text_size Method
text_size 专门用于单行文字(TEXT 实体),其计算逻辑相对简单:text_size is designed for single-line text (TEXT entities) with relatively simple calculation logic:
# ezdxf 风格的 text_size 计算逻辑(示意)
def text_size(text_entity, font_info) -> Tuple[float, float]:
"""
计算 TEXT 实体的渲染宽度和高度。
返回: (width, height)
"""
text = text_entity.dxf.text # 组码 1
height = text_entity.dxf.height # 组码 40
width_factor = text_entity.dxf.width # 组码 41(默认 1.0)
oblique = text_entity.dxf.oblique # 组码 51(倾斜角)
# 1. 获取每个字符在指定字体下的宽度
char_widths = [font_info.get_char_width(c) for c in text]
base_width = sum(char_widths) * height * width_factor
# 2. 倾斜会增加有效宽度
if oblique != 0:
base_width += height * abs(tan(radians(oblique)))
# 3. 高度 = 文字高度 + 下伸部(如果有)
# 注意:不同字体的 ascent/descent 比例不同
actual_height = height * font_info.ascent_ratio
return (base_width, actual_height)
– 文字内容:直接影响宽度
– 文字高度(组码 40):基准高度,字符按比例缩放
– 宽度因子(组码 41):水平方向的额外缩放
– 倾斜角度(组码 51):使文字倾斜,增加有效宽度 – Text content: directly affects width
– Text height (code 40): base height, characters scale proportionally
– Width factor (code 41): additional horizontal scaling
– Oblique angle (code 51): tilts text, increasing effective width
mtext_size 方法mtext_size Method
mtext_size 用于多行文字(MTEXT 实体),计算更复杂,需要解析格式代码:mtext_size is for multiline text (MTEXT entities) and requires more complex calculation involving format code parsing:
# ezdxf 风格的 mtext_size 计算逻辑(示意)
def mtext_size(mtext_entity, font_info) -> Tuple[float, float]:
"""
计算 MTEXT 实体的渲染宽度和高度。
返回: (width, height)
"""
content = mtext_entity.text # 组码 1 + 3 拼接
nominal_height = mtext_entity.dxf.height # 组码 40(标称高度)
rect_width = mtext_entity.dxf.width # 组码 41(参考宽度)
line_spacing = mtext_entity.dxf.line_spacing_factor # 组码 44
# 1. 解析格式代码(如 \H0.5x; 表示高度为标称的 0.5 倍)
formatted_lines = parse_mtext_formatting(content)
# 2. 逐行计算尺寸
max_line_width = 0
total_height = 0
for i, line in enumerate(formatted_lines):
line_height = line.get_height(nominal_height) # 考虑 \H 代码
line_width = calculate_line_width(line, font_info, line_height)
max_line_width = max(max_line_width, line_width)
# 首行不加间距,后续行加行间距
if i > 0:
total_height += line_height * line_spacing
else:
total_height += line_height
# 3. 如果设定了参考宽度,实际宽度 = min(内容宽度, 参考宽度)
actual_width = max_line_width
if rect_width > 0:
actual_width = min(max_line_width, rect_width)
# 若内容被截断换行,高度需要重新计算
if max_line_width > rect_width:
total_height = recalc_wrapped_height(formatted_lines, rect_width)
return (actual_width, total_height)
– 文字内容:需解析内嵌格式代码(\H, \W, \F, \P 等)
– 标称高度(组码 40):默认行高,但每行可被格式代码覆盖
– 参考宽度(组码 41):触发自动换行的阈值,0 表示不换行
– 行间距因子(组码 44):控制行与行之间的间距倍数
– 绘制方向(组码 72):影响宽/高的定义(左到右 vs 上到下) – Text content: requires parsing inline format codes (\H, \W, \F, \P, etc.)
– Nominal height (code 40): default line height, but each line may be overridden by format codes
– Reference width (code 41): threshold for automatic wrapping, 0 means no wrapping
– Line spacing factor (code 44): controls spacing multiplier between lines
– Drawing direction (code 72): affects width/height definitions (L-R vs T-B)
常见 MTEXT 格式代码Common MTEXT Format Codes
| 代码Code | 含义Meaning | 示例Example |
|---|---|---|
\H<value>; | 设置文字高度Set text height | \H2.5; 高度为 2.5height=2.5 |
\H<value>x; | 设置相对高度倍数Set relative height multiplier | \H0.5x; 高度为默认的一半half default height |
\W<value>; | 设置宽度因子Set width factor | \W1.5; 宽度因子 1.5width factor 1.5 |
\F<font>; | 设置字体Set font | \FSimSun; 宋体SimSun font |
\C<index>; | 设置颜色(AutoCAD 颜色索引)Set color (ACI) | \C1; 红色Red |
\P | 段落换行Paragraph break | Line1\PLine2 |
\S<num>^<den>; | 堆叠分数Stacked fraction | \S1^2; 二分之一1/2 |
\L ... \l | 下划线Underline | \Ltext\l |
\O ... \o | 上划线Overline | \Otext\o |
若你的库(如 ezdxf)提供了
text_size() 和 mtext_size(),优先使用库自带方法而非自己计算,因为字体测量和格式解析非常依赖底层实现。如果没有提供,可以使用 matplotlib.font_manager 或 PIL.ImageFont 来近似测量。
If your library (like ezdxf) provides text_size() and mtext_size(), prefer the built-in methods over manual calculation, as font measurement and format parsing depend heavily on underlying implementation. If not available, you can approximate using matplotlib.font_manager or PIL.ImageFont.
常见错误与注意事项Common Errors & Pitfalls
错误 1:对 MTEXT 读取 halign / valignError 1: Reading halign/valign on MTEXT
代码同时遍历 TEXT 和 MTEXT,直接访问
entity.dxf.halign 会在遇到 MTEXT 时抛出 AttributeError,因为 MTEXT 没有这两个属性。
Code iterating over both TEXT and MTEXT and directly accessing entity.dxf.halign will raise AttributeError when encountering MTEXT, because MTEXT does not have these attributes.
# 错误代码 ❌
for entity in msp:
h = entity.dxf.halign # MTEXT 会报错!
v = entity.dxf.valign
# 正确代码 ✅
for entity in msp:
if entity.dxftype() == 'TEXT':
h = entity.dxf.halign
v = entity.dxf.valign
elif entity.dxftype() == 'MTEXT':
att = entity.dxf.attachment_point
错误 2:混淆默认插入点Error 2: Confusing Default Insertion Points
将 TEXT 的插入点当作文字左上角处理,导致文字位置向下偏移一个文字高度;或将 MTEXT 的插入点当作左上角处理时忽略了 attachment_point 的修改。 Treating TEXT’s insertion point as the top-left corner causes a downward offset of one text height; or ignoring attachment_point changes when treating MTEXT’s insertion point as top-left.
错误 3:忽略对齐点(组码 11/21/31)Error 3: Ignoring Alignment Point (codes 11/21/31)
当 TEXT 的 halign 或 valign 非零时,组码 10/20/30 不再是文字的实际定位点,真正的对齐参考点是组码 11/21/31。若只读取插入点而忽略对齐点,渲染位置会偏差。When TEXT’s halign or valign is non-zero, group codes 10/20/30 are no longer the actual positioning point; the true alignment reference is codes 11/21/31. Reading only the insertion point while ignoring the alignment point will cause rendering position errors.
错误 4:MTEXT 内容截断Error 4: MTEXT Content Truncation
MTEXT 的内容超过 250 字符时会被拆分到多个组码 3 中。只读取组码 1 会导致内容不完整,必须按顺序拼接所有组码 3 的值。When MTEXT content exceeds 250 characters, it is split across multiple group code 3 entries. Reading only group code 1 will result in incomplete content; you must concatenate all group code 3 values in order.
# ezdxf 会自动处理拼接,但原生 DXF 解析需注意
# 正确读取 MTEXT 内容的逻辑:
content = code_1_value
for extra in all_code_3_values:
content += extra
1. 始终通过
entity.dxftype() 判断实体类型后再访问特定属性2. TEXT 渲染时同时考虑插入点和对齐点,检查 halign/valign 是否为 0
3. MTEXT 读取时拼接组码 1 和所有组码 3 的内容
4. 计算 BBox 时确保字体文件可用,否则使用近似值
5. 处理旋转文字时,BBox 需要在旋转后重新计算(或计算旋转后的角点) 1. Always check
entity.dxftype() before accessing entity-specific attributes2. When rendering TEXT, consider both insertion and alignment points; check if halign/valign are 0
3. When reading MTEXT, concatenate code 1 with all code 3 values
4. Ensure font files are available when calculating BBox; otherwise use approximations
5. For rotated text, BBox must be recalculated after rotation (or compute rotated corner points)
DXF TEXT & MTEXT 实体参考 | 基于 AutoCAD DXF Reference 与 ezdxf 文档编制DXF TEXT & MTEXT Entity Reference | Based on AutoCAD DXF Reference and ezdxf documentation
