using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Electrical; using Autodesk.Revit.UI; using FWindSoft.Revit; using FWindSoft.Revit.Menu; using FWindSoft.Revit.Mep; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LRH.Tool { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] [Button(ButtonName = "创建线管",Index =-1)] public class ConduitJoinCommand : ExternalCommand { public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { try { double length = 800d.MmToFt(); var document = commandData.Application.ActiveUIDocument.Document; using (Transaction tran = new Transaction(document, "管道链接测试")) { try { tran.Start(); var conduitType = document.GetConduitTypes().FirstOrDefault(); if (conduitType == null) { TaskDialog.Show("提示", "不存在线管类型"); } List> conduitGroups = new List>(); XYZ basePoint = XYZ.Zero; List line1 = new List(); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length*XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisY * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = XYZ.Zero + 3 * length * XYZ.BasisY; basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length)); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisY * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); basePoint = basePoint + 3 * length * XYZ.BasisX; line1 = new List(); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length)); line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length)); line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length)); conduitGroups.Add(CreateConduits(document, conduitType, line1)); #region 创建连接件 ConduitJoin conduitJoin = new ConduitJoin(); foreach (List conduits in conduitGroups) { try { var count = conduits.Count; var elementWraps = conduits.Select(e => new JoinElementWraper(e)).ToList(); switch (count) { case 3: { conduitJoin.Join3Curve(elementWraps, new JoinContext() { JunctionBoxPlace= JunctionBoxPlaceType.NoAdjustment }); break; } } } catch (Exception ex) { // throw; } } #endregion tran.Commit(); } catch (Exception) { tran.RollBack(); } } } catch (Exception ex) { //throw; } return Result.Succeeded; //加入全局异常捕获 } private List CreateConduits(Document document,ConduitType conduitType,List lines) { List conduits = new List(); foreach (Line line in lines) { try { Transform angle45 = Transform.CreateRotationAtPoint(XYZ.BasisZ, Math.PI / 5, line.StartPoint()); var useline = line.CreateTransformed(angle45) as Line; //LevelId可以使用不能使用null,又没有的情况。可以使用 ElementId.InvalidElementId conduits.Add(Conduit.Create(document, conduitType.Id, useline.StartPoint(), useline.EndPoint(), ElementId.InvalidElementId)); } catch (Exception e) { } } return conduits; } } }