颜色、图层与样式
颜色设置、图层管理、线型、线宽、透明度、扩展数据。
颜色索引(ACI)
entity.color = 1; // 红色
entity.color = 2; // 黄色
entity.color = 3; // 绿色
entity.color = 4; // 青色
entity.color = 5; // 蓝色
entity.color = 6; // 洋红
entity.color = 7; // 白色
entity.color = 256; // 随层(ByLayer)
entity.color = 0; // 随块(ByBlock)
RGB 真彩色
const { ColorConverter } = vjcad;
// 十六进制字符串
entity.color = ColorConverter.createRgbColorIndexFromHexStr("#FFD700");
// RGB 分量
entity.color = ColorConverter.createRgbColorIndexFromComponents(255, 192, 203);
// 十六进制数值
entity.color = ColorConverter.createRgbColorIndex(0x800080);
系统当前颜色
const { Engine } = vjcad;
Engine.CECOLOR = 1; // 设置当前绘图颜色
entity.color = Engine.CECOLOR; // 使用当前颜色
图层管理
创建图层
const { Engine } = vjcad;
// 方式1:Engine.createLayer(推荐)
const layer = Engine.createLayer("标注层", {
color: 1,
lineType: "CONTINUOUS",
layerOn: true,
isFrozen: false,
isLocked: false
});
// 方式2:doc.layers.addlayer
const doc = Engine.currentDoc;
if (!doc.layers.itemByName("辅助线层")) {
doc.layers.addlayer("辅助线层", true, 3);
}
// 方式3:自动命名
const autoLayer = Engine.createLayer(); // "图层1", "图层2" ...
切换图层
const { Engine } = vjcad;
Engine.setCurrentLayer("图层A"); // 切换到指定图层
const currentLayer = Engine.getCurrentLayer(); // 获取当前图层
获取图层
const { Engine } = vjcad;
const layers = Engine.getLayers(); // 所有图层
const layer = Engine.getLayerByName("标注层"); // 按名称获取
const exists = !!Engine.currentDoc.layers.itemByName("建筑层"); // 检查是否存在
图层可见性/冻结/锁定
const layer = Engine.getLayerByName("图层名");
layer.layerOn = false; // 关闭(不可见,参与重生成)
layer.isFrozen = true; // 冻结(不可见,不参与重生成)
layer.isLocked = true; // 锁定(可见但不可编辑)
Engine.regen(true); // 必须重绘
| 状态 | 可见 | 可选择 | 可编辑 | 参与重生成 |
|---|---|---|---|---|
| 正常 | ✓ | ✓ | ✓ | ✓ |
| 关闭 | ✗ | ✗ | ✗ | ✓ |
| 冻结 | ✗ | ✗ | ✗ | ✗ |
| 锁定 | ✓ | ✗ | ✗ | ✓ |
按图层获取实体
const buildingEnts = Engine.getEntities(ent => ent.layer === "建筑层");
线型设置
entity.lineType = "CONTINUOUS"; // 实线(默认)
entity.lineType = "HIDDEN"; // 虚线
entity.lineType = "CENTER"; // 中心线
entity.lineType = "PHANTOM"; // 幻影线
entity.lineType = "DASHED"; // 短划线
entity.lineTypeScale = 1.0; // 线型比例(0.5 更密,2.0 更稀)
系统当前线型:
Engine.CELTYPE = "HIDDEN";
Engine.CELTSCALE = 1.5;
线宽设置
entity.lineWeight = -1; // 默认(随层 ByLayer;构造即为 -1,setDefaults 不改它)
entity.lineWeight = 0; // 0.00mm(最细)
entity.lineWeight = 15; // 0.15mm
entity.lineWeight = 30; // 0.30mm
entity.lineWeight = 50; // 0.50mm
entity.lineWeight = 100; // 1.00mm
entity.lineWeight = -2; // 随块
透明度
const { CircleEnt } = vjcad;
const circle = new CircleEnt([50, 50], 25);
circle.setDefaults();
circle.transpMgr.setTp100(50); // 0=不透明,有效范围 0–90(>90 被钳到 90,无法完全透明;隐藏用 visible=false)
circle.transpMgr.setValue(-1); // 随层透明度
扩展数据(XData)
用于存储自定义数据到实体。
设置
entity.setExtData("MY_APP", {
name: "温度传感器",
value: 25.5,
unit: "℃",
config: { minValue: 0, maxValue: 100 }
});
读取
if (entity.hasExtData()) {
const appName = entity.extDataAppName; // "MY_APP"
const data = entity.extData;
console.log(data.name); // "温度传感器"
console.log(data.value); // 25.5
}
修改
const data = entity.extData;
data.value = 30.2;
entity.setExtData("MY_APP", data);
清除
entity.clearExtData();
批量查询
const withXData = Engine.getEntities(ent => ent.hasExtData?.());
const sensors = Engine.getEntities(ent =>
ent.hasExtData?.() && ent.extDataAppName === "SENSOR_DATA"
);
注意:扩展数据支持导出到 DWG 并重新加载,数据会保留。