KwanYee Tan
KwanYee Tan

Reputation: 11

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').

Upvotes: 0

Views: 430

Answers (3)

KwanYee Tan
KwanYee Tan

Reputation: 11

Regarding this, I was trying to achieve 3 things at this stage.

  1. Get the externalIds from Mongodb.
  2. Compare the externalIds with the ones gotten from getExternalIdMapping().
  3. Get DbIds of those that matched.

Solved these by realising 2 and 3 can be put inside a.then() after .loadDocumentNode().

Upvotes: 0

Arrotech
Arrotech

Reputation: 109

The extensions get loaded before the model so the getExternalIdMapping() method does not have the model properties yet. 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. 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));
        });

Upvotes: 3

Matt H
Matt H

Reputation: 640

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

Upvotes: 0

Related Questions