如何在Autodesk Forge中编写onMouseOver事件的代码

时间:2017-07-23 06:15:14

标签: javascript jquery autodesk-forge

我在autodesk forge中创建了一个简单的扩展。 我的想法是,当我在3D对象上制作鼠标悬停事件时,它必须向我显示悬停对象/子对象的ID,我的扩展代码就像这样运行。

AutodeskNamespace("Autodesk.ADN.Viewing.Extension");


Autodesk.ADN.Viewing.Extension.MouseEvent = function (viewer, options) {

    Autodesk.Viewing.Extension.call(this, viewer, options);

    var _self = this;
    var _viewer = viewer;
    var _selectedId = null;

//On Load of the exension function
    _self.load = function () {

	_viewer.addEventListener(
            Autodesk.Viewing.MOUSE_OVER_EVENT,
            _self.onMouseOver);

         	console.log("Autodesk.ADN.MouseEvent loaded");
       	 return true;
	};

	//On unload of the exension function
      _self.unload = function () {
 
	_viewer.removeEventListener(
            Autodesk.Viewing.MOUSE_OVER_EVENT,
            _self.onMouseOver);

        console.log("Autodesk.ADN.MouseEvent unloaded");
        return true;
    };

// Event function initialization
_self.onMouseOver = function (event) {

        var dbId = event.dbIdArray[0];

        if (typeof dbId !== 'undefined') {

            _selectedId = dbId;
            alert('ID: ' + _selectedId);
        }
        else _selectedId = null;
    }
};
Autodesk.ADN.Viewing.Extension.MouseEvent.prototype =
    Object.create(Autodesk.Viewing.Extension.prototype);

Autodesk.ADN.Viewing.Extension.MouseEvent.prototype.constructor =
    Autodesk.ADN.Viewing.Extension.MouseEvent;

Autodesk.Viewing.theExtensionManager.registerExtension(
    'Autodesk.ADN.Viewing.Extension.MouseEvent',
    Autodesk.ADN.Viewing.Extension.MouseEvent);

但onMouseOver功能不起作用,有人可以帮帮我吗?提前谢谢。

PS:我已在脚本标记中包含扩展名,并且同样加载了扩展名。

oViewer.loadExtension('Autodesk.ADN.Viewing.Extension.MouseEvent');

我还从控制台收到确认扩展已成功加载的确认。

1 个答案:

答案 0 :(得分:0)

没有像Autodesk.Viewing.MOUSE_OVER_EVENT这样的事件......你刚刚做过这件事,或者你是从某些人那里得到的 - 显然是不正确的 - 来源?

处理这种方法的方法是使用查看器工具(请参阅details的帖子),然后在handleMouseMove回调中执行以下操作:

handleMouseMove (event) {

  var hitTest = _self.viewer.clientToWorld(
    event.canvasX,
    event.canvasY,
    true)

  if (hitTest) {

    console.log(hitTest)
  }
}

这是我写的关于viewer events的另一篇文章,它有点旧,所以现在还有一些,但可以给你一个很好的起点。