欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Revit二开算法--根据圆弧上的点分割圆弧

程序员文章站 2022-07-12 14:57:18
...

Revit二开算法–根据圆弧上的点分割圆弧

在revit二开工作中,基础算法很重要,例如有同学问,怎么用圆弧上的点分割圆弧,这是基本算法,根据圆弧上的点与圆弧中心和起点构成的角度依次排序,并生成新的圆弧线 删除旧的圆弧线即可。
实现效果如下Revit二开算法--根据圆弧上的点分割圆弧

代码如下:

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

            var uiapp = commandData.Application;
            var uidoc = uiapp.ActiveUIDocument;
            var doc = uidoc.Document;
            var sel = uidoc.Selection;

            //1 选择圆弧

            var arcline = sel.PickObject(ObjectType.Element).GetElement(doc) as ModelArc;

            var points = new List<XYZ>();

            //循环选点
            while (true)
            {
                try
                {
                    var point = sel.PickObject(ObjectType.PointOnElement).GlobalPoint;
                    points.Add(point);
                }
                catch
                {
                    break;
                }
            }


            var arc = (arcline.Location as LocationCurve).Curve as Arc;

            var arcCenter = arc.Center;
            var arcRadius = arc.Radius;

            //排除不在弧上点
            points = points.Where(m => m.DistanceTo(arcCenter) - arcRadius < 1e-3).ToList();


            var arcstartPo = arc.GetEndPoint(0);
            var arcendPo = arc.GetEndPoint(1);
            var arcNorm = arc.Normal;

            //将圆弧起始点加入列表
            points.Add(arcstartPo);
            points.Add(arcendPo);

            var poanpairList = new List<PoAnglePair>();
            //MessageBox.Show("共有点数:"+points.Count.ToString());

            foreach (var point in points)
            {
                var poanpair = new PoAnglePair(arc, point);
                poanpairList.Add(poanpair);
            }



            //排序 点角度对排序
            poanpairList = poanpairList.OrderBy(m => { return m.angle(); }).ToList();
 
            var arclist = new List<Arc>();
            for (int i = 1; i < poanpairList.Count; i++)
            {
                var poanpair = poanpairList.ElementAt(i);
                var startangle = poanpairList.ElementAt(i - 1).angle();
                var endangle = poanpairList.ElementAt(i).angle();
                var newstartangle = startangle < endangle ? startangle : endangle;
                var newendangle = endangle > startangle ? endangle : startangle;

                var newarc = Arc.Create(arcCenter, arcRadius, newstartangle, newendangle, arc.XDirection, arc.YDirection);
                arclist.Add(newarc);
            }

            doc.Invoke(m =>
            {
                var sketchplane = SketchPlane.Create(doc, new Plane(arcNorm, arcCenter));
                foreach (var arc1 in arclist)
                {
                    doc.Create.NewModelCurve(arc1, sketchplane);
                }

                doc.Delete(arcline.Id);

            }, "分割圆弧线");

            return Result.Succeeded;
        }
    }

    public class PoAnglePair
    {
        public PoAnglePair(Arc arc, XYZ po)
        {
            originalArc = arc;
            point = po;
        }
        public Arc originalArc;

        public XYZ point;

        public double angle()
        {
            double result = default(double);

            var arc = originalArc;

            var arcCenter = arc.Center;
            var arcRadius = arc.Radius;
            var arcstartPo = arc.GetEndPoint(0);
            var arcendPo = arc.GetEndPoint(1);
            var arcNorm = arc.Normal;

            var vector0 = (arcstartPo - arcCenter).Normalize();
            var vector1 = (point - arcCenter).Normalize();

            result = vector0.AngleOnPlaneTo(vector1, arcNorm);

            return result;
        }

    }
更多Revit二开知识尽在唐僧课堂!

Revit二开算法--根据圆弧上的点分割圆弧

Revit二开算法--根据圆弧上的点分割圆弧

相关标签: Revit二次开发