Autodesk Forge:查看器扩展无法使用 .getExternalIdMapping()

Autodesk Forge: Viewer Extension cant use .getExternalIdMapping()

class IBSProgressExtension extends Autodesk.Viewing.Extension{
    constructor(viewer, options) {
        super(viewer, options);
    }
    load() {
        //For proof of concept project, I will simply store the externalIds here in a variable.
        const allExternalIds = [
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-0006879a',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-000688ee',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068961',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068963',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a78',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a0d',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a0f',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a11',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a13',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c2f',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c31',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c33',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b2e',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b30',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b32',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b34',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3e',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b36',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b38',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3a',
            '8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3c'
        ];

        this.viewer.model.getExternalIdMapping(data => onSuccessMapping(data));

        function onSuccessMapping(data) {
            const resArray = [];
            allExternalIds.forEach(externalId => {
                if (data[externalId]) resArray.push(data[externalId], externalId);
            });
            console.log(resArray);
        };

        console.log('IBSProgressExtension is loaded.');
        return true;
    }
};


Autodesk.Viewing.theExtensionManager.registerExtension("IBSProgressExtension", IBSProgressExtension);

请查看我的扩展,请帮我弄清楚为什么会这样。 每次我 运行 它时,devtools 都会记录:ViewerExtension.js:31 Uncaught TypeError: Cannot read properties of undefined (reading 'getExternalIdMapping').

请测试一下,看看是否有帮助。我试图从您的评论中合并项目以帮助您构建它。

class IBSProgressExtension extends Autodesk.Viewing.Extension {
  constructor(viewer, options) {
    super(viewer, options);
    this._externalIds = null;
    //Eventually will want to pass in your external IDs to this function, I assume:
    //this._externalIds = options.externalIds
  
    this._doStuff = () => {
      this.startDoingStuff();
    };
  }
  load() {
    console.log("loading extension");
    //For now, hard coded like your example.
    this._externalIds = [
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-0006879a",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-000688ee",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068961",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068963",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a78",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a0d",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a0f",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a11",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068a13",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c2f",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c31",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068c33",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b2e",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b30",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b32",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b34",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3e",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b36",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b38",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3a",
      "8a00f4c7-0709-4749-88b6-abb0ddccf965-00068b3c",
    ];
    //Not sure if this is truly the right event, but it worked when I tested on mine.
    this.viewer.addEventListener(Autodesk.Viewing.MODEL_LAYERS_LOADED_EVENT, this._doStuff);
  }
  startDoingStuff() {
    console.log("startDoingStuff executing");
    this.getDbIds(this._externalIds).then((CombinedArray) => {
      this.setCustomColors(CombinedArray);
    });
  }
  setCustomColors(arrayOfIDs) {
    console.log("setCustomColors executing");
    var somecolor = "#7D5B51";
    var threecolor = new THREE.Color(somecolor);
    var vectcolor = new THREE.Vector4(threecolor.r, threecolor.g, threecolor.b, 1);

    arrayOfIDs.forEach((e) => {
      this.viewer.setThemingColor(e[0], vectcolor, this.viewer.getVisibleModels()[0]);
    });
  }
  getDbIds(externalIds) {
    console.log("getDbIds executing");
    return new Promise((resolve) => {
      this.viewer.model.getExternalIdMapping((d) => {
        //console.log("getDbIdFromExternalId Executed");
        let responseArr = [];
        externalIds.forEach((externalId) => {
          if (d[externalId]) responseArr.push([d[externalId], externalId]);
        });
        console.log("resolving", responseArr);
        resolve(responseArr);
      });
    });
  }
}

Autodesk.Viewing.theExtensionManager.registerExtension("IBSProgressExtension", IBSProgressExtension);

关于这一点,我在这个阶段试图实现 3 件事。

  1. Mongodb 中获取 externalIds
  2. 将外部 ID 与从 getExternalIdMapping() 获得的外部 ID 进行比较。
  3. 获得 DbIds 个匹配项。

通过实现 2 和 3 可以放在 .loadDocumentNode() 之后的 a.then() 中解决了这些问题。

扩展模型之前加载,因此getExternalIdMapping()方法还没有模型属性。为了处理这种情况,我们通常建议使用 查看器事件 例如 Autodesk.Viewing.GEOMETRY_LOADED_EVENT 来“捕捉”模型可用的时刻。最好等待事件发生。这将在 model/drawing 完成加载时触发。

而不是:

this.viewer.model.getExternalIdMapping(data => onSuccessMapping(data));

试试这个:

        this.viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, (x) => {
            this.viewer.model.getExternalIdMapping(data => onSuccessMapping(data));
        });