多行文字实体 (MTextEnt)
大约 3 分钟
多行文字实体 (MTextEnt)
MTextEnt 表示 CAD 中的多行文字实体,支持多行文本和简单格式化。
概述
多行文字可以包含多行文本,支持自动换行、文字样式和简单的格式控制代码。适合添加较长的说明文字、表格等。
构造函数
import { MTextEnt, Point2D } from 'vjcad';
// 创建多行文字
const mtext = new MTextEnt();
mtext.insertionPoint = [100, 100]; // 支持数组写法
mtext.text = '这是第一行\n这是第二行\n这是第三行';
mtext.width = 100;
mtext.height = 10;
mtext.setDefaults();简化写法
insertionPoint 属性支持 [x, y] 数组形式的坐标。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
insertionPoint | Point2D | 插入点(根据 attachment 确定位置) |
text | string | 文字内容(textString 的别名) |
textString | string | 文字内容(支持换行符) |
contents | string | 富文本内容(含格式代码) |
maxWidth | number | 文字框宽度 |
height | number | 文字高度 |
rotation | number | 旋转角度(弧度) |
textAttachment | number | 附着点位置 |
rowSpcFac | number | 行间距因子 |
styleName | string | 文字样式名称 |
textStyle | TextStyle | 文字样式对象(只读,设置 styleName 后自动获取) |
附着点位置
1 --- 2 --- 3
| | |
4 --- 5 --- 6
| | |
7 --- 8 --- 9| 值 | 位置 |
|---|---|
| 1 | 左上 |
| 2 | 中上 |
| 3 | 右上 |
| 4 | 左中 |
| 5 | 正中 |
| 6 | 右中 |
| 7 | 左下 |
| 8 | 中下 |
| 9 | 右下 |
格式控制代码
| 代码 | 说明 |
|---|---|
\n | 换行 |
\P | 段落换行 |
\\ | 反斜杠 |
\{ | 左花括号 |
\} | 右花括号 |
示例
使用字体
import { Engine, MTextEnt } from 'vjcad';
// 方式一:直接使用字体名作为 styleName(推荐)
// MainView 会自动为配置的字体创建同名的 TextStyle
const mtext1 = new MTextEnt();
mtext1.insertionPoint = [0, 100];
mtext1.text = '使用楷体字体';
mtext1.height = 10;
mtext1.styleName = "simkai"; // 直接使用字体名
mtext1.setDefaults();
// 使用 SHX 字体
const mtext2 = new MTextEnt();
mtext2.insertionPoint = [0, 80];
mtext2.text = 'SHX Font Text';
mtext2.height = 10;
mtext2.styleName = "_default"; // 直接使用字体名
mtext2.setDefaults();
Engine.addEntities([mtext1, mtext2]);提示
字体需要在 MainView 初始化时配置。详见 文本实体-字体配置。
创建多行说明文字
import { Engine, MTextEnt, Point2D } from 'vjcad';
const mtext = new MTextEnt();
mtext.position = new Point2D(50, 150);
mtext.text = '项目名称:WebCAD 示例\n' +
'日期:2024-01-01\n' +
'设计者:开发团队';
mtext.width = 120;
mtext.height = 8;
mtext.attachment = 1; // 左上对齐
mtext.setDefaults();
Engine.addEntities(mtext);创建居中标题
import { Engine, MTextEnt, Point2D } from 'vjcad';
const title = new MTextEnt();
title.position = new Point2D(100, 200);
title.text = '设计图纸';
title.width = 100;
title.height = 15;
title.attachment = 2; // 中上对齐
title.setDefaults();
Engine.addEntities(title);创建带边框的说明
import { Engine, MTextEnt, PolylineEnt, BulgePoints, BulgePoint, Point2D } from 'vjcad';
function drawTextBox(text: string, x: number, y: number, width: number, padding: number = 5) {
// 创建文字
const mtext = new MTextEnt();
mtext.position = new Point2D(x + padding, y - padding);
mtext.text = text;
mtext.width = width - 2 * padding;
mtext.height = 8;
mtext.attachment = 1; // 左上
mtext.setDefaults();
// 估算高度(简化:每行约 10 单位)
const lines = text.split('\n').length;
const height = lines * 12 + 2 * padding;
// 创建边框
const points = new BulgePoints();
points.add(new BulgePoint(new Point2D(x, y), 0));
points.add(new BulgePoint(new Point2D(x + width, y), 0));
points.add(new BulgePoint(new Point2D(x + width, y - height), 0));
points.add(new BulgePoint(new Point2D(x, y - height), 0));
const border = new PolylineEnt(points);
border.isClosed = true;
border.setDefaults();
Engine.addEntities([border, mtext]);
}
drawTextBox('注意事项:\n1. 所有尺寸单位为毫米\n2. 未注公差按 GB/T 1804-m', 0, 0, 150);