Skip to content

Commit

Permalink
re
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Nov 20, 2024
1 parent d23b684 commit 1dcc60f
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 46 deletions.
24 changes: 0 additions & 24 deletions Test/Sample/BenchmarkCommand/BenchmarkCommand.cs

This file was deleted.

122 changes: 122 additions & 0 deletions Test/Sample/BenchmarkCommand/CategoriesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Test.BenchmarkCommand;

namespace Test.Sample.BenchmarkCommand;

[Transaction(TransactionMode.Manual)]
public class CategoriesCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
List<CategoryBenchmark> categoryBenchmarks = new List<CategoryBenchmark>();
var categories = new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.Where(x=>x.Category!=null)
.GroupBy(x => x.Category.Name);
foreach (IGrouping<string, Element>? category in categories)
{
var categoryBenchmark = new CategoryBenchmark();
categoryBenchmark.ModelName = doc.Title + ".rvt";
categoryBenchmark.Count = category.Count();
categoryBenchmark.Id = category.First().Category.Id.ToString();
categoryBenchmark.Name = category.Key;
categoryBenchmark.Workset = CountWorksetName(doc,category.ToList());
categoryBenchmark.Family = CountFamilyName(category.ToList());
categoryBenchmark.Type = CountFamilyTypeName(category.ToList());
categoryBenchmark.Nestest = CountNestestElement(category.ToList());
categoryBenchmark.Length = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Length")?.AsDouble() ?? 0));
categoryBenchmark.Width = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Width")?.AsDouble() ?? 0));
categoryBenchmark.Height = category.Sum(x=> UnitFeetToMeter(x.LookupParameter("Height")?.AsDouble() ?? 0));
categoryBenchmarks.Add(categoryBenchmark);
}
// save csv
string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string filePath = System.IO.Path.Combine(folderPath, "categories.csv");
CsvUtils.WriteCsvCategories(categoryBenchmarks, filePath);
Process.Start(filePath);
return Result.Succeeded;
}
public double CountWorksetName(Document doc,List<Element> elements)
{
List<string> names = new List<string>();
WorksetTable worksetTable = doc.GetWorksetTable();
foreach (Element element in elements)
{
WorksetId worksetId = element.WorksetId;
if (worksetId == WorksetId.InvalidWorksetId) continue;
Workset workset = worksetTable.GetWorkset(worksetId);
names.Add(workset.Name);
}
return names.Distinct().Count();
}
public double CountFamilyTypeName(List<Element> elements)
{
List<string> names = new List<string>();
foreach (Element element in elements)
{
string? familyName = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM)?.AsValueString();
string? typeName = element.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM)?.AsValueString();
if (familyName != null && typeName != null) names.Add(familyName + typeName);
}
return names.Distinct().Count();
}
public double CountFamilyName(List<Element> elements)
{
List<string?> names = new List<string?>();
foreach (Element element in elements)
{
string? familyName = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM)?.AsValueString();
if (familyName != null) names.Add(familyName);

}
return names.Distinct().Count();
}

public double CountNestestElement(List<Autodesk.Revit.DB.Element> elements)
{
List<string> names = new List<string>();
foreach (Element element in elements)
{
if(element is FamilyInstance familyInstance)
{
ICollection<ElementId> subComponentIds = familyInstance.GetSubComponentIds();
foreach (ElementId subComponentId in subComponentIds)
{
names.Add(element.Document.GetElement(subComponentId).Name);
}
}
}
return names.Distinct().Count();
}


public double UnitFeetToMeter(double value)
{
return UnitUtils.Convert(value,UnitTypeId.Feet,UnitTypeId.Meters);
}
public class CategoryBenchmark
{
public string ModelName { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public double Count { get; set; } = 0;
public double Workset { get; set; }
public double Family { get; set; } = 0;
public double Type { get; set; } = 0;
public double Nestest { get; set; } = 0;
// set feild name csv
[CsvHelper.Configuration.Attributes.Name("Length(m)")]
public double Length { get; set; }
[CsvHelper.Configuration.Attributes.Name("Width(m)")]
public double Width { get; set; }
[CsvHelper.Configuration.Attributes.Name("Height(m)")]
public double Height { get; set; }
}
}
115 changes: 115 additions & 0 deletions Test/Sample/BenchmarkCommand/CsvUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using CsvHelper;
using Test.Sample.BenchmarkCommand;

namespace Test.BenchmarkCommand;

public static class CsvUtils
{
public static void WriteOverallBenmark(List<BenchmarkReport> items, string fileName)
{
// get from csv, if rows not exist, overwrite, else append
var csvFile = new FileInfo(fileName);
if (csvFile.Exists && csvFile.Length > 0)
{
// delete data have same model name
var records = new List<BenchmarkReport>();
using var reader = new StreamReader(fileName);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
records.AddRange(csv.GetRecords<BenchmarkReport>());
records.RemoveAll(x => x.ModelName == items[0].ModelName);
records.AddRange(items);
reader.Close();
using var writer = new StreamWriter(fileName);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(records);

}
else
{
using var writer = new StreamWriter(fileName,true);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(items);
}
}
public static void WriteCsvWarnings(List<WarningCommand.WarningBenchmark> items, string fileName)
{
// get from csv, if rows not exist, overwrite, else append
var csvFile = new FileInfo(fileName);
if (csvFile.Exists && csvFile.Length > 0)
{
// delete data have same model name
var records = new List<WarningCommand.WarningBenchmark>();
using var reader = new StreamReader(fileName);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
records.AddRange(csv.GetRecords<WarningCommand.WarningBenchmark>());
records.RemoveAll(x => x.ModelName == items[0].ModelName);
records.AddRange(items);
reader.Close();
using var writer = new StreamWriter(fileName);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(records);

}
else
{
using var writer = new StreamWriter(fileName,true);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(items);
}
}
public static void WriteCsvWorksets(List<WorksetsCommand.WorksetsBenchmark> items, string fileName)
{
// get from csv, if rows not exist, overwrite, else append
var csvFile = new FileInfo(fileName);
if (csvFile.Exists && csvFile.Length > 0)
{
// delete data have same model name
var records = new List<WorksetsCommand.WorksetsBenchmark>();
using var reader = new StreamReader(fileName);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
records.AddRange(csv.GetRecords<WorksetsCommand.WorksetsBenchmark>());
records.RemoveAll(x => x.ModelName == items[0].ModelName);
records.AddRange(items);
reader.Close();
using var writer = new StreamWriter(fileName);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(records);

}
else
{
using var writer = new StreamWriter(fileName,true);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(items);
}
}
public static void WriteCsvCategories(List<CategoriesCommand.CategoryBenchmark> items, string fileName)
{
// get from csv, if rows not exist, overwrite, else append
var csvFile = new FileInfo(fileName);
if (csvFile.Exists && csvFile.Length > 0)
{
// delete data have same model name
var records = new List<CategoriesCommand.CategoryBenchmark>();
using var reader = new StreamReader(fileName);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
records.AddRange(csv.GetRecords<CategoriesCommand.CategoryBenchmark>());
records.RemoveAll(x => x.ModelName == items[0].ModelName);
records.AddRange(items);
reader.Close();
using var writer = new StreamWriter(fileName);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(records);

}
else
{
using var writer = new StreamWriter(fileName,true);
using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
csvWriter.WriteRecords(items);
}
}
}
Loading

0 comments on commit 1dcc60f

Please sign in to comment.