/// <summary>
/// 根据两个点创建剖面视图
/// </summary>
/// <param name="doc">文档</param>
/// <param name="el">元素</param>
/// <param name="pointF">起点</param>
/// <param name="pointT">终点</param>
/// <param name="bodyWidth">视图可见范围的长度</param>
private void CreateSectionBox(Document doc, Element el, XYZ pointF, XYZ pointT, double bodyWidth)
{
var elOffset=el.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM)?.AsDouble();
// Determine view family type to use
ViewFamilyType viewFamilyType
= new FilteredElementCollector(doc)
.OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>()
.FirstOrDefault<ViewFamilyType>(x =>
ViewFamily.Section == x.ViewFamily);
// Determine section box
XYZ vecPoint = pointT - pointF;
BoundingBoxXYZ boundingBoxXYZ = el.get_BoundingBox(null);
double minZ = boundingBoxXYZ.Min.Z;
double maxZ = boundingBoxXYZ.Max.Z;
double len = vecPoint.GetLength();
double offset = 0.1 * len;
var boxWidth = bodyWidth / 304.8;
if (elOffset == null)
{
elOffset = 0;
}
XYZ min = new XYZ(-len, minZ - offset- (double)elOffset, -offset);
XYZ max = new XYZ(len, maxZ + offset- (double)elOffset, boxWidth);
XYZ midpoint = pointF + 0.5 * vecPoint;
XYZ eldir = vecPoint.Normalize();
XYZ up = XYZ.BasisZ;
XYZ viewdir = eldir.CrossProduct(up);
Transform transform = Transform.Identity;
transform.Origin = midpoint;
transform.BasisX = eldir;
transform.BasisY = up;
transform.BasisZ = viewdir;
BoundingBoxXYZ sectionBox = new BoundingBoxXYZ();
sectionBox.Transform = transform;
sectionBox.Min = min;
sectionBox.Max = max;
// Create section view
using (Transaction tx = new Transaction(doc))
{
tx.Start("Create Section View");
var temp = ViewSection.CreateSection(doc, viewFamilyType.Id, sectionBox);
tx.Commit();
}
}
关于SectionBox的方向:把两个点调换一下就可以翻转SectionBox的方向。