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;
}
}
}
}