From e4a4cb9973a31ad35d8c98859914ee685cf09216 Mon Sep 17 00:00:00 2001 From: chuongmep <31106432+chuongmep@users.noreply.github.com> Date: Wed, 20 Nov 2024 21:54:33 +0800 Subject: [PATCH] rvt link --- Test/Sample/BenchmarkCommand/CsvUtils.cs | 45 ++++++++++++++++- .../BenchmarkCommand/RvtLinksCommand.cs | 50 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 Test/Sample/BenchmarkCommand/RvtLinksCommand.cs diff --git a/Test/Sample/BenchmarkCommand/CsvUtils.cs b/Test/Sample/BenchmarkCommand/CsvUtils.cs index ea95478..553ba29 100644 --- a/Test/Sample/BenchmarkCommand/CsvUtils.cs +++ b/Test/Sample/BenchmarkCommand/CsvUtils.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using CsvHelper; @@ -112,4 +113,46 @@ public static void WriteCsvCategories(List csvWriter.WriteRecords(items); } } + public static void WriteRvtLinks(List? items, string fileName) + { + // Ensure items is not null or empty + if (items == null || items.Count == 0) + return; + + var csvFile = new FileInfo(fileName); + + if (csvFile.Exists && csvFile.Length > 0) + { + // Read existing records from the CSV file + var records = new List(); + using (var reader = new StreamReader(fileName)) + using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) + { + records.AddRange(csv.GetRecords()); + } + + // Remove existing records with the same ModelName + var modelName = items[0].ModelName; // Assuming all items have the same ModelName + records.RemoveAll(x => x.ModelName == modelName); + + // Append the new items to the list + records.AddRange(items); + + // Overwrite the file with updated records + using (var writer = new StreamWriter(fileName)) + using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csvWriter.WriteRecords(records); + } + } + else + { + // File doesn't exist or is empty, write new records + using (var writer = new StreamWriter(fileName, false)) // Use `false` to overwrite or create a new file + using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture)) + { + csvWriter.WriteRecords(items); + } + } + } } \ No newline at end of file diff --git a/Test/Sample/BenchmarkCommand/RvtLinksCommand.cs b/Test/Sample/BenchmarkCommand/RvtLinksCommand.cs new file mode 100644 index 0000000..0d29439 --- /dev/null +++ b/Test/Sample/BenchmarkCommand/RvtLinksCommand.cs @@ -0,0 +1,50 @@ +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 RvtLinksCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var doc = commandData.Application.ActiveUIDocument.Document; + var rvtLinks = new FilteredElementCollector(doc) + .OfClass(typeof(RevitLinkType)) + .Cast(); + List rvtLinkBenchmarks = new List(); + foreach (RevitLinkType rvtLink in rvtLinks) + { + var rvtLinkBenchmark = new RvtLinkBenchmark(); + rvtLinkBenchmark.ModelName = doc.Title + ".rvt"; + rvtLinkBenchmark.Name = rvtLink.Name; + ExternalFileReference externalFileReference = rvtLink.GetExternalFileReference(); + string visiblePath = ModelPathUtils.ConvertModelPathToUserVisiblePath(externalFileReference.GetAbsolutePath()); + rvtLinkBenchmark.Path = visiblePath; + rvtLinkBenchmark.Type = rvtLink.AttachmentType.ToString(); + rvtLinkBenchmark.Status = rvtLink.GetLinkedFileStatus().ToString(); + rvtLinkBenchmarks.Add(rvtLinkBenchmark); + } + // save csv + string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile); + string filePath = System.IO.Path.Combine(folderPath, "RvtLinks.csv"); + CsvUtils.WriteRvtLinks(rvtLinkBenchmarks, filePath); + Process.Start(filePath); + return Result.Succeeded; + } + + public class RvtLinkBenchmark + { + public string ModelName { get; set; } + public string Name { get; set; } + public string Path { get; set; } + public string Type { get; set; } + public string Status { get; set; } + } +} \ No newline at end of file