Skip to content

Commit

Permalink
Arch: Added .ifcjson export type
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Nov 27, 2020
1 parent 15e6610 commit c3b0af2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Mod/Arch/Init.py
Expand Up @@ -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")
Expand Down
43 changes: 40 additions & 3 deletions src/Mod/Arch/exportIFC.py
Expand Up @@ -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()

Expand Down Expand Up @@ -1534,15 +1544,18 @@ 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
FreeCAD.ActiveDocument.recompute()

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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2364,6 +2377,7 @@ def getText(field,obj):
result = result.encode("utf8")
return result


def getAxisContext(ifcfile):

"""gets or creates an axis context"""
Expand All @@ -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"""
Expand All @@ -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()

0 comments on commit c3b0af2

Please sign in to comment.