add python
This commit is contained in:
parent
1ef0ba57cb
commit
c354085b3b
2 changed files with 135 additions and 0 deletions
47
python/create.py
Normal file
47
python/create.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import ifcopenshell
|
||||
from ifcopenshell.api import run
|
||||
|
||||
# Create a blank model
|
||||
model = ifcopenshell.file()
|
||||
|
||||
# All projects must have one IFC Project element
|
||||
project = run("root.create_entity", model, ifc_class="IfcProject", name="My Project")
|
||||
|
||||
# Geometry is optional in IFC, but because we want to use geometry in this example, let's define units
|
||||
# Assigning without arguments defaults to metric units
|
||||
run("unit.assign_unit", model)
|
||||
|
||||
# Let's create a modeling geometry context, so we can store 3D geometry (note: IFC supports 2D too!)
|
||||
context = run("context.add_context", model, context_type="Model")
|
||||
|
||||
# In particular, in this example we want to store the 3D "body" geometry of objects, i.e. the body shape
|
||||
body = run("context.add_context", model, context_type="Model",
|
||||
context_identifier="Body", target_view="MODEL_VIEW", parent=context)
|
||||
|
||||
# Create a site, building, and storey. Many hierarchies are possible.
|
||||
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")
|
||||
|
||||
# Since the site is our top level location, assign it to the project
|
||||
# Then place our building on the site, and our storey in the building
|
||||
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)
|
||||
|
||||
# Let's create a new wall
|
||||
wall = run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# Give our wall a local origin at (0, 0, 0)
|
||||
run("geometry.edit_object_placement", model, product=wall)
|
||||
|
||||
# Add a new wall-like body geometry, 5 meters long, 3 meters high, and 200mm thick
|
||||
representation = run("geometry.add_wall_representation", model, context=body, length=5, height=3, thickness=0.2)
|
||||
# Assign our new body geometry back to our wall
|
||||
run("geometry.assign_representation", model, product=wall, representation=representation)
|
||||
|
||||
# Place our wall in the ground floor
|
||||
run("spatial.assign_container", model, relating_structure=storey, product=wall)
|
||||
|
||||
# Write out to a file
|
||||
model.write("output.ifc")
|
||||
88
python/extract.py
Normal file
88
python/extract.py
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
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 lst:
|
||||
print("adding",item)
|
||||
run("spatial.assign_container", model, relating_structure=storey, product=item)
|
||||
|
||||
|
||||
model.write(name)
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
arg = sys.argv[1]
|
||||
print(f">> filename:{arg}")
|
||||
if (arg == "list"):
|
||||
print(os.listdir("./files"))
|
||||
p = "./files/" + arg + ".ifc"
|
||||
if os.path.exists(p):
|
||||
model = load_model(p)
|
||||
lst = get_walls(model)
|
||||
select(model,"IfcBuildingStorey")
|
||||
create("test.ifc",lst)
|
||||
else:
|
||||
print(os.listdir("./files"))
|
||||
|
||||
else:
|
||||
print("extract [filepath]")
|
||||
Loading…
Add table
Reference in a new issue