This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ts
44 lines (36 loc) · 2.15 KB
/
Utils.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { IModelApp } from "@itwin/core-frontend";
import { UnitConversionProps } from "@itwin/core-quantity";
import { SchemaUnitProvider } from "@itwin/ecschema-metadata";
import { ECSchemaRpcLocater } from "@itwin/ecschema-rpcinterface-common";
export default class Utils {
// variable to cache quantity conversions.
private static conversions: {[index: string]: UnitConversionProps} = {};
// iModel quantity formatter to convert units.
public static async convertQuantity(quantity: number, sourceUnit: string, targetUnit: string) {
let newQuantity;
let conversion: UnitConversionProps;
// signature to identify a particular quantity conversion.
const conversionSignature = sourceUnit + "_" + targetUnit;
// if conversion already cached, use local value.
if (Utils.conversions[conversionSignature]) {
conversion = Utils.conversions[conversionSignature];
} else {
const imodel = IModelApp.viewManager.selectedView?.iModel;
if (!imodel) throw new Error("iModel not loaded! Cannot perform unit conversion.");
const schemaLocater = new ECSchemaRpcLocater(imodel); // requires ECSchemaRpcInterface (see App.tsx)
const formatter = IModelApp.quantityFormatter;
await formatter.setUnitsProvider(new SchemaUnitProvider(schemaLocater));
const fromUnit = await formatter.findUnitByName(`Units.${sourceUnit}`);
const toUnit = await formatter.findUnitByName(`Units.${targetUnit}`);
conversion = await formatter.getConversion(fromUnit, toUnit);
// cache conversion for future use.
Utils.conversions[conversionSignature] = conversion;
}
newQuantity = conversion.factor * quantity + conversion.offset;
return newQuantity;
}
}