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
/
ParametersExtractor.cs
172 lines (157 loc) · 6.83 KB
/
ParametersExtractor.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Design Automation team for Inventor
//
// 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.Linq;
using Inventor;
using Newtonsoft.Json;
using Shared;
namespace PluginUtilities
{
public class ParametersExtractor
{
public void Extract(Document doc, Parameters parameters, InventorParameters updateResultParameters = null)
{
// extract user parameters
InventorParameters allParams = ExtractParameters(doc, parameters);
// save current state
LogTrace("Updating");
doc.Update2();
LogTrace("Saving");
doc.Save2(SaveDependents: true);
// detect iLogic forms
iLogicFormsReader reader = new iLogicFormsReader(doc, allParams);
iLogicForm[] forms = reader.Extract();
LogTrace($"Found {forms.Length} iLogic forms");
foreach (var form in forms)
{
LogTrace($" - {form.Name}");
}
// Choose set of parameters to use with the following algorithm:
// - extract all iLogic forms from the document
// - keep only 'user parameters' from a form
// - use _first_ iLogic form with non-empty list of parameters
// - if no forms - use UserParameters from the document
InventorParameters resultingParameters;
var candidate = forms.FirstOrDefault(form => form.Parameters.Count > 0);
if (candidate != null)
{
LogTrace($"Using '{candidate.Name}' form as a parameter filter");
resultingParameters = candidate.Parameters;
}
else
{
LogTrace("No non-empty iLogic forms found. Using all user parameters.");
resultingParameters = ExtractParameters(doc, parameters.UserParameters);
}
// if parameters from an update result were passed in, and if validation errors were set on any of them, replace values with the overrides
if (updateResultParameters != null)
{
foreach (KeyValuePair<string, InventorParameter> updateResultParam in updateResultParameters)
{
if (updateResultParam.Value.ErrorMessage != null)
{
resultingParameters[updateResultParam.Key] = updateResultParam.Value;
}
}
}
// generate resulting JSON. Note it's not formatted (to have consistent hash)
string paramsJson = JsonConvert.SerializeObject(resultingParameters, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None });
System.IO.File.WriteAllText("documentParams.json", paramsJson);
LogTrace("Closing");
doc.Close(true);
}
private InventorParameters ExtractParameters(Document doc, dynamic userParameters)
{
/* The resulting json will be like this:
{
"length" : {
"unit": "in",
"value": "10 in",
"values": ["5 in", "10 in", "15 in"]
},
"width": {
"unit": "in",
"value": "20 in",
}
}
*/
try
{
var parameters = new InventorParameters();
var docUnitsOfMeasure = doc.UnitsOfMeasure;
foreach (dynamic param in userParameters)
{
var nominalValue = param.Expression;
string errorMessage = null;
try
{
var unitType = docUnitsOfMeasure.GetTypeFromString(param.Units);
LogTrace($"Checking expression validity on extraction for {param.Expression} and unit type {param.Units} / {unitType}");
if (docUnitsOfMeasure.IsExpressionValid(param.Expression, unitType))
{
var value = docUnitsOfMeasure.GetValueFromExpression(param.Expression, unitType);
nominalValue = docUnitsOfMeasure.GetPreciseStringFromValue(value, unitType);
}
else
{
LogTrace($"Expression '{param.Expression}' invalid for unit type '{unitType}' on extract attempt");
errorMessage = "Parameter's expression is not valid for its unit type";
}
}
// not all unitTypes seem to be convertible (e.g. kTextUnits). In that case, we'll go on with param.Expression assigned before.
catch (Exception e)
{
LogError("Can't get nominalValue for " + param.Name + ": " + e.Message);
}
var parameter = new InventorParameter
{
Unit = param.Units,
Value = nominalValue,
Values = param.ExpressionList?.GetExpressionList() ?? new string[0],
ErrorMessage = errorMessage
};
parameters.Add(param.Name, parameter);
}
return parameters;
}
catch (Exception e)
{
LogError("Error reading params: " + e.Message);
return null;
}
}
#region Logging utilities
/// <summary>
/// Log message with 'trace' log level.
/// </summary>
public void LogTrace(string message)
{
Trace.TraceInformation(message);
}
/// <summary>
/// Log message with 'error' log level.
/// </summary>
public void LogError(string message)
{
Trace.TraceError(message);
}
#endregion
}
}