-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandMaterial.cs
116 lines (100 loc) · 5.22 KB
/
CommandMaterial.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using OfficeOpenXml;
namespace TestWorks
{
[Transaction(TransactionMode.Manual)]
public class CommandMaterial : 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 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();
// Create a list to store the data to be exported to Excel
List<List<string>> data = new List<List<string>>(); //this is new part
// Loop through the layers in the compound structure
for (int i = 0; i < layers.Count; i++)
{
using (Transaction t = new Transaction(doc))
{
t.Start("Get Material Layer");
// Create a list to store the data for each layer
List<string> layerData = new List<string>(); //this is new part
// Get the material IDs of each
CompoundStructureLayer layer = layers[i];
Material material = doc.GetElement(layer.MaterialId) as Material;
//Get the property set element to get the structural asset ID
PropertySetElement propertySetElement = doc.GetElement(material.StructuralAssetId) as PropertySetElement;
string assetlayerName = material.Name;
string assetUniqueId = material.UniqueId;
string assetTrueName = propertySetElement.Name;
// Add the layer data to the list
layerData.Add((i + 1).ToString());
layerData.Add(assetUniqueId);
layerData.Add(assetlayerName);
layerData.Add(assetTrueName);//this is new part
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();
// Add the parameter data to the list
layerData.Add((x + 1).ToString());
layerData.Add(parameterName);
layerData.Add(parameterValue);//this is new part
}
}
// Add the layer data to the overall data list
data.Add(layerData); //this is new part
t.Commit();
}
}
//this is new part
// Export the data to an Excel file
using (ExcelPackage excelPackage = new ExcelPackage())
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Compound Wall Layers");
for (int i = 0; i < data.Count; i++)
{
List<string> rowData = data[i];
for (int j = 0; j < rowData.Count; j++)
{
worksheet.Cells[i + 1, j + 1].Value = rowData[j];
}
}
// Save the Excel file to the desktop
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath = Path.Combine(desktopPath, "Compound Wall Layers.xlsx");
FileInfo fileInfo = new FileInfo(filePath);
excelPackage.SaveAs(fileInfo);
}//new part ends here
TaskDialog.Show("Completed", $"Layer Detail ok");
}
return Result.Succeeded;
}
}
}