revit 二次开发梁底创建高程点

解决H钢结构标高

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    Document revitDoc = commandData.Application.ActiveUIDocument.Document;
    FilteredElementCollector beamCL = new FilteredElementCollector(revitDoc).OfCategory(BuiltInCategory.OST_StructuralFraming).OfClass(typeof(FamilyInstance));//获取视图中所有的梁
    View view = commandData.Application.ActiveUIDocument.ActiveView;

    Reference instance = commandData.Application.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "选择一个梁");

    if (instance != null)
    {
        FamilyInstance familyInstance = revitDoc.GetElement(instance.ElementId) as FamilyInstance;
        Options options = new Options()
        {
            ComputeReferences = true,
            DetailLevel = ViewDetailLevel.Fine
        };
        GeometryElement geometryElement = familyInstance.get_Geometry(options);
        GetBeamFaces(geometryElement);

        faces.OrderBy(p=>p.Origin.Z);
        faces.Reverse();
        LocationCurve curve = familyInstance.Location as LocationCurve;
        XYZ origin = curve.Curve.Evaluate(0.5, true);

        Reference faceRef = faces[0].Reference;
        string referStr = faceRef.ConvertToStableRepresentation(revitDoc);
        string actualStr = $"{instance.ConvertToStableRepresentation(revitDoc)}:0:INSTANCE:{referStr}";
        Reference reference = Reference.ParseFromStableRepresentation(revitDoc, actualStr);

        using (Transaction tr = new Transaction(revitDoc, "lg"))
        {
            tr.Start();
            try
            {
                SpotDimension spotDimension = revitDoc.Create.NewSpotElevation(view, reference, origin, origin, origin, origin, false);
            }
            catch (Exception ex)
            {
                TaskDialog.Show("提示", $"{ex.Message}");
            }
            tr.Commit();
        }
    }
    return Result.Succeeded;

}
List<PlanarFace> faces = new List<PlanarFace>();
private void GetBeamFaces(GeometryElement geometryElement)
{
    foreach (GeometryObject item in geometryElement)
    {
        Solid solid = item as Solid;
        if (solid != null)
        {
            foreach (Face face in solid.Faces)
            {
                PlanarFace planarFace = face as PlanarFace;
                if (planarFace != null)
                {
                    if (planarFace.FaceNormal.IsAlmostEqualTo(-XYZ.BasisZ))
                    {
                        faces.Add(planarFace);
                    }
                }
            }
        }
        GeometryInstance geometryInstance = item as GeometryInstance;
        if (geometryInstance != null)
        {
            GeometryElement subGE = geometryInstance.GetInstanceGeometry();
            GetBeamFaces(subGE);
        }
    }
}
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值