161 lines
8.2 KiB
C#
161 lines
8.2 KiB
C#
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.UI.Selection;
|
|
using Autodesk.Revit.UI;
|
|
using RookieStation.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using RookieStation.Extension;
|
|
using System.Windows.Controls;
|
|
using RookieStation.Drawing.Models;
|
|
using RookieStation.ProjectConfig;
|
|
|
|
namespace RookieStation.Drawing.ExecuteCmds
|
|
{
|
|
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
|
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
|
|
internal class CmdCreateLightLegend : IExternalCommand
|
|
{
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
UIDocument uidoc = commandData.Application.ActiveUIDocument;
|
|
Document doc = uidoc.Document;
|
|
|
|
if (doc.ActiveView.ViewType == ViewType.DrawingSheet)
|
|
{
|
|
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))
|
|
{
|
|
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 items = GetLightData(doc);
|
|
CreateLightLegend(doc, GetLightData(doc), 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<LegendItem> GetLightData(Document doc)
|
|
{
|
|
var lights = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_LightingFixtures).Cast<FamilyInstance>().Where(s => s.Symbol.FamilyName.Contains("灯"));
|
|
var lightFamilyGroup = lights.GroupBy(g => g.Symbol.FamilyName);//按族分组
|
|
List<LegendItem> items = new List<LegendItem>();
|
|
|
|
var lightAnnotationSymboltypes = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation).Cast<FamilySymbol>().Where(s => s.Name.Contains("灯"));
|
|
|
|
foreach (var g1 in lightFamilyGroup)
|
|
{
|
|
var lightSymbolGroup = g1.ToList().GroupBy(f => f.Name).Reverse();//同一族按类型分组
|
|
foreach (var g2 in lightSymbolGroup)
|
|
{
|
|
var instance = g2.ElementAt(0);
|
|
//var wattage = instance.Symbol.get_Parameter(BuiltInParameter.FBX_LIGHT_WATTAGE).AsValueString();
|
|
var mark = instance.Symbol.get_Parameter(BuiltInParameter.WINDOW_TYPE_ID).AsString();
|
|
var annotation = instance.Symbol.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS).AsString();
|
|
var comment = annotation == null ? $"{instance.Symbol.FamilyName}" : $"{instance.Symbol.FamilyName},{annotation}";
|
|
|
|
var planeLegendTypes = from type in lightAnnotationSymboltypes
|
|
where type.Name.Contains(instance.Symbol.FamilyName) && type.Name.Contains("平面")
|
|
select type;
|
|
LegendItem item = new LegendItem
|
|
{
|
|
PlaneLegendType = planeLegendTypes.FirstOrDefault(),
|
|
Description = comment,
|
|
Mark = mark,
|
|
Count = g2.Count(),
|
|
ColorTemperature = instance.Name,
|
|
};
|
|
items.Add(item);
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
private void CreateLightLegend(Document doc, List<LegendItem> items, 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 * 2;//图例网格高度
|
|
var opts = new TextNoteOptions(textNoteType.Id)
|
|
{
|
|
HorizontalAlignment = HorizontalTextAlignment.Center,
|
|
VerticalAlignment = VerticalTextAlignment.Middle,
|
|
};
|
|
double[] widths = new double[5] { tableGridHeight * 2, tableGridHeight * 2, tableGridHeight * 6, tableGridHeight * 2, tableGridHeight * 2 };
|
|
RsRevitUtils.CreateTable(doc, newLegend, items.Count + 1, 5, tableGridHeight, widths);
|
|
var y = XYZ.BasisY * (items.Count + 0.5) * tableGridHeight;
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * tableGridHeight + y, "图例", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 3) + y, "编号", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 7) + y, "说明", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 11) + y, "色温", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 13) + y, "数量", opts);
|
|
for (int i = items.Count - 1; i >= 0; i--)
|
|
{
|
|
LegendItem item = items[i];
|
|
y = XYZ.BasisY * (i + 0.5) * tableGridHeight;
|
|
if (item.PlaneLegendType != null)
|
|
{
|
|
doc.Create.NewFamilyInstance(XYZ.BasisX * tableGridHeight + y, item.PlaneLegendType, newLegend);
|
|
}
|
|
if (!string.IsNullOrEmpty(item.Mark))
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 3) + y, item.Mark, opts);
|
|
}
|
|
if (!string.IsNullOrEmpty(item.Description))
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 7) + y, item.Description, opts);
|
|
}
|
|
if (!string.IsNullOrEmpty(item.ColorTemperature))
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 11) + y, item.ColorTemperature, opts);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(item.Count.ToString()))
|
|
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridHeight * 13) + y, $"{item.Count}个", opts);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |