Skip to content

Commit

Permalink
rvt link
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Nov 20, 2024
1 parent d417c5e commit e4a4cb9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
45 changes: 44 additions & 1 deletion Test/Sample/BenchmarkCommand/CsvUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using CsvHelper;
Expand Down Expand Up @@ -112,4 +113,46 @@ public static void WriteCsvCategories(List<CategoriesCommand.CategoryBenchmark>
csvWriter.WriteRecords(items);
}
}
public static void WriteRvtLinks(List<RvtLinksCommand.RvtLinkBenchmark>? 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<RvtLinksCommand.RvtLinkBenchmark>();
using (var reader = new StreamReader(fileName))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
records.AddRange(csv.GetRecords<RvtLinksCommand.RvtLinkBenchmark>());
}

// 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);
}
}
}
}
50 changes: 50 additions & 0 deletions Test/Sample/BenchmarkCommand/RvtLinksCommand.cs
Original file line number Diff line number Diff line change
@@ -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<RevitLinkType>();
List<RvtLinkBenchmark> rvtLinkBenchmarks = new List<RvtLinkBenchmark>();
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; }
}
}

0 comments on commit e4a4cb9

Please sign in to comment.