#Revit二开-设备连接(以消火栓为例)
在Revit建模过程中,系统自带的设备连接功能往往让我们对其又爱又恨,爱其自动连接,省掉了好几步操作,恨其傻缺无比,不解风情,不懂我们的想法和需要。例如消火栓连接中 我们想让消火栓连接到管道。用系统自带连接功能出现了奇怪的连接,这不是我们需要的,如下图:
消火栓首先水平出管,这与实际情况是不相符的,我们要让消火栓首先伸出一定距离的管道大约300mm左右的管道,然后垂直向上,最后再水平连接管道,如下图:
- 工程师A:对,就要这个样子!
- BIM建模师:Yes,就这个范!
- 产品经理:对,就按他们说的来!
- 我:哈哈哈,就知道你们是这么想的!
- 怎么实现?写程序呀!
不多说 上代码! 唐僧解瓦 github :https://github.com/binbinstrong/tangsengjiewa
/// <summary>
/// 设备连接 (消火栓)
/// </summary>
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.UsingCommandData)]
class Cmd_ConnectEquipMent : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Selection sel = uidoc.Selection;
View acview = uidoc.ActiveView;
//UIView acuivew = uidoc.Activeuiview();
var familyInsRef = sel.PickObject(ObjectType.Element, doc.GetSelectionFilter(m => m is FamilyInstance));
var pipeRef = sel.PickObject(ObjectType.Element, doc.GetSelectionFilter(m => m is Pipe));
var pipe = pipeRef.GetElement(doc) as Pipe;
var familyIns = familyInsRef.GetElement(doc) as FamilyInstance;
var equipmentCons = familyIns.MEPModel.ConnectorManager.Connectors;
var firstEquipCon = equipmentCons.Cast<Connector>().First(m =>
{
return m.ConnectorType == ConnectorType.Curve || m.ConnectorType == ConnectorType.End;
});
var conRadius = firstEquipCon.Radius;
var condia = conRadius * 2;
var origin = firstEquipCon.Origin;
var conDir = firstEquipCon.CoordinateSystem.BasisZ;
var pipelocationline = pipe.LocationLine();
Transaction ts = new Transaction(doc, "设备连接");
try
{
ts.Start();
//由连接件 水平生成一段管
var firstlineEnd1 = origin;
var firstlineEnd2 = origin + 200d.MetricToFeet() * conDir;
var firsline = Line.CreateBound(firstlineEnd1, firstlineEnd2);
var firstPipe = Pipe.Create(doc, pipe.MEPSystem.GetTypeId(), pipe.GetTypeId(), pipe.ReferenceLevel.Id, firstlineEnd1, firstlineEnd2);
firstPipe.LookupParameter("直径").Set(conRadius * 2);
//生成垂直管道
var secondlineEnd1 = firstlineEnd2;
var secondlineEnd2 = secondlineEnd1 + XYZ.BasisZ * (pipelocationline.StartPoint().Z - secondlineEnd1.Z);
var secondPipe = Pipe.Create(doc, pipe.MEPSystem.GetTypeId(), pipe.GetTypeId(), pipe.ReferenceLevel.Id, secondlineEnd1, secondlineEnd2);
secondPipe.LookupParameter("直径").Set(conRadius * 2);
//生成第三根管道
var thirdlineEnd1 = secondlineEnd2;
var thirdlineEnd2 = thirdlineEnd1.ProjectToXLine(pipelocationline);
var thirdPipe = Pipe.Create(doc, pipe.MEPSystem.GetTypeId(), pipe.GetTypeId(), pipe.ReferenceLevel.Id, thirdlineEnd1, thirdlineEnd2);
thirdPipe.LookupParameter("直径").Set(conRadius * 2);
//链接所有管道
var firstpipeCons = firstPipe.ConnectorManager.Connectors;
foreach (Connector item in firstpipeCons)
{
if (item.ConnectorType == ConnectorType.End || ConnectorType.Curve == item.ConnectorType)
if (firstEquipCon.Origin.IsAlmostEqualTo(item.Origin))
{
item.ConnectTo(firstEquipCon);
}
}
firstPipe.ElbowConnect(secondPipe);
secondPipe.ElbowConnect(thirdPipe);
//链接剩余管道
//Do it yourself!!
//…………
ts.Commit();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
if (ts.GetStatus() == TransactionStatus.Started)
{
ts.RollBack();
}
//throw;
}
return Result.Succeeded;
}
}