如何在Autodesk APS中根据选择更新dbids
解决方案
要实现「无选择时显示全部数据,有选择/隔离操作时仅显示对应项」的需求,核心是在更新数据网格时优先使用当前选中的dbid,无选中时再加载全部叶子节点,同时需要监听Viewer的选择变化事件来触发网格更新。具体修改如下:
1. 修改DataGridExtension.js
需要添加选择事件监听,并调整update方法的逻辑:
// 在扩展构造函数中绑定事件处理函数 constructor(viewer, options) { super(viewer, options); this._onSelectionChanged = this._onSelectionChanged.bind(this); } initialize() { super.initialize(); // 监听选择变化事件 this.viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, this._onSelectionChanged); } onModelLoaded(model) { super.onModelLoaded(model); if (this._panel && this._panel.isVisible()) { this.update(); } } async update() { // 先获取当前选中的dbid数组 const selectedDbids = this.viewer.getSelection(); let dbids; if (selectedDbids.length > 0) { // 有选中项时,使用选中的dbid dbids = selectedDbids; } else { // 无选中时,加载全部叶子节点 dbids = await this.findLeafNodes(this.viewer.model); } this._panel.update(this.viewer.model, dbids); } // 选择变化时自动更新网格 _onSelectionChanged() { if (this._panel && this._panel.isVisible()) { this.update(); } } // 扩展销毁时移除事件监听,避免内存泄漏 unload() { this.viewer.removeEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, this._onSelectionChanged); super.unload(); }
2. DataGridPanel.js的可选调整
原update方法可保留,若需处理空数据场景,可添加简单判断:
update(model, dbids) { if (!dbids || dbids.length === 0) { // 无数据时清空网格 this.table.replaceData([]); return; } model.getBulkProperties(dbids, { propFilter: DATAGRID_CONFIG.requiredProps }, (results) => { this.table.replaceData(results.map((result) => DATAGRID_CONFIG.createRow(result.dbId, result.name, result.properties))); }, (err) => { console.error(err); }); }
关键逻辑说明
- 通过
viewer.getSelection()获取当前选中元素的dbid数组,有选中时优先使用该数组加载属性 - 监听
SELECTION_CHANGED_EVENT事件,每次选择状态变化时自动触发网格更新 - 扩展销毁时移除事件监听,避免内存泄漏
内容的提问来源于stack exchange,提问作者김태헌
相关产品推荐
相关产品推荐

