Revit中MEP避让功能的实现

看到已经有大神实现了,MEP构建避让的功能,我也试着写了一下插件。但是,因为能力有限暂时还不能生成相应的弯头,我后面会补上的!!!

下面直接上代码:

 

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


namespace 管道打断
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]

    public class BreakPipe : IExternalCommand
    {

       

        public static LocationCurve Check(Element el)
        {
            if (el.Category.Name == "风管")
            {
                Duct duct = el as Duct;
                LocationCurve lc = duct.Location as LocationCurve;
                return lc;
            }


            else if (el.Category.Name == "管道")
            {
                Pipe pp = el as Pipe;
                LocationCurve lc = pp.Location as LocationCurve;
                return lc;
            }


            else
            {
                CableTray cc = el as CableTray;
                LocationCurve lc = cc.Location as LocationCurve;
                return lc;
            }
        }

        public void MepBreak(Document doc,Selection sel)
        {
            //获得需要打断的MEP
   
            Reference reff = sel.PickObject(ObjectType.Element, new MEPSelection(), "请选择需要打断的MEP构建");

            //获得需要打断的MEP的位置信息

            Element el = doc.GetElement(reff);
         LocationCurve lc=   Check(el);
            //MEP上获得两个点
            XYZ p1 = sel.PickPoint("请选择第一个打断点");
            XYZ projectFirstPoint = lc.Curve.Project(p1).XYZPoint;       
            XYZ p2 = sel.PickPoint("请选择第一个打断点");
            XYZ projectSecondPoint = lc.Curve.Project(p2).XYZPoint;
            //通过判断得到管道的新的位置基线
            Curve newCurve1 = null;
            Curve newCurve2 = null;
            Curve newCurve3 = Line.CreateBound(new XYZ(projectFirstPoint.X,projectFirstPoint.Y,projectFirstPoint.Z+1000/304.8), new XYZ(projectSecondPoint.X,projectSecondPoint.Y,projectSecondPoint.Z+1000/304.8)) as Curve;
           Curve newCurve4 = Line.CreateBound(new XYZ(projectFirstPoint.X, projectFirstPoint.Y, projectFirstPoint.Z + 1000 / 304.8), projectFirstPoint);
            Curve newCurve5 = Line.CreateBound(new XYZ(projectSecondPoint.X, projectSecondPoint.Y, projectSecondPoint.Z+1000/304.8), projectSecondPoint);
            if (projectFirstPoint.DistanceTo(lc.Curve.GetEndPoint(0)) < projectFirstPoint.DistanceTo(lc.Curve.GetEndPoint(1)))
            {
                newCurve1 = Line.CreateBound(lc.Curve.GetEndPoint(0), projectFirstPoint) as Curve;
                newCurve2 = Line.CreateBound(projectSecondPoint, lc.Curve.GetEndPoint(1)) as Curve;

            }
            else
            {
                newCurve1 = Line.CreateBound(projectSecondPoint, lc.Curve.GetEndPoint(1)) as Curve;
                newCurve2 = Line.CreateBound(lc.Curve.GetEndPoint(0), projectFirstPoint) as Curve;
            }

            //将原来的管道复制一份,并得到其位置信息
            LocationCurve copyLocationCurve = Copy(doc, el);
            LocationCurve copy3 = Copy(doc, el);
            LocationCurve copy4 = Copy(doc, el);
            LocationCurve copy5 = Copy(doc, el);


            //设置新的位置基线
            lc.Curve = newCurve1;
            copyLocationCurve.Curve = newCurve2;
            copy3.Curve = newCurve3;
           copy4.Curve = newCurve4;
            copy5.Curve = newCurve5;
        }
       //public static  List<Element> elementList = new List<Element>();
        public static LocationCurve Copy(Document doc, Element el)
        {

            if (el.Category.Name == "风管")
            {
                Duct duct = el as Duct;
                Duct copyDuct = doc.GetElement(ElementTransformUtils.CopyElement(doc, duct.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Duct;
              
                LocationCurve copyCurve = copyDuct.Location as LocationCurve;
                return copyCurve;
            }


            else if (el.Category.Name == "管道")
            {
                Pipe pp = el as Pipe;
                Pipe copyPipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, pp.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Pipe;
                LocationCurve copyCurve = copyPipe.Location as LocationCurve;
                return copyCurve;
            }


            else
            {
                CableTray cc = el as CableTray;
                CableTray copyPipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, cc.Id, new XYZ(0, 0, 0)).ElementAt(0)) as CableTray;
                LocationCurve copyCurve = copyPipe.Location as LocationCurve;
                return copyCurve;
            }
        }

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // throw new NotImplementedException();
            try
            {
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                Document doc = uidoc.Document;
                View view = uidoc.ActiveView;
                Selection selection = uidoc.Selection;
               // Duct.Create
                using (Transaction ts = new Transaction(doc, "打断管道"))
                {
                    ts.Start();
                    MepBreak(doc, selection);
                    ts.Commit();
                }
                return Result.Succeeded;

            }

            catch (Exception e)
            {
                message = e.Message;
                return Result.Failed;

            }


        }
    }
    public class MEPSelection : ISelectionFilter
    {

        public bool AllowElement(Element elem)
        {
            if (elem.Category.Name == "管道" || elem.Category.Name == "风管" || elem.Category.Name == "电缆桥架")
            {
                return true;
            }
            else return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            throw new NotImplementedException();
        }
    }
}
 

下面放几张图

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值