-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathCountExportJsonBatchCommand.cs
More file actions
196 lines (184 loc) · 7.78 KB
/
CountExportJsonBatchCommand.cs
File metadata and controls
196 lines (184 loc) · 7.78 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using CsvHelper;
using Microsoft.Win32;
using Newtonsoft.Json;
namespace Test;
[Transaction(TransactionMode.Manual)]
public class CountExportJsonBatchCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
string region = "US";
// browser to item path
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// create new folder data
string dataFolder = Path.Combine(folder, "data");
if (!Directory.Exists(dataFolder))
{
Directory.CreateDirectory(dataFolder);
}
using (var reader = new StreamReader(BrowsePathItems()))
{
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<DataInput>().ToList();
foreach (DataInput dataInput in records)
{
try
{
var projectGuid = new Guid(dataInput.project_guid);
var modelGuid = new Guid(dataInput.model_guid);
var modelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(region, projectGuid, modelGuid);
Document document = doc.Application.OpenDocumentFile(modelPath, new OpenOptions());
var eles = new FilteredElementCollector(document).WhereElementIsNotElementType().ToElements();
// save json with length of ducts unit meter, pipes length unit meter, all count by category
Output output = new Output();
output.modelName = document.Title;
output.PipeLenght = 0;
output.DuctLenght = 0;
output.CableTrayLenght = 0;
output.ConduitsLenght = 0;
output.ConduitRunsLength = 0;
output.CableTrayRunsLength = 0;
output.CountByCategory = new Dictionary<string?, string>();
foreach (var ele in eles)
{
if (ele is Pipe pipe)
{
output.PipeLenght +=
ConvertToRealLength(
pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is Duct duct)
{
output.DuctLenght +=
ConvertToRealLength(
duct.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is CableTray cableTray)
{
output.CableTrayLenght +=
ConvertToRealLength(cableTray.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)
?.AsDouble() ?? 0);
}
else if (ele is Conduit conduit)
{
output.ConduitsLenght +=
ConvertToRealLength(conduit.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)
?.AsDouble() ?? 0);
}
else if (ele is ConduitRun conduitRun)
{
output.ConduitRunsLength += ConvertToRealLength(
conduitRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
else if (ele is CableTrayRun cableTrayRun)
{
output.CableTrayRunsLength += ConvertToRealLength(
cableTrayRun.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)?.AsDouble() ?? 0);
}
}
// group by category and count
var groupByCategory = eles.GroupBy(x => x.Category?.Name);
foreach (var group in groupByCategory)
{
if (group.Key == null)
{
continue;
}
output.CountByCategory.Add(group.Key!, group.Count().ToString());
}
// sort by key
output.CountByCategory =
output.CountByCategory.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
// save to json
string json =
JsonConvert.SerializeObject(output, Formatting.Indented);
string filePath = Path.Combine(dataFolder, $"{document.Title}.json");
File.WriteAllText(filePath, json, Encoding.UTF8);
OpenLogFileAndWrite($"Save to {filePath}", folder);
}
catch (Exception e)
{
OpenLogFileAndWrite(e.Message, folder);
}
}
}
}
MessageBox.Show("Done");
// Process.Start("output.json");
// close model
//doc.Close(false);
return Result.Succeeded;
}
public void OpenLogFileAndWrite(string message,string folder)
{
string fileName = "log.txt";
string logFile = Path.Combine(folder, fileName);
if (!File.Exists(logFile))
{
using (StreamWriter sw = File.CreateText(logFile))
{
sw.WriteLine(message);
}
}
else
{
using (StreamWriter sw = File.AppendText(logFile))
{
sw.WriteLine(message);
}
}
}
public class DataInput
{
public string item_id { get; set; }
public string item_name { get; set; }
public string project_guid { get; set; }
public string model_guid { get; set; }
}
public string BrowsePathItems()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Revit Files (*.csv)|*.csv";
dialog.Title = "Select a items file";
dialog.ShowDialog();
return dialog.FileName;
}
public double ConvertToRealLength(double length)
{
// convert to meter
if (length == null)
{
return 0;
}
double convert = UnitUtils.Convert(length, UnitTypeId.Feet, UnitTypeId.Meters);
return convert;
}
public class Output
{
public string modelName { get; set; }
public double? PipeLenght { get; set; }
public double? DuctLenght { get; set; }
public double? CableTrayLenght { get; set; }
public double? ConduitsLenght { get; set; }
public double? ConduitRunsLength { get; set; }
public double? CableTrayRunsLength { get; set; }
public Dictionary<string?, string> CountByCategory { get; set; }
}
}