123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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<List<Conduit>> conduitGroups = new List<List<Conduit>>();
- XYZ basePoint = XYZ.Zero;
- List<Line> line1 = new List<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Line>();
- 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<Conduit> 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<Conduit> CreateConduits(Document document,ConduitType conduitType,List<Line> lines)
- {
- List<Conduit> conduits = new List<Conduit>();
- 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;
- }
- }
- }
|