上一篇文章中我介绍了使用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);

}

}

下面就是运行效果了:

为Autodesk Viewer添加自定义工具条的更好方法的更多相关文章

  1. 为Autodesk Viewer添加自定义工具条

    如果你参加过我们近期的活动,你就会频繁的听到我们现在正在做的Autodesk Viewer大模型浏览器,这是一个不需要下载任何插件,基于WebGL技术的浏览器,可以支持几十种数据格式.同时viewer ...

  2. 无插件的大模型浏览器Autodesk Viewer开发培训-武汉-2014年8月28日 9:00 – 12:00

    武汉附近的同学们有福了,这是全球第一次关于Autodesk viewer的教室培训. :) 你可能已经在各种场合听过或看过Autodesk最新推出的大模型浏览器,这是无需插件的浏览器模型,支持几十种数 ...

  3. MapControl Application 添加自定义的工具条

    现在想用二次开发做一些东西,然后需要自定义的工具条,但是如何向MapControl Application 添加自定义的工具条呢,经过多次试验后,终于找到了相应的方法(左图是添加自定义的工具条之前,右 ...

  4. Autodesk 产品开发培训开始报名-8月26~28-武汉– Revit, Vault, Autodesk Viewer, Navisworks

    为了帮助Autodesk中国地区的二次开发人员有机会系统地了解与学习Autodesk 在BIM解决方案中的旗舰产品 Revit以及Navisworks等产品的最新开发技术,并有机会与Autodesk ...

  5. 如何往IE工具条添加按钮(转载)

    如何往IE工具条添加按钮 问题提出:金山词霸.网络蚂蚁等软件安装后会向IE的工具条添加自己的按钮.按下按钮后还会作出相应的动作,这种功能是如何实现的呢?读完本文,您也可以将自己应用程序的按钮添加到IE ...

  6. [原创]ObjectARX开发环境搭建之VS2010+ObjectARX2012Wizard+Addin工具条问题修复

    目前ObjectARX版本越来越高,也越来越简化开发,如果需要同时开发低版本和高版本的ARX程序,就需要搭建批量编译环境,以满足ARX开发的需要. 批量编译的搭建网络上已经有了很多的教程,基本上都是基 ...

  7. Navisworks API 简单二次开发 (自定义工具条)

    在Navisworks软件运行的时候界面右侧有个工具条.比较方便.但是在二次开发的时候我不知道在Api那里调用.如果有网友知道请告诉我.谢谢. 我用就自己设置一个工具.界面比较丑!没有美工. 代码: ...

  8. 小菜的系统框架界面设计-灰姑娘到白雪公主的蜕变(工具条OutLookBar)

    灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌   --蜕变-->  我这里讲的是一个工具条的蜕变过程, ...

  9. 浅析WINFORM工具条的重用实现

    一直以来,我都想看看别人家的工具栏重用(图1)到底是如何实现的,但在网上搜索了很久都没有找到过,即使找到一些程序,要么就是把这个工具栏写在具体的画面(图2),要么就是没有源代码的, 我在想,是否别人也 ...

随机推荐

  1. Robot Framework自动化测试(六)--- robotremoteserver使用

    robotremoteserver 是什么? Python Remote Server for Robot Framework 下载地址:https://pypi.python.org/pypi/ro ...

  2. postgres中几个复杂的sql语句

    postgres中几个复杂的sql语句 需求一 需要获取一个问题列表,这个问题列表的排序方式是分为两个部分,第一部分是一个已有的数组[0,579489,579482,579453,561983,561 ...

  3. Laravel学习

    Laravel是个很强大的PHP框架,它剔除了开发中Web开发中比较痛苦的过程,提供了验证(authentication),路由(routing),Session和缓存(caching)等开发过程中常 ...

  4. JavaScript的setTimeout和setInterval的深入理解

    发表过一片博客<跟着我用JavaScript写计时器>,比较基础.....有网友说应该写一下setTimeout的原理和机制,嗯,今天就来写一下吧: 直奔主题:setTimeout和set ...

  5. 再谈ABC

    最近一直在看蒋老师那13篇<我的WCF之旅>,终于看完了,看得很慢,记得最初出来工作的时候那时的技术总监建议我去看的,可几个月前我才开始看,看了几个月才把13篇看完.第一篇WCF的博文是我 ...

  6. 在Android开发中调用Rest web服务(转)

    首先从维基百科上拷贝一点Rest的基本概念给大家看看,然后我们再开始详解在Android中如何调用Rest服务.表象化状态转变(英文:Representational State Transfer,简 ...

  7. Windows Server 2012中安装Active Directory域服务

    1.登陆Windows Server 2012,打开服务器管理器,选择"添加角色和功能" 2.在"开始之前"页面,直接点击"下一步" 3.选 ...

  8. Hello, Android 快速入门

    Hello, Android Android 开发与 Xamarin 简介 在这两节指南中,我们将 (使用 Xamarin Studio或 Visual Studio)建立我们的第一个 Xamarin ...

  9. jquery简单原则器(匹配除了指定选择器之外的元素 selector 表示选择器)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 鼠标滑动加载div