如何在autodesk伪造中为不同对象定义不同的上下文菜单

时间:2017-08-14 05:21:52

标签: viewer autodesk-forge forge

我想为伪造查看器中的不同对象定义不同的上下文菜单,这是我的代码

viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT,function(e){
     if(viewer.getSelection().length==0){return;}
     var selectId=viewer.getSelection()[0];
     viewer.search("Cabinet",function(ids){
          if(ids.indexOf(selectId)!=-1){
               viewer.registerContextMenuCallback('CabinetMsg', function (menu, status) {
                   if (status.hasSelected) {
                        menu.push({
                            title: "CabinetMsg",
                            target: function () {
                                openLayer('CabinetMsg','954','775','CabinetMsg.html')
                            }
                        });
                    }
                });
          }else{
                viewer.registerContextMenuCallback('CabinetMsg', function (menu, status) {
                    if (status.hasSelected) {
                        menu.forEach(function(el,index){
                            if(el.title=="CabinetMsg"){
                                menu.splice(menu.indexOf(index),1)
                            }
                        })
                    }
                });
            }

        })
    });

但是推送元素到数组的时间总是比上下文菜单显示的要晚。当我选择另一个对象时,我的自定义上下文菜单总是显示。我能做什么?

1 个答案:

答案 0 :(得分:1)

您提供的代码将为上下文菜单创建2个新子项。这是这种情况的一种方式,即你必须自己编写<?php class Upload extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } public function index() { $this->load->view('upload_form', array('error' => ' ' )); } public function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); // get the user submitted file name here if ( ! $this->upload->do_upload('userfile')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } } } ?> 。此外,您需要在ViewerObjectContextMenu中执行hitTest以通过鼠标右键单击选择dbId。以下是您的示例:

ViewerObjectContextMenu.buildMenu

在此之后,您可以编写一个扩展名,用您自己的菜单替换默认查看器上下文菜单。这也是一个例子:

class MyContextMenu extends Autodesk.Viewing.Extensions.ViewerObjectContextMenu {
  constructor( viewer ) {
    super( viewer );
  }

  isCabinet( dbId ) {
    // Your logic for determining if selected element is cabinet or not.
    return false;
  }

  buildMenu( event, status ) {
    const menu = super.buildMenu( event, status );

    const viewport = this.viewer.container.getBoundingClientRect();
    const canvasX = event.clientX - viewport.left;
    const canvasY = event.clientY - viewport.top;

    const result = that.viewer.impl.hitTest(canvasX, canvasY, false);

    if( !result || !result.dbId ) return menu;

    if( status.hasSelected && this.isCabinet( result.dbId ) ) {
      menu.push({
          title: 'CabinetMsg',
          target: function () {
              openLayer( 'CabinetMsg', '954', '775', 'CabinetMsg.html' );
          }
      });
    }

    return menu;
   }
}

希望得到这个帮助。