You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多模型加载时如何在Forge Viewer中正确获取选中元素属性

Fixing Forge Viewer getProperties for Multi-Model Selections

Ah, I’ve run into this exact issue before! The problem here is that when you call viewer.getProperties() without specifying a model, it defaults to using the active model—which is almost always the last one you loaded into the viewer. That’s why only elements from your final model return properties correctly.

The Solution: Use the Model Reference from the Selection Event

The AGGREGATE_SELECTION_CHANGED_EVENT gives you all the info you need to target the right model. The event’s detail.selections array contains objects that link each selected dbId directly to its parent model. Here’s how to adjust your code:

First, update your onSelectionBinded handler to iterate over each selection entry, and fetch properties using the associated model:

onSelectionBinded(event) {
  // Get all selected items from the event
  const selectedItems = event.detail.selections;
  
  selectedItems.forEach(item => {
    const targetModel = item.model;
    const targetDbId = item.dbId;
    
    // Use the specific model to get properties instead of the viewer instance
    targetModel.getProperties(targetDbId, (propsResult) => {
      // Do something with the properties here
      console.log(`Properties for dbId ${targetDbId}:`, propsResult);
      // Example: Access a specific property
      const partNumber = propsResult.properties.find(p => p.displayName === "Part Number")?.displayValue;
    }, (error) => {
      console.error(`Failed to get properties for dbId ${targetDbId}:`, error);
    });
  });
}

Alternative: Use viewer.getProperties with the Model Parameter

If you prefer to stick with viewer.getProperties(), you can pass the target model as the optional fourth argument:

this.viewer.getProperties(targetDbId, (propsResult) => {
  // Handle properties
}, (error) => {
  // Handle error
}, targetModel); // Pass the model here to override the active model

Why This Works

Each model loaded into Forge Viewer maintains its own set of dbIds (they aren’t unique across models). By using the model reference from the selection event, you ensure you’re querying the correct model’s database for the selected element’s properties.

内容的提问来源于stack exchange,提问作者DGroves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:59:48