-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandMaterialExportCSV.cs
81 lines (68 loc) · 3.67 KB
/
CommandMaterialExportCSV.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace TestWorks
{
[Transaction(TransactionMode.Manual)]
public class CommandMaterialExportCSV : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Get the current active document
//Document doc = ActiveUIDocument.Document;
// Get the selected element (assumed to be a compound wall)
ElementId selectedElementId = uidoc.Selection.GetElementIds().FirstOrDefault();
Element selectedElement = doc.GetElement(selectedElementId);
// Check if the selected element is a wall
if (selectedElement is Wall selectedWall)
{
// Get the compound structure of the wall type
CompoundStructure compoundStructure = selectedWall.WallType.GetCompoundStructure();
// Get the layers of the compound structure
IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
// Define the output CSV file path and create the file
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MaterialLayers.csv");
using (StreamWriter sw = new StreamWriter(filePath))
{
// Write the header row to the CSV file
sw.WriteLine("Layer,Material,Property Name,Property Value");
// Loop through the layers in the compound structure
for (int i = 0; i < layers.Count; i++)
{
// Get the material IDs of each layer
CompoundStructureLayer layer = layers[i];
Material material = doc.GetElement(layer.MaterialId) as Material;
//Get the then get the property set element to get the structural asset ID
PropertySetElement propertySetElement = doc.GetElement(material.StructuralAssetId) as PropertySetElement;
// Loop through the parameters in the material's structural asset property set
IList<Parameter> parameters = propertySetElement.GetOrderedParameters();
for (int x = 0; x < parameters.Count; x++)
{
Parameter parameter = parameters[x];
InternalDefinition internalDefinition = parameter.Definition as InternalDefinition;
if (internalDefinition != null)
{
string parameterName = internalDefinition.Name;
string parameterValue = parameter.AsValueString();
// Write the data to the CSV file
sw.WriteLine($"{i + 1},{material.Name},{parameterName},{parameterValue}");
}
}
}
}
// Show a message box when the export is complete
TaskDialog.Show("Export Material Layers", $"The material layer data has been exported to {filePath}");
}
return Result.Succeeded;
}
}
}