0

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

基于示例代码,如何将选项传递给我实例化的扩展?我想给我的扩展一个回调。

谢谢!

4

1 回答 1

2

我们需要修复文档,使其不再依赖未记录的 A360 查看器附加代码,该代码应该是内部的。抱歉给您带来不便,我们会尽快处理...

目前,您可以使用我的查看器样板示例中的代码:

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!')
 })

请参阅微事件以获取超级简单的事件库。

于 2016-07-20T16:03:30.767 回答