hackathon-zurich/python/extract.py
2024-02-03 14:29:39 +01:00

108 lines
3.6 KiB
Python

import sys
import os
import ifcopenshell
import ifcopenshell.util.element
import ifcopenshell.util.selector
from ifcopenshell.api import run
def load_model(filepath):
print("loading " + filepath)
model = ifcopenshell.open(filepath)
return model
def get_container(obj):
if obj is not None:
container = ifcopenshell.util.element.get_container(obj)
if container is not None:
return container.Name
def get_walls(model):
i=1
lst = []
for wall in model.by_type("IfcWall"):
print(f"{i}:{wall.Name},container:{get_container(wall)}")
lst.append(wall)
i+=1
return lst
def wall_type(model):
walls = model.by_type("IfcWall")
if len(walls) > 0:
wall = walls[0]
if wall is not None:
print(wall)
wall_type = ifcopenshell.util.element.get_type(wall)
if wall_type is not None:
print(f"The wall type of {wall.Name} is {wall_type.Name}")
def select(model,what):
print(f"{what}")
lst = ifcopenshell.util.selector.filter_elements(model, what)
for elem in lst:
print(elem.Name)
def create(name,objects):
print("creating")
model = ifcopenshell.file()
project = run("root.create_entity", model, ifc_class="IfcProject", name="My Project")
run("unit.assign_unit", model)
context = run("context.add_context", model, context_type="Model")
body = run("context.add_context", model, context_type="Model",
context_identifier="Body", target_view="MODEL_VIEW", parent=context)
site = run("root.create_entity", model, ifc_class="IfcSite", name="My Site")
building = run("root.create_entity", model, ifc_class="IfcBuilding", name="Building A")
storey = run("root.create_entity", model, ifc_class="IfcBuildingStorey", name="Ground Floor")
run("aggregate.assign_object", model, relating_object=project, product=site)
run("aggregate.assign_object", model, relating_object=site, product=building)
run("aggregate.assign_object", model, relating_object=building, product=storey)
if False:
wall = run("root.create_entity", model, ifc_class="IfcWall")
run("geometry.edit_object_placement", model, product=wall)
representation = run("geometry.add_wall_representation", model, context=body, length=5, height=3, thickness=0.2)
run("geometry.assign_representation", model, product=wall, representation=representation)
run("spatial.assign_container", model, relating_structure=storey, product=wall)
for item in objects:
print("adding",item)
run("spatial.assign_container", model, relating_structure=storey, product=item)
print("writing " + name)
model.write(name)
def compute(p):
model = load_model(p)
lst = get_walls(model)
select(model,"IfcBuildingStorey")
print("writing test.ifc")
create("../output/test.ifc",lst)
def compute_server(p):
model = load_model(p)
lst = get_walls(model)
select(model,"IfcBuildingStorey")
print("writing test.ifc")
arg = "test"
p = "/home/fordj/SOURCES/FORDJ/fordjx/hackathon-zurich/ifc/" + arg + ".ifc"
create(p,lst)
if len(sys.argv) == 1:
arg = "duplex"
#p = "../ifc/" + arg + ".ifc"
p = "/home/fordj/SOURCES/FORDJ/fordjx/hackathon-zurich/ifc/" + arg + ".ifc"
compute_server(p)
elif len(sys.argv) > 1:
arg = sys.argv[1]
print(f">> filename:{arg}")
if (arg == "list"):
print(os.listdir("../ifc"))
p = "../ifc/" + arg + ".ifc"
if os.path.exists(p):
compute(p)
else:
print(os.listdir("../ifc"))
else:
print("extract [filepath]")
# ....