feat(ifc): Pset extraction + refactoring of repeated code

This commit is contained in:
Alan Rynne
2022-02-15 11:09:01 +01:00
parent ccbbe8e6d0
commit 8421456ccd
+49 -56
View File
@@ -126,6 +126,14 @@ module.exports = class IFCParser {
// Find children and populate element
const childrenIds = this.getAllRelatedItemsOfType( element.expressID, WebIFC.IFCRELCONTAINEDINSPATIALSTRUCTURE, 'RelatingStructure', 'RelatedElements' )
if( childrenIds.length > 0 ) element.rawChildren = childrenIds.map( ( childId ) => this.api.GetLine( this.modelId, childId, true ) )
// Find related property sets
const psetsIds = this.getAllRelatedItemsOfType( element.expressID, WebIFC.IFCRELDEFINESBYPROPERTIES, 'RelatingPropertyDefinition', 'RelatedObjects' )
if( psetsIds.length > 0 ) element.rawPsets = psetsIds.map( ( childId ) => this.api.GetLine( this.modelId, childId, true ) )
// Find related type properties
const typePropsId = this.getAllRelatedItemsOfType( element.expressID, WebIFC.IFCRELDEFINESBYTYPE, 'RelatingType', 'RelatedObjects' )
if( typePropsId.length > 0 ) element.rawTypeProps = typePropsId.map( ( childId ) => this.api.GetLine( this.modelId, childId, true ) )
// Lookup geometry in generated geometries object
if( this.productGeo[element.expressID] ) {
@@ -139,63 +147,13 @@ module.exports = class IFCParser {
const isSpecial = specialTypes.find( t => t.type === element.speckle_type )
// Recurse all children
if ( recursive ) {
await this.processSubElements( element, 'rawSpatialChildren', 'spatialChildren', isSpecial, recursive, depth, specialTypes )
await this.processSubElements( element, 'rawChildren', 'children', isSpecial, recursive, depth, specialTypes )
await this.processSubElements( element, 'rawPsets', 'propertySets', false, recursive, depth, specialTypes )
await this.processSubElements( element, 'rawTypeProps', 'typeProps', false, recursive, depth, specialTypes )
if( element.rawSpatialChildren ) {
if( !isSpecial )
element.spatialChildren = []
let childCount = {
}
for( let child of element.rawSpatialChildren ) {
let res = await this.traverse( child, recursive, depth + 1, specialTypes )
if( res.referencedId ) {
if( isSpecial ) {
let name = child[isSpecial.key]
if ( !name || name.length === 0 )
name = 'Undefined'
if( !childCount[name] )
childCount[name] = 0
if( childCount[name] > 0 )
name += '-' + childCount[name]++
element[name] = res
} else
element.spatialChildren.push( res )
this.project.__closure[res.referencedId.toString()] = depth
element.__closure[res.referencedId.toString()] = 1
// adds to parent (this element) the child's closure tree.
if( this.closureCache[child.expressID.toString()] ) {
for( let key of Object.keys( this.closureCache[child.expressID.toString()] ) ) {
element.__closure[key] = this.closureCache[child.expressID.toString()][key] + 1
}
}
}
}
delete element.rawSpatialChildren
}
if ( element.rawChildren ) {
element.children = []
for( let child of element.rawChildren ) {
let res = await this.traverse( child, recursive, depth + 1 )
if( res.referencedId ) {
element.children.push( res )
this.project.__closure[res.referencedId.toString()] = depth
element.__closure[res.referencedId.toString()] = 1
// adds to parent (this element) the child's closure tree.
if( this.closureCache[child.expressID.toString()] ) {
for( let key of Object.keys( this.closureCache[child.expressID.toString()] ) ) {
element.__closure[key] = this.closureCache[child.expressID.toString()][key] + 1
}
}
}
}
delete element.rawChildren
}
if( element.children || element.spatialChildren ) {
console.log( `${element.constructor.name} ${element.GlobalId}: children count: ${ element.children ? element.children.length : '0'}; spatial children count: ${element.spatialChildren ? element.spatialChildren.length : '0'} ` )
if( element.children || element.spatialChildren || element.propertySets || element.typeProps ) {
console.log( `${element.constructor.name} ${element.GlobalId}:\n\tchildren count: ${ element.children ? element.children.length : '0'};\n\tspatial children count: ${element.spatialChildren ? element.spatialChildren.length : '0'};\n\tproperty sets count: ${element.propertySets ? element.propertySets.length : 0};\n\ttype properties: ${element.typeProps ? element.typeProps.length : 0}` )
}
}
@@ -214,6 +172,41 @@ module.exports = class IFCParser {
}
async processSubElements( element, key, newKey, isSpecial, recursive, depth, specialTypes ) {
if ( element[key] ) {
if ( !isSpecial )
element[newKey] = []
let childCount = {}
for ( let child of element[key] ) {
let res = await this.traverse( child, recursive, depth + 1, specialTypes )
if ( res.referencedId ) {
if ( isSpecial ) {
let name = child[isSpecial.key]
if ( !name || name.length === 0 )
name = 'Undefined'
if ( !childCount[name] )
childCount[name] = 0
if ( childCount[name] > 0 )
name += '-' + childCount[name]++
element[name] = res
}
else
element[newKey].push( res )
this.project.__closure[res.referencedId.toString()] = depth
element.__closure[res.referencedId.toString()] = 1
// adds to parent (this element) the child's closure tree.
if ( this.closureCache[child.expressID.toString()] ) {
for ( let key of Object.keys( this.closureCache[child.expressID.toString()] ) ) {
element.__closure[key] = this.closureCache[child.expressID.toString()][key] + 1
}
}
}
}
delete element[key]
}
}
// (c) https://github.com/agviegas/web-ifc-three
extractVertexData( vertexData ) {
const vertices = []