与Viewer API和示例相混淆,将选项传递给扩展

时间:2016-07-19 11:16:06

标签: javascript autodesk autodesk-viewer

我很困惑,因为有关如何使用Viewer的示例似乎与API的文档不匹配,某些功能不在文档中,或者它们的签名不同。

根据示例代码,如何将选项传递给我实例化的扩展?我想通过我的扩展回调。

谢谢!

1 个答案:

答案 0 :(得分:2)

我们需要修复文档,以便它不再依赖于未记录的A360查看器附加代码,这应该是内部的。对不起,我们会尽快做到这一点...

目前,您可以使用我的viewer boilerplate sample

中的代码
function initializeViewer(containerId, urn) {

    Autodesk.Viewing.Document.load(urn, function (model) {

      var rootItem = model.getRootItem();

      // Grab all 3D items
      var geometryItems3d = Autodesk.Viewing.Document.getSubItemsWithProperties(
        rootItem,
        { 'type': 'geometry', 'role': '3d' },
        true);

      // Grab all 2D items
      var geometryItems2d = Autodesk.Viewing.Document.getSubItemsWithProperties(
        rootItem,
        { 'type': 'geometry', 'role': '2d' },
        true);

      var domContainer = document.getElementById(containerId);

      //UI-less Version: viewer without any Autodesk buttons and commands
      //viewer = new Autodesk.Viewing.Viewer3D(domContainer);

      //GUI Version: viewer with controls
      viewer = new Autodesk.Viewing.Private.GuiViewer3D(domContainer);

      viewer.initialize();

      viewer.setLightPreset(8);

      //Button events - two buttons to load/unload a sample extension
      // Irrelevant to viewer code itself
      var loadBtn = document.getElementById('loadBtn');

      loadBtn.addEventListener("click", function(){

        loadExtension(viewer);
      });

      var unloadBtn = document.getElementById('unloadBtn');

      unloadBtn.addEventListener("click", function(){

        unloadExtension(viewer);
      });

      // Illustrates how to listen to events
      // Geometry loaded is fired once the model is fully loaded
      // It is safe to perform operation involving model structure at this point
      viewer.addEventListener(
        Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
        onGeometryLoaded);

      //optional
      var options = {
        globalOffset: {
          x: 0, y: 0, z: 0
        }
      }

      // Pick the first 3D item ortherwise first 2D item
      var viewablePath = (geometryItems3d.length ?
        geometryItems3d[0] :
        geometryItems2d[0]);

      viewer.loadModel(
        model.getViewablePath(viewablePath),
        options);

    }, function(err) {

      logError(err);
    });
  }

初始化查看器后,您可以独立加载每个扩展名并传递回调,如下所示:

var options = {
     onCustomEventFiredByMyExtension: function() {
         console.log('LMV rulez!')
     }
 }

 viewer.loadExtension('MyExtensionId', options)

但我认为更优雅的方法是从扩展本身触发事件,这可能是这样的:

 viewer.loadExtension('MyExtensionId')

 var myExtension = viewer.getExtension('MyExtensionId')

 myExtension.on('CustomEvent', function () {
     console.log('LMV still rulez!')
 })

有关超级简单事件库,请参阅micro-events