|
| 1 | +# IFCJSON_python - ifc2json4.py |
| 2 | +# Copyright (C) 2020 Jan Brouwer <jan@brewsky.nl> |
| 3 | + |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | + |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | + |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +# Convert IFC SPF file to IFC.JSON-4 |
| 18 | +# https://github.com/IFCJSON-Team |
| 19 | + |
| 20 | +import os |
| 21 | +import argparse |
| 22 | +import json |
| 23 | +import uuid |
| 24 | +import ifcopenshell |
| 25 | +import ifcopenshell.guid as guid |
| 26 | +import ifcjson.common as common |
| 27 | + |
| 28 | +# ifc_file = ifcopenshell.open('../samples/7m900_tue_hello_wall_with_door.ifc') |
| 29 | +# schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(ifc_file.schema) |
| 30 | +id_objects = {} |
| 31 | + |
| 32 | +# Create dictionary of OwnerHistory objects |
| 33 | +ownerHistories = {} |
| 34 | + |
| 35 | +# Create dictionary of IfcGeometricRepresentationContext objects |
| 36 | +representationContexts = {} |
| 37 | + |
| 38 | +def entityToDict(entity): |
| 39 | + attr_dict = entity.__dict__ |
| 40 | + |
| 41 | + ref = { |
| 42 | + "type": entity.is_a() |
| 43 | + } |
| 44 | + |
| 45 | + # Add missing GlobalId to OwnerHistory |
| 46 | + if entity.is_a() == 'IfcOwnerHistory': |
| 47 | + if not entity.id() in ownerHistories: |
| 48 | + ownerHistories[entity.id()] = guid.new() |
| 49 | + attr_dict["GlobalId"] = ownerHistories[entity.id()] |
| 50 | + |
| 51 | + # Add missing GlobalId to IfcGeometricRepresentationContext |
| 52 | + if entity.is_a() == 'IfcGeometricRepresentationContext': |
| 53 | + if not entity.id() in representationContexts: |
| 54 | + representationContexts[entity.id()] = guid.new() |
| 55 | + attr_dict["GlobalId"] = representationContexts[entity.id()] |
| 56 | + |
| 57 | + # check for globalid |
| 58 | + if "GlobalId" in attr_dict: |
| 59 | + uuid = guid.split(guid.expand(attr_dict["GlobalId"]))[1:-1] |
| 60 | + ref["ref"] = uuid |
| 61 | + if not attr_dict["GlobalId"] in id_objects: |
| 62 | + d = { |
| 63 | + "type": entity.is_a() |
| 64 | + } |
| 65 | + |
| 66 | + # Add missing GlobalId to OwnerHistory |
| 67 | + if entity.is_a() == 'IfcOwnerHistory': |
| 68 | + d["GlobalId"] = guid.split(guid.expand(ownerHistories[entity.id()]))[1:-1] |
| 69 | + |
| 70 | + # Add missing GlobalId to IfcGeometricRepresentationContext |
| 71 | + if entity.is_a() == 'IfcGeometricRepresentationContext': |
| 72 | + d["GlobalId"] = guid.split(guid.expand(representationContexts[entity.id()]))[1:-1] |
| 73 | + |
| 74 | + for i in range(0,len(entity)): |
| 75 | + attr = entity.attribute_name(i) |
| 76 | + attrKey = common.toLowerCamelcase(attr) |
| 77 | + if attr == "GlobalId": |
| 78 | + d[attrKey] = uuid |
| 79 | + else: |
| 80 | + if attr in attr_dict: |
| 81 | + jsonValue = getEntityValue(attr_dict[attr]) |
| 82 | + if jsonValue: |
| 83 | + if ((entity.is_a() == 'IfcOwnerHistory') and (attr == "GlobalId")): |
| 84 | + pass |
| 85 | + else: |
| 86 | + d[attrKey] = jsonValue |
| 87 | + if attr_dict[attr] == None: |
| 88 | + continue |
| 89 | + elif isinstance(attr_dict[attr], ifcopenshell.entity_instance): |
| 90 | + d[attrKey] = entityToDict(attr_dict[attr]) |
| 91 | + elif isinstance(attr_dict[attr], tuple): |
| 92 | + subEnts = [] |
| 93 | + for subEntity in attr_dict[attr]: |
| 94 | + if isinstance(subEntity, ifcopenshell.entity_instance): |
| 95 | + subEntJson = entityToDict(subEntity) |
| 96 | + if subEntJson: |
| 97 | + subEnts.append(subEntJson) |
| 98 | + else: |
| 99 | + subEnts.append(subEntity) |
| 100 | + if len(subEnts) > 0: |
| 101 | + d[attrKey] = subEnts |
| 102 | + else: |
| 103 | + d[attrKey] = attr_dict[attr] |
| 104 | + id_objects[attr_dict["GlobalId"]] = d |
| 105 | + return ref |
| 106 | + else: |
| 107 | + d = { |
| 108 | + "type": entity.is_a() |
| 109 | + } |
| 110 | + |
| 111 | + for i in range(0,len(entity)): |
| 112 | + attr = entity.attribute_name(i) |
| 113 | + attrKey = common.toLowerCamelcase(attr) |
| 114 | + if attr in attr_dict: |
| 115 | + if not attr == "OwnerHistory": |
| 116 | + jsonValue = getEntityValue(attr_dict[attr]) |
| 117 | + if jsonValue: |
| 118 | + d[attrKey] = jsonValue |
| 119 | + if attr_dict[attr] == None: |
| 120 | + continue |
| 121 | + elif isinstance(attr_dict[attr], ifcopenshell.entity_instance): |
| 122 | + d[attrKey] = entityToDict(attr_dict[attr]) |
| 123 | + elif isinstance(attr_dict[attr], tuple): |
| 124 | + subEnts = [] |
| 125 | + for subEntity in attr_dict[attr]: |
| 126 | + if isinstance(subEntity, ifcopenshell.entity_instance): |
| 127 | + # subEnts.append(None) |
| 128 | + subEntJson = entityToDict(subEntity) |
| 129 | + if subEntJson: |
| 130 | + subEnts.append(subEntJson) |
| 131 | + else: |
| 132 | + subEnts.append(subEntity) |
| 133 | + if len(subEnts) > 0: |
| 134 | + d[attrKey] = subEnts |
| 135 | + else: |
| 136 | + d[attrKey] = attr_dict[attr] |
| 137 | + return d |
| 138 | + |
| 139 | +def getEntityValue(value): |
| 140 | + if value == None: |
| 141 | + jsonValue = None |
| 142 | + elif isinstance(value, ifcopenshell.entity_instance): |
| 143 | + jsonValue = entityToDict(value) |
| 144 | + elif isinstance(value, tuple): |
| 145 | + jsonValue = None |
| 146 | + subEnts = [] |
| 147 | + for subEntity in value: |
| 148 | + subEnts.append(getEntityValue(subEntity)) |
| 149 | + jsonValue = subEnts |
| 150 | + else: |
| 151 | + jsonValue = value |
| 152 | + return jsonValue |
| 153 | + |
| 154 | + |
| 155 | +# jsonObjects= [] |
| 156 | +# entityIter = iter(ifc_file) |
| 157 | +# for entity in entityIter: |
| 158 | +# entityToDict(entity) |
| 159 | +# for key in id_objects: |
| 160 | +# jsonObjects.append(id_objects[key]) |
| 161 | +# with open('../7m900_tue_hello_wall_with_door.json', 'w') as outfile: |
| 162 | +# json.dump(jsonObjects, outfile, indent=4) |
| 163 | + |
| 164 | +def spf2Json(ifcFilePath, jsonFilePath): |
| 165 | + ifc_file = ifcopenshell.open(ifcFilePath) |
| 166 | + |
| 167 | + jsonObjects= [] |
| 168 | + entityIter = iter(ifc_file) |
| 169 | + for entity in entityIter: |
| 170 | + entityToDict(entity) |
| 171 | + for key in id_objects: |
| 172 | + jsonObjects.append(id_objects[key]) |
| 173 | + with open(jsonFilePath, 'w') as outfile: |
| 174 | + json.dump(jsonObjects, outfile, indent=4) |
0 commit comments