Revit二次開發之打斷構建

打斷LocationCurve Drived Elements

這個功能實現了打斷一個可以獲取到LocationCurve構建的連續操作

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using System.Linq;

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, "請選擇需要打斷的點");
                        XYZ point = refElement.GlobalPoint;
                        Element elem = doc.GetElement(refElement);
                        LocationCurve lc = elem.Location as LocationCurve;
                        XYZ newPoint = lc.Curve.Project(point).XYZPoint;
                        if(lc==null)
                        {
                            TaskDialog.Show("Error", "您選擇的構建不可以分割,請重新選擇");
                            continue;
                        }
                        XYZ startPoint = lc.Curve.GetEndPoint(0);
                        XYZ endPoint = lc.Curve.GetEndPoint(1);

                        Line l1 = Line.CreateBound(startPoint, newPoint);
                        Line l2 = Line.CreateBound(newPoint, 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;
        }
    }
}

效果展示:

點我查看效果

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