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