-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummaryPanel.js
52 lines (48 loc) · 2.37 KB
/
SummaryPanel.js
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
export class SummaryPanel extends Autodesk.Viewing.UI.PropertyPanel {
constructor(extension, id, title) {
super(extension.viewer.container, id, title);
this.extension = extension;
}
async update(model, dbids, propNames) {
this.removeAllProperties();
for (const propName of propNames) {
const initialValue = { sum: 0, count: 0, min: Infinity, max: -Infinity };
const aggregateFunc = (aggregate, value, property) => {
return {
count: aggregate.count + 1,
sum: aggregate.sum + value,
min: Math.min(aggregate.min, value),
max: Math.max(aggregate.max, value),
units: property.units,
precision: property.precision
};
};
const { sum, count, min, max, units, precision } = await this.aggregatePropertyValues(model, dbids, propName, aggregateFunc, initialValue);
if (count > 0) {
const category = propName;
this.addProperty('Count', count, category);
this.addProperty('Sum', this.toDisplayUnits(sum, units, precision), category);
this.addProperty('Avg', this.toDisplayUnits((sum / count), units, precision), category);
this.addProperty('Min', this.toDisplayUnits(min, units, precision), category);
this.addProperty('Max', this.toDisplayUnits(max, units, precision), category);
}
}
}
async aggregatePropertyValues(model, dbids, propertyName, aggregateFunc, initialValue = 0) {
return new Promise(function (resolve, reject) {
let aggregatedValue = initialValue;
model.getBulkProperties(dbids, { propFilter: [propertyName] }, function (results) {
for (const result of results) {
if (result.properties.length > 0) {
const prop = result.properties[0];
aggregatedValue = aggregateFunc(aggregatedValue, prop.displayValue, prop);
}
}
resolve(aggregatedValue);
}, reject);
});
}
toDisplayUnits(value, units, precision) {
return Autodesk.Viewing.Private.formatValueWithUnits(value, units, 3, precision);
}
}