319 lines
16 KiB
C#
319 lines
16 KiB
C#
using Autodesk.Revit.UI;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.UI.Selection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using RookieStation.Utils;
|
|
using RookieStation.ProjectConfig;
|
|
|
|
namespace RookieStation.Drawing.ExecuteCmd
|
|
{
|
|
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
|
|
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
|
|
internal class CreateGroundPavingLegend : 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 items = GetData(doc, viewport.ViewId);
|
|
if (items.Count > 0)
|
|
{
|
|
CreateLegendContent(doc, items, 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 floors = new FilteredElementCollector(doc, viewId).OfClass(typeof(Floor)).OfCategory(BuiltInCategory.OST_Floors);
|
|
var sites = new FilteredElementCollector(doc, viewId).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Site);
|
|
var filledRegionTypes = new FilteredElementCollector(doc).OfClass(typeof(FilledRegionType)).OfCategory(BuiltInCategory.OST_DetailComponents);
|
|
var data = new List<dynamic>();
|
|
|
|
//按族分组
|
|
var floorTypeGroups = floors.GroupBy(g => g.Name);
|
|
|
|
//遍历每个族的分组
|
|
foreach (var instanceByFamily in floorTypeGroups)
|
|
{
|
|
var floor = instanceByFamily.ElementAt(0) as Floor;
|
|
var floorType = floor.FloorType;
|
|
|
|
string floorTypeName = floorType.Name;
|
|
|
|
for (int i = 0; i < filledRegionTypes.Count(); i++)
|
|
{
|
|
var t = filledRegionTypes.ElementAt(i) as FilledRegionType;
|
|
|
|
if (floorTypeName.Contains(t.Name))
|
|
{
|
|
dynamic datarow = new { FilledRegionTypeName = t.Name, TypeName = floorTypeName };
|
|
data.Add(datarow);
|
|
break;
|
|
}
|
|
//如果没找到填充图案则新建
|
|
if (i == filledRegionTypes.Count() - 1)
|
|
GetOrNewFloorTypeFilledRegionType(doc, floorType, floorTypeName, t);
|
|
}
|
|
}
|
|
|
|
var siteGroups = sites.GroupBy(s => s.Name);
|
|
foreach (var siteGroup in siteGroups)
|
|
{
|
|
var site = siteGroup.ElementAt(0) as FamilyInstance;
|
|
var symbol = site.Symbol;
|
|
string symbolTypeName = symbol.Name;
|
|
for (int i = 0; i < filledRegionTypes.Count(); i++)
|
|
{
|
|
var t = filledRegionTypes.ElementAt(i) as FilledRegionType;
|
|
|
|
if (symbolTypeName.Contains(t.Name))
|
|
{
|
|
dynamic datarow = new { FilledRegionTypeName = t.Name, TypeName = symbolTypeName };
|
|
data.Add(datarow);
|
|
break;
|
|
}
|
|
//如果没找到填充图案则新建
|
|
if (i == filledRegionTypes.Count() - 1)
|
|
GetOrNewSiteFilledRegion(doc, symbol, symbolTypeName, t);
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
private static void GetOrNewSiteFilledRegion(Document doc, FamilySymbol familySymbol, string floorTypeName, FilledRegionType defaultFilledRegionType)
|
|
{
|
|
FilledRegionType newFilledRegionType = null;
|
|
|
|
newFilledRegionType = defaultFilledRegionType.Duplicate(floorTypeName) as FilledRegionType;
|
|
try
|
|
{
|
|
ElementId mId = null;
|
|
foreach (Parameter item in familySymbol.ParametersMap)
|
|
{
|
|
if (item.Definition.ParameterType == ParameterType.Material)
|
|
{
|
|
mId = item.AsElementId();
|
|
break;
|
|
}
|
|
}
|
|
|
|
var material = doc.GetElement(mId) as Material;
|
|
|
|
//根据墙体核心层材质,赋予填充图案
|
|
if (material.CutForegroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.CutForegroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.CutForegroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.CutForegroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.CutForegroundPatternColor;
|
|
}
|
|
else if (material.CutBackgroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.CutBackgroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.CutBackgroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.CutBackgroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.CutBackgroundPatternColor;
|
|
}
|
|
else if (material.SurfaceForegroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.SurfaceForegroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.SurfaceForegroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.SurfaceForegroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.SurfaceForegroundPatternColor;
|
|
}
|
|
else if (material.SurfaceBackgroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.SurfaceBackgroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.SurfaceBackgroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.SurfaceBackgroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.SurfaceBackgroundPatternColor;
|
|
}
|
|
else
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = ElementId.InvalidElementId;
|
|
newFilledRegionType.BackgroundPatternId = ElementId.InvalidElementId;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = ElementId.InvalidElementId;
|
|
newFilledRegionType.BackgroundPatternId = ElementId.InvalidElementId;
|
|
}
|
|
//填充图案默认为空
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新建填充图案
|
|
/// </summary>
|
|
/// <param name="doc"></param>
|
|
/// <param name="floorType"></param>
|
|
/// <param name="floorTypeName"></param>
|
|
/// <param name="defaultFilledRegionType"></param>
|
|
private static void GetOrNewFloorTypeFilledRegionType(Document doc, FloorType floorType, string floorTypeName, FilledRegionType defaultFilledRegionType)
|
|
{
|
|
FilledRegionType newFilledRegionType = null;
|
|
|
|
newFilledRegionType = defaultFilledRegionType.Duplicate(floorTypeName) as FilledRegionType;
|
|
try
|
|
{
|
|
var compound = floorType.GetCompoundStructure();
|
|
var mId = compound.GetMaterialId(compound.GetFirstCoreLayerIndex());
|
|
|
|
var material = doc.GetElement(mId) as Material;
|
|
|
|
//根据墙体核心层材质,赋予填充图案
|
|
if (material.CutForegroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.CutForegroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.CutForegroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.CutForegroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.CutForegroundPatternColor;
|
|
}
|
|
else if (material.CutBackgroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.CutBackgroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.CutBackgroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.CutBackgroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.CutBackgroundPatternColor;
|
|
}
|
|
else if (material.SurfaceForegroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.SurfaceForegroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.SurfaceForegroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.SurfaceForegroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.SurfaceForegroundPatternColor;
|
|
}
|
|
else if (material.SurfaceBackgroundPatternId != ElementId.InvalidElementId)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = material.SurfaceBackgroundPatternId;
|
|
newFilledRegionType.ForegroundPatternColor = material.SurfaceBackgroundPatternColor;
|
|
newFilledRegionType.BackgroundPatternId = material.SurfaceBackgroundPatternId;
|
|
newFilledRegionType.BackgroundPatternColor = material.SurfaceBackgroundPatternColor;
|
|
}
|
|
else
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = ElementId.InvalidElementId;
|
|
newFilledRegionType.BackgroundPatternId = ElementId.InvalidElementId;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
newFilledRegionType.ForegroundPatternId = ElementId.InvalidElementId;
|
|
newFilledRegionType.BackgroundPatternId = ElementId.InvalidElementId;
|
|
}
|
|
//填充图案默认为空
|
|
}
|
|
|
|
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 headGridHeight = fontSize * newLegend.Scale * 2;//图例网格高度
|
|
double tableGridWidth = headGridHeight * 2.5;
|
|
double tableGridHeight = headGridHeight * 3;
|
|
double[] widths = new double[2] { tableGridWidth * 2, tableGridWidth * 3 };
|
|
RsRevitUtils.CreateTable(doc, newLegend, data.Count + 1, 2, headGridHeight, tableGridHeight, widths);
|
|
|
|
var filledRegionTypes = new FilteredElementCollector(doc).OfClass(typeof(FilledRegionType)).OfCategory(BuiltInCategory.OST_DetailComponents);
|
|
|
|
var opts = new TextNoteOptions(textNoteType.Id)
|
|
{
|
|
HorizontalAlignment = HorizontalTextAlignment.Center,
|
|
VerticalAlignment = VerticalTextAlignment.Middle,
|
|
};
|
|
XYZ y = null;
|
|
for (int i = 0; i < data.Count; i++)
|
|
{
|
|
y = XYZ.BasisY * (i + 0.5) * tableGridHeight;
|
|
var datarow = data[i];
|
|
var floorTypeName = datarow.GetType().GetProperty("TypeName").GetValue(datarow).ToString();
|
|
FilledRegionType wallfilledRegionType = null;
|
|
foreach (var filledRegionType in filledRegionTypes)
|
|
{
|
|
if (floorTypeName.Contains(filledRegionType.Name))
|
|
{
|
|
wallfilledRegionType = filledRegionType as FilledRegionType;
|
|
break;
|
|
}
|
|
}
|
|
CurveLoop cl = new CurveLoop();
|
|
var cenP = XYZ.BasisX * tableGridWidth + y;
|
|
var lefttop = cenP - tableGridWidth * XYZ.BasisX / 2 + tableGridHeight * XYZ.BasisY * 0.4;
|
|
var leftbottom = cenP - tableGridWidth * XYZ.BasisX / 2 - tableGridHeight * XYZ.BasisY * 0.4;
|
|
var righttop = cenP + tableGridWidth * XYZ.BasisX / 2 + tableGridHeight * XYZ.BasisY * 0.4;
|
|
var rightbottom = cenP + tableGridWidth * XYZ.BasisX / 2 - tableGridHeight * XYZ.BasisY * 0.4;
|
|
Line l1 = Line.CreateBound(leftbottom, lefttop);
|
|
Line l2 = Line.CreateBound(lefttop, righttop);
|
|
Line l3 = Line.CreateBound(righttop, rightbottom);
|
|
Line l4 = Line.CreateBound(rightbottom, leftbottom);
|
|
cl.Append(l1);
|
|
cl.Append(l2);
|
|
cl.Append(l3);
|
|
cl.Append(l4);
|
|
List<CurveLoop> l = new List<CurveLoop>();
|
|
l.Add(cl);
|
|
if (wallfilledRegionType != null)
|
|
{
|
|
FilledRegion.Create(doc, wallfilledRegionType.Id, newLegend.Id, l);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 7 / 2) + y, floorTypeName, opts);
|
|
}
|
|
}
|
|
y += XYZ.BasisY * (tableGridHeight + headGridHeight) / 2;
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth) + y, "图例", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (tableGridWidth * 7 / 2) + y, "图例说明", opts);
|
|
}
|
|
}
|
|
} |