-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfragments.ts
More file actions
64 lines (56 loc) · 2.09 KB
/
Copy pathfragments.ts
File metadata and controls
64 lines (56 loc) · 2.09 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
import * as FRAGS from '@thatopen/fragments';
import fs from 'fs'
import { safeRegExp } from './utils';
export const convertIfcToFragment = async (
inputPath: string,
outputPath: string
) => {
const ifcFile = await fs.promises.readFile(inputPath)
const typedArray = new Uint8Array(ifcFile)
const serializer = new FRAGS.IfcImporter()
serializer.wasm = {
path: '/',
absolute: false,
}
const bytes = await serializer.process({ bytes: typedArray, raw: false })
const model = new FRAGS.SingleThreadedFragmentsModel('model', bytes)
await fs.promises.writeFile(outputPath, bytes)
return model
}
export const loadFragments = async (filePath: string) => {
const file = await fs.promises.readFile(filePath)
const fragments = new FRAGS.SingleThreadedFragmentsModel('model', file)
return fragments
}
// Returns categories that have associated geometry, along with their counts.
export const fetchCategoriesWithGeometry = (
fragments: FRAGS.SingleThreadedFragmentsModel
) => {
const categories = fragments.getCategories();
const fragmentsWithGeometry = fragments.getItemsWithGeometry();
const geometrySet = new Set(fragmentsWithGeometry);
const categoriesWithCount = categories.map((category) => {
const items = fragments.getItemsOfCategories([safeRegExp(category)]);
const ids = Object.values(items)
.flatMap((arr) => arr)
.filter((id) => id !== undefined && id !== null);
const count = ids.filter((id) => geometrySet.has(id)).length;
return { category, count };
});
return categoriesWithCount.filter((c) => c.count > 0);
}
// Fetches elements of a given category from the fragments model using a safe regular expression.
export const fetchElementsOfCategory = (
fragments: FRAGS.SingleThreadedFragmentsModel,
category: string,
config: Partial<FRAGS.ItemsDataConfig>
) => {
const elementsOfCategory = fragments.getItemsOfCategories([
safeRegExp(category),
]);
const ids = Object.values(elementsOfCategory)
.flat()
.filter((id) => id !== undefined && id !== null);
const itemsData = fragments.getItemsData(ids, config);
return itemsData;
};