Revit二次开发 ---->一点翻弯

1.交互选择两个点

        第一个点是翻弯起点,第二个点是判断翻弯方向

2.偏移定位线

3.根据定位线建立管道

4.创建弯头

实现代码如下

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


namespace Test
{
    [Transaction(TransactionMode.Manual)]
    public class TestCommand : IExternalCommand

    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument Uidoc = commandData.Application.ActiveUIDocument;
            Document Doc = Uidoc.Document;



            Reference point1refer = Uidoc.Selection.PickObject(ObjectType.PointOnElement,new PipeFilter());
            Reference point2refer = Uidoc.Selection.PickObject(ObjectType.PointOnElement,new PipeFilter());
            

            Pipe pipe = Doc.GetElement(point1refer) as Pipe;
            Curve pipeline = (pipe.Location as LocationCurve).Curve;

            XYZ point1 = point1refer.GlobalPoint;
            XYZ point2 = point2refer.GlobalPoint;

            XYZ startpoint = pipeline.GetEndPoint(0);
            XYZ endpoint = pipeline.GetEndPoint(1);

            //投影到管道中心线上

            XYZ point1project = pipeline.Project(point1).XYZPoint;
            XYZ point2project = pipeline.Project(point2).XYZPoint;


            Curve newpipeline;
            Curve shupipeline;
            double offsetvalue=5;


            //判断翻弯方向
            if (startpoint.DistanceTo(point1project)<startpoint.DistanceTo(point2project))
            {
                XYZ newend = endpoint + new XYZ(0,0,offsetvalue);
                XYZ newpoint1=point1project+ new XYZ(0, 0, offsetvalue);
                newpipeline = Line.CreateBound(newend,newpoint1);
                shupipeline = Line.CreateBound(point1project, newpoint1);
                pipeline = Line.CreateBound(startpoint,point1project);

            }
            else
            {

                XYZ newstart = startpoint + new XYZ(0, 0, offsetvalue);
                XYZ newpoint1 = point1project + new XYZ(0, 0, offsetvalue);
                newpipeline = Line.CreateBound(newstart, newpoint1);
                shupipeline = Line.CreateBound(point1project, newpoint1);
                pipeline = Line.CreateBound(endpoint,point1project);
            }


            //创建管道
            Transaction transaction = new Transaction(Doc,"fuzhi");
            transaction.Start("fuzhi");
            Pipe newpipe= ElementOperate.Copyself(pipe,Doc) as Pipe;
            (newpipe.Location as LocationCurve).Curve = newpipeline;
            Pipe shupipe = ElementOperate.Copyself(pipe,Doc) as Pipe;
            (shupipe.Location as LocationCurve).Curve = shupipeline;
            (pipe.Location as LocationCurve).Curve = pipeline;

            //创建弯头
            Pipeconnectutils.ConnectTwopipesWithElbow(Doc, newpipe as MEPCurve,shupipe as MEPCurve);
            Pipeconnectutils.ConnectTwopipesWithElbow(Doc,pipe as MEPCurve,shupipe as MEPCurve);

            transaction.Commit();


            
            return Result.Succeeded;

        }


        //限制选择
        public class PipeFilter : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                if (elem is Pipe)
                {
                    return true;
                }
                return false;
            }

            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;
            }
        }


       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值