Experimental code for a user interface

This commit is contained in:
Dion Moult
2019-10-03 22:11:05 +10:00
parent 624888adab
commit e37c882fe2
3 changed files with 12886 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+78
View File
@@ -0,0 +1,78 @@
import bpy
import json
class IfcSchema():
def __init__(self):
with open('/home/dion/Projects/IfcOpenShell/src/ifcblenderexport/IfcElement.json') as f:
self.elements = json.load(f)
ifc_schema = IfcSchema()
class BIMProperties(bpy.types.PropertyGroup):
def getIfcClasses(self, context):
return [(e, e, '') for e in ifc_schema.elements]
def getIfcPredefinedTypes(self, context):
results = []
for name, data in ifc_schema.elements.items():
if name != bpy.context.scene.BIMProperties.ifc_class:
continue
for attribute in data['attributes']:
if attribute['name'] != 'PredefinedType':
continue
return [(e, e, '') for e in attribute['enum_values']]
data_dir: bpy.props.StringProperty(name="Data Directory")
ifc_class: bpy.props.EnumProperty(items = getIfcClasses, name="Class")
ifc_predefined_type: bpy.props.EnumProperty(
items = getIfcPredefinedTypes,
name="Predefined Type", default=None)
ifc_userdefined_type: bpy.props.StringProperty(name="Userdefined Type")
class BIMPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "Building Information Modeling"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
scene = context.scene
bim_properties = bpy.context.scene.BIMProperties
layout.label(text="System Setup:")
row = layout.row()
row.prop(bim_properties, "data_dir")
layout.label(text="IFC Categorisation:")
row = layout.row()
row.prop(bim_properties, "ifc_class")
row = layout.row()
row.prop(bim_properties, "ifc_predefined_type")
row = layout.row()
row.prop(bim_properties, "ifc_userdefined_type")
row = layout.row()
row.operator("render.render")
classes = (
BIMProperties,
BIMPanel
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.BIMProperties = bpy.props.PointerProperty(type=BIMProperties)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del(bpy.types.Scene.BIMProperties)
if __name__ == "__main__":
register()
+134
View File
@@ -0,0 +1,134 @@
"""This script converts the computer iterpretable listing ifcXML XSD into a list
of non-abstract children of IfcProduct"""
import xml.etree.ElementTree as ET
import json
class IFC4Extractor:
def __init__(self, xsd_file):
self.xsd_file = xsd_file
tree = ET.parse(self.xsd_file)
self.root = tree.getroot()
self.ns = {'xs': 'http://www.w3.org/2001/XMLSchema'}
self.elements = {}
#self.filters = ['IfcBuildingElement']
self.filters = ['IfcElement']
self.filtered_elements = {}
def extract(self):
for element in self.root.findall("xs:element", self.ns):
if self.is_descendant_from_class(element, "IfcRoot"):
print('Processing {}'.format(element.attrib['name']))
data = {
'is_abstract': self.is_abstract(element),
'parent': element.attrib['substitutionGroup'].replace('ifc:', ''),
'attributes': self.get_attributes(element),
'complex_attributes': self.get_complex_attributes(element)
}
self.elements[element.attrib['name']] = data
for filter in self.filters:
if self.is_descendant_from_class(element, filter):
self.filtered_elements.setdefault(filter, {})[element.attrib['name']] = data
def export(self):
for filter in self.filters:
with open("{}.json".format(filter), "w") as file:
file.write(json.dumps(self.filtered_elements[filter], indent=4))
#with open("ifc_rooted_IFC2X3.json", "w") as file:
# file.write(json.dumps(self.elements, indent=4))
def is_descendant_from_class(self, element, class_name):
if element is None \
or "substitutionGroup" not in element.attrib:
return False
if element.attrib["substitutionGroup"] == 'ifc:{}'.format(class_name):
return True
return self.is_descendant_from_class(self.get_parent_element(element), class_name)
def is_abstract(self, element):
return True if 'abstract' in element.attrib else False
def get_attributes(self, element, attributes = None):
if attributes is None:
attributes = []
if element.attrib['substitutionGroup'] != self.get_ifcroot_parent_name():
attributes = self.get_attributes(self.get_parent_element(element), attributes)
for attribute in self.root.findall(self.get_attribute_xpath(element), self.ns):
try:
attributes.append({
'name': attribute.attrib['name'],
'type': attribute.attrib['type'].replace('ifc:', ''),
'is_enum': self.is_enum(attribute),
'enum_values': self.get_enum_values(attribute)
})
except KeyError as e:
print('Attribute {} is missing key {}'.format(attribute.attrib, e))
return attributes
def get_attribute_xpath(self, element):
return "./xs:complexType[@name='{}']/xs:complexContent/xs:extension/xs:attribute[@name][@type]".format(element.attrib['name'])
def get_ifcroot_parent_name(self):
return "ifc:Entity"
def get_complex_attributes(self, element, attributes = None):
if attributes is None:
attributes = []
if element.attrib['substitutionGroup'] != self.get_ifcroot_parent_name():
attributes = self.get_complex_attributes(self.get_parent_element(element), attributes)
for attribute in self.root.findall(
"./xs:complexType[@name='{}']/xs:complexContent/xs:extension/xs:sequence/xs:element[@name]".format(
element.attrib['name']
), self.ns):
if 'type' in attribute.attrib:
attributes.append({
'name': attribute.attrib['name'],
'type': attribute.attrib['type'].replace('ifc:', '')
})
else:
type_element = attribute.find('./xs:complexType/xs:sequence/xs:element[@ref]', self.ns)
if type_element is not None:
attributes.append({
'name': attribute.attrib['name'],
'type': type_element.attrib['ref'].replace('ifc:', '')
})
return attributes
def get_parent_element(self, element):
return self.root.find("./xs:element[@name='{}']".format(
element.attrib["substitutionGroup"].replace('ifc:', '')
), self.ns)
def is_enum(self, attribute):
return 'Enum' in attribute.attrib['type']
def get_enum_values(self, attribute):
if not self.is_enum(attribute):
return []
values = []
for enumeration in self.root.findall(
"./xs:simpleType[@name='{}']/xs:restriction/xs:enumeration".format(
attribute.attrib['type'].replace('ifc:', '')
), self.ns):
values.append(enumeration.attrib['value'].upper())
return values
def is_ifc_version(self, version):
return version in self.xsd_file
class IFC2X3Extractor(IFC4Extractor):
# IFC2X3 seems to store regular attributes where IFC4 stores complex attributes
def get_attribute_xpath(self, element):
return "./xs:complexType[@name='{}']/xs:complexContent/xs:extension/xs:sequence/xs:element[@name]".format(element.attrib['name'])
def get_ifcroot_parent_name(self):
return "ex:Entity"
# IFC2X3 does not seem to store complex attributes in the XSD file
def get_complex_attributes(self, element, attributes = None):
return []
extractor = IFC4Extractor("IFC4_ADD2.xsd")
#extractor = IFC2X3Extractor("IFC2X3.xsd")
extractor.extract()
extractor.export()