上一篇文章中我介绍了使用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. javascript学习总结(一):基础知识。

    1 数据类型a.数据类型共有7种,字符串(string).数字(number).布尔(boolean).数组(array).对象(object).Null.Undefined. 其中布尔(逻辑)类型只 ...

  2. java变量的加载顺序

    学习编程思想 package com.test.java.classs; /** * Created by Administrator on 2015/12/7. * 在类的内部,变量定义的顺序决定了 ...

  3. IIS各个版本中你需要知道的那些事儿

    一.写在前面 目前市面上所用的IIS版本估计都是>=6.0的.所以我们主要以下面三个版本进行讲解 服务器版本 IIS默认版本 server2003 6.0 server2008 7.0 serv ...

  4. 地图定位IOS8.0之前的定位

    在ios8.0之前定位的步骤如下: 1.首先将我们的项目版本切换到7.0

  5. JavaScript基础插曲-练习

    Javascript基础学习 eg:利用正则表达式来去掉空格. 1:msg=' Hello ': <script type="text/javascript">     ...

  6. 暴力枚举 + 24点 --- hnu : Cracking the Safe

    Cracking the Safe Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit u ...

  7. ASP.NET MVC系列:添加模型

    模型(Model)是应用程序中用于处理应用程序数据逻辑的部分;通常模型对象在数据库中存取数据 添加模型类 在解决方案中右击Models文件夹,然后选择“添加”,在“添加”项里选择“类”:或者选中Mod ...

  8. lua的string.gsub初使用

    今天在学习lua,熟悉项目代码的过程中,发现string.gsub好高级,所以在此mark下. 以下是lua5.1的官方文档介绍. string.gsub (s, pattern, repl [, n ...

  9. 【UWP】使用Action代替Command

    在Xaml中,说到绑定,我们用的最多的应该就是ICommand了,通过Command实现ViewModel到View之间的命令处理,例如Button默认就提供了Command支持,如下 Xaml: & ...

  10. 驱动开发利器Microsoft Windows Driver Kit 7.1.0下载

    在Windows 2000 与Windows XP 系统采用是WINDDK来开发WINDOWS驱动程序,我手头也有WINDDK,可是从Windows Vista开始之后,一般采用Microsoft W ...