187 lines
9.7 KiB
C#
187 lines
9.7 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;
|
|
|
|
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;
|
|
}
|
|
doc.InvokeGroup(tg =>
|
|
{
|
|
var viewScale = viewport.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger();
|
|
CreateLegend(doc, GetShelfData(uidoc), viewScale);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
TaskDialog.Show("温馨提示", "请打开图纸视图");
|
|
return Result.Failed;
|
|
}
|
|
return Result.Succeeded;
|
|
}
|
|
|
|
private static List<ShelfStatistic> GetShelfData(UIDocument uidoc)
|
|
{
|
|
Document doc = uidoc.Document;
|
|
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 = Math.Round(instance.Symbol.GetParameters("长度").FirstOrDefault().AsDouble() * 304.8 / 1000);
|
|
|
|
ShelfStatistic shelfStatistic = new ShelfStatistic
|
|
{
|
|
ShelfType = instance.Name,
|
|
Count = group.Count(),
|
|
GroundArea = Math.Round(area, 2),
|
|
DesignOrders = UserConstant.Orders
|
|
};
|
|
shelfStatistic.LinearMetre = shelfStatistic.Count * l;
|
|
shelfStatistic.CapacityOrders = (int)shelfStatistic.LinearMetre * 80;
|
|
statistics.Add(shelfStatistic);
|
|
}
|
|
return statistics;
|
|
}
|
|
|
|
private static Autodesk.Revit.DB.View CreateLegend(Document doc, List<ShelfStatistic> statistics, int scale)
|
|
{
|
|
var legends = doc.QueryByClassAndBuiltInCategory<Autodesk.Revit.DB.View>(BuiltInCategory.OST_Views).Cast<Autodesk.Revit.DB.View>().Where(v => v.ViewType == ViewType.Legend);
|
|
Autodesk.Revit.DB.View newLegend = null;
|
|
|
|
if (legends.Count() == 0)
|
|
{
|
|
TaskDialog.Show("温馨提示", "请先新建图例");
|
|
return newLegend;
|
|
}
|
|
doc.Invoke(ts =>
|
|
{
|
|
var legendToCopy = legends.FirstOrDefault();
|
|
|
|
var newLegendId = legendToCopy.Duplicate(ViewDuplicateOption.Duplicate);
|
|
newLegend = doc.GetElement(newLegendId) as Autodesk.Revit.DB.View;
|
|
for (int i = 0; i < 100; i++)
|
|
{
|
|
try
|
|
{
|
|
newLegend.Name = $"货架说明 {i}";
|
|
break;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
newLegend.Scale = scale;
|
|
});
|
|
|
|
TextNoteType textNoteType = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).Where(x => x.Name.Contains("宋")).FirstOrDefault() as TextNoteType;
|
|
double fontSize = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
|
|
double actualTextHeight = fontSize * 1.6;
|
|
double legendGridHeight = actualTextHeight * scale * 1.2;//图例网格高度
|
|
double legendGridLength = legendGridHeight * 5;//图例网格长度
|
|
doc.Invoke(ts =>
|
|
{
|
|
CurveArray curveArray = new CurveArray();
|
|
for (int i = 0; i < statistics.Count() + 2; i++)//水平线
|
|
{
|
|
if (i == 0 || i == statistics.Count() || i == statistics.Count() + 1)
|
|
{
|
|
Curve line = Line.CreateBound(XYZ.Zero, XYZ.BasisX * legendGridLength * 6).CreateOffset(legendGridHeight * i, -XYZ.BasisZ);
|
|
curveArray.Append(line);
|
|
}
|
|
else
|
|
{
|
|
Curve line = Line.CreateBound(XYZ.Zero, XYZ.BasisX * legendGridLength * 3).CreateOffset(legendGridHeight * i, -XYZ.BasisZ);
|
|
curveArray.Append(line);
|
|
}
|
|
}
|
|
for (int i = 0; i < 7; i++)//垂直线
|
|
{
|
|
Curve line = Line.CreateBound(XYZ.Zero, XYZ.BasisY * (statistics.Count() + 1) * legendGridHeight).CreateOffset(legendGridLength * i, XYZ.BasisZ);
|
|
curveArray.Append(line);
|
|
}
|
|
var opts = new TextNoteOptions(textNoteType.Id);
|
|
for (int i = 0; i <= statistics.Count(); i++)
|
|
{
|
|
var yElevation = XYZ.BasisY * legendGridHeight * (i + 1);
|
|
if (i < statistics.Count())
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * 0.5 + yElevation, statistics[i].ShelfType, opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength + 0.5) + yElevation, statistics[i].Count.ToString(), opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 2 + 0.5) + yElevation,
|
|
statistics[i].LinearMetre.ToString(), opts);
|
|
}
|
|
else
|
|
{
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * 0.5 + yElevation, "货架类型", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength + 0.5) + yElevation, "数量", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 2 + 0.5) + yElevation, "总延米", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 3 + 0.5) + yElevation, "总承载单量", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 4 + 0.5) + yElevation, "设计单量", opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 5 + 0.5) + yElevation, "场地面积", opts);
|
|
}
|
|
}
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 3 + 0.5) + XYZ.BasisY * legendGridHeight * (statistics.Count() + 1) / 2, statistics[0].CapacityOrders.ToString(), opts);
|
|
TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 4 + 0.5) + XYZ.BasisY * legendGridHeight * (statistics.Count() + 1) / 2, statistics[0].DesignOrders.ToString(), opts);
|
|
TextNote tn = TextNote.Create(doc, newLegend.Id, XYZ.BasisX * (legendGridLength * 5 + 0.5) + XYZ.BasisY * legendGridHeight * (statistics.Count() + 1) / 2, statistics[0].GroundArea + "m2".ToString(), opts);
|
|
FormattedText formattedText = new FormattedText(tn.Text);
|
|
TextRange range = new TextRange(tn.Text.Length - 2, 1);
|
|
formattedText.SetSuperscriptStatus(range, true);
|
|
tn.SetFormattedText(formattedText);
|
|
doc.Create.NewDetailCurveArray(newLegend, curveArray);
|
|
});
|
|
|
|
doc.Invoke(ts =>
|
|
{
|
|
//doc.Create.NewDetailCurve(doc.ActiveView, Line.CreateBound(XYZ.Zero, XYZ.BasisX));
|
|
//XYZ p = new XYZ(6.0335, 1, 0) * 0.03606;
|
|
//ViewSheet.Create(doc, newLegend.Id);
|
|
var viewport = Viewport.Create(doc, doc.ActiveView.Id, newLegend.Id, XYZ.Zero);
|
|
double l = viewport.GetBoxOutline().MaximumPoint.X - viewport.GetBoxOutline().MinimumPoint.X;
|
|
double w = viewport.GetBoxOutline().MaximumPoint.Y - viewport.GetBoxOutline().MinimumPoint.Y;
|
|
XYZ c = new XYZ(l / 2 + 7 / 304.8, w / 2 + 7 / 304.8, 0);
|
|
|
|
viewport.SetBoxCenter(c);
|
|
|
|
var ele = new FilteredElementCollector(doc).OfClass(typeof(ElementType)).WhereElementIsElementType().FirstOrDefault(x => x.Name.Contains("无"));
|
|
viewport.ChangeTypeId(ele.Id);//选择视口-无标题
|
|
});
|
|
|
|
return newLegend;
|
|
}
|
|
}
|
|
} |