using ACadSharp.Entities; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Plumbing; using CSMath; using Microsoft.Win32; using Nice3point.Revit.Toolkit.External; using System; using System.IO; using System.Linq; using System.Text; using Line = Autodesk.Revit.DB.Line; using XYZ = Autodesk.Revit.DB.XYZ; namespace ShrlAlgo.Addin.Test { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class GASInstancesCreator : ExternalCommand { private XYZ XYToRevitXYZ(CSMath.XY point) { return new XYZ(point.X, point.Y, 0) / 304.8; } private XYZ XYZToRevitXYZ(CSMath.XYZ point) { return new XYZ(point.X, point.Y, 0) / 304.8; } public override void Execute() { //var id = UiDocument.Selection.GetElementIds().First(); //var elem = Document.GetElement(id) as FamilyInstance; //var point = elem.GetLocXYZ(); //var pipes = new FilteredElementCollector(Document).OfClass(typeof(Pipe)).Cast(); //Pipe p; //foreach (var pipe in pipes) //{ // var line = pipe.GetCurve() as Line; // var lineOrigin = line.Origin.Flatten(); // var temp = double.MaxValue; // if (line.Direction == XYZ.BasisZ) // { // var dis = point.DistanceTo(lineOrigin); // if (dis < temp) // { // p = pipe; // } // } //} //var conns = elem.MEPModel.ConnectorManager.UnusedConnectors.OfType().FirstOrDefault(); } private void GASCreator() { var dialog = new OpenFileDialog { Filter = "dwg文件(*.dwg)|*.dwg" }; if (dialog.ShowDialog() == true) { var file = dialog.FileName; var cad = new ACadSharp.IO.DwgReader(file).Read(); var polylines = cad.Entities.OfType().Where(l => l.Layer.Name == "RQ" && l.Color.Equals(ACadSharp.Color.ByLayer)); var sizes = cad.Entities.OfType().Where(t => t.Layer.Name == "RQ" && t.Value.Contains("D")); var entities = cad.Entities.OfType().Where(i => i.Layer.Name == "0" || i.Layer.Name == "RQ" || i.Layer.Name == "GAS" || i.Layer.Name == "燃具"); Create(polylines, sizes, entities); } } private void Create(IEnumerable polylines, IEnumerable sizes, IEnumerable entities) { var pipeType = new FilteredElementCollector(Document).OfClass(typeof(PipeType)) .FirstOrDefault(pt => pt.Name == "低压燃气管道"); var systemType = new FilteredElementCollector(Document).OfClass(typeof(PipingSystemType)) .FirstOrDefault(pt => pt.Name == "低压燃气系统"); var mb = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("盲板")) as FamilySymbol; var xs = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("旋塞")) as FamilySymbol; var flqf = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("法兰球阀")) as FamilySymbol; var tqf = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("铜制球阀")) as FamilySymbol; var rqb = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("燃气表")) as FamilySymbol; var zbf = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("自闭阀")) as FamilySymbol; var rsq = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("热水器")) as FamilySymbol; var rqz = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name.Contains("燃气灶")) as FamilySymbol; var qf = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name == "球阀") as FamilySymbol; var sd = new FilteredElementCollector(Document).OfClass(typeof(FamilySymbol)) .FirstOrDefault(pt => pt.Name == "丝堵") as FamilySymbol; var sb = new StringBuilder(); using (var ts = new Transaction(Document, "翻模")) { ts.Start(); foreach (var line in polylines) { var tempDistance = double.MaxValue; TextEntity textNoteNearest = null; var vector = XYZ.BasisX; var locXY = line.Vertices.FirstOrDefault().Location; var firstPoint = XYToRevitXYZ(locXY); var lastPoint = XYToRevitXYZ(line.Vertices.LastOrDefault().Location); if (firstPoint.DistanceTo(lastPoint) < Application.ShortCurveTolerance) { continue; } var rLine = Line.CreateBound(firstPoint, lastPoint); TextEntity size = null; foreach (var text in sizes) { var rotation = text.Rotation; var transform = Autodesk.Revit.DB.Transform.CreateRotation(XYZ.BasisZ, rotation); var textVector = transform.OfVector(vector); XYZ flattenCoord = XYZToRevitXYZ(text.InsertPoint); var currentDistance = rLine.Distance(flattenCoord); if (currentDistance < tempDistance && 1 - rLine.Direction.DotProduct(textVector) < 10E-4) { tempDistance = currentDistance; textNoteNearest = text; size = text; //vector = text.ObliqueAngle; } } try { var first = line.Vertices.First().Location; var last = line.Vertices.Last().Location; var pipe = Pipe.Create( Document, systemType.Id, pipeType.Id, Document.ActiveView.GenLevel.Id, XYToRevitXYZ(first) + 3200 / 304.8 * XYZ.BasisZ, XYToRevitXYZ(last) + 3200 / 304.8 * XYZ.BasisZ); var str = size.Value.Split('D').LastOrDefault(); var str1 = str.Trim('N'); if (double.TryParse(str1, out var d)) { pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(d / 304.8); } } catch (Exception ex) { sb.AppendLine(ex.Message); continue; } } foreach (var insert in entities) { try { if (insert.Block.Name == "立管0") { var temp = double.MaxValue; string t = default; foreach (var item in sizes) { if (item.Value.StartsWith("DN") && item.Rotation < 10E-3) { var current = item.InsertPoint.DistanceFrom(insert.InsertPoint); if (current < temp) { temp = current; t = item.Value.Replace("DN", string.Empty); } } if (item.Value.StartsWith("D") && item.Rotation < 10E-3) { var current = item.InsertPoint.DistanceFrom(insert.InsertPoint); if (current < temp) { temp = current; t = item.Value.Replace("D", string.Empty).Split('*').First(); } } } var pipe = Pipe.Create( Document, systemType.Id, pipeType.Id, Document.ActiveView.GenLevel.Id, XYZToRevitXYZ(insert.InsertPoint), XYZToRevitXYZ(insert.InsertPoint) + 3200 / 304.8 * XYZ.BasisZ); if (double.TryParse(t, out var d)) { pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(d / 304.8); } } //盲板 else if (insert.Block.Name == "盲板") { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 2000 / 304.8 * XYZ.BasisZ, mb, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //旋塞阀 else if (insert.Block.Name.Contains("旋塞")) { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 1100 / 304.8 * XYZ.BasisZ, rqb, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //商业预留,法兰球阀 else if (insert.Block.Name.Contains("法兰球阀")) { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 2000 / 304.8 * XYZ.BasisZ, rqb, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //锁闭型螺纹铜球阀 else if (insert.Block.Name.Contains("户内阀")) { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 1800 / 304.8 * XYZ.BasisZ, tqf, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //燃气表 else if (insert.Block.Name == "计量表") { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 1600 / 304.8 * XYZ.BasisZ, rqb, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //自闭阀 else if (insert.Block.Name == "自闭阀") { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 900 / 304.8 * XYZ.BasisZ, zbf, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //热水器 //else if (insert.Block.Name == "RSQ") //{ // Document.Create // .NewFamilyInstance( // XYZToRevitXYZ(insert.InsertPoint) + 1100 / 304.8 * XYZ.BasisZ, // rsq, // Document.ActiveView.GenLevel, // Autodesk.Revit.DB.Structure.StructuralType.NonStructural); //} //燃气灶 else if (insert.Block.Name == "燃气灶") { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 900 / 304.8 * XYZ.BasisZ, rqz, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } //球阀+丝堵 else if (insert.Block.Name.Contains("丝堵")) { Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 1800 / 304.8 * XYZ.BasisZ, qf, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); Document.Create .NewFamilyInstance( XYZToRevitXYZ(insert.InsertPoint) + 1600 / 304.8 * XYZ.BasisZ, sd, Document.ActiveView.GenLevel, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } } catch (Exception ex) { sb.AppendLine(ex.Message); continue; } } ts.Commit(); if (sb.Length > 0) { File.WriteAllText(@"D:\Users\Zhanggg\Desktop\Errors.txt", sb.ToString(), Encoding.UTF8); } } } } }