简体   繁体   English

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);

Please have a look at my extension and please help me figure out why is this happening.请查看我的扩展,请帮我弄清楚为什么会这样。 Every time i run it, the devtools logs: ViewerExtension.js:31 Uncaught TypeError: Cannot read properties of undefined (reading 'getExternalIdMapping').每次我运行它时,devtools 都会记录:ViewerExtension.js:31 Uncaught TypeError: Cannot read properties of undefined (reading 'getExternalIdMapping')。

The extensions get loaded before the model so the getExternalIdMapping() method does not have the model properties yet.扩展在 model之前加载,因此getExternalIdMapping()方法还没有 model 属性。 To handle this scenario, we usually recommend using the viewer events such as Autodesk.Viewing.GEOMETRY_LOADED_EVENT to “catch” the moment when the model is available.为了处理这种情况,我们通常建议使用Autodesk.Viewing.GEOMETRY_LOADED_EVENT查看器事件来“捕捉”model 可用的时刻。 It's better to wait for the event.最好等待事件发生。 This will be fired when the model/drawing finishes loading.这将在模型/绘图完成加载时触发。

Instead of:代替:

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

Try this:尝试这个:

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

Please test this and see if it's helpful.请对此进行测试,看看它是否有帮助。 I tried to incorporate items from your comments to help you structure it out.我试图从您的评论中合并项目以帮助您构建它。

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);

Regarding this, I was trying to achieve 3 things at this stage.关于这一点,我在这个阶段试图实现三件事。

  1. Get the externalIds from Mongodb .Mongodb获取externalIds
  2. Compare the externalIds with the ones gotten from getExternalIdMapping() .将 externalIds 与从getExternalIdMapping()获得的那些进行比较。
  3. Get DbIds of those that matched.获取匹配的DbIds

Solved these by realising 2 and 3 can be put inside a.then() after .loadDocumentNode() .通过实现 2 和 3 可以在a.then()之后.loadDocumentNode() () 来解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM