Revit二次開發之間隙打斷

Revit二次開發之間隙打斷

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.Attributes;
using Autodesk.Revit.UI.Selection;

namespace 間隙打斷
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.UsingCommandData)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection selection = uidoc.Selection;
            while (true)
            {
                try
                {
                    using (Transaction ts = new Transaction(doc))
                    {
                        ts.Start("間隙打斷");
                        Reference refElement = selection.PickObject(ObjectType.PointOnElement, "請選擇需要打斷的點");
                        Reference refElement1 = selection.PickObject(ObjectType.PointOnElement, "請選擇第二個點");
                        XYZ point = refElement.GlobalPoint;
                        XYZ point1 = refElement1.GlobalPoint;
                        Element elem = doc.GetElement(refElement);
                        LocationCurve lc = elem.Location as LocationCurve;

                        XYZ newPoint = lc.Curve.Project(point).XYZPoint;
                        XYZ newPoint1 = lc.Curve.Project(point1).XYZPoint;

                        XYZ startPoint = lc.Curve.GetEndPoint(0);
                        XYZ endPoint = lc.Curve.GetEndPoint(1);

                        Line l1 = Line.CreateBound(startPoint, GetNearPoint(startPoint,newPoint1,newPoint));
                        Line l2 = Line.CreateBound(GetNearPoint(endPoint,newPoint,newPoint1), endPoint);
                        Element el1 = doc.GetElement(ElementTransformUtils.CopyElement(doc, elem.Id, new XYZ(1, 0, 0)).First());
                        Element el2 = doc.GetElement(ElementTransformUtils.CopyElement(doc, elem.Id, new XYZ(1, 0, 0)).First());
                        (el1.Location as LocationCurve).Curve = l1;
                        (el2.Location as LocationCurve).Curve = l2;
                        doc.Delete(elem.Id);
                        ts.Commit();
                    }
                }
                catch
                {
                    break;
                }
            }


            return Result.Succeeded;
        }
        public static XYZ GetNearPoint(XYZ origin,XYZ pt1,XYZ pt2)
        {
            if(origin.DistanceTo(pt1) >= origin.DistanceTo(pt2))
            {
                return pt2;
            }
            else
            {
                return pt1;
            }
        }
    }
}

效果:點擊我查看效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章