EntityStore API
大约 2 分钟
EntityStore API
EntityStore 是实体数据的中心仓库,负责增删查改、选择集管理、空间索引和绘图顺序控制。
通过 cadLayer.store 访问实例。
CRUD 方法
add
add(entity: EntityBase): void添加实体到存储,自动更新空间索引并触发 entity:added 事件。
| 参数 | 类型 | 说明 |
|---|---|---|
entity | EntityBase | 实体实例 |
delete
delete(id: string): void按 ID 删除实体,触发 entity:deleted 事件。
| 参数 | 类型 | 说明 |
|---|---|---|
id | string | 实体 ID |
getById
getById(id: string): EntityBase | undefined返回: 对应 ID 的实体,不存在时返回 undefined。
getAll
getAll(): EntityBase[]返回: 所有实体数组(按绘图顺序排列)。
getAllIds
getAllIds(): string[]返回: 所有实体 ID 数组。
选择集
select
select(id: string): void将指定实体加入选择集,触发 selection:changed 事件。
deselect
deselect(id: string): void将指定实体从选择集中移除。
toggleSelect
toggleSelect(id: string): void切换指定实体的选择状态。
clearSelection
clearSelection(): void清除所有选择。
getSelectedIds
getSelectedIds(): string[]返回: 当前选中实体的 ID 数组。
getSelectedEntities
getSelectedEntities(): EntityBase[]返回: 当前选中的实体数组。
isSelected
isSelected(id: string): boolean返回: 指定实体是否处于选中状态。
示例:
const store = cadLayer.store;
store.select(entity.id);
console.log(store.isSelected(entity.id)); // true
console.log(store.getSelectedIds()); // [entity.id]
store.clearSelection();绘图顺序
moveToFront
moveToFront(ids: string[]): void将指定实体移至最前(最后绘制,覆盖其他实体)。
moveToBack
moveToBack(ids: string[]): void将指定实体移至最后(最先绘制,被其他实体覆盖)。
moveUp
moveUp(ids: string[]): void将指定实体上移一层。
moveDown
moveDown(ids: string[]): void将指定实体下移一层。
脏标记
setDirty
setDirty(): void标记存储为"脏",在下一帧触发渲染管线更新。当直接修改实体属性后需手动调用。
属性
spatialIndex
readonly spatialIndex: SpatialIndexR-tree 空间索引实例,用于高效的空间查询(如框选、碰撞检测)。
示例:
const hits = cadLayer.store.spatialIndex.search({
minX: 0, minY: 0,
maxX: 100, maxY: 100,
});