Skip to content

Commit

Permalink
feat(core): allow to export / import classifications
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Oct 17, 2024
1 parent 2048aac commit 3346a39
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@thatopen/components",
"description": "Collection of core functionalities to author BIM apps.",
"version": "2.4.0-alpha.6",
"version": "2.4.0-alpha.7",
"author": "That Open Company",
"contributors": [
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
Expand Down
56 changes: 56 additions & 0 deletions packages/core/src/fragments/Classifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export interface Classification {
};
}

interface ExportedClassification {
[system: string]: {
[groupName: string]: {
map: string;
name: string;
id: number | null;
};
};
}

/**
* The Classifier component is responsible for classifying and categorizing fragments based on various criteria. It provides methods to add, remove, find, and filter fragments based on their classification. 📕 [Tutorial](https://docs.thatopen.com/Tutorials/Components/Core/Classifier). 📘 [API](https://docs.thatopen.com/api/@thatopen/components/classes/Classifier).
*/
Expand Down Expand Up @@ -494,6 +504,52 @@ export class Classifier extends Component implements Disposable {
}
}

/**
* Exports the computed classification to persists them and import them back
* later for faster loading.
*/
export() {
const exported: ExportedClassification = {};

for (const systemName in this.list) {
exported[systemName] = {};
const system = this.list[systemName];
for (const groupName in system) {
const group = system[groupName];
exported[systemName][groupName] = {
map: FRAGS.FragmentUtils.export(group.map),
name: group.name,
id: group.id,
};
}
}

return JSON.stringify(exported);
}

/**
* Imports a classification previously exported with .export().
* @param data the serialized classification to import.
*/
import(data: string) {
const imported = JSON.parse(data) as ExportedClassification;

for (const systemName in imported) {
if (!this.list[systemName]) {
this.list[systemName] = {};
}
const system = imported[systemName];
for (const groupName in system) {
const group = system[groupName];
this.list[systemName][groupName] = {
map: FRAGS.FragmentUtils.import(group.map),
name: group.name,
id: group.id,
};
}
}
}

protected saveItem(
group: FRAGS.FragmentsGroup,
systemName: string,
Expand Down

0 comments on commit 3346a39

Please sign in to comment.