梁齐斜板之常见插件做法

梁齐斜板

常见的梁齐斜板

根据常见插件梁齐板插件,会出现一个问题,板上布置梁功能好像不是很理想(当然普遍都是板下布置梁)
在这里插入图片描述
下面代码有序包含WPF部分内容,所以不要之间引用,思路可以参考一下

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlignedBeamWPF
{
    [Transaction(TransactionMode.Manual)]
    class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {


            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            FloorISelection floorISelection = new FloorISelection();


            MainWindow wpf = new MainWindow();//实例化主窗口
            wpf.ShowDialog();//展示界面

            if (!wpf.IsClickClose)
            {
                return Result.Cancelled;
            }

            Reference reference = uidoc.Selection.PickObject(ObjectType.Element, floorISelection, "选择楼板");
            //获得元素表面
            Floor floor = doc.GetElement(reference) as Floor;

            Reference bottomFaceRef = HostObjectUtils.GetBottomFaces(floor).First();

            Reference GetTopFaceRef = HostObjectUtils.GetTopFaces(floor).First();
            Reference targetFace;
            if (wpf.btnselect)
            {
                targetFace = GetTopFaceRef;
            }
            else
                targetFace = bottomFaceRef;

            Face face = floor.GetGeometryObjectFromReference(targetFace) as Face;

            BeamISelection beamISelection = new BeamISelection();
            IList<Reference> beamReferences = uidoc.Selection.PickObjects(ObjectType.Element, beamISelection, "框选所需操作的梁");
            foreach (var refer in beamReferences)
            {
                FamilyInstance beam = doc.GetElement(refer) as FamilyInstance;
                //获取梁定位线
                LocationCurve beamLocation = beam.Location as LocationCurve;
                Line bLine = beamLocation.Curve as Line;
                //获取梁的两个端点
                XYZ pStart = bLine.GetEndPoint(0);
                XYZ pEnd = bLine.GetEndPoint(1);
                //通过两个端点做z方向直线,然后利用直线与面的交点求出对应位置的梁端点
                Line lStart = Line.CreateUnbound(pStart, XYZ.BasisZ);
                Line lEnd = Line.CreateUnbound(pEnd, XYZ.BasisZ);

                XYZ sPoint = IntersectPointOfFaceAndCurve(face, lStart);
                XYZ ePoint = IntersectPointOfFaceAndCurve(face, lEnd);
                Transaction transaction = new Transaction(doc, "梁随板");
                transaction.Start();
                beamLocation.Curve = Line.CreateBound(sPoint, ePoint);
                transaction.Commit();
            }
            return Result.Succeeded;
        }
        class FloorISelection : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                Categories categories = elem.Document.Settings.Categories;
                if (elem is Floor && elem.Category.Id == categories.get_Item(BuiltInCategory.OST_StructuralFoundation).Id)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                ;//返回true时内容可被鼠标选定
            }
            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;//返回true时表示内容可以被边、点、面选中
            }
        }
        class BeamISelection : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                Categories categories = elem.Document.Settings.Categories;
                if (elem is FamilyInstance && elem.Category.Id == categories.get_Item(BuiltInCategory.OST_StructuralFraming).Id)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                ;//返回true时内容可被鼠标选定
            }
            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;//返回true时表示内容可以被边、点、面选中
            }
        }

        #region IntersectPointOfFaceAndCurve:求面与线的交点
        public XYZ IntersectPointOfFaceAndCurve(Face face, Curve curve)
        {
            //交点数组
            IntersectionResultArray result = new IntersectionResultArray();
            //枚举,用于判断相交类型
            SetComparisonResult setResult = face.Intersect(curve, out result);
            XYZ interResult = null;
            //Disjoint为不相交
            if (SetComparisonResult.Disjoint != setResult)
            {
                //isEmpty判断是否为空
                if (!result.IsEmpty)

                    interResult = result.get_Item(0).XYZPoint;
            }
            return interResult;
        }
        #endregion
    }
}


猜你喜欢

转载自blog.csdn.net/waiting233/article/details/117375715