Files
RookieStation/RookieStation/Drawing/ExecuteCmd/CreateMainMaterialsTable.cs
2021-12-07 11:25:05 +08:00

222 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RookieStation.Drawing.ExecuteCmd
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
internal class CreateMainMaterialsTable : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
View activeview = doc.ActiveView;
if (uidoc.ActiveView.ViewType == ViewType.DrawingSheet)
{
try
{
CurveArray curveArray = new CurveArray();
var tableLocation = uidoc.Selection.PickPoint(ObjectSnapTypes.Endpoints, "请选择布图区域左上角点");
var tableLocation1 = uidoc.Selection.PickPoint(ObjectSnapTypes.Endpoints, "请选择布图区域右下角点");
var sql = SQLiteUtil.GetInstance();
sql.CreateDb(UserConstant.DbFolder + "Inventory.db");
List<string[]> tableRow = sql.QueryTable("DecorativeMaterials");
sql.CloseConncetion();
//FilteredElementCollector collectorAll = new FilteredElementCollector(doc);
//collectorAll.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true)));
var collectorAll = new FilteredElementCollector(doc);
ElementClassFilter instanceFilter = new ElementClassFilter(typeof(FamilyInstance));
ElementClassFilter hostobjectFilter = new ElementClassFilter(typeof(HostObject));
LogicalOrFilter orFilter = new LogicalOrFilter(instanceFilter, hostobjectFilter);
collectorAll.WherePasses(orFilter);
List<MaterialItem> materialItems = new List<MaterialItem>();
double similarity = 0.4;//相似度
for (int i = 0; i < tableRow.Count; i++)
{
var description = tableRow[i][1];
var position = tableRow[i][6];
bool isExist = false;
foreach (var ele in collectorAll)
{
string name = string.Empty;
if (ele is FamilyInstance fi)
{
name = fi.Symbol.FamilyName;
}
else
{
name = ele.Name;
}
if (CommonUtils.levenshtein(description, name) > similarity || CommonUtils.levenshtein(position, name) > similarity)
{
isExist = true;
break;
}
}
if (isExist)
{
MaterialItem item = new MaterialItem()
{
Description = tableRow[i][1],
FireRating = tableRow[i][2],
Legend = $"{UserConstant.MainMaterialFolder}{tableRow[i][1]}.png",
Specifications = tableRow[i][4],
Pigment = tableRow[i][5],
Position = tableRow[i][6],
Remark = tableRow[i][7],
};
materialItems.Add(item);
}
}
//var width = 346 / 304.8;
//var height = 283 / 304.8;
var width = tableLocation1.X - tableLocation.X;
var height = tableLocation.Y - tableLocation1.Y;
var cellWidth = width / 7;
var cellHeight = height / (materialItems.Count + 1);//表头要加1
doc.InvokeGroup(tg =>
{
doc.Invoke(ts =>
{
for (int j = 0; j < 8; j++)
{
var colStart = new XYZ(tableLocation.X + j * cellWidth, tableLocation.Y, 0);
var colEnd = new XYZ(tableLocation.X + j * cellWidth, tableLocation.Y - height, 0);
Line colLine = Line.CreateBound(colStart, colEnd);
curveArray.Append(colLine);
}
TextNoteType textNoteType = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).Where(x => x.Name.Contains("宋")).FirstOrDefault() as TextNoteType;
var opts = new TextNoteOptions(textNoteType.Id)
{
HorizontalAlignment = HorizontalTextAlignment.Center,
VerticalAlignment = VerticalTextAlignment.Middle
};
for (int i = 0; i <= materialItems.Count; i++)
{
var rowStart = new XYZ(tableLocation.X, tableLocation.Y - i * cellHeight, 0);
var rowEnd = new XYZ(tableLocation.X + width, tableLocation.Y - i * cellHeight, 0);
Line rowLine = Line.CreateBound(rowStart, rowEnd);
curveArray.Append(rowLine);
if (i == 0)//创建表头
{
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 0.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"材料名称\nDESCRIPTION",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 1.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"防火等级\nFIRE RATING",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 2.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"图例\nLEGEND",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 3.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"规格\nSPECIFICATIONS",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 4.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"颜色\nPIGMENT",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 5.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"位置\nPOSITION",
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 6.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
"备注\nREMARK",
opts);
continue;
}
var item = materialItems[i - 1];//从0开始i=0已运行并跳过此段
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 0.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.Description,
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 1.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.FireRating,
opts);
var it = ImageType.Create(doc, new ImageTypeOptions(item.Legend));
var ii = ImageInstance.Create(doc,
activeview,
it.Id,
new ImagePlacementOptions(new XYZ(tableLocation.X + 2.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
BoxPlacement.Center));
doc.Regenerate();
ii.get_Parameter(BuiltInParameter.RASTER_SHEETHEIGHT).SetValueString("21");
ii.get_Parameter(BuiltInParameter.RASTER_SHEETWIDTH).SetValueString("21");
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 3.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.Specifications,
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 4.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.Pigment,
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 5.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.Position,
opts);
TextNote.Create(doc,
doc.ActiveView.Id,
new XYZ(tableLocation.X + 6.5 * cellWidth, tableLocation.Y - (i + 0.5) * cellHeight, 0),
item.Remark,
opts);
}
doc.Create.NewDetailCurveArray(activeview, curveArray);
//ii.Width = 10;
//ii.Height = 10;
});
});
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Log.WriteLog(ex.Message);
}
}
else
{
message = "请打开图纸视图";
//TaskDialog.Show("温馨提示", "请打开图纸视图");
return Result.Failed;
}
return Result.Succeeded;
}
}
}