看到已经有大神实现了,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();
}
}
}
下面放几张图