Files
RookieStation/RookieStation/Drawing/ExecuteCmds/CmdCreateShelvesLegend.cs
2021-09-09 12:39:12 +08:00

179 lines
9.2 KiB
C#

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<Viewport>(), "请选择参考的视口确定比例尺");
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);
});
}, "创建货架图例");
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
}
}
else
{
TaskDialog.Show("温馨提示", "请打开图纸视图");
return Result.Failed;
}
return Result.Succeeded;
}
private List<ShelfStatistic> GetShelfData(Document doc)
{
var shelves = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Furniture).Cast<FamilyInstance>().Where(s => s.Symbol.FamilyName.Contains("货架"));
var rooms = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).Cast<Room>();
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<ShelfStatistic> statistics = new List<ShelfStatistic>();
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;
}
/// <summary>
/// 创建表格、文字
/// </summary>
/// <param name="doc"></param>
/// <param name="statistics"></param>
/// <param name="newLegend"></param>
/// <param name="viewScale"></param>
private void CreateShelfLegend(Document doc, List<ShelfStatistic> 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<TextNoteType>().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 tableGridHeight = actualTextHeight * viewScale * 1.2;//图例网格高度
double tableGridWidth = tableGridHeight * 5;//图例网格长度
double[] widths = new double[6] { tableGridHeight * 2, tableGridHeight * 2, tableGridHeight * 6, tableGridHeight * 2, tableGridHeight * 2, tableGridHeight * 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 * tableGridWidth * 3).CreateOffset(tableGridHeight * i, -XYZ.BasisZ);
}
else
{
line = Line.CreateBound(XYZ.Zero, XYZ.BasisX * tableGridWidth * 6).CreateOffset(tableGridHeight * i, -XYZ.BasisZ);
}
curveArray.Append(line);
}
for (int i = 0; i < 7; i++)//垂直线
{
Curve line = Line.CreateBound(XYZ.Zero, XYZ.BasisY * (statistics.Count() + 1) * tableGridHeight).CreateOffset(tableGridWidth * i, XYZ.BasisZ);
curveArray.Append(line);
}
var y = XYZ.BasisY * tableGridHeight * (statistics.Count + 0.5);
var opts = new TextNoteOptions(textNoteType.Id)
{
VerticalAlignment = VerticalTextAlignment.Middle,
HorizontalAlignment = HorizontalTextAlignment.Center
};
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * tableGridWidth * 0.5 + y, "货架类型", opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 1.5) + y, "数量", opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 2.5) + y, "总延米", opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 3.5) + y, "总承载单量", opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 4.5) + y, "设计单量", opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 5.5) + y, "场地面积", opts);
for (int i = 0; i < statistics.Count(); i++)
{
var yElevation = XYZ.BasisY * tableGridHeight * (i + 0.5);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * tableGridWidth * 0.5 + yElevation, statistics[i].ShelfType, opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 1.5) + yElevation, statistics[i].Count.ToString(), opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 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 * (tableGridWidth * 3.5) + XYZ.BasisY * tableGridHeight * statistics.Count() / 2, orders.ToString(), opts);
//设计单量
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 4.5) + XYZ.BasisY * tableGridHeight * statistics.Count() / 2, statistics.FirstOrDefault().DesignOrders.ToString(), opts);
//面积
TextNote area = TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 5.5) + XYZ.BasisY * tableGridHeight * 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);
}
}
}