using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
namespace WorkingDymo
{
[Transaction(TransactionMode.Manual)]
class 拾取墙体创建垂直剖面 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Reference reference = uiDoc.Selection.PickObject(ObjectType.Element);
Wall wall = doc.GetElement(reference) as Wall;
BoundingBoxXYZ boundingBox= GetSectionViewPerpendiculatToWall(wall);
//获取默认三维视图类型
ElementTypeGroup typeGroup = ElementTypeGroup.ViewTypeSection;
ElementId vid = doc.GetDefaultElementTypeId(typeGroup);
//新建并启动事务
Transaction transaction = new Transaction(doc, "创建平面视图");
transaction.Start();
//创建轴测图
ViewSection view = ViewSection.CreateSection(doc, vid, boundingBox);
transaction.Commit();
//将当前视图跳转到该平面
uiDoc.ActiveView = view;
return Result.Succeeded;
}
/// <summary>
/// Return a section box for a view perpendicular
/// to the given wall location line.
/// </summary>
BoundingBoxXYZ GetSectionViewPerpendiculatToWall(
Wall wall)
{
LocationCurve lc = wall.Location
as LocationCurve;
// Using 0.5 and "true" to specify that the
// parameter is normalized places the transform
// origin at the center of the location curve
Transform curveTransform = lc.Curve
.ComputeDerivatives(0.5, true);
// The transform contains the location curve
// mid-point and tangent, and we can obtain
// its normal in the XY plane:
XYZ origin = curveTransform.Origin;
XYZ viewdir = curveTransform.BasisX.Normalize();
XYZ up = XYZ.BasisZ;
XYZ right = up.CrossProduct(viewdir);
// Set up view transform, assuming wall's "up"
// is vertical. For a non-vertical situation
// such as section through a sloped floor, the
// surface normal would be needed
Transform transform = Transform.Identity;
transform.Origin = origin;
transform.BasisX = right;
transform.BasisY = up;
transform.BasisZ = viewdir;
BoundingBoxXYZ sectionBox = new BoundingBoxXYZ();
sectionBox.Transform = transform;
// Min & Max X values define the section
// line length on each side of the wall.
// Max Y is the height of the section box.
// Max Z (5) is the far clip offset.
double d = wall.WallType.Width;
BoundingBoxXYZ bb = wall.get_BoundingBox(null);
double minZ = bb.Min.Z;
double maxZ = bb.Max.Z;
double h = maxZ - minZ;
sectionBox.Min = new XYZ(-2 * d, -1, 0);
sectionBox.Max = new XYZ(2 * d, h + 1, 5);
return sectionBox;
}
}
}
获得与墙体垂直的平面——用于查看构造详图
最新推荐文章于 2024-08-16 17:58:49 发布