Skip to content

Commit

Permalink
First Set of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
beachtom committed Jul 5, 2023
1 parent 1327355 commit a4bb13e
Show file tree
Hide file tree
Showing 15 changed files with 26,006 additions and 27,282 deletions.
49,588 changes: 24,163 additions & 25,425 deletions src/ifc-schema.ts

Large diffs are not rendered by default.

104 changes: 51 additions & 53 deletions src/schema-generator/gen_functional_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ tsSchema.push(`// This is a generated file, please see: gen_functional_types.js`


tsSchema.push(`export class Handle<_> {`);
tsSchema.push(`\ttype: number=5;`);
tsSchema.push(`\tconstructor(public value: number) {}`);
tsSchema.push(`type: number=5;`);
tsSchema.push(`constructor(public value: number) {}`);
tsSchema.push(`}`);

tsSchema.push(`export abstract class IfcLineObject {`);
tsSchema.push(`\ttype: number=0;`);
tsSchema.push(`\tconstructor(public expressID: number) {}`);
tsSchema.push(`type: number=0;`);
tsSchema.push(`constructor(public expressID: number = -1) {}`);
tsSchema.push(`}`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const FromRawLineData: any = [];`);
tsSchema.push(`export const FromRawLineData: any=[];`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const InversePropertyDef: any = {};`);
tsSchema.push(`export const InversePropertyDef: any={};`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const InheritanceDef: any = {};`);
tsSchema.push(`export const InheritanceDef: any={};`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const Constructors: any = {};`);
tsSchema.push(`export const Constructors: any={};`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const ToRawLineData: any = {};`);
tsSchema.push(`export const ToRawLineData: any={};`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const TypeInitialisers: any = {};`);
tsSchema.push(`export const TypeInitialisers: any={};`);
tsSchema.push('/** @ignore */');
tsSchema.push(`export const SchemaNames: Array<Array<string>> = [];`);
tsSchema.push(`export const SchemaNames: Array<Array<string>>=[];`);


tsSchema.push('function TypeInitialiser(schema:number,tapeItem:any) {');
tsSchema.push('\tif (Array.isArray(tapeItem)) tapeItem.map((p:any) => TypeInitialiser(schema,p));');
tsSchema.push('\tif (tapeItem.typecode) return TypeInitialisers[schema][tapeItem.typecode](tapeItem.value); else return tapeItem.value;');
tsSchema.push('if (Array.isArray(tapeItem)) tapeItem.map((p:any)=>TypeInitialiser(schema,p));');
tsSchema.push('if (tapeItem.typecode) return TypeInitialisers[schema][tapeItem.typecode](tapeItem.value); else return tapeItem.value;');
tsSchema.push('}');
tsSchema.push('function Labelise(tapeItem:any) {');
tsSchema.push('\ttapeItem.value=tapeItem.value.toString(); tapeItem.valueType=tapeItem.type; tapeItem.type=2; tapeItem.label=tapeItem.constructor.name.toUpperCase();');
tsSchema.push('\treturn tapeItem;');
tsSchema.push('tapeItem.value=tapeItem.value.toString(); tapeItem.valueType=tapeItem.type; tapeItem.type=2; tapeItem.label=tapeItem.constructor.name.toUpperCase();');
tsSchema.push('return tapeItem;');
tsSchema.push('}')

var files = fs.readdirSync("./");
Expand All @@ -71,7 +71,7 @@ tsSchema.push("// supported ifc schemas");
for (var i = 0; i < files.length; i++) {
if (!files[i].endsWith(".exp")) continue;
const schemaName = files[i].replace(".exp","");
tsSchema.push(`\t${schemaName.replace(".", "_")} = "${schemaName}",`);
tsSchema.push(`\t${schemaName.replace(".", "_")}="${schemaName}",`);
}
tsSchema.push(`};`);

Expand Down Expand Up @@ -113,15 +113,15 @@ for (var i = 0; i < files.length; i++) {
for (var x=0; x < entities.length; x++)
{
let constructorArray = entities[x].derivedProps.filter(j => !entities[x].ifcDerivedProps.includes(j.name));
tsSchema.push(`\t${crc32(entities[x].name.toUpperCase(),crcTable)}:(id:number, ${constructorArray.length==0? '_:any' :'v:any[]'}) => new ${schemaNameClean}.${entities[x].name}(id, ${entities[x].derivedProps.filter(j => !entities[x].ifcDerivedProps.includes(j.name)).map((p, j) => generatePropAssignment(p,j,types,schemaNameClean,i)).join(", ")}),`);
tsSchema.push(`${crc32(entities[x].name.toUpperCase(),crcTable)}:(${constructorArray.length==0? '_:any' :'v:any[]'})=>new ${schemaNameClean}.${entities[x].name}(${entities[x].derivedProps.filter(j => !entities[x].ifcDerivedProps.includes(j.name)).map((p, j) => generatePropAssignment(p,j,types,schemaNameClean,i)).join(", ")}),`);
}
tsSchema.push('}');

//generate InheritanceDef
tsSchema.push(`InheritanceDef[${i}]={`)
for (var x=0; x < entities.length; x++)
{
if (entities[x].children.length > 0) tsSchema.push(`\t${crc32(entities[x].name.toUpperCase(),crcTable)}: [${entities[x].children.map((c) => `${c.toUpperCase()}`).join(",")}],`);
if (entities[x].children.length > 0) tsSchema.push(`${crc32(entities[x].name.toUpperCase(),crcTable)}: [${entities[x].children.map((c) => `${c.toUpperCase()}`).join(",")}],`);
}
tsSchema.push('}');

Expand All @@ -131,7 +131,7 @@ for (var i = 0; i < files.length; i++) {
{
if (entities[x].derivedInverseProps.length > 0)
{
let inverseProp:string =`\t${crc32(entities[x].name.toUpperCase(),crcTable)}:[`;
let inverseProp:string =`${crc32(entities[x].name.toUpperCase(),crcTable)}:[`;
entities[x].derivedInverseProps.forEach((prop) => {
let pos = 0;
//find the target element
Expand Down Expand Up @@ -164,14 +164,14 @@ for (var i = 0; i < files.length; i++) {
for (var x=0; x < entities.length; x++)
{
let constructorArray = entities[x].derivedProps.filter(j => !entities[x].ifcDerivedProps.includes(j.name));
tsSchema.push(`\t${crc32(entities[x].name.toUpperCase(),crcTable)}:(ID:number, ${constructorArray.length==0? '_:any':'a: any[]'}) => new ${schemaNameClean}.${entities[x].name}(ID, ${constructorArray.map((_, i) => 'a['+i+']').join(", ")}),`);
tsSchema.push(`${crc32(entities[x].name.toUpperCase(),crcTable)}:(${constructorArray.length==0? '_:any':'a: any[]'})=>new ${schemaNameClean}.${entities[x].name}(${constructorArray.map((_, i) => 'a['+i+']').join(", ")}),`);

}
tsSchema.push('}');

//generate ToRawLineData
tsSchema.push(`ToRawLineData[${i}]={`)
for (var x=0; x < entities.length; x++) tsSchema.push(`\t${crc32(entities[x].name.toUpperCase(),crcTable)}:(${entities[x].derivedProps.length==0?'_:any': `i:${schemaNameClean}.${entities[x].name}`}):unknown[] => [${entities[x].derivedProps.map((p) => generateTapeAssignment(p,types)).join(", ")}],`);
for (var x=0; x < entities.length; x++) tsSchema.push(`${crc32(entities[x].name.toUpperCase(),crcTable)}:(${entities[x].derivedProps.length==0?'_:any': `i:${schemaNameClean}.${entities[x].name}`}):unknown[]=>[${entities[x].derivedProps.map((p) => generateTapeAssignment(p,types)).join(", ")}],`);
tsSchema.push('}');

//initialisers
Expand All @@ -188,33 +188,33 @@ for (var i = 0; i < files.length; i++) {

if (type.isList)
{
tsSchema.push(`\texport class ${type.name} {`);
tsSchema.push(`\t\tconstructor(public value: Array<${type.typeName}>) {}`);
tsSchema.push(`\t};`);
tsSchema.push(`export class ${type.name} { constructor(public value: Array<${type.typeName}>) {} };`);
typeList.add(type.name);
}
else if (type.isSelect)
{
let selectOutput: string = `\texport type ${type.name} = `;
let selectOutput: string = `export type ${type.name} = `;
let first = true;
type.values.forEach(refType => {
let isType: boolean = types.some( x => x.name == refType);
if(!first) selectOutput+='|'
if (isType)
{
selectOutput+=` | ${refType}`;
{
selectOutput+=refType;
}
else
{
selectOutput+=` | (Handle<${refType}> | ${refType})`;
selectOutput+=`(Handle<${refType}> | ${refType})`;
}
first = false;

});
selectOutput+=";";
tsSchema.push(selectOutput);
}
else if (type.isEnum)
{
tsSchema.push(`\texport class ${type.name} {`);
tsSchema.push('\t\t'+type.values.map((v) => `static ${v} : any = { type:3, value:'${v}'}; `).join(''));
tsSchema.push(`\t}`);
tsSchema.push(`export class ${type.name} {` + type.values.map((v) => `static ${v} : any = { type:3, value:'${v}'}; `).join('') +'}');
}
else
{
Expand All @@ -228,25 +228,23 @@ for (var i = 0; i < files.length; i++) {
}

typeList.add(type.name);
tsSchema.push(`\texport class ${type.name} {`);
tsSchema.push(`\t\ttype: number=${typeNum};`);
tsSchema.push(`export class ${type.name} {`);
tsSchema.push(`type: number=${typeNum};`);
if (typeName=="number") {
tsSchema.push(`\t\tpublic value: number;`);
tsSchema.push(`\t\tconstructor(v: any) { this.value = parseFloat(v);}`);
tsSchema.push(`public value: number;`);
tsSchema.push(`constructor(v: any) { this.value = parseFloat(v);}`);
} else if (typeName=="boolean") {
tsSchema.push(`\t\tpublic value: boolean;`);
tsSchema.push(`\t\tconstructor(v: any) { this.value = v == "true" ? true : false; }`);
tsSchema.push(`public value: boolean;`);
tsSchema.push(`constructor(v: any) { this.value = v == "true" ? true : false; }`);
} else {
tsSchema.push(`\t\tconstructor(public value: ${typeName}) {}`);
tsSchema.push(`constructor(public value: ${typeName}) {}`);
}
tsSchema.push(`\t}`);
tsSchema.push(`}`);
}
});

for (var x=0; x < entities.length; x++) generateClass(entities[x], tsSchema,types,crcTable);
tsSchema.push("}");


}

// now write out the global c++/ts metadata. All the WASM needs to know about is a list of all entities
Expand All @@ -272,31 +270,31 @@ cppSchema.push("#include <unordered_set>");
cppSchema.push("#include \"ifc-schema.h\"");
cppSchema.push("#include \"IfcSchemaManager.h\"");
cppSchema.push("namespace webifc::schema {")
cppSchema.push("\tvoid IfcSchemaManager::initSchemaData() {");
cppSchema.push("void IfcSchemaManager::initSchemaData() {");
completeifcElementList.forEach(element => {
cppSchema.push(`\t\t_ifcElements.insert(${element.toUpperCase()});`);
cppSchema.push(`_ifcElements.insert(${element.toUpperCase()});`);
});
chSchema.push(`enum IFC_SCHEMA {`)
for (var i = 0; i < files.length; i++) {
if (!files[i].endsWith(".exp")) continue;
var schemaName = files[i].replace(".exp","");
var schemaNameClean = schemaName.replace(".","_");
chSchema.push(`\t${schemaNameClean},`)
cppSchema.push(`\t\t_schemaNames.push_back("${schemaNameClean}");`);
cppSchema.push(`\t\t_schemas.push_back(${schemaNameClean});`);
chSchema.push(`${schemaNameClean},`)
cppSchema.push(`_schemaNames.push_back("${schemaNameClean}");`);
cppSchema.push(`_schemas.push_back(${schemaNameClean});`);
}
cppSchema.push("\t}");
cppSchema.push("}");
chSchema.push(`};`)

cppSchema.push("\tstd::string IfcSchemaManager::IfcTypeCodeToType(uint32_t typeCode) const {");
cppSchema.push("\t\tswitch(typeCode) {");
cppSchema.push("std::string IfcSchemaManager::IfcTypeCodeToType(uint32_t typeCode) const {");
cppSchema.push("switch(typeCode) {");
new Set([...completeEntityList,...typeList]).forEach(entity => {
cppSchema.push(`\t\t\tcase schema::${entity.toUpperCase()}: return "${entity.toUpperCase()}";`);
cppSchema.push(`case schema::${entity.toUpperCase()}: return "${entity.toUpperCase()}";`);
});

cppSchema.push(`\t\t\tdefault: return "<web-ifc-type-unknown>";`);
cppSchema.push("\t\t}");
cppSchema.push("\t}");
cppSchema.push(`default: return "<web-ifc-type-unknown>";`);
cppSchema.push("}");
cppSchema.push("}");
cppSchema.push("}");

fs.writeFileSync("../wasm/schema/ifc-schema.h", chSchema.join("\n"));
Expand Down
32 changes: 16 additions & 16 deletions src/schema-generator/gen_functional_types_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {Entity, Type, Prop} from "./gen_functional_types_interfaces";


export function generateInitialiser(type: Type, initialisersDone: Set<string>,buffer: Array<string>, crcTable:any,types: Type[],schemaName:string,schemaNo: number)
{
if (type.isEnum) return;

if (type.isList)
{
if (initialisersDone.has(type.name)) return;
buffer.push(`\t${crc32(type.name.toUpperCase(),crcTable)}:(v:any) => new ${schemaName}.${type.name}(v.map( (x:any) => x.value)),`);
buffer.push(`${crc32(type.name.toUpperCase(),crcTable)}:(v:any) => new ${schemaName}.${type.name}(v.map( (x:any) => x.value)),`);
initialisersDone.add(type.name);
return
}
Expand All @@ -24,7 +23,7 @@ export function generateInitialiser(type: Type, initialisersDone: Set<string>,bu

if (initialisersDone.has(type.name)) return;
initialisersDone.add(type.name);
buffer.push(`\t${crc32(type.name.toUpperCase(),crcTable)}:(v:any) => new ${schemaName}.${type.name}(v),`);
buffer.push(`${crc32(type.name.toUpperCase(),crcTable)}:(v:any) => new ${schemaName}.${type.name}(v),`);
return;
}

Expand All @@ -44,7 +43,7 @@ export function generatePropAssignment(p: Prop, i:number, types:Type[],schemaNam

if (p.set)
{
content = 'v['+i+'].map((p:any) => '
content = 'v['+i+']?.map((p:any) => p?.value ?'
if (type?.isSelect){
let isEntitySelect = type?.values.some(refType => types.findIndex( t => t.name==refType)==-1);
if (isEntitySelect) content+='new Handle(p.value)';
Expand All @@ -53,7 +52,7 @@ export function generatePropAssignment(p: Prop, i:number, types:Type[],schemaNam
else if (isType) content+='new '+schemaName+'.'+p.type+'(p.value)';
else if (p.primitive) content+='p.value';
else content+='new Handle<'+schemaName+'.'+p.type+'>(p.value)';
content +=')';
content +=' : null) || []';
}
else if (type?.isSelect)
{
Expand Down Expand Up @@ -150,24 +149,24 @@ export function generateClass(entity:Entity, classBuffer: Array<string>, types:T
{
classBuffer.push(`export class ${entity.name} extends ${entity.parent} {`);
}
classBuffer.push("\ttype:number="+crc32(entity.name.toUpperCase(),crcTable)+";");
classBuffer.push("type:number="+crc32(entity.name.toUpperCase(),crcTable)+";");


entity.inverseProps.forEach((prop) => {
let type = `${"(Handle<" + prop.type + `> | ${prop.type})` }${prop.set ? "[]" : ""} ${"| null"}`;
classBuffer.push(`\t${prop.name}!: ${type};`);
let type = `${"(Handle<" + prop.type + `>|${prop.type})` }${prop.set ? "[]" : ""} ${"| null"}`;
classBuffer.push(`${prop.name}!: ${type};`);
});

classBuffer.push(`\tconstructor(expressID: number, ${entity.derivedProps.filter(i => !entity.ifcDerivedProps.includes(i.name)).map((p) => `public ${p.name}: ${(types.some( x => x.name == p.type) || p.primitive) ? p.type : "(Handle<" + p.type + `> | ${p.type})` }${p.set ? "[]" : ""} ${p.optional ? "| null" : ""}`).join(", ")})`)
classBuffer.push(`\t{`)
classBuffer.push(`constructor(${entity.derivedProps.filter(i => !entity.ifcDerivedProps.includes(i.name)).map((p) => `public ${p.name}: ${(types.some( x => x.name == p.type) || p.primitive) ? p.type : "(Handle<" + p.type + `> | ${p.type})` }${p.set ? "[]" : ""} ${p.optional ? "| null" : ""}`).join(", ")})`)
classBuffer.push(`{`)
if (!entity.parent) {
classBuffer.push(`\t\tsuper(expressID);`)
classBuffer.push(`super();`)
} else {
var nonLocalProps = entity.derivedProps.filter(n => !entity.props.includes(n))
if (nonLocalProps.length ==0) classBuffer.push(`\t\t\tsuper(expressID);`);
else classBuffer.push(`\t\tsuper(expressID,${nonLocalProps.map((p) => generateSuperAssignment(p,entity.ifcDerivedProps,types)).join(", ")});`)
if (nonLocalProps.length ==0) classBuffer.push(`super();`);
else classBuffer.push(`super(${nonLocalProps.map((p) => generateSuperAssignment(p,entity.ifcDerivedProps,types)).join(", ")});`)
}
classBuffer.push("\t}");
classBuffer.push("}");
classBuffer.push("}");
}

Expand Down Expand Up @@ -197,7 +196,7 @@ export function crc32(str:string,crcTable:Array<number> ) {
export function expTypeToTSType(expTypeName:string)
{
let tsType = expTypeName;
if (expTypeName == "REAL" || expTypeName == "INTEGER" || expTypeName == "NUMBER")
if (expTypeName == "REAL" || expTypeName == "NUMBER" || expTypeName == "INTEGER")
{
tsType = "number";
}
Expand All @@ -223,7 +222,8 @@ export function expTypeToTSType(expTypeName:string)

export function expTypeToTypeNum(expTypeName:string) : number
{
if (expTypeName == "REAL" || expTypeName == "INTEGER" || expTypeName == "NUMBER") return 4;
if (expTypeName == "INTEGER") return 10;
else if (expTypeName == "REAL" || expTypeName == "NUMBER") return 4;
else if (expTypeName == "STRING") return 1;
else if (expTypeName == "BOOLEAN") return 3;
else if (expTypeName == "BINARY") return 4;
Expand Down
Loading

0 comments on commit a4bb13e

Please sign in to comment.