在Revit二次开发过程中,对于Revit内部的坐标系之间的关系的掌握是非常必要的。下面是一个典型的坐标问题。
族的几何体和该族实例的几何体间什么对应关系?如何从族的局部坐标系,通过怎样的矩阵旋转、缩放、平移得到族实例在全局坐标系中的实际位置呢?
首先说明下概念:
在制作族的时候,族文件中的几何体具有自己的坐标系。当加载族文件到Revit模型文件中,插入改族实例到模型后,族实例中的几何体具有自己的坐标。从族实例获取几何体的几何信息由两种方法:
FamilyInstance.GetOriginalGeometry() 方法: 获取这个族实例在被剪切,合并等操作之前的几何数据GeometryElement。其数字是相对于Revit的模型坐标系。
FamilyInstance.Geometry 属性: 可以获得这个族实例经过后期处理(剪切,合并等)之后的几何数据(GeometryElement)。
现在我们了解下上面方法返回的几何体的坐标值与族文件中的几何体的坐标值之间的关系。Revit API在这点上作了非常好的工作它提供了一个属性GeometryInstance.Transform,我们可以获得转换矩阵Transform。这个转换矩阵可以把族文件中几何实体的坐标信息直接转成成族实例在模型文件坐标系中的坐标。
GeometryInstance 对象可以从GeometryElement) 中获得。
请看下面示例,获取柱子族定义中的坐标转成成柱子在模型坐标系下的转换矩阵。然后将柱子族中的原点(0,0,0)点转成到模型坐标系下的坐标。
Selection sel = this.ActiveUIDocument.Selection;
Reference ref1 = sel.PickObject(ObjectType.Element,"Please a column");
//For simplicity, ignore the code to check if the picked element is column.
FamilyInstance col = doc.GetElement(ref1) as FamilyInstance;
Options opt = new Options();
opt.ComputeReferences = false;
opt.View = doc.ActiveView;
GeometryElement geoElement = col.get_Geometry(opt);
Transform trans = null;
foreach(GeometryObject geoObj in geoElement)
{
if(geoObj is GeometryInstance)
{
GeometryInstance geoInst = geoObj as GeometryInstance;
trans = geoInst.Transform;
break;
}
}
if(trans !=null)
{
XYZ ptFamilyOrigin = new XYZ(0,0,0);
XYZ ptInModel = trans.OfPoint(ptFamilyOrigin);
//compare with the column's position in the model.
XYZ ptColumnPos = (col.Location as LocationPoint).Point;
string result = "The transformed family origin in model is:("
+ ptInModel.X.ToString() + "," + ptInModel.Y.ToString() + "," + ptInModel.Z.ToString() +")"
+ "\r\nThe column's position in model is:("
+ ptColumnPos.X.ToString() + "," + ptColumnPos.Y.ToString() + "," + ptColumnPos.Z.ToString() + ")";
TaskDialog.Show("Result",result);
}
结果显示,计算出来的坐标和柱子的位置一样的。
如下图:
转载请复制以下信息:
原文链接: http://blog.csdn.net/joexiongjin/article/details/16883451
作者: 叶雄进
2013.11.22