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

Forge Viewer多模型场景下设置viewer.select()默认模型的方法咨询

Solution to Force viewer.select() to Target the First Loaded Model

Great question! You don’t have to rewrite large chunks of your code to switch to model.selector.select()—there’s a straightforward way to make viewer.select() always operate on your first loaded model, using the method’s built-in options parameter. Here’s how to do it:

Step 1: Capture the First Loaded Model Instance

First, you need to store a reference to the primary (first loaded) model so you can reference it later. You can do this either when loading the model directly, or by listening to the viewer’s load completion events:

Option A: Capture during model loading

If you’re explicitly loading the models via code, save the primary model reference when its load promise resolves:

let primaryModel;

// Load primary model first
viewer.loadModel('/path/to/primary-model.svf')
  .then(model => {
    primaryModel = model;
    // Load reference models afterward (keep current models active)
    return viewer.loadModel('/path/to/reference-model.svf', { keepCurrentModels: true });
  })
  .catch(err => console.error('Model load error:', err));

Option B: Capture via viewer event

If models are loaded through other workflows (like the viewer’s default UI), listen for the AGGREGATE_FINISHED event to grab the first model once all loading is done:

let primaryModel;

viewer.addEventListener(Autodesk.Viewing.AGGREGATE_FINISHED, () => {
  const allModels = viewer.getModels();
  if (allModels.length > 0) {
    primaryModel = allModels[0];
    // Remove the listener if you don't need it anymore
    viewer.removeEventListener(Autodesk.Viewing.AGGREGATE_FINISHED, arguments.callee);
  }
});

Step 2: Modify viewer.select() Calls to Target the Primary Model

The viewer.select() method accepts an optional second parameter (options) where you can explicitly specify which model to operate on. By passing your primaryModel reference here, you override the default behavior (which uses the viewer’s currently active model):

// Original call (may target wrong model)
viewer.select(yourDbIdsList);

// Modified call (forces selection on primary model)
viewer.select(yourDbIdsList, { model: primaryModel });

Step 3: (Optional) Wrap in a Helper Function for Minimal Code Changes

To avoid updating every single viewer.select() call in your codebase, create a simple helper function that encapsulates this logic:

function selectPrimaryModel(dbIds) {
  if (!primaryModel) {
    console.warn('Primary model not yet loaded');
    return;
  }
  viewer.select(dbIds, { model: primaryModel });
}

Then just replace all instances of viewer.select(...) with selectPrimaryModel(...)—this cuts down on repetitive changes.

Why This Works

By default, viewer.select() uses the viewer’s activeModel property, which switches to whichever model the user clicks or interacts with. By explicitly setting the model option in the select call, you bypass this default and force the operation to run on your first loaded model every time.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:34:56