|
不知大家注意到没有,在Revit视图中的文字的宽度在不同的视图比例下,同一个文字兑现给的TextNote.Width 属性值是变换的。这就给大家造成一个麻烦,那就是如何在不同的比例下,创建文字,且是文字编辑器的宽度刚好容纳下文字的宽度呢?
文字是编程中大量使用的功能,所以这个应该对大家的工作有启发意义。
Revit API提供了Element..Boundary(view)属性 用于获取在指定视图下的外包box,我们可以利用这个属性来获取文字的实际宽度,然后把这个值赋给TextNote.Width属性,达到文字的编辑器宽度正好容纳下所有文字。这样当客户双击文字进行编辑时,文字不会换行。
请看下面的实现代码。
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">[csharp] view plaincopy</span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">Transaction transaction = new Transaction(activeDoc, "create text note"); //created by Joe Ye </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> transaction.Start(); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> string str = "ABCDabcd1234"; </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> TextNote tn = activeDoc.Create.NewTextNote(activeDoc.ActiveView, new XYZ(0, 0, 0), new XYZ(1, 0, 0), new XYZ(0, 1, 0),0.001, TextAlignFlags.TEF_ALIGN_LEFT, str); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> BoundingBoxXYZ bounding = tn.get_BoundingBox(activeDoc.ActiveView); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> tn.Width = (bounding.Max.X - bounding.Min.X) * (1.0 + 1.0 / str.Length); //增加一个文字的宽度,防止编辑时变成两行 </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> //show the bounding in the view to see the calculated boundary of the text.,you can remove it. </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> Line line1 = activeDoc.Application.Create.NewLineBound(bounding.Min, new XYZ(bounding.Max.X,bounding.Min.Y,0)); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> Line line2 = activeDoc.Application.Create.NewLineBound(new XYZ(bounding.Max.X, bounding.Min.Y, 0), bounding.Max); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> Line line3 = activeDoc.Application.Create.NewLineBound(bounding.Max,new XYZ(bounding.Min.X, bounding.Max.Y, 0) ); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> Line line4 = activeDoc.Application.Create.NewLineBound(new XYZ(bounding.Min.X, bounding.Max.Y, 0), bounding.Min); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> activeDoc.Create.NewModelCurve(line1, activeDoc.ActiveView.SketchPlane); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> activeDoc.Create.NewModelCurve(line2, activeDoc.ActiveView.SketchPlane); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> activeDoc.Create.NewModelCurve(line3, activeDoc.ActiveView.SketchPlane); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> activeDoc.Create.NewModelCurve(line4, activeDoc.ActiveView.SketchPlane); </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> </span>
- <span style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; "> transaction.Commit(); </span>
复制代码 使用这个方法,你可以获得Revit中任何一个对象的外包box
作者:叶雄进 转自:http://blog.csdn.net/joexiongjin/article/details/6977984
|
|