Files
RookieStation/RookieStation/Drawing/ExecuteCmds/CreateFurnitureLegend.cs
2021-12-03 18:21:26 +08:00

157 lines
7.5 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using RookieStation.Drawing.Models;
using RookieStation.Utils;
using RookieStation.ProjectConfig;
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 CreateFurnitureLegend : 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)
{
try
{
var portRefer = uidoc.Selection.PickObject(ObjectType.Element, new SelectFilter<Viewport>(), "请选择参考的视口以确定比例");
var location = uidoc.Selection.PickPoint(UserConstant.SnapAll, "请选择左下角点放置图例");
var viewport = doc.GetElement(portRefer) as Viewport;
var viewPlan = doc.GetElement(viewport.ViewId);
if (!(viewPlan is ViewPlan))
{
message = "请选择平面视图的视口";
//TaskDialog.Show("温馨提示", "请选择平面视图的视口");
return Result.Failed;
}
doc.InvokeGroup(tg =>
{
var viewScale = viewport.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger();
View legend = null;
doc.Invoke(ts =>
{
legend = RsRevitUtils.NewLegend(doc, "家具图例", viewScale);
});
doc.Invoke(ts =>
{
var furnitureData = GetData(doc, viewport.ViewId);
if (furnitureData.Count > 0)
{
CreateLegendContent(doc, furnitureData, legend);
}
});
doc.Invoke(ts =>
{
RsRevitUtils.SetLegendLocation(doc, legend, location);
});
}, "创建家具图例");
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
}
}
else
{
message = "请打开图纸视图";
//TaskDialog.Show("温馨提示", "请打开图纸视图");
return Result.Failed;
}
return Result.Succeeded;
}
/// <summary>
/// 获取货架数据
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private List<dynamic> GetData(Document doc, ElementId viewId)
{
var furnitures = new FilteredElementCollector(doc, viewId).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Furniture).Cast<FamilyInstance>();
//按族分组
var familyGroups = furnitures.GroupBy(g => g.Symbol.FamilyName);
var data = new List<dynamic>();
//遍历每个族的分组
foreach (var instanceByFamily in familyGroups)
{
//按族类型分组
var symbolGroups = instanceByFamily.GroupBy(g => g.Name);
//遍历每个族类型
foreach (var instancesBySymbol in symbolGroups)
{
var instance = instancesBySymbol.ElementAt(0);
var n = instancesBySymbol.Count();
if (instance.Symbol.FamilyName.Contains("货架"))
{
foreach (var fi in instancesBySymbol)
{
var jdStr = fi.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString();
if (jdStr != null)
n -= 1;
}
}
if (true)
{
}
dynamic datarow = new { FamilyName = instanceByFamily.Key, SymbolName = instancesBySymbol.Key, Count = n.ToString() };
data.Add(datarow);
}
}
dynamic headrow = new { FamilyName = "家具类型", SymbolName = "家具尺寸", Count = "数量" };
data.Add(headrow);
return data;
}
private void CreateLegendContent(Document doc, List<dynamic> data, View newLegend)
{
TextNoteType textNoteType = RsRevitUtils.GetOrNewTextNoteType(doc, UserConstant.TextSymbolName);
double fontSize = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
double tableGridHeight = fontSize * newLegend.Scale * 2;//图例网格高度
double tableGridWidth = tableGridHeight * 2.5;
double[] widths = new double[3] { tableGridWidth * 2, tableGridWidth * 3, tableGridWidth };
RsRevitUtils.CreateTable(doc, newLegend, data.Count, 3, tableGridHeight, widths);
var opts = new TextNoteOptions(textNoteType.Id)
{
HorizontalAlignment = HorizontalTextAlignment.Center,
VerticalAlignment = VerticalTextAlignment.Middle,
};
for (int i = 0; i < data.Count; i++)
{
var y = XYZ.BasisY * (i + 0.5) * tableGridHeight;
var datarow = data[i];
//doc.Create.NewFamilyInstance(XYZ.BasisX * (tableGridWidth / 2) + y, datarow.GetType().GetProperty("FamilyName").GetValue(datarow).ToString(), newLegend);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth) + y, datarow.GetType().GetProperty("FamilyName").GetValue(datarow).ToString(), opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 7 / 2) + y, datarow.GetType().GetProperty("SymbolName").GetValue(datarow).ToString(), opts);
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 11 / 2) + y, datarow.GetType().GetProperty("Count").GetValue(datarow).ToString(), opts);
//TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 7 / 2) + y, $"{item.Count}个", opts);
//LegendItem item = data[i];
//y = XYZ.BasisY * (i + 0.5) * tableGridHeight;
//if (item.PlaneLegendType != null)
//{
// doc.Create.NewFamilyInstance(XYZ.BasisX * (tableGridWidth / 2) + y, item.PlaneLegendType, newLegend);
//}
//TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 3 / 2) + y, item.Description, opts);
//if (item.ElevationLegend != null)
//{
// doc.Create.NewFamilyInstance(XYZ.BasisX * (tableGridWidth * 5 / 2) + y, item.ElevationLegend, newLegend);
//}
//if (item.Count != 0)
//{
// TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 7 / 2) + y, $"{item.Count}个", opts);
//}
}
}
}
}