diff --git a/src/Mod/Arch/Init.py b/src/Mod/Arch/Init.py index 884d62d40de9..b0ad6d0bf7d6 100644 --- a/src/Mod/Arch/Init.py +++ b/src/Mod/Arch/Init.py @@ -22,6 +22,7 @@ # add import/export types FreeCAD.addImportType("Industry Foundation Classes (*.ifc)","importIFC") FreeCAD.addExportType("Industry Foundation Classes (*.ifc)","exportIFC") +FreeCAD.addExportType("Industry Foundation Classes - IFCJSON (*.ifcJSON)","exportIFC") FreeCAD.addImportType("Wavefront OBJ - Arch module (*.obj)","importOBJ") FreeCAD.addExportType("Wavefront OBJ - Arch module (*.obj)","importOBJ") FreeCAD.addExportType("WebGL file (*.html)","importWebGL") diff --git a/src/Mod/Arch/exportIFC.py b/src/Mod/Arch/exportIFC.py index e85e8080f337..5d3b491d9745 100644 --- a/src/Mod/Arch/exportIFC.py +++ b/src/Mod/Arch/exportIFC.py @@ -195,6 +195,16 @@ def export(exportList, filename, colors=None, preferences=None): "Visit https://wiki.freecadweb.org/IfcOpenShell " "to learn about installing it.") return + if filename.lower().endswith("json"): + import json + try: + from ifcjson import ifc2json5a + except: + try: + import ifc2json5a + except: + _err("Error: Unable to locate ifc2json5a module. Aborting.") + return starttime = time.time() @@ -1534,7 +1544,10 @@ def export(exportList, filename, colors=None, preferences=None): filename = decode(filename) - ifcfile.write(filename) + if filename.lower().endswith("json"): + writeJson(filename,ifcfile) + else: + ifcfile.write(filename) if preferences['STORE_UID']: # some properties might have been changed @@ -1542,7 +1555,7 @@ def export(exportList, filename, colors=None, preferences=None): os.remove(templatefile) - if preferences['DEBUG'] and ifcbin.compress: + if preferences['DEBUG'] and ifcbin.compress and (not filename.lower().endswith("json")): f = pyopen(filename,"r") s = len(f.read().split("\n")) f.close() @@ -1637,7 +1650,7 @@ def getIfcTypeFromObj(obj): if not "::" in ifctype: ifctype = "Ifc" + ifctype - elif ifctype == "App::DocumentObjctGroup": + elif ifctype == "IfcApp::DocumentObjctGroup": ifctype = "IfcGroup" else: # it makes no sense to return IfcPart::Cylinder for a Part::Cylinder @@ -2364,6 +2377,7 @@ def getText(field,obj): result = result.encode("utf8") return result + def getAxisContext(ifcfile): """gets or creates an axis context""" @@ -2379,6 +2393,7 @@ def getAxisContext(ifcfile): nctx = ifcfile.createIfcGeometricRepresentationSubContext('Axis','Model',None,None,None,None,ctx,None,"MODEL_VIEW",None); return nctx + def createAxis(ifcfile,obj,preferences): """Creates an axis for a given wall, if applicable""" @@ -2391,3 +2406,25 @@ def createAxis(ifcfile,obj,preferences): axis = ifcfile.createIfcShapeRepresentation(ctx,'Axis','Curve2D',[curve]) return axis return None + + +def writeJson(filename,ifcfile): + + """writes an .ifcjson file""" + + import json + try: + from ifcjson import ifc2json5a + except: + try: + import ifc2json5a + except: + print("Error: Unable to locate ifc2json5a module. Aborting.") + return + print("Converting IFC to JSON...") + jsonfile = ifc2json5a.IFC2JSON5a(ifcfile).spf2Json() + f = pyopen(filename,'w') + s = json.dumps(jsonfile,indent=4) + #print("json:",s) + f.write(s) + f.close()