Autodesk Viewer扩展:removeProperty()失效及属性分类管理问题
问题1:属性移除后无法重新添加的解决方法
原因分析
你调用removeProperty时传入了三个参数(属性名、属性值、分类名),但Autodesk Viewer的PropertyPanel.removeProperty方法仅接受两个参数:属性名和分类名。错误的参数传递导致属性并未被真正移除,因此后续调用addProperty时会因属性已存在而返回false。
修正代码
将移除属性的逻辑改为正确的参数传递方式:
onPropertyClick(property, event) { if (property.name !== "Sensor A") return; // 仅处理Sensor A的点击事件 this.active = !this.active; const propName = "test"; const categoryName = "category 1"; if (this.active) { this.addProperty(propName, "value", categoryName); } else { // 正确调用removeProperty:仅传入属性名和分类名 this.removeProperty(propName, categoryName); } }
问题2:移除空分类的方法
Autodesk Viewer的PropertyPanel未提供公开的分类移除方法,但可以通过访问内部的_categories映射实现该功能:
实现步骤
- 移除属性后,检查对应分类的属性列表是否为空
- 若分类已空,从
_categories中删除该分类 - 调用
refresh()方法更新面板UI
完整代码示例
onPropertyClick(property, event) { if (property.name !== "Sensor A") return; this.active = !this.active; const propName = "test"; const categoryName = "category 1"; if (this.active) { this.addProperty(propName, "value", categoryName); } else { this.removeProperty(propName, categoryName); // 检查分类是否已无属性 const category = this._categories.get(categoryName); if (category && category.properties.size === 0) { // 删除空分类 this._categories.delete(categoryName); // 刷新面板同步UI变化 this.refresh(); } } }
注意事项
_categories是Viewer的内部属性(以下划线开头),当前版本可正常访问,但未来版本可能存在兼容性变化,使用时需留意。- 必须调用
refresh(),否则面板UI不会同步更新空分类的移除状态。
内容的提问来源于stack exchange,提问作者FunkyPenguin
相关产品推荐
相关产品推荐

