hatch-solid
本文回答:
HatchEnt是图案填充,patternName选图案(如ANSI31),边界用Edge / Edges + EdgeType.Polyline + bulgePoints描述,最后hatch.setLoops(edges);SolidEnt是实体填充,new SolidEnt(p1, p2, p3, p4)四点构造(顺序按四边形)。
一、HatchEnt(图案填充)
import {
Engine, HatchEnt, PolylineEnt, Point2D,
Edge, Edges, EdgeType
} from 'vjcad';
// 1. 先准备一条闭合多段线作为边界
const boundary = new PolylineEnt();
boundary.setPoints([[-30, -20], [30, -20], [30, 25], [-30, 25]]);
boundary.isClosed = true;
boundary.setDefaults();
// 2. 创建 HatchEnt 并配置图案 + 边界
const hatch = new HatchEnt();
hatch.patternName = 'ANSI31';
const edge = new Edge();
edge.edgeType = EdgeType.Polyline;
edge.bulgePoints = boundary.bulgePoints.clone();
const edges = new Edges();
edges.add(edge);
hatch.setLoops(edges);
hatch.setDefaults();
hatch.color = 4;
Engine.addEntities([boundary, hatch]);
MCP 参考:vjcad/example/entity/all/01entities.js、vjcad/example/entity/hatch/01create.js ~ 04custom.js。
二、HatchEnt 关键 API
| 属性 / 方法 | 说明 | 来源 |
|---|---|---|
patternName |
图案名(ANSI31 / ANSI37 / SOLID / 自定义...) |
vjcad/example/entity/all/01entities.js |
setLoops(edges) |
设置边界(多个回路) | 同上 |
Edge / Edges |
边界类型 | 同上 |
EdgeType.Polyline |
多段线边界 | 同上 |
bulgePoints.clone() |
从 PolylineEnt 拷贝边界 | 同上 |
更多图案(vjcad/example/entity/hatch/02patterns.js 列了所有内置图案),自定义图案 vjcad/example/entity/hatch/04custom.js,多种边界写法 vjcad/example/entity/hatch/03boundary.js。
三、SolidEnt(实体填充)
const solid = new SolidEnt(
[-30, -20],
[ 30, -20],
[ 20, 25],
[-20, 25]
);
solid.setDefaults();
solid.color = 1;
Engine.addEntities(solid);
MCP 参考:vjcad/example/entity/all/01entities.js、vjcad/example/entity/solid/01create.js。
注意 SolidEnt 的四个点顺序按四边形顺序,否则会出现"沙漏形" / 自相交。三角形可重复第三第四点(p3 === p4),见 vjcad/example/entity/solid/02arrow.js 的箭头示例。
四、SolidEnt vs HatchEnt 选择
| 想要的 | 用 |
|---|---|
| 简单四边形 / 三角形 / 箭头实色填充 | SolidEnt |
| 任意多边形 / 带圆弧的边界 / 图案(斜线 / 网格 / 自定义图案) | HatchEnt |
| 大区域填充性能更好 | HatchEnt(SOLID 图案) |
反例
- 给
HatchEnt设了patternName但没setLoops(edges):渲染不出来。 SolidEnt四点顺序写错:得到沙漏形,不是预期的填充。- 修改
boundary的点之后没重建hatch.setLoops(...):填充与边界不一致。
参考(官方文档 MCP)
- 文档:
vjcad/doc/entity/hatch-ent.md、vjcad/doc/entity/solid-ent.md - 示例:
vjcad/example/entity/all/01entities.js、vjcad/example/entity/hatch/01create.js~04custom.js、vjcad/example/entity/solid/01create.js~03transform.js