This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ExportBOMAutomation.cs
194 lines (162 loc) · 8 KB
/
ExportBOMAutomation.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
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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Inventor;
using Autodesk.Forge.DesignAutomation.Inventor.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Shared;
namespace ExportBOMPlugin
{
// heavily based on https://github.com/akenson/da-update-bom
[ComVisible(true)]
public class ExportBOMAutomation : AutomationBase
{
/// <summary>
/// File name of output JSON with BOM data.
/// The file name is expected by the corresponding Activity.
/// </summary>
private const string OutputJsonName = "bom.json";
private const string TrackingProperties = "Design Tracking Properties";
private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};
private static readonly ExtractedBOM EmptyBOM = new ExtractedBOM { Columns = new Shared.Column[0], Data = new object[0][] };
public ExportBOMAutomation(InventorServer inventorApp) : base (inventorApp)
{
}
public override void Run(Document doc)
{
LogTrace("Processing " + doc.FullFileName);
try
{
ExtractedBOM extractedBOM = EmptyBOM;
switch (doc.DocumentType)
{
case DocumentTypeEnum.kPartDocumentObject:
LogTrace("No BOM for Part documents.");
break;
case DocumentTypeEnum.kAssemblyDocumentObject:
try
{
extractedBOM = ProcessAssembly((AssemblyDocument)doc);
}
catch (Exception e)
{
LogError("Failed to extract BOM. " + e.ToString());
}
break;
// complain about non-supported document types
default:
throw new ArgumentOutOfRangeException(nameof(doc), "Unsupported document type");
}
// save as JSON
string bomJson = JsonConvert.SerializeObject(extractedBOM, SerializerSettings);
System.IO.File.WriteAllText(OutputJsonName, bomJson);
}
catch (Exception e)
{
LogError("Processing failed. " + e.ToString());
}
}
public override void ExecWithArguments(Document doc, NameValueMap map)
{
LogError("Unexpected execution path! ExportBom does not expect any extra arguments!");
}
private void ActivateProject(string dir)
{
var defaultProjectName = "FDADefault";
var projectFullFileName = System.IO.Path.Combine(dir, defaultProjectName + ".ipj");
DesignProject project = null;
if (System.IO.File.Exists(projectFullFileName))
{
project = _inventorApplication.DesignProjectManager.DesignProjects.AddExisting(projectFullFileName);
Trace.TraceInformation("Adding existing default project file: {0}", projectFullFileName);
}
else
{
project = _inventorApplication.DesignProjectManager.DesignProjects.Add(MultiUserModeEnum.kSingleUserMode, defaultProjectName, dir);
Trace.TraceInformation("Creating default project file with name: {0} at {1}", defaultProjectName, dir);
}
Trace.TraceInformation("Activating default project {0}", project.FullFileName);
project.Activate(true);
}
private ExtractedBOM ProcessAssembly(AssemblyDocument doc)
{
using (new HeartBeat())
{
var extractedBOM = new ExtractedBOM
{
Columns = new[]
{
new Shared.Column { Label = "Row Number" },
new Shared.Column { Label = "Part Number" },
new Shared.Column { Label = "Quantity", Numeric = true },
new Shared.Column { Label = "Description" },
new Shared.Column { Label = "Material" }
},
};
var rows = new List<object[]>();
AssemblyComponentDefinition assemblyComponentDef = doc.ComponentDefinition;
BOM bom = assemblyComponentDef.BOM;
BOMViews bomViews = bom.BOMViews;
// enable structured view in case of iAssembly
if (assemblyComponentDef.IsiAssemblyFactory && bom.StructuredViewEnabled == false)
bom.StructuredViewEnabled = true;
BOMView structureView = bomViews["Structured"];
GetBomRowProperties(structureView.BOMRows, rows);
extractedBOM.Data = rows.ToArray();
return extractedBOM;
}
}
private void GetBomRowProperties(BOMRowsEnumerator bomRowsEnumerator, List<object[]> rows)
{
foreach (BOMRow row in bomRowsEnumerator)
{
ComponentDefinition componentDef = row.ComponentDefinitions[1];
var trackingSet = componentDef.Document.PropertySets[TrackingProperties];
// Assumes not virtual component (if so add conditional for that here)
Property partNum = trackingSet["Part Number"];
Property description = trackingSet["Description"];
Property material = trackingSet["Material"];
object[] data = { // order is important. a place to improve
row.ItemNumber,
partNum.Value,
row.ItemQuantity,
description.Value,
material.Value
};
rows.Add(data);
// iterate through child rows
if (row.ChildRows != null)
{
GetBomRowProperties(row.ChildRows, rows);
}
}
}
}
}