using Autodesk.Revit.DB; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using RookieStation.Drawing.Models; using RookieStation.Extension; using RookieStation.ProjectConfig; using RookieStation.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Controls; namespace RookieStation.Drawing.ExecuteCmds { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] internal class CmdCreateShelvesLegend : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; if (uidoc.ActiveView.ViewType == ViewType.DrawingSheet) { var portRefer = uidoc.Selection.PickObject(ObjectType.Element, new SelectFilter(), "请选择参考的视口确定比例尺"); var location = uidoc.Selection.PickPoint(UserConstant.SnapAll, "请选择左下角点放置图例"); var viewport = doc.GetElement(portRefer) as Viewport; var viewPlan = doc.GetElement(viewport.ViewId); if (!(viewPlan is ViewPlan)) { TaskDialog.Show("温馨提示", "请选择平面视图的视口"); return Result.Failed; } try { doc.InvokeGroup(tg => { var viewScale = viewport.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger(); View legend = null; doc.Invoke(ts => { legend = RsRevitUtils.CreateNewLegend(doc, "货架说明", viewScale); }); doc.Invoke(ts => { var shelvesData = GetShelfData(doc); CreateShelfLegend(doc, shelvesData, legend, viewScale); }); doc.Invoke(ts => { RsRevitUtils.SetLegendLocation(doc, legend, location); }); }, "创建货架图例"); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { } } else { TaskDialog.Show("温馨提示", "请打开图纸视图"); return Result.Failed; } return Result.Succeeded; } private List GetShelfData(Document doc) { var shelves = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Furniture).Cast().Where(s => s.Symbol.FamilyName.Contains("货架")); var rooms = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).Cast(); double area = 0.0; foreach (var room in rooms) { area += RsRevitUtils.ConvertSquareFeetToSquareMetre(room.get_Parameter(BuiltInParameter.ROOM_AREA).AsDouble()); } var shelfGroup = shelves.GroupBy(g => g.Name); List statistics = new List(); foreach (var group in shelfGroup) { var instance = group.ElementAt(0); var l = instance.Symbol.GetParameters("长度").FirstOrDefault().AsValueString(); ShelfStatistic shelfStatistic = new ShelfStatistic { ShelfType = instance.Name, Count = group.Count(), GroundArea = Math.Round(area, 2), DesignOrders = UserConstant.Orders }; shelfStatistic.LinearMetre = shelfStatistic.Count * Convert.ToDouble(l) / 1000; shelfStatistic.CapacityOrders = Convert.ToInt32(shelfStatistic.LinearMetre * 80); statistics.Add(shelfStatistic); } return statistics; } /// /// 创建表格、文字 /// /// /// /// /// private void CreateShelfLegend(Document doc, List statistics, View newLegend, int viewScale) { TextNoteType textNoteType = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).Where(x => x.Name.Contains(UserConstant.FontName)).FirstOrDefault() as TextNoteType; if (textNoteType == null) { textNoteType = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).Cast().FirstOrDefault().Duplicate(UserConstant.FontName) as TextNoteType; textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).SetValueString("2.5"); textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).SetValueString("0.8"); } double fontSize = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble(); double actualTextHeight = fontSize * 1.6; double tableCellHeight = actualTextHeight * viewScale * 1.2;//图例网格高度 double tableCellWidth = tableCellHeight * 5;//图例网格长度 double[] widths = new double[6] { tableCellHeight * 2, tableCellHeight * 2, tableCellHeight * 6, tableCellHeight * 2, tableCellHeight * 2, tableCellHeight * 2 }; CurveArray curveArray = new CurveArray(); for (int i = statistics.Count + 1; i >= 0; i--)//水平线 { Curve line; if (i > 0 && i < statistics.Count) { line = Line.CreateBound(XYZ.Zero, XYZ.BasisX * tableCellWidth * 3).CreateOffset(tableCellHeight * i, -XYZ.BasisZ); } else { line = Line.CreateBound(XYZ.Zero, XYZ.BasisX * tableCellWidth * 6).CreateOffset(tableCellHeight * i, -XYZ.BasisZ); } curveArray.Append(line); } for (int i = 0; i < 7; i++)//垂直线 { Curve line = Line.CreateBound(XYZ.Zero, XYZ.BasisY * (statistics.Count() + 1) * tableCellHeight).CreateOffset(tableCellWidth * i, XYZ.BasisZ); curveArray.Append(line); } var y = XYZ.BasisY * tableCellHeight * (statistics.Count + 0.5); var opts = new TextNoteOptions(textNoteType.Id) { VerticalAlignment = VerticalTextAlignment.Middle, HorizontalAlignment = HorizontalTextAlignment.Center }; TextNote.Create(doc, newLegend.Id, XYZ.BasisX * tableCellWidth * 0.5 + y, "货架类型", opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 1.5) + y, "数量", opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 2.5) + y, "总延米", opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 3.5) + y, "总承载单量", opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 4.5) + y, "设计单量", opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 5.5) + y, "场地面积", opts); for (int i = 0; i < statistics.Count(); i++) { var yElevation = XYZ.BasisY * tableCellHeight * (i + 0.5); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * tableCellWidth * 0.5 + yElevation, statistics[i].ShelfType, opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 1.5) + yElevation, statistics[i].Count.ToString(), opts); TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 2.5) + yElevation, statistics[i].LinearMetre.ToString(), opts); } //承载单量总和 int orders = 0; for (int i = 0; i < statistics.Count; i++) { orders += statistics[i].CapacityOrders; } TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 3.5) + XYZ.BasisY * tableCellHeight * statistics.Count() / 2, orders.ToString(), opts); //设计单量 TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 4.5) + XYZ.BasisY * tableCellHeight * statistics.Count() / 2, statistics.FirstOrDefault().DesignOrders.ToString(), opts); //面积 TextNote area = TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableCellWidth * 5.5) + XYZ.BasisY * tableCellHeight * statistics.Count() / 2, statistics[0].GroundArea + "m2".ToString(), opts); FormattedText formattedText = new FormattedText(area.Text); TextRange range = new TextRange(area.Text.Length - 2, 1); formattedText.SetSuperscriptStatus(range, true); area.SetFormattedText(formattedText); var curves = doc.Create.NewDetailCurveArray(newLegend, curveArray); } } }