mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-06 07:51:47 +00:00
Add ifccityjson to start with supporting conversions between IFC and CityJSON files
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# CJ2IFC
|
||||
Converter for CityJSON files to IFC.
|
||||
|
||||
-- WARNING --
|
||||
|
||||
CJ2IFC only came into being 14/04/2021. Be prepared for lots of bugs, unfinished implementations and little to no documentation!
|
||||
|
||||
## Dependencies
|
||||
- [IfcOpenShell](https://github.com/IfcOpenShell/IfcOpenShell)
|
||||
- [CJIO](https://github.com/cityjson/cjio)
|
||||
|
||||
## Usage of CJ2IFC
|
||||
Following command will execute a conversion from CityJSON to IFC
|
||||
|
||||
python CJ2IFC.py [-i input file] [-o output file] [-n name of identification attribute]
|
||||
|
||||
The example file that could be used is example/3D_BAG_example.json
|
||||
|
||||
python CJ2IFC.py -i example/3DBAG_example.json -o example/3DBAG_example.ifc -n identificatie
|
||||
|
||||
## Implemented geometries
|
||||
- [ ] "MultiPoint"
|
||||
- [ ] "MultiLineString"
|
||||
- [ ] "MultiSurface"
|
||||
- [ ] "CompositeSurface"
|
||||
- [x] "Solid"
|
||||
- [ ] "MultiSolid": exterior shell
|
||||
- [ ] "MultiSolid": interior shell
|
||||
- [ ] "CompositeSolid"
|
||||
- [ ] "GeometryInstance"
|
||||
|
||||
## TODO
|
||||
- [x] CityJSON Attributes as IFC properties in 'CityJSON_attributes' pset
|
||||
- [x] Implement georeferencing
|
||||
- [ ] Do not use template IFC for new IFC file, but make IFC file from scratch
|
||||
@@ -0,0 +1,190 @@
|
||||
import ifcopenshell
|
||||
import warnings
|
||||
from geometry import GeometryIO
|
||||
|
||||
JSON_TO_IFC = {
|
||||
"Building": ["IfcBuilding"],
|
||||
"BuildingPart": ["IfcBuilding", {"CompositionType": "Partial"}], # CompositionType: Partial
|
||||
"BuildingInstallation": ["IfcDistributionElement"],
|
||||
"Road": ["IfcCivilElement"],
|
||||
"TransportSquare": ["IfcSpace"],
|
||||
"TINRelief": ["IfcGeographicElement"],
|
||||
"WaterBody": ["IfcGeographicElement"],
|
||||
"LandUse": ["IfcGeographicElement"],
|
||||
"PlantCover": ["IfcGeographicElement"],
|
||||
"SolitaryVegetationObject": ["IfcGeographicElement"],
|
||||
"CityFurniture": ["IfcFurnishingElement"],
|
||||
"GenericCityObject": ["IfcCivilElement"],
|
||||
"Bridge": ["IfcCivilElement"],
|
||||
"BridgePart": ["IfcCivilElement"],
|
||||
"BridgeInstallation": ["IfcCivilElement"],
|
||||
"BridgeConstructionElement": ["IfcCivilElement"],
|
||||
"Tunnel": ["IfcCivilElement"],
|
||||
"TunnelPart": ["IfcCivilElement"],
|
||||
"TunnelInstallation": ["IfcCivilElement"],
|
||||
"CityObjectGroup": ["IfcCivilElement"],
|
||||
"GroundSurface": ["IfcSlab"],
|
||||
"RoofSurface": ["IfcRoof"],
|
||||
"WallSurface": ["IfcWall"]
|
||||
}
|
||||
|
||||
class Cityjson2ifc:
|
||||
def __init__(self):
|
||||
self.city_model = None
|
||||
self.IFC_model = None
|
||||
self.properties = {}
|
||||
self.geometry = GeometryIO()
|
||||
self.configuration()
|
||||
|
||||
|
||||
def configuration(self, file_destination="output.ifc", name_attribute=None):
|
||||
self.properties["file_destination"] = file_destination
|
||||
self.properties["name_attribute"] = name_attribute
|
||||
|
||||
|
||||
def convert(self, city_model):
|
||||
self.city_model = city_model
|
||||
self.create_new_file()
|
||||
self.create_metadata()
|
||||
self.geometry.build_vertices(self.IFC_model,
|
||||
coords=city_model.j["vertices"],
|
||||
scale=self.properties["local_scale"])
|
||||
# self.build_vertices()
|
||||
self.create_IFC_classes()
|
||||
self.write_file()
|
||||
|
||||
def create_metadata(self):
|
||||
# Georeferencing
|
||||
self.properties["local_translation"] = None
|
||||
self.properties["local_scale"] = None
|
||||
if self.city_model.is_transform():
|
||||
self.properties["local_scale"] = self.city_model.j['transform']['scale']
|
||||
local_translation = self.city_model.j['transform']['translate']
|
||||
self.properties["local_translation"] = {
|
||||
"Eastings": local_translation[0],
|
||||
"Northings": local_translation[1],
|
||||
"OrthogonalHeight": local_translation[2]
|
||||
}
|
||||
|
||||
epsg = self.city_model.get_epsg()
|
||||
if epsg:
|
||||
# Meter is assumed as unit for now
|
||||
unit = self.IFC_model.createIfcSIUnit(None, "LENGTHUNIT", None, "METRE")
|
||||
crs = self.IFC_model.create_entity("IfcProjectedCrs", Name=f"epsg:{epsg}")
|
||||
self.IFC_model.create_entity("IfcMapConversion", self.IFC_representation_context, **self.properties["local_translation"])
|
||||
|
||||
self.properties["owner_history"] = self.IFC_model.by_type("IfcOwnerHistory")[0]
|
||||
|
||||
def create_new_file(self):
|
||||
self.IFC_model = ifcopenshell.open('example/template.ifc')
|
||||
self.IFC_site = self.IFC_model.by_type('IfcSite')[0]
|
||||
self.IFC_representation_sub_context = self.IFC_model.by_type("IFCGEOMETRICREPRESENTATIONSUBCONTEXT")[0]
|
||||
self.IFC_representation_context = self.IFC_model.by_type("IFCGEOMETRICREPRESENTATIONCONTEXT")[0]
|
||||
# self.IFC_model = ifcopenshell.file(schema='IFC4')
|
||||
|
||||
def write_file(self):
|
||||
self.IFC_model.write(self.properties["file_destination"])
|
||||
|
||||
def create_IFC_classes(self):
|
||||
for obj_id, obj in self.city_model.get_cityobjects().items():
|
||||
|
||||
# CityJSON type to class
|
||||
mapping = JSON_TO_IFC[obj.type]
|
||||
IFC_class = mapping[0]
|
||||
data = {}
|
||||
# Add attributes if it is specified in mapping
|
||||
# Example: BuildingPart to IfcBuilding with CompositionType: Partial
|
||||
if len(mapping) > 1:
|
||||
data.update(mapping[1])
|
||||
|
||||
# attributes
|
||||
IFC_name = None
|
||||
if "name_attribute" in self.properties and self.properties["name_attribute"] in obj.attributes:
|
||||
IFC_name = obj.attributes[self.properties["name_attribute"]]
|
||||
|
||||
# TODO children
|
||||
|
||||
# TODO parents
|
||||
|
||||
# TODO geometry_type
|
||||
|
||||
# geometry_lod
|
||||
lod = 0
|
||||
geometry = None
|
||||
for geom in obj.geometry:
|
||||
if geom.lod > lod:
|
||||
geometry = geom
|
||||
lod = geom.lod
|
||||
|
||||
IFC_children = []
|
||||
if geometry.surfaces:
|
||||
for surface_id in geometry.surfaces:
|
||||
IFC_child_class = JSON_TO_IFC[geometry.surfaces[surface_id]["type"]][0]
|
||||
child_data = {"GlobalId": ifcopenshell.guid.new(),
|
||||
"Name": IFC_child_class
|
||||
}
|
||||
# CREATE ENTITY
|
||||
surface_geometry = self.geometry.create_IFC_surface(self.IFC_model, geometry, surface_id)
|
||||
if surface_geometry:
|
||||
child_data["Representation"] = self.create_IFC_representation(surface_geometry)
|
||||
IFC_children.append(self.IFC_model.create_entity(IFC_child_class, **child_data))
|
||||
|
||||
else:
|
||||
IFC_geometry = self.geometry.create_IFC_geometry(self.IFC_model, geometry)
|
||||
if IFC_geometry:
|
||||
data["Representation"] = self.create_IFC_representation(IFC_geometry)
|
||||
data["GlobalId"] = ifcopenshell.guid.new()
|
||||
data["Name"] = IFC_name
|
||||
|
||||
IFC_object = self.IFC_model.create_entity(IFC_class, **data)
|
||||
# Define aggregation
|
||||
self.IFC_model.create_entity("IfcRelAggregates",
|
||||
**{"GlobalId": ifcopenshell.guid.new(),
|
||||
"RelatedObjects": [IFC_object],
|
||||
"RelatingObject": self.IFC_site}
|
||||
)
|
||||
if IFC_children:
|
||||
self.IFC_model.create_entity("IfcRelAggregates",
|
||||
**{"GlobalId": ifcopenshell.guid.new(),
|
||||
"RelatedObjects": IFC_children,
|
||||
"RelatingObject": IFC_object})
|
||||
|
||||
self.create_property_set(obj.attributes, IFC_object)
|
||||
|
||||
def create_IFC_representation(self, IFC_geometry):
|
||||
shape_representation = self.IFC_model.create_entity("IfcShapeRepresentation",
|
||||
self.IFC_representation_sub_context, 'Body', 'Brep',
|
||||
[IFC_geometry])
|
||||
product_representation = self.IFC_model.create_entity("IfcProductDefinitionShape",
|
||||
Representations=[shape_representation])
|
||||
return product_representation
|
||||
|
||||
def create_property_set(self, CJ_attributes, IFC_entity):
|
||||
IFC_object_properties = []
|
||||
for property, val in CJ_attributes.items():
|
||||
if val == None:
|
||||
continue
|
||||
|
||||
if type(val) == int:
|
||||
IFC_type = "IfcInteger"
|
||||
elif type(val) == float:
|
||||
IFC_type = "IfcReal"
|
||||
elif type(val) == bool:
|
||||
IFC_type = "IfcBoolean"
|
||||
else:
|
||||
IFC_type = "IfcText"
|
||||
|
||||
IFC_object_properties.append(
|
||||
self.IFC_model.createIfcPropertySingleValue(property, property,
|
||||
self.IFC_model.create_entity(IFC_type, val), None)
|
||||
)
|
||||
property_set = self.IFC_model.createIfcPropertySet(ifcopenshell.guid.new(),
|
||||
self.properties["owner_history"],
|
||||
"CityJSON_attributes",
|
||||
None,
|
||||
IFC_object_properties)
|
||||
|
||||
self.IFC_model.createIfcRelDefinesByProperties(ifcopenshell.guid.new(),
|
||||
self.properties["owner_history"],
|
||||
None, None, [IFC_entity],
|
||||
property_set)
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
|
||||
ISO-10303-21;
|
||||
HEADER;
|
||||
FILE_DESCRIPTION(('ViewDefinition [CoordinationView]'),'2;1');
|
||||
FILE_NAME('template.ifc','2021-03-17T14:54:00+11:00',(),(),'IfcOpenShell 0.6.0b0','BlenderBIM 0.0.999999','Nobody');
|
||||
FILE_SCHEMA(('IFC4'));
|
||||
ENDSEC;
|
||||
DATA;
|
||||
#1=IFCPERSON('HSeldon','Seldon','Hari',$,$,$,$,$);
|
||||
#2=IFCORGANIZATION('APTR','Aperture Science',$,$,$);
|
||||
#3=IFCPERSONANDORGANIZATION(#1,#2,$);
|
||||
#4=IFCACTORROLE(.USERDEFINED.,'CONTRIBUTOR',$);
|
||||
#5=IFCTELECOMADDRESS(.USERDEFINED.,'The main webpage of the software collection.','WEBPAGE',$,$,$,$,'https://ifcopenshell.org',$);
|
||||
#6=IFCTELECOMADDRESS(.USERDEFINED.,'The CJ2IFC webpage of the software collection.','WEBPAGE',$,$,$,$,'https://github.com/LaurensJN/IFC2JS',$);
|
||||
#7=IFCTELECOMADDRESS(.USERDEFINED.,'The source code repository of the software collection.','REPOSITORY',$,$,$,$,'https://github.com/IfcOpenShell/IfcOpenShell.git',$);
|
||||
#8=IFCORGANIZATION($,'IfcOpenShell','IfcOpenShell is an open source (LGPL) software library that helps users and software developers to work with the IFC file format.',(#4),(#5,#6,#7));
|
||||
#9=IFCAPPLICATION(#8,'0.0.999999','CityJSON to IFC converter','CJ2IFC');
|
||||
#10=IFCOWNERHISTORY(#3,#9,.READWRITE.,.ADDED.,1615934569,#3,#9,1615934569);
|
||||
#11=IFCPROJECT('2yjpApQSX3ZAZudc2AGHb3',#10,'My Project',$,$,$,$,(#20,#27),#15);
|
||||
#12=IFCSIUNIT(*,.LENGTHUNIT.,$,.METRE.);
|
||||
#13=IFCSIUNIT(*,.AREAUNIT.,$,.SQUARE_METRE.);
|
||||
#14=IFCSIUNIT(*,.VOLUMEUNIT.,$,.CUBIC_METRE.);
|
||||
#15=IFCUNITASSIGNMENT((#12,#13,#14));
|
||||
#16=IFCCARTESIANPOINT((0.,0.,0.));
|
||||
#17=IFCDIRECTION((0.,0.,1.));
|
||||
#18=IFCDIRECTION((1.,0.,0.));
|
||||
#19=IFCAXIS2PLACEMENT3D(#16,#17,#18);
|
||||
#20=IFCGEOMETRICREPRESENTATIONCONTEXT($,'Model',3,1.E-05,#19,$);
|
||||
#21=IFCGEOMETRICREPRESENTATIONSUBCONTEXT('Body','Model',*,*,*,*,#20,$,.MODEL_VIEW.,$);
|
||||
#22=IFCOWNERHISTORY(#3,#9,.READWRITE.,.ADDED.,1615934569,#3,#9,1615934569);
|
||||
#23=IFCSITE('1maxFQa9z7q8w50tN19Dw9',#22,'My Site',$,$,#30,$,$,$,$,$,$,$,$);
|
||||
#24=IFCOWNERHISTORY(#3,#9,.READWRITE.,.ADDED.,1615934569,#3,#9,1615934569);
|
||||
#25=IFCRELAGGREGATES('1GuJ2jz2TA49gFFSYhrqGa',#24,$,$,#11,(#23));
|
||||
#26=IFCCARTESIANPOINT((0.,0.,0.));
|
||||
#27=IFCDIRECTION((0.,0.,1.));
|
||||
#28=IFCDIRECTION((1.,0.,0.));
|
||||
#29=IFCAXIS2PLACEMENT3D(#26,#27,#28);
|
||||
#30=IFCLOCALPLACEMENT($,#29);
|
||||
ENDSEC;
|
||||
END-ISO-10303-21;
|
||||
@@ -0,0 +1,81 @@
|
||||
import warnings
|
||||
|
||||
class GeometryIO:
|
||||
def __init__(self):
|
||||
self.vertices = {}
|
||||
|
||||
def build_vertices(self, IFC_model, coords, scale=None):
|
||||
for coord in coords:
|
||||
if scale:
|
||||
IFC_vertex = tuple([float(xyz) * coord_scale
|
||||
for xyz, coord_scale
|
||||
in zip(coord, scale)])
|
||||
else:
|
||||
IFC_vertex = [float(xyz) for xyz in coord]
|
||||
|
||||
IFC_cartesian_point = IFC_model.create_entity("IfcCartesianPoint", IFC_vertex)
|
||||
self.vertices[tuple(coord)] = IFC_cartesian_point
|
||||
|
||||
def create_IFC_geometry(self, IFC_model, geometry):
|
||||
if geometry.type == "Solid":
|
||||
return self.create_IFC_closed_shell(IFC_model, geometry)
|
||||
elif geometry.type in ["CompositeSolid", "MultiSolid"]:
|
||||
return self.create_IFC_composite_closed_shell(IFC_model, geometry)
|
||||
else:
|
||||
warnings.warn("Types other than solids are not yet supported")
|
||||
return
|
||||
|
||||
def create_IFC_composite_closed_shell(self, IFC_model, geometry):
|
||||
shells = []
|
||||
for shell in geometry.boundaries:
|
||||
outershell = shell[0]
|
||||
faces = []
|
||||
for face in outershell: # exterior shell
|
||||
for triangle in face:
|
||||
faces.append(self.create_IFC_face(IFC_model, triangle))
|
||||
|
||||
shells.append(IFC_model.create_entity("IfcClosedShell", faces))
|
||||
IFC_geometry = IFC_model.create_entity("IfcShellBasedSurfaceModel", shells)
|
||||
return IFC_geometry
|
||||
|
||||
def create_IFC_closed_shell(self, IFC_model, geometry):
|
||||
outershell = geometry.boundaries[0]
|
||||
# print(geometry.surfaces[0]['surface_idx'][0])
|
||||
faces = []
|
||||
for face in outershell: # exterior shell
|
||||
for triangle in face:
|
||||
faces.append(self.create_IFC_face(IFC_model, triangle))
|
||||
|
||||
if len(geometry.boundaries) == 1:
|
||||
shell = IFC_model.create_entity("IfcClosedShell", faces)
|
||||
IFC_geometry = IFC_model.create_entity("IfcShellBasedSurfaceModel", [shell])
|
||||
return IFC_geometry
|
||||
|
||||
# TODO: INTERIOR SHELL
|
||||
warnings.warn("Solid interior shell not yet supported")
|
||||
return
|
||||
# for boundary in geometry.boundaries[1]: # interior shell
|
||||
# for face in boundary:
|
||||
# for triangle in face:
|
||||
# print(triangle)
|
||||
# print(geometry.boundaries)
|
||||
|
||||
def create_IFC_surface(self, IFC_model, geometry, surface_id):
|
||||
face_ids = geometry.surfaces[surface_id]["surface_idx"]
|
||||
faces = []
|
||||
|
||||
for shell, face_id in face_ids:
|
||||
for triangle in geometry.boundaries[shell][face_id]:
|
||||
faces.append(self.create_IFC_face(IFC_model, triangle))
|
||||
|
||||
shell = IFC_model.create_entity("IfcOpenShell", faces)
|
||||
IFC_geometry = IFC_model.create_entity("IfcShellBasedSurfaceModel", [shell])
|
||||
return IFC_geometry
|
||||
|
||||
def create_IFC_face(self, IFC_model, face):
|
||||
vertices = []
|
||||
for vertex in face:
|
||||
vertices.append(self.vertices[tuple(vertex)])
|
||||
polyloop = IFC_model.create_entity("IfcPolyLoop", vertices)
|
||||
outerbound = IFC_model.create_entity("IfcFaceOuterBound", polyloop, True)
|
||||
return IFC_model.create_entity("IfcFace", [outerbound])
|
||||
@@ -0,0 +1,24 @@
|
||||
import argparse
|
||||
from cjio import cityjson
|
||||
from cityjson2ifc import Cityjson2ifc
|
||||
|
||||
# Press the green button in the gutter to run the script.
|
||||
if __name__ == '__main__':
|
||||
# Example:
|
||||
# python ifccityjson.py -i example/3DBAG_example.json -o example/output.ifc -n identificatie
|
||||
parser = argparse.ArgumentParser(description="")
|
||||
parser.add_argument("-i", "--input", type=str, help="input CityJSON file", required=True)
|
||||
parser.add_argument("-o", "--output", type=str, help="output IFC file. Standard is output.ifc")
|
||||
parser.add_argument("-n", "--name", type=str, help="Attribute containing the name")
|
||||
args = parser.parse_args()
|
||||
|
||||
city_model = cityjson.load(args.input)
|
||||
data = {}
|
||||
if args.name:
|
||||
data["name_attribute"] = args.name
|
||||
if args.output:
|
||||
data["file_destination"] = args.output
|
||||
|
||||
converter = Cityjson2ifc()
|
||||
converter.configuration(**data)
|
||||
converter.convert(city_model)
|
||||
Reference in New Issue
Block a user