单行文字实体 (TextEnt)
大约 3 分钟
单行文字实体 (TextEnt)
TextEnt 表示 CAD 中的单行文字实体,用于添加简单的文字标注。
在线示例
| 示例 | 描述 | 链接 |
|---|---|---|
| 单行文字 | TextEnt 基础与属性示例 | 在线演示{target="_blank"} |
| 文字样式 | 设置文字的各种样式属性 | 在线演示{target="_blank"} |
| 字体设置 | TrueType 字体与 SHX 字体示例 | 在线演示{target="_blank"} |
概述
单行文字是最基本的文字元素,适合添加简短的标签、尺寸值等。每个 TextEnt 只包含一行文字。
构造函数
import { TextEnt, TextAlignmentEnum } from 'vjcad';
// 方式一:逐属性设置(推荐)
const text1 = new TextEnt();
text1.insertionPoint = [100, 100]; // 支持数组形式
text1.text = "WebCAD";
text1.height = 10;
text1.setDefaults();
// 方式二:构造函数参数
const text2 = new TextEnt([100, 100], "Hello", 10, 0, TextAlignmentEnum.Left);
text2.setDefaults();属性
| 属性 | 类型 | 说明 |
|---|---|---|
insertionPoint | Point2D | 文字插入点(支持 [x, y] 数组形式) |
text | string | 文字内容(textString 的别名) |
textString | string | 文字内容 |
height | number | 文字高度 |
rotation | number | 旋转角度(弧度) |
textAlignment | TextAlignmentEnum | 对齐方式 |
font | string | 字体名称 |
styleName | string | 文字样式名称 |
textStyle | TextStyle | 文字样式对象(只读,设置 styleName 后自动获取) |
对齐方式 (TextAlignmentEnum)
| 枚举值 | 值 | 说明 |
|---|---|---|
TextAlignmentEnum.Left | 1 | 左下对齐(基线) |
TextAlignmentEnum.Center | 2 | 中下对齐(基线) |
TextAlignmentEnum.Right | 3 | 右下对齐(基线) |
TextAlignmentEnum.MidLeft | 4 | 左中对齐 |
TextAlignmentEnum.MidCenter | 5 | 中心对齐 |
TextAlignmentEnum.MidRight | 6 | 右中对齐 |
TextAlignmentEnum.TopLeft | 7 | 左上对齐 |
TextAlignmentEnum.TopCenter | 8 | 中上对齐 |
TextAlignmentEnum.TopRight | 9 | 右上对齐 |
示例
使用字体
import { Engine, TextEnt } from 'vjcad';
// 方式一:直接使用字体名作为 styleName(推荐)
// MainView 会自动为配置的字体创建同名的 TextStyle
const text1 = new TextEnt();
text1.insertionPoint = [0, 100];
text1.text = "使用楷体字体";
text1.height = 10;
text1.styleName = "simkai"; // 直接使用字体名
text1.setDefaults();
// 使用 SHX 字体
const text2 = new TextEnt();
text2.insertionPoint = [0, 80];
text2.text = "SHX Font Text";
text2.height = 10;
text2.styleName = "_default"; // 直接使用字体名
text2.setDefaults();
Engine.addEntities([text1, text2]);提示
字体需要在 MainView 初始化时配置。详见 文本实体-字体配置。
创建基本文字
import { Engine, TextEnt } from 'vjcad';
const text = new TextEnt();
text.insertionPoint = [0, 100]; // 支持数组形式
text.text = "基础单行文字";
text.height = 10;
text.setDefaults();
Engine.addEntities(text);创建旋转文字
import { Engine, TextEnt } from 'vjcad';
const text = new TextEnt();
text.insertionPoint = [100, 100];
text.text = "旋转文字";
text.height = 8;
text.rotation = Math.PI / 4; // 45°
text.setDefaults();
Engine.addEntities(text);不同对齐方式
import { Engine, TextEnt, TextAlignmentEnum } from 'vjcad';
const alignments = [
{ align: TextAlignmentEnum.Left, name: "Left(1)", y: 50 },
{ align: TextAlignmentEnum.Center, name: "Center(2)", y: 40 },
{ align: TextAlignmentEnum.Right, name: "Right(3)", y: 30 },
{ align: TextAlignmentEnum.MidLeft, name: "MidLeft(4)", y: 20 },
{ align: TextAlignmentEnum.MidCenter, name: "MidCenter(5)", y: 10 },
{ align: TextAlignmentEnum.MidRight, name: "MidRight(6)", y: 0 },
];
alignments.forEach(({ align, name, y }) => {
const text = new TextEnt();
text.insertionPoint = [100, y];
text.text = name;
text.height = 6;
text.textAlignment = align;
text.setDefaults();
Engine.addEntities(text);
});设置颜色
import { Engine, TextEnt } from 'vjcad';
const text = new TextEnt();
text.insertionPoint = [0, 0];
text.text = "颜色文字";
text.height = 8;
text.setDefaults();
text.color = 1; // 颜色必须在 setDefaults() 之后设置
Engine.addEntities(text);绘制尺寸标注
import { Engine, TextEnt, LineEnt } from 'vjcad';
function drawDimensionText(p1: [number, number], p2: [number, number], offset: number = 10) {
// 计算长度
const dx = p2[0] - p1[0];
const dy = p2[1] - p1[1];
const length = Math.sqrt(dx * dx + dy * dy);
// 计算角度
const angle = Math.atan2(dy, dx);
// 计算中点
const midX = (p1[0] + p2[0]) / 2;
const midY = (p1[1] + p2[1]) / 2;
// 创建尺寸线
const line = new LineEnt(p1, p2);
line.setDefaults();
// 创建尺寸文字
const text = new TextEnt();
text.insertionPoint = [
midX - offset * Math.sin(angle),
midY + offset * Math.cos(angle)
];
text.text = length.toFixed(2);
text.height = 5;
text.rotation = angle;
text.setDefaults();
Engine.addEntities([line, text]);
}
drawDimensionText([0, 0], [100, 50]);