Skip to content

Commit

Permalink
Legacy metamodel support
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Nov 7, 2024
1 parent dbd997a commit 29f327e
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 30 deletions.
21 changes: 21 additions & 0 deletions packages/metamodel/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://mjtdev.medium.com/how-to-create-a-single-file-bundle-of-a-large-typescript-project-in-2023-5693c8b6b142

const {build} = require("esbuild");
const {peerDependencies} = require('./package.json');

const sharedConfig = {
entryPoints: [
"./src/index.ts"
],
bundle: true,
minify: false,
external: Object.keys(peerDependencies),
};

build({
...sharedConfig,
platform: 'node',
format: 'cjs',
target: "node10.4",
outfile: "dist/index.cjs.js",
});
75 changes: 75 additions & 0 deletions packages/metamodel/meta2data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env node

const {convertMetaModel} = require("./dist/index.cjs.js");

const commander = require('commander');
const npmPackage = require('./package.json');
const fs = require('fs');
const path = require("path");
const program = new commander.Command();

program.version(npmPackage.version, '-v, --version');

program
.description(`CLI to convert a JSON MetaModel file into a JSON DataModel file`)
.option('-i, --input [file]', 'path to input JSON MetaModel file (required)')
.option('-d, --datamodel [file]', 'path to target JSON DataModel file (required)')
.option('-l, --log', 'enable logging (optional)');

program.on('--help', () => {
// console.log(`\n\nXGF version: 10`);
});

program.parse(process.argv);

const options = program.opts();

function logInfo(msg) {
if (options.log) {
console.log(`[meta2data] ${msg}`);
}
}

function logError(msg) {
console.error(`[meta2data] ${msg}`);
}

if (!options.input) {
logError(`Argument expected: -i`);
process.exit(-1);
}

if (!options.datamodel) {
logError(`Argument expected: -d`);
process.exit(-1);
}

try {

const metaModelSrc = options.input;
const dataModelSrc = options.datamodel;

logInfo(`\nReading JSON MetaModel file: ${metaModelSrc}`);

const metaModelJSON = JSON.parse(fs.readFileSync(metaModelSrc));

const dataModelDir = path.dirname(dataModelSrc);
if (dataModelDir !== "" && !fs.existsSync(dataModelDir)) {
fs.mkdirSync(dataModelDir, {recursive: true});
}

logInfo(`Writing target DataModel JSON: ${dataModelSrc}`);

fs.writeFileSync(dataModelSrc, convertMetaModel(metaModelJSON));

process.exit(1);

} catch (err) {
logError(err);
process.exit(-1);
}

function getBasePath(src) {
const i = src.lastIndexOf("/");
return (i !== 0) ? src.substring(0, i + 1) : "";
}
5 changes: 4 additions & 1 deletion packages/metamodel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"module": "dist/index.js",
"scripts": {
"test": "jest --passWithNoTests",
"dist": "tsc",
"dist": "node build.js & tsc",
"lint": "eslint . --fix",
"docs": "typedoc --json ../../docs/json/metamodel.json --options typedoc.json --validation.invalidLink false",
"publish": "npm publish --access public"
Expand Down Expand Up @@ -36,6 +36,9 @@
"@xeokit/data": "^0.1.0",
"@xeokit/ifctypes": "^0.1.0"
},
"peerDependencies": {
"commander": "^11.0.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"jest-html-reporter": "^3.10.2"
Expand Down
43 changes: 43 additions & 0 deletions packages/metamodel/src/MetaModelParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {MetaObjectParams} from "./MetaObjectParams";
import {MetaPropertySetParams} from "./MetaPropertySetParams";

/**
* Legacy metadata model parameters.
*/
export interface MetaModelParams {

/**
* ID of the project the model belongs to.
*/
projectId: string,

/**
* Author of the model.
*/
author: string,

/**
* Date the model was created.
*/
createdAt: string,

/**
* Identifies the model schema.
*/
schema: string,

/**
* Identifies the application that created the metadata model.
*/
creatingApplication: string,

/**
* Metaobject parameters.
*/
metaObjects: MetaObjectParams[],

/**
* Property set parameters.
*/
propertySets: MetaPropertySetParams[]
}
40 changes: 40 additions & 0 deletions packages/metamodel/src/MetaObjectParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Legacy metadata object parameters.
*/
export interface MetaObjectParams {

/**
* IDs of propery sets linked to the metadata object.
*/
propertySetIds: string[];

/**
* Property sets belonging to the metadata object.
*/
propertySets?: string[];

/**
* The original system ID of the metadata object.
*/
originalSystemId?: string;

/**
* The ID of the metadata object.
*/
id:string,

/**
* The name of the metadata object.
*/
name: string,

/**
* The type of the metadata object.
*/
type: string,

/**
* ID of the parent metadata object.
*/
parent: string
}
45 changes: 45 additions & 0 deletions packages/metamodel/src/MetaPropertyParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Legacy metadata property parameters.
*/
export interface MetaPropertyParams {

/**
* The name of the property.
*
* @property name
* @type {String}
*/
name: string;

/**
* The type of the property.
*
* @property type
* @type {Number|String}
*/
type: string;

/**
* The value of the property.
*
* @property value
* @type {*}
*/
value: string;

/**
* The type of the property's value.
*
* @property valueType
* @type {Number|String}
*/
valueType: string;

/**
* Informative text to explain the property.
*
* @property name
* @type {String}
*/
description: string;
}
34 changes: 34 additions & 0 deletions packages/metamodel/src/MetaPropertySetParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import {MetaPropertyParams} from "./MetaPropertyParams";

/**
* Legacy metadata property set parameters.
*/
export interface MetaPropertySetParams {

/**
* Globally-unique ID for the metadata property set.
*/
id: string;

/**
* ID of the corresponding object within the originating system, if any.
*/
originalSystemId: string;

/**
* Human-readable name of the metadata property set.
*/
name: string;

/**
* Type of the metadata property set.
*/
type: string;

/**
* Properties within the metadata property set.
*/
properties: MetaPropertyParams [];

}
42 changes: 42 additions & 0 deletions packages/metamodel/src/convertMetaModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type {DataModelParams, DataObjectParams, RelationshipParams} from "@xeokit/data";
import {IfcRelAggregates, ifcTypeCodes} from "@xeokit/ifctypes";
import {MetaModelParams} from "./MetaModelParams";

/***
* Converts a {@link @xeokit/metamodel!MetaModelParams | MetaModelParams} to a {@link @xeokit/data!DataModelParams | DataModelParams}.
*/
export function convertMetaModel(metaModelParams: MetaModelParams): DataModelParams {
const dataModelParams: DataModelParams = {
id: "",
objects: [],
relationships: [],
propertySets: []
};
if (metaModelParams.propertySets) {
for (let i = 0, len = metaModelParams.propertySets.length; i < len; i++) {
const propertySetParams = metaModelParams.propertySets[i];
if (!propertySetParams.properties) { // HACK: https://github.com/Creoox/creoox-ifc2gltfcxconverter/issues/8
propertySetParams.properties = [];
}
dataModelParams.propertySets.push(propertySetParams);
}
}
if (metaModelParams.metaObjects) {
for (let i = 0, len = metaModelParams.metaObjects.length; i < len; i++) {
const metaObject = metaModelParams.metaObjects[i];
dataModelParams.objects.push({
id: metaObject.id,
name: metaObject.name,
type: 0//metaObject.type
});
if (metaObject.parent) {
dataModelParams.relationships.push(<RelationshipParams>{
relatingObjectId: metaObject.parent,
relatedObjectId: metaObject.id,
type: IfcRelAggregates
})
}
}
}
return dataModelParams;
}
Loading

0 comments on commit 29f327e

Please sign in to comment.