为Autodesk Viewer添加自定义工具条的更好方法

简介:


上一篇文章中我介绍了使用Autodesk Viewer的UI API来给viewer添加自定义工具条的方法,看起来很简单是吧。不过有个问题,就是关于自定义工具条的信息(包括按钮的文本、图标、样式、callback等等)全都散落在代码中,如果要添加或者修改的话,得特别小心的扫描代码,非常容易出错。有没有更好的办法呢?这篇文章就来介绍一下。

既然关于Toolbar button等京城需要更改的部分散落到各处不方便维护,那我就把他们集中到一起独立出来。于是我写了个自定义的toolbarConfig对象,采用JSON格式,刚好符合JavaScript的语法,如果我需要添加或者修改工具条或按钮,只需要修改这个config对象就可以了:

/////////////////////////////////////////////////////////////////////
// custom toobar config
var toolbarConfig = {
    'id': 'toolbar_id_1',
    'containerId': 'toolbarContainer',
    'subToolbars': [
        {
            'id': 'subToolbar_id_non_radio_1',
            'isRadio': false,
            'visible': true,
            'buttons': [
                {
                    'id': 'buttonRotation',
                    'buttonText' : 'Rotation',
                    'tooltip': 'Ratate the model at X direction',
                    'cssClassName': 'glyphicon glyphicon glyphicon-play-circle',
                    'iconUrl' :'Images/3d_rotation.png',
                    'onclick': buttonRotationClick
                },
                {
                    'id': 'buttonExplode',
                    'buttonText': 'Explode',
                    'tooltip': 'Explode the model',
                    'cssClassName': '',
                    'iconUrl': 'Images/explode_icon.jpg',
                    'onclick': buttonExplodeClick
                }

            ]
        },
        {
            'id': 'subToolbar_id_radio_1',
            'isRadio': true,
            'visible': true,
            'buttons': [
                {
                    'id': 'radio_button1',
                    'buttonText': 'radio_button1',
                    'tooltip': 'this is tooltip for radio button1',
                    'cssClassName': '',
                    'iconUrl': '',
                    'onclick': radioButton1ClickCallback
                },
                {
                    'id': 'radio_button2',
                    'buttonText': 'radio_button2',
                    'tooltip': 'this is tooltip for radio button2',
                    'cssClassName': '',
                    'iconUrl': '',
                    'onclick': radioButton2ClickCallback
                }

            ]
        }
    ]

};

function buttonRotationClick(e) {

   
}
 

function buttonExplodeClick() {
    
}

function button2ClickCallback(e) {
    alert('Button2 is clicked');
}
function radioButton1ClickCallback(e) {
    alert('radio Button1 is clicked');
}
function radioButton2ClickCallback(e) {
    alert('radio Button2 is clicked');
}

 

接下来创建一个工具方法,解读这个toolbarConfig并利用viewer UI API来创建对于的工具条和按钮,使用方法也和简单: 

////add custom toolbar , usage example: 
//addToolbar(toolbarConfig); 

//////////////////////////////////////////////////////////////////////////// 
function addToolbar(toolbarConfig, viewer) { 

    //find the container element in client webpage first 
    var containter = document.getElementById(toolbarConfig.containerId); 

    // if no toolbar container on client's webpage, create one and append it to viewer 
    if (!containter) { 
        containter = document.createElement('div'); 
        containter.id = 'custom_toolbar'
        //'position: relative;top: 75px;left: 0px;z-index: 200;'; 
        containter.style.position = 'relative'
        containter.style.top = '75px'
        containter.style.left = '0px'
        containter.style.zIndex= '200'
        viewer.clientContainer.appendChild(containter); 
    } 

    //create a toolbar 
    var toolbar = new Autodesk.Viewing.UI.ToolBar(containter); 

    for (var i = 0, len = toolbarConfig.subToolbars.length; i < len; i++) { 
        var stb = toolbarConfig.subToolbars[i]; 
        //create a subToolbar 
        var subToolbar = toolbar.addSubToolbar(stb.id, stb.isRadio); 
        subToolbar.setToolVisibility(stb.visible); 

        //create buttons 
        for (var j = 0, len2 = stb.buttons.length; j < len2; j++) { 
            var btn = stb.buttons[j]; 
            var button = Autodesk.Viewing.UI.ToolBar.createMenuButton(btn.id, btn.tooltip, btn.onclick); 
            //set css calss if availible 
            if (btn.cssClassName) { 
                button.className = btn.cssClassName; 
            } 
            //set button text if availible 
            if (btn.buttonText) { 
                var btnText = document.createElement('span'); 
                btnText.innerText = btn.buttonText; 
                button.appendChild(btnText); 
            } 
            //set icon image if availible 
            if (btn.iconUrl) { 
                var ico = document.createElement('img'); 
                ico.src = btn.iconUrl; 
                ico.className = 'toolbar-button'
                button.appendChild(ico); 
            } 
            //add button to sub toolbar 
            toolbar.addToSubToolbar(stb.id, button); 

        } 



    }

下面就是运行效果了:

image

作者: 峻祁连
邮箱:junqilian@163.com 
出处: http://junqilian.cnblogs.com 
转载请保留此信息。



本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/p/3912274.html ,如需转载请自行联系原作者
相关文章
|
7月前
|
数据可视化
使用 SAP UI5 3D Viewer 控件显示 3D 模型效果试读版
使用 SAP UI5 3D Viewer 控件显示 3D 模型效果试读版
33 0
Revit API中可设置的面板控件
Revit API中可设置的面板控件
Revit API中可设置的面板控件
基于C#的ArcEngine二次开发31:addin开发时调用ArcMap的进度条
基于C#的ArcEngine二次开发31:addin开发时调用ArcMap的进度条
基于C#的ArcEngine二次开发31:addin开发时调用ArcMap的进度条
|
C# Windows
使WPF程序应用预置的控件风格, 如Aero, Luna, Royale, Classic等
原文:使WPF程序应用预置的控件风格, 如Aero, Luna, Royale, Classic等      WPF预设有Aero, Classic, Luna, Royale主题, WPF程序会根据Windows主...
1232 0
|
C# Windows
让你的WPF程序在Win7下呈现Win8风格主题
原文:让你的WPF程序在Win7下呈现Win8风格主题 今天在Win8下使用了一个我之前写的一个WPF程序的时候,发现现在也支持Win8效果了(记得以前的.net 4.0的版本是不支持的)。由于WPF的控件是自绘的,并不受系统主题所控制,也就是说.net 4.5中是附带了Win8主题样式文件的,按理说这个风格在Win7下也可以使用的。
1200 0
|
JavaScript BI 数据格式