大家在使用View and Data API开发过程中,经常会用到的就是改变某些元素的颜色已区别显示。比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用户以示区别。作为开发者,一定会喜欢看的这样的API:

  1. //load the extension
  2. viewer.loadExtension('Autodesk.ADN.Viewing.Extension.Color');
  3. // an array of node Id
  4. var elementIds= [1735, 1736];
  5. //set color to red
  6. viewer.setColorMaterial(elementIds,0xff0000);
  7. //restore to original color
  8. viewer.restoreColorMaterial(elementIds);

可惜View and Data API中并没有提供这样的API,不过我们可以自己来做。下面就是一个这样的扩展,如果你正好需要,可以直接拿去用。

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Autodesk.ADN.Viewing.Extension.Color
  3. //
  4. ///////////////////////////////////////////////////////////////////////////////
  5. AutodeskNamespace("Autodesk.ADN.Viewing.Extension");
  6. Autodesk.ADN.Viewing.Extension.Color = function(viewer, options) {
  7.  
  8. Autodesk.Viewing.Extension.call(this, viewer, options);
  9.  
  10. var overlayName = "temperary-colored-overlay";
  11. var _self = this;
  12.  
  13. _self.load = function() {
  14.  
  15. console.log('Autodesk.ADN.Viewing.Extension.Color loaded');
  16. ///////////////////////////////////////////////////////////////////////////
  17. // Generate GUID
  18. //
  19. ///////////////////////////////////////////////////////////////////////////
  20. function newGuid() {
  21. var d = new Date().getTime();
  22. var guid = 'xxxx-xxxx-xxxx-xxxx-xxxx'.replace(/[xy]/g, function(c) {
  23. var r = (d + Math.random() * 16) % 16 | 0;
  24. d = Math.floor(d / 16);
  25. return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  26. });
  27. return guid;
  28. };
  29.  
  30. ///////////////////////////////////////////////////////////////////////////
  31. // add new material
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. function addMaterial(color) {
  35. var material = new THREE.MeshPhongMaterial({
  36. color: color
  37. });
  38. //viewer.impl.matman().addMaterial(newGuid(), material);
  39. viewer.impl.createOverlayScene(overlayName, material, material);
  40. return material;
  41. }
  42.  
  43. ///////////////////////////////////////////////////////////////////////////
  44. // Set color for nodes
  45. // objectIds should be an array of dbId
  46. //
  47. //
  48. ///////////////////////////////////////////////////////////////////////////
  49. Autodesk.Viewing.Viewer3D.prototype.setColorMaterial = function(objectIds, color) {
  50. var material = addMaterial(color);
  51.  
  52. for (var i=0; i<objectIds.length; i++) {
  53.  
  54. var dbid = objectIds[i];
  55.  
  56. //from dbid to node, to fragid
  57. var it = viewer.model.getData().instanceTree;
  58.  
  59. it.enumNodeFragments(dbid, function (fragId) {
  60.  
  61. var renderProxy = viewer.impl.getRenderProxy(viewer.model, fragId);
  62.  
  63. renderProxy.meshProxy = new THREE.Mesh(renderProxy.geometry, renderProxy.material);
  64.  
  65. renderProxy.meshProxy.matrix.copy(renderProxy.matrixWorld);
  66. renderProxy.meshProxy.matrixWorldNeedsUpdate = true;
  67. renderProxy.meshProxy.matrixAutoUpdate = false;
  68. renderProxy.meshProxy.frustumCulled = false;
  69.  
  70. viewer.impl.addOverlay(overlayName, renderProxy.meshProxy);
  71. viewer.impl.invalidate(true);
  72.  
  73. }, false);
  74. }
  75.  
  76. }
  77.  
  78. Autodesk.Viewing.Viewer3D.prototype.restoreColorMaterial = function(objectIds) {
  79.  
  80. for (var i=0; i<objectIds.length; i++) {
  81.  
  82. var dbid = objectIds[i];
  83.  
  84. //from dbid to node, to fragid
  85. var it = viewer.model.getData().instanceTree;
  86.  
  87. it.enumNodeFragments(dbid, function (fragId) {
  88.  
  89. var renderProxy = viewer.impl.getRenderProxy(viewer.model, fragId);
  90.  
  91. if(renderProxy.meshProxy){
  92.  
  93. //remove all overlays with same name
  94. viewer.impl.clearOverlay(overlayName);
  95. //viewer.impl.removeOverlay(overlayName, renderProxy.meshProxy);
  96. delete renderProxy.meshProxy;
  97.  
  98. //refresh the sence
  99.  
  100. viewer.impl.invalidate(true);
  101.  
  102. }
  103.  
  104. }, true);
  105. }
  106.  
  107. }
  108.  
  109. _self.unload = function() {
  110. console.log('Autodesk.ADN.Viewing.Extension.Color unloaded');
  111. return true;
  112. };
  113. };
  114. };
  115. Autodesk.ADN.Viewing.Extension.Color.prototype = Object.create(Autodesk.Viewing.Extension.prototype);
  116. Autodesk.ADN.Viewing.Extension.Color.prototype.constructor = Autodesk.ADN.Viewing.Extension.Color;
  117. Autodesk.Viewing.theExtensionManager.registerExtension('Autodesk.ADN.Viewing.Extension.Color', Autodesk.ADN.Viewing.Extension.Color);
  1.  

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这段代码我也在我的IoT示例中使用,源代码在 github 上。

在View and Data API中更改指定元素的颜色的更多相关文章

  1. 使用AxisHelper帮助理解View and Data API中的坐标系统

    大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的.Three.js里面提供了一个AxisHelpe ...

  2. 特大喜讯,View and Data API 现在支持中文界面了

    大家经常会问到,使用View and Data API怎么做界面的本地化,来显示中文,现在好消息来了,从v1.2.19起,View and Data API开始支持多国语言界面了.你需要制定版本号为v ...

  3. View and Data API tips: 缓存Access Token

    对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用.虽然Autodesk的view and Data API目前还没 ...

  4. Autodesk View and Data API二次开发学习指南

    什么是View and Data API? 使用View and Data API,你可以轻松的在网页上显示大型三维模型或者二维图纸而不需要安装任何插件.通过View and Data API,你可以 ...

  5. Using View and Data API with Meteor

    By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...

  6. View and Data API Tips: Constrain Viewer Within a div Container

    By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...

  7. View and Data API Tips: Hide elements in viewer completely

    By Daniel Du With View and Data API, you can hide some elements in viewer by calling "viewer.hi ...

  8. View and Data API 现在支持IE11了

    By Daniel Du After a long time waiting, IE11 finally supports WebGL, which enables us viewing our 3D ...

  9. View and Data API Tips: how to make viewer full screen

    By Daniel Du If you have not heard of View and Data API, here is the idea, the View & Data API e ...

随机推荐

  1. 嵌入式C语言代码的调试技巧

    在项目开发的过程中,不可避免的会遇到调试代码的情况. 刚开始写代码时,我们想看具体执行到哪儿时,往往这么写: printf("***** Code is here! *****\n" ...

  2. 录像时调用MediaRecorder的start()时发生start failed: -19错误

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. Spark笔记:复杂RDD的API的理解(下)

    本篇接着谈谈那些稍微复杂的API. 1)   flatMapValues:针对Pair RDD中的每个值应用一个返回迭代器的函数,然后对返回的每个元素都生成一个对应原键的键值对记录 这个方法我最开始接 ...

  4. .NET应用程序与数据库交互的若干问题

    我们知道,在应用程序中与数据库进行交互是一个比较耗时的过程,首先应用程序需要与应用程序建立连接,然后将请求发送到数据库,数据库执行操作,然后将结果集返回.所以在程序中,要尽量晚的与数据库建立连接,并且 ...

  5. 自己开发实现OAuth做webapi认证

    看到园子里面有人写的OAuth,就想把自己实现的OAuth也分享一下,关于OAuth协议这里就不再赘述. 一.作为认证服务器,首先需要提供一个可以通过appid/appsecret来获取token这样 ...

  6. 使用自定义 classloader 的正确姿势

    详细的原理就不多说了,网上一大把, 但是, 看了很多很多, 即使看了jdk 源码, 说了罗里吧嗦, 还是不很明白: 到底如何正确自定义ClassLoader, 需要注意什么 ExtClassLoade ...

  7. [Java定时器]用Spring Task实现一个简单的定时器.

    今天做一个项目的的时候需要用到定时器功能.具体需求是: 每个月一号触发一次某个类中的方法去拉取别人的接口获取上一个月份车险过期的用户.如若转载请附上原文链接:http://www.cnblogs.co ...

  8. CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)

    ---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update....   yum install 或者 添加y ...

  9. 【Win 10应用开发】使用RichEditBox控件应注意的问题

    RichEditBox控件支持对多格式文本进行编辑,一般的文本输入控件可以使用TextBox,不过,如果希望编辑格式较为复杂的文本,就可以考虚使用RichEditBox控件. RichEditBox控 ...

  10. Angular Service和Factory应用的区别

    Service可以用来将返回同类业务的多种返回值 Factory可以用来提供对同类业务的多个方法的调用 另外:Provider可以用来封装各独立职责