大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的。Three.js里面提供了一个AxisHelper,但如果你直接运用的话,你会发现在viewer中并不显示,并且控制台中会有这样的错误信息:"Only THREE.Mesh can be rendered by the Firefly renderer. Use THREE.Mesh to draw lines."  原因是因为View and Data API采用的是Three.js上定制的render,有些three.js中可以使用的功能在viewer中并不能用。所以我重写了一个这样的AxisHelper,希望对你有帮助。

 

使用非常简单,只需要加载这个扩展即可。建议在GEOMETRY_LOADED_EVENT事件中加载:

// It is recommended to load the extension when geometry is loaded
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
function(){ viewer.loadExtension('Autodesk.ADN.Viewing.Extension.AxisHelper'); });

.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; }

下面是截图,你可以看到红线是X轴,绿线是Y轴,蓝线是Z轴。请注意和右上角的view cube的对应关系。

下面是这个扩展的源代码,不过最好到 github 上下载,因为我可能会时不时的做更新,github上的才是最新的。

///////////////////////////////////////////////////////////////////////////////
AutodeskNamespace("Autodesk.ADN.Viewing.Extension"); Autodesk.ADN.Viewing.Extension.AxisHelper = function (viewer, options) { Autodesk.Viewing.Extension.call(this, viewer, options); var _self = this; var _axisLines = []; _self.load = function () { console.log('Autodesk.ADN.Viewing.Extension.AxisHelper loaded'); addAixsHelper(); //workaround
//have to call this to show up the axis
viewer.restoreState(viewer.getState()); return true;
}; _self.unload = function () { removeAixsHelper(); console.log('Autodesk.ADN.Viewing.Extension.AxisHelper unloaded');
return true;
}; var addAixsHelper = function() { _axisLines = []; //get bounding box of the model
var boundingBox = viewer.model.getBoundingBox();
var maxpt = boundingBox.max;
var minpt = boundingBox.min; var xdiff = maxpt.x - minpt.x;
var ydiff = maxpt.y - minpt.y;
var zdiff = maxpt.z - minpt.z; //make the size is bigger than the max bounding box
//so that it is visible
var size = Math.max(xdiff,ydiff,zdiff) * 1.2;
//console.log('axix size :' + size); // x-axis is red
var material_X_Axis = new THREE.LineBasicMaterial({
color: 0xff0000, //red
linewidth: 2
});
viewer.impl.matman().addMaterial('material_X_Axis',material_X_Axis,true);
//draw the x-axix line
var xLine = drawLine(
{x : 0, y : 0, z : 0} ,
{x : size, y : 0, z : 0} ,
material_X_Axis); _axisLines.push(xLine); // y-axis is green
var material_Y_Axis = new THREE.LineBasicMaterial({
color: 0x00ff00, //green
linewidth: 2
});
viewer.impl.matman().addMaterial('material_Y_Axis',material_Y_Axis,true);
//draw the y-axix line
var yLine = drawLine(
{x : 0, y : 0, z : 0} ,
{x : 0, y : size, z : 0} ,
material_Y_Axis); _axisLines.push(yLine); // z-axis is blue
var material_Z_Axis = new THREE.LineBasicMaterial({
color: 0x0000ff, //blue
linewidth: 2
});
viewer.impl.matman().addMaterial('material_Z_Axis',material_Z_Axis,true);
//draw the z-axix line
var zLine = drawLine(
{x : 0, y : 0, z : 0} ,
{x : 0, y : 0, z : size} ,
material_Z_Axis); _axisLines.push(zLine); } var drawLine = function(start, end, material) { var geometry = new THREE.Geometry(); geometry.vertices.push(new THREE.Vector3(
start.x, start.y, start.z)); geometry.vertices.push(new THREE.Vector3(
end.x, end.y, end.z)); var line = new THREE.Line(geometry, material); viewer.impl.scene.add(line);
//refresh viewer
viewer.impl.invalidate(true); return line;
} var removeAixsHelper = function() { _axisLines = []; _axisLines.forEach(function(line){ viewer.impl.scene.remove(line);
}); //remove materials
delete viewer.impl.matman().materials.material_X_Axis;
delete viewer.impl.matman().materials.material_Y_Axis;
delete viewer.impl.matman().materials.material_Z_Axis; } }; Autodesk.ADN.Viewing.Extension.AxisHelper.prototype =
Object.create(Autodesk.Viewing.Extension.prototype); Autodesk.ADN.Viewing.Extension.AxisHelper.prototype.constructor =
Autodesk.ADN.Viewing.Extension.AxisHelper; Autodesk.Viewing.theExtensionManager.registerExtension(
'Autodesk.ADN.Viewing.Extension.AxisHelper',
Autodesk.ADN.Viewing.Extension.AxisHelper);

Hope it helps.

使用AxisHelper帮助理解View and Data API中的坐标系统的更多相关文章

  1. 在View and Data API中更改指定元素的颜色

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

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

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

  3. 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 ...

  4. 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 ...

  5. 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 ...

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

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

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

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

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

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

  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. Android(4)—Mono For Android 第一个App应用程序

    0.前言 年前就计划着写这篇博客,总结一下自己做的第一个App,却一直被新项目所累,今天抽空把它写完,记录并回顾一下相关知识点,也为刚学习Mono的同学提供佐证->C#也是开发Android的! ...

  3. MyBatis5:MyBatis集成Spring事物管理(上篇)

    前言 有些日子没写博客了,主要原因一个是工作,另一个就是健身,因为我们不仅需要努力工作,也需要有健康的身体嘛. 那有看LZ博客的网友朋友们放心,LZ博客还是会继续保持更新,只是最近两三个月LZ写博客相 ...

  4. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  5. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

  6. ASP.NET MVC Model绑定(三)

    ASP.NET MVC Model绑定(三) 前言 看过前两篇的朋友想必对Model绑定有个大概的了解,然而MVC框架给我们提供了更高的可扩展性的提供程序编程模式,也就是本篇的主题了,会讲解一下Mod ...

  7. ABP源码分析十五:ABP中的实用扩展方法

    类名 扩展的类型 方法名 参数 作用 XmlNodeExtensions XmlNode GetAttributeValueOrNull attributeName Gets an   attribu ...

  8. TODO:一不顺眼就换字体Go之代码篇

    TODO:一不顺眼就换字体Go之代码篇 image包实现了一个基本的2D图像库,该包中包含基本的接口叫做image,这个里面包含color,这个将在image/color中描述:新增字体font,进行 ...

  9. Struts2+Spring+Hibernate框架整合总结详细教程

    一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...

  10. CATransition自定义转场动画

    我们可以通过CATransiton来自定义一些漂亮的转场动画, CATransition继承自CAAnimation, 所以用法跟CAAnimation差不多 先直接上一个代码: #import &q ...