|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using Autodesk.Revit.Attributes; |
| 7 | +using Autodesk.Revit.DB; |
| 8 | +using Autodesk.Revit.UI; |
| 9 | +using CsvHelper; |
| 10 | +using CsvHelper.Configuration; |
| 11 | +using System.Globalization; |
| 12 | + |
| 13 | +namespace Test; |
| 14 | + |
| 15 | +[Transaction(TransactionMode.Manual)] |
| 16 | +public class AssemblyCodeFamilyExport : IExternalCommand |
| 17 | +{ |
| 18 | + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) |
| 19 | + { |
| 20 | + var doc = commandData.Application.ActiveUIDocument.Document; |
| 21 | + string desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
| 22 | + string dataDir = Path.Combine(desktopDir, "data"); |
| 23 | + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); |
| 24 | + string filePath = Path.Combine(dataDir, "assemblycode.csv"); |
| 25 | + ExportAssemblyCodes(doc, filePath); |
| 26 | + Process.Start(filePath); |
| 27 | + return Result.Succeeded; |
| 28 | + } |
| 29 | + private void ExportAssemblyCodes(Document doc, string filePath) |
| 30 | + { |
| 31 | + var familySymbols = new FilteredElementCollector(doc) |
| 32 | + .OfClass(typeof(FamilySymbol)) |
| 33 | + .Cast<FamilySymbol>(); |
| 34 | + |
| 35 | + var dataToExport = familySymbols.Select(symbol => new DataAssembly |
| 36 | + { |
| 37 | + Category = symbol.Category?.Name ?? "N/A", |
| 38 | + FamilyName = symbol.Family.Name, |
| 39 | + TypeName = symbol.Name, |
| 40 | + AssemblyCode = symbol.LookupParameter("Assembly Code")?.AsString() ?? "N/A", |
| 41 | + AssemblyCodeDescription = "" |
| 42 | + }).ToList(); |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + using (var writer = new StreamWriter(filePath)) |
| 47 | + using (var csv = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture))) |
| 48 | + { |
| 49 | + csv.WriteRecords(dataToExport); |
| 50 | + } |
| 51 | + TaskDialog.Show("Export Completed", $"Assembly codes exported to: {filePath}"); |
| 52 | + } |
| 53 | + catch (Exception ex) |
| 54 | + { |
| 55 | + TaskDialog.Show("Error", "Failed to export CSV: " + ex.Message); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +[Transaction(TransactionMode.Manual)] |
| 63 | +public class AssemblyCodeFamilyImport : IExternalCommand |
| 64 | +{ |
| 65 | + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) |
| 66 | + { |
| 67 | + var uiDoc = commandData.Application.ActiveUIDocument; |
| 68 | + var doc = uiDoc.Document; |
| 69 | + |
| 70 | + // Mở hộp thoại chọn file CSV |
| 71 | + string desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); |
| 72 | + string dataDir = Path.Combine(desktopDir, "data"); |
| 73 | + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); |
| 74 | + string filePath = Path.Combine(dataDir, "assemblycode.csv"); |
| 75 | + List<DataAssembly> dataAssemblies = ReadCsv(filePath); |
| 76 | + if (!dataAssemblies.Any()) |
| 77 | + { |
| 78 | + TaskDialog.Show("Error", "No valid data found in the CSV file."); |
| 79 | + return Result.Failed; |
| 80 | + } |
| 81 | + |
| 82 | + using (Transaction tx = new Transaction(doc, "Update Assembly Codes")) |
| 83 | + { |
| 84 | + tx.Start(); |
| 85 | + var familySymbols = new FilteredElementCollector(doc) |
| 86 | + .OfClass(typeof(FamilySymbol)) |
| 87 | + .Cast<FamilySymbol>(); |
| 88 | + |
| 89 | + int updatedCount = 0; |
| 90 | + foreach (var data in dataAssemblies) |
| 91 | + { |
| 92 | + var symbol = familySymbols.FirstOrDefault(fam => |
| 93 | + fam.Family.Name.Equals(data.FamilyName, StringComparison.OrdinalIgnoreCase) && |
| 94 | + fam.Name.Equals(data.TypeName, StringComparison.OrdinalIgnoreCase)); |
| 95 | + |
| 96 | + if (symbol != null) |
| 97 | + { |
| 98 | + Parameter assemblyCodeParam = symbol.get_Parameter(BuiltInParameter.UNIFORMAT_CODE); |
| 99 | + if (assemblyCodeParam != null && !assemblyCodeParam.IsReadOnly) |
| 100 | + { |
| 101 | + if(data.AssemblyCode=="N/A") continue; |
| 102 | + assemblyCodeParam.Set(data.AssemblyCode); |
| 103 | + updatedCount++; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + tx.Commit(); |
| 109 | + TaskDialog.Show("Completed", $"Updated {updatedCount} assembly codes."); |
| 110 | + } |
| 111 | + |
| 112 | + return Result.Succeeded; |
| 113 | + } |
| 114 | + private List<DataAssembly> ReadCsv(string filePath) |
| 115 | + { |
| 116 | + try |
| 117 | + { |
| 118 | + using (var reader = new StreamReader(filePath)) |
| 119 | + using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture))) |
| 120 | + { |
| 121 | + return csv.GetRecords<DataAssembly>().ToList(); |
| 122 | + } |
| 123 | + } |
| 124 | + catch (Exception ex) |
| 125 | + { |
| 126 | + TaskDialog.Show("Error", "Failed to read CSV: " + ex.Message); |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +public class DataAssembly |
| 135 | +{ |
| 136 | + public string Category { get; set; } |
| 137 | + public string FamilyName { get; set; } |
| 138 | + public string TypeName { get; set; } |
| 139 | + public string AssemblyCode { get; set; } |
| 140 | + public string AssemblyCodeDescription { get; set; } |
| 141 | +} |
0 commit comments