2019-09-26 07:37:08 +10:00
|
|
|
import os
|
|
|
|
|
import math
|
|
|
|
|
import svgwrite
|
|
|
|
|
import time
|
2019-12-08 21:14:04 +11:00
|
|
|
import numpy
|
2019-11-02 20:08:34 +11:00
|
|
|
import pickle
|
2019-10-28 20:59:16 +11:00
|
|
|
from pathlib import Path
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
import OCC.gp
|
|
|
|
|
import OCC.Geom
|
|
|
|
|
import OCC.Bnd
|
|
|
|
|
import OCC.BRepBndLib
|
|
|
|
|
import OCC.BRep
|
|
|
|
|
import OCC.BRepPrimAPI
|
|
|
|
|
import OCC.BRepAlgoAPI
|
|
|
|
|
import OCC.BRepBuilderAPI
|
|
|
|
|
import OCC.TopOpeBRepTool
|
|
|
|
|
import OCC.TopOpeBRepBuild
|
|
|
|
|
import OCC.ShapeExtend
|
|
|
|
|
import OCC.GProp
|
|
|
|
|
import OCC.BRepGProp
|
|
|
|
|
import OCC.GC
|
|
|
|
|
import OCC.ShapeAnalysis
|
|
|
|
|
import OCC.TopTools
|
|
|
|
|
import OCC.TopExp
|
|
|
|
|
import OCC.HLRAlgo
|
|
|
|
|
import OCC.HLRBRep
|
|
|
|
|
import OCC.TopLoc
|
|
|
|
|
import OCC.Bnd
|
|
|
|
|
import OCC.BRepBndLib
|
|
|
|
|
import OCC.BRepTools
|
|
|
|
|
import OCC.TopoDS
|
|
|
|
|
import OCC.GeomLProp
|
2019-12-08 21:14:04 +11:00
|
|
|
import OCC.IntCurvesFace
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
from OCC.TopoDS import topods
|
|
|
|
|
|
|
|
|
|
import ifcopenshell
|
|
|
|
|
import ifcopenshell.geom
|
|
|
|
|
|
|
|
|
|
class IfcCutter:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.product_shapes = []
|
|
|
|
|
self.background_elements = []
|
|
|
|
|
self.cut_polygons = []
|
2019-10-28 20:59:16 +11:00
|
|
|
self.ifc_files = []
|
2019-09-26 07:37:08 +10:00
|
|
|
self.unit = None
|
2019-12-08 21:14:04 +11:00
|
|
|
self.resolved_pixels = set()
|
2019-09-26 07:37:08 +10:00
|
|
|
self.section_box = {
|
|
|
|
|
'top_left_corner': (-3., -2.45, 4.75),
|
|
|
|
|
'projection': (0., 1., 0.),
|
|
|
|
|
'x_axis': (1., 0., 0.),
|
|
|
|
|
'x': 15.,
|
|
|
|
|
'y': 7.,
|
|
|
|
|
'z': 3.5,
|
|
|
|
|
'shape': None,
|
|
|
|
|
'face': None
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
self.section_box = {
|
|
|
|
|
'top_left_corner': (-3.097275733947754, 0.42036718130111694, 11.297039031982422),
|
|
|
|
|
'projection': (0.0, 1.0000001192092896, 0.0),
|
|
|
|
|
'x_axis': (1.0000001192092896, 0.0, 0.0),
|
|
|
|
|
'x': 16.4,
|
|
|
|
|
'y': 16.4,
|
|
|
|
|
'z': 16.5,
|
|
|
|
|
'shape': None,
|
|
|
|
|
'face': None
|
|
|
|
|
}
|
|
|
|
|
self.section_box = {
|
|
|
|
|
'projection': (0.0, 1.0000001192092896, 0.0),
|
|
|
|
|
#'top_left_corner': (11.5675630569458, -4.225902557373047, 7.368159770965576),
|
|
|
|
|
'top_left_corner': (11.5675630569458, 0.1, 7.368159770965576),
|
|
|
|
|
'x_axis': (1.0000001192092896, 0.0, 0.0),
|
|
|
|
|
'x': 17.4,
|
|
|
|
|
'y': 8.37,
|
|
|
|
|
'z': 5.23,
|
|
|
|
|
'shape': None,
|
|
|
|
|
'face': None
|
|
|
|
|
}
|
|
|
|
|
self.section_box = {
|
|
|
|
|
'projection': (0.0, 3.2584136988589307e-07, -1.0),
|
|
|
|
|
'top_left_corner': (12.898375511169434, 31.655664443969727, 2.339174270629883),
|
|
|
|
|
'x_axis': (1.0, 0.0, 0.0),
|
|
|
|
|
'x': 17.4,
|
|
|
|
|
'y': 34.4,
|
2019-12-08 21:14:04 +11:00
|
|
|
'z': 1.9,
|
|
|
|
|
'shape': None,
|
|
|
|
|
'face': None
|
|
|
|
|
}
|
|
|
|
|
self.section_boxno = {
|
|
|
|
|
'projection': (0, 1, 0),
|
|
|
|
|
'x_axis': (1, 0, 0),
|
|
|
|
|
'y_axis': (0, 0, -1),
|
|
|
|
|
'top_left_corner': (-2, 2, 8),
|
|
|
|
|
'x': 14,
|
|
|
|
|
'y': 9,
|
|
|
|
|
'z': 2,
|
2019-10-28 20:59:16 +11:00
|
|
|
'shape': None,
|
|
|
|
|
'face': None
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 07:37:08 +10:00
|
|
|
def cut(self):
|
2019-10-28 20:59:16 +11:00
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Load files')
|
|
|
|
|
self.load_ifc_files()
|
|
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Get units')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.get_units()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Get product shapes')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.get_product_shapes()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Create section box')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.create_section_box()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Get cut polygons')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.get_cut_polygons()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Get background elements')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.get_background_elements()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Sort background elements')
|
2019-12-08 21:14:04 +11:00
|
|
|
self.sort_background_elements(reverse=True)
|
|
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Merge background_elements')
|
|
|
|
|
self.merge_background_elements()
|
|
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
print('# Sort background elements')
|
2019-09-26 07:37:08 +10:00
|
|
|
self.sort_background_elements()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# Timer logged at {:.2f} seconds'.format(time.time() - start_time))
|
|
|
|
|
|
|
|
|
|
def load_ifc_files(self):
|
|
|
|
|
for filename in Path('ifc/').glob('*.ifc'):
|
|
|
|
|
print('Loading file {} ...'.format(filename))
|
|
|
|
|
self.ifc_files.append(ifcopenshell.open(filename))
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
def get_units(self):
|
2019-10-28 20:59:16 +11:00
|
|
|
unit_assignment = self.ifc_files[0].by_type('IfcUnitAssignment')[0]
|
2019-09-26 07:37:08 +10:00
|
|
|
for unit in unit_assignment.Units:
|
|
|
|
|
if unit.UnitType == 'LENGTHUNIT':
|
|
|
|
|
self.unit = unit
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
def get_product_shapes(self):
|
2019-11-02 20:08:34 +11:00
|
|
|
shape_map = {}
|
|
|
|
|
|
|
|
|
|
if os.path.isfile('shapes.pickle'):
|
|
|
|
|
with open('shapes.pickle', 'rb') as shape_file:
|
|
|
|
|
shape_map = pickle.load(shape_file)
|
|
|
|
|
|
2019-09-26 07:37:08 +10:00
|
|
|
settings = ifcopenshell.geom.settings()
|
|
|
|
|
settings.set(settings.USE_PYTHON_OPENCASCADE, True)
|
2019-10-28 20:59:16 +11:00
|
|
|
products = []
|
|
|
|
|
for ifc_file in self.ifc_files:
|
|
|
|
|
products.extend(ifc_file.by_type('IfcProduct'))
|
|
|
|
|
total_products = len(products)
|
|
|
|
|
for i, product in enumerate(products):
|
|
|
|
|
print('{}/{} geometry processed ...'.format(i, total_products), end='\r', flush=True)
|
2019-09-26 07:37:08 +10:00
|
|
|
if product.is_a('IfcOpeningElement') or product.is_a('IfcSite'):
|
|
|
|
|
continue
|
|
|
|
|
if product.Representation is not None:
|
2019-11-02 20:08:34 +11:00
|
|
|
if product.GlobalId in shape_map:
|
|
|
|
|
shape = shape_map[product.GlobalId]
|
|
|
|
|
else:
|
|
|
|
|
shape = ifcopenshell.geom.create_shape(settings, product).geometry
|
|
|
|
|
shape_map[product.GlobalId] = shape
|
2019-09-26 07:37:08 +10:00
|
|
|
self.product_shapes.append((product, shape))
|
|
|
|
|
|
2019-11-02 20:08:34 +11:00
|
|
|
if not os.path.isfile('shapes.pickle'):
|
|
|
|
|
with open('shapes.pickle', 'wb') as shape_file:
|
|
|
|
|
pickle.dump(shape_map, shape_file, protocol=pickle.HIGHEST_PROTOCOL)
|
|
|
|
|
|
2019-12-08 21:14:04 +11:00
|
|
|
def sort_background_elements(self, reverse=None):
|
|
|
|
|
if reverse:
|
|
|
|
|
new_list = sorted(self.background_elements, key=lambda k: -k['z'])
|
|
|
|
|
else:
|
|
|
|
|
new_list = sorted(self.background_elements, key=lambda k: k['z'])
|
2019-09-26 07:37:08 +10:00
|
|
|
self.background_elements = new_list
|
|
|
|
|
|
2019-12-08 21:14:04 +11:00
|
|
|
def process_grid(self, face, resolution):
|
|
|
|
|
try:
|
|
|
|
|
bbox = self.get_bbox(face)
|
|
|
|
|
xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
|
|
|
|
|
except:
|
|
|
|
|
return
|
|
|
|
|
current_x = 0
|
|
|
|
|
current_y = 0
|
|
|
|
|
is_visible = False
|
|
|
|
|
while current_x < self.section_box['x']:
|
|
|
|
|
current_y = 0
|
|
|
|
|
while current_y > -self.section_box['y']:
|
|
|
|
|
if current_x < xmin \
|
|
|
|
|
or current_x > xmax \
|
|
|
|
|
or current_y < ymin \
|
|
|
|
|
or current_y > ymax:
|
|
|
|
|
current_y -= resolution
|
|
|
|
|
continue
|
|
|
|
|
if (current_x, current_y) in self.resolved_pixels:
|
|
|
|
|
current_y -= resolution
|
|
|
|
|
continue
|
|
|
|
|
point = numpy.array((current_x, current_y, 0))
|
|
|
|
|
hit = self.raycast(face, point)
|
|
|
|
|
if hit:
|
|
|
|
|
is_visible = True
|
|
|
|
|
self.resolved_pixels.add((current_x, current_y))
|
|
|
|
|
current_y -= resolution
|
|
|
|
|
current_x += resolution
|
|
|
|
|
return is_visible
|
|
|
|
|
|
|
|
|
|
def merge_background_elements(self):
|
|
|
|
|
background_elements = []
|
|
|
|
|
|
|
|
|
|
resolution = 0.1 # 10cm
|
|
|
|
|
|
|
|
|
|
# DO CUT
|
|
|
|
|
total_product_shapes = len(self.cut_polygons)
|
|
|
|
|
n = 0
|
|
|
|
|
for element in self.cut_polygons:
|
|
|
|
|
#print('{}/{} background elements processed ...'.format(n, total_product_shapes), end='\r', flush=True)
|
|
|
|
|
print('{}/{} cut polygons processed ...'.format(n, total_product_shapes))
|
|
|
|
|
print('{} resolved pixels'.format(len(self.resolved_pixels)))
|
|
|
|
|
n += 1
|
|
|
|
|
self.process_grid(element['geometry_face'], resolution)
|
|
|
|
|
|
|
|
|
|
# DO BACKGROUND
|
|
|
|
|
total_product_shapes = len(self.background_elements)
|
|
|
|
|
n = 0
|
|
|
|
|
for element in self.background_elements:
|
|
|
|
|
#print('{}/{} background elements processed ...'.format(n, total_product_shapes), end='\r', flush=True)
|
|
|
|
|
print('{}/{} background elements processed ...'.format(n, total_product_shapes))
|
|
|
|
|
print('{} resolved pixels'.format(len(self.resolved_pixels)))
|
|
|
|
|
n += 1
|
|
|
|
|
if element['type'] != 'polygon':
|
|
|
|
|
background_elements.append(element)
|
|
|
|
|
continue
|
|
|
|
|
is_visible = self.process_grid(element['geometry_face'], resolution)
|
|
|
|
|
if is_visible:
|
|
|
|
|
background_elements.append(element)
|
|
|
|
|
|
|
|
|
|
print('##### BEFORE it had {} and after it had {}'.format(
|
|
|
|
|
len(self.background_elements), len(background_elements)))
|
|
|
|
|
self.background_elements = background_elements
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
# my previous attempt
|
|
|
|
|
fused_elements = []
|
|
|
|
|
previous_element = None
|
|
|
|
|
for element in self.background_elements:
|
|
|
|
|
if previous_element is None:
|
|
|
|
|
previous_element = element
|
|
|
|
|
continue
|
|
|
|
|
if element['type'] == previous_element['type'] \
|
|
|
|
|
and element['raw'].GlobalId == previous_element['raw'].GlobalId \
|
|
|
|
|
and element['type'] == 'polygon':
|
|
|
|
|
fuse = OCC.BRepAlgoAPI.BRepAlgoAPI_Fuse(
|
|
|
|
|
previous_element['geometry'], element['geometry']).Shape()
|
|
|
|
|
print('FUSING')
|
|
|
|
|
exp = OCC.TopExp.TopExp_Explorer(fuse, OCC.TopAbs.TopAbs_WIRE)
|
|
|
|
|
previous_element = {
|
|
|
|
|
'raw': element['raw'],
|
|
|
|
|
'geometry': exp.Current(),
|
|
|
|
|
'geometry_face': fuse,
|
|
|
|
|
'type': 'polygon',
|
|
|
|
|
'z': element['z']
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
fused_elements.append(element)
|
|
|
|
|
#print(element)
|
|
|
|
|
self.background_elements = fused_elements
|
|
|
|
|
|
2019-09-26 07:37:08 +10:00
|
|
|
def create_section_box(self):
|
|
|
|
|
top_left_corner = OCC.gp.gp_Pnt(
|
|
|
|
|
self.section_box['top_left_corner'][0],
|
|
|
|
|
self.section_box['top_left_corner'][1],
|
|
|
|
|
self.section_box['top_left_corner'][2])
|
|
|
|
|
axis = OCC.gp.gp_Ax2(
|
|
|
|
|
top_left_corner,
|
|
|
|
|
OCC.gp.gp_Dir(
|
|
|
|
|
self.section_box['projection'][0],
|
|
|
|
|
self.section_box['projection'][1],
|
|
|
|
|
self.section_box['projection'][2]),
|
|
|
|
|
OCC.gp.gp_Dir(
|
|
|
|
|
self.section_box['x_axis'][0],
|
|
|
|
|
self.section_box['x_axis'][1],
|
|
|
|
|
self.section_box['x_axis'][2])
|
|
|
|
|
)
|
|
|
|
|
section_box = OCC.BRepPrimAPI.BRepPrimAPI_MakeBox(
|
|
|
|
|
axis, self.section_box['x'], self.section_box['y'], self.section_box['z']
|
|
|
|
|
)
|
|
|
|
|
self.section_box['shape'] = section_box.Shape()
|
|
|
|
|
self.section_box['face'] = section_box.BottomFace()
|
|
|
|
|
|
|
|
|
|
source = OCC.gp.gp_Ax3(axis)
|
|
|
|
|
destination = OCC.gp.gp_Ax3(
|
2019-10-28 20:59:16 +11:00
|
|
|
OCC.gp.gp_Pnt(0, 0, 0),
|
|
|
|
|
OCC.gp.gp_Dir(0, 0, -1),
|
2019-09-26 07:37:08 +10:00
|
|
|
OCC.gp.gp_Dir(1, 0, 0))
|
|
|
|
|
self.transformation = OCC.gp.gp_Trsf()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.transformation.SetDisplacement(source, destination)
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
def get_background_elements(self):
|
2019-10-28 20:59:16 +11:00
|
|
|
total_product_shapes = len(self.product_shapes)
|
|
|
|
|
n = 0
|
2019-12-08 21:14:04 +11:00
|
|
|
intersections = []
|
|
|
|
|
compound = OCC.TopoDS.TopoDS_Compound()
|
|
|
|
|
builder = OCC.BRep.BRep_Builder()
|
|
|
|
|
builder.MakeCompound(compound)
|
2019-09-26 07:37:08 +10:00
|
|
|
for product, shape in self.product_shapes:
|
2019-12-08 21:14:04 +11:00
|
|
|
builder.Add(compound, shape)
|
|
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
print('{}/{} background elements processed ...'.format(n, total_product_shapes), end='\r', flush=True)
|
|
|
|
|
#print('Processing product {} '.format(product.Name))
|
|
|
|
|
n += 1
|
2019-12-08 21:14:04 +11:00
|
|
|
|
2019-09-26 07:37:08 +10:00
|
|
|
intersection = OCC.BRepAlgoAPI.BRepAlgoAPI_Common(self.section_box['shape'], shape).Shape()
|
|
|
|
|
intersection_edges = self.get_booleaned_edges(intersection)
|
|
|
|
|
if len(intersection_edges) <= 0:
|
|
|
|
|
continue
|
2019-12-08 21:14:04 +11:00
|
|
|
intersections.append(intersection)
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
transformed_intersection = OCC.BRepBuilderAPI.BRepBuilderAPI_Transform(
|
|
|
|
|
intersection, self.transformation)
|
|
|
|
|
intersection = transformed_intersection.Shape()
|
|
|
|
|
|
|
|
|
|
edge_face_map = OCC.TopTools.TopTools_IndexedDataMapOfShapeListOfShape()
|
|
|
|
|
OCC.TopExp.topexp.MapShapesAndAncestors(
|
|
|
|
|
intersection, OCC.TopAbs.TopAbs_EDGE,
|
|
|
|
|
OCC.TopAbs.TopAbs_FACE, edge_face_map)
|
|
|
|
|
|
|
|
|
|
exp = OCC.TopExp.TopExp_Explorer(intersection, OCC.TopAbs.TopAbs_FACE)
|
|
|
|
|
while exp.More():
|
|
|
|
|
face = topods.Face(exp.Current())
|
2019-11-02 20:08:34 +11:00
|
|
|
normal = self.get_normal(face)
|
|
|
|
|
# Cull back-faces
|
|
|
|
|
if normal.Z() <= 0:
|
|
|
|
|
exp.Next()
|
|
|
|
|
continue
|
2019-09-26 07:37:08 +10:00
|
|
|
zpos, zmax = self.calculate_face_zpos(face)
|
|
|
|
|
self.build_new_face(face, zpos, product)
|
|
|
|
|
self.get_split_edges(edge_face_map, face, zmax, product)
|
|
|
|
|
exp.Next()
|
|
|
|
|
|
2019-12-08 21:14:04 +11:00
|
|
|
#print('test')
|
|
|
|
|
#print(compound)
|
|
|
|
|
#hits = self.get_raycast_hits(compound)
|
|
|
|
|
#print(hits)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_raycast_hits(self, shape):
|
|
|
|
|
resolution = 0.1 # 5cm
|
|
|
|
|
hits = []
|
|
|
|
|
current_x = 0
|
|
|
|
|
current_y = 0
|
|
|
|
|
while current_x < self.section_box['x'] /2:
|
|
|
|
|
current_y = 0
|
|
|
|
|
while current_y < self.section_box['y']/4:
|
|
|
|
|
point = numpy.array(self.section_box['top_left_corner'])
|
|
|
|
|
point = numpy.add(point, current_x * numpy.array(self.section_box['x_axis']))
|
|
|
|
|
point = numpy.add(point, current_y * numpy.array(self.section_box['y_axis']))
|
|
|
|
|
hit = self.raycast(shape, point)
|
|
|
|
|
if hit:
|
|
|
|
|
hits.append(hit)
|
|
|
|
|
current_y += resolution
|
|
|
|
|
current_x += resolution
|
|
|
|
|
print('row down')
|
|
|
|
|
return hits
|
|
|
|
|
|
|
|
|
|
def raycast(self, shape, point):
|
|
|
|
|
raycast = OCC.IntCurvesFace.IntCurvesFace_ShapeIntersector()
|
|
|
|
|
raycast.Load(shape, 0.01)
|
|
|
|
|
line = OCC.gp.gp_Lin(
|
|
|
|
|
OCC.gp.gp_Pnt(float(point[0]), float(point[1]), float(point[2])),
|
|
|
|
|
OCC.gp.gp_Dir( 0, 0, -1))
|
|
|
|
|
raycast.Perform(line, 0, self.section_box['z'])
|
|
|
|
|
return raycast.NbPnt() != 0
|
|
|
|
|
|
|
|
|
|
def raycast_at_projection_dir(self, shape, point):
|
|
|
|
|
raycast = OCC.IntCurvesFace.IntCurvesFace_ShapeIntersector()
|
|
|
|
|
raycast.Load(shape, 0.01)
|
|
|
|
|
line = OCC.gp.gp_Lin(
|
|
|
|
|
OCC.gp.gp_Pnt(float(point[0]), float(point[1]), float(point[2])),
|
|
|
|
|
OCC.gp.gp_Dir(
|
|
|
|
|
self.section_box['projection'][0],
|
|
|
|
|
self.section_box['projection'][1],
|
|
|
|
|
self.section_box['projection'][2]))
|
|
|
|
|
raycast.Perform(line, 0, self.section_box['z'])
|
|
|
|
|
if raycast.NbPnt() != 0:
|
|
|
|
|
# The smaller WParameter is the closer z-index
|
|
|
|
|
# Should be the first
|
|
|
|
|
return { 'face': raycast.Face(1), 'z': raycast.WParameter(1) }
|
|
|
|
|
|
|
|
|
|
def get_bbox(self, shape):
|
2019-09-26 07:37:08 +10:00
|
|
|
bbox = OCC.Bnd.Bnd_Box()
|
2019-12-08 21:14:04 +11:00
|
|
|
OCC.BRepBndLib.brepbndlib_Add(shape, bbox)
|
|
|
|
|
return bbox
|
|
|
|
|
|
|
|
|
|
def calculate_face_zpos(self, face):
|
|
|
|
|
bbox = self.get_bbox(face)
|
2019-09-26 07:37:08 +10:00
|
|
|
xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
|
|
|
|
|
zpos = zmin + ((zmax - zmin)/2)
|
|
|
|
|
return zpos, zmax
|
|
|
|
|
|
|
|
|
|
def get_split_edges(self, edge_face_map, face, zmax, product):
|
|
|
|
|
exp2 = OCC.TopExp.TopExp_Explorer(face, OCC.TopAbs.TopAbs_EDGE)
|
|
|
|
|
while exp2.More():
|
|
|
|
|
edge = topods.Edge(exp2.Current())
|
|
|
|
|
adjface = OCC.TopoDS.TopoDS_Face()
|
|
|
|
|
getadj = OCC.TopOpeBRepBuild.TopOpeBRepBuild_Tools.GetAdjacentFace(face, edge, edge_face_map, adjface)
|
|
|
|
|
if getadj:
|
2019-10-28 20:59:16 +11:00
|
|
|
try:
|
|
|
|
|
edge_angle = math.degrees(self.get_angle_between_faces(face, adjface))
|
|
|
|
|
except:
|
|
|
|
|
# TODO: Figure out when a math domain error might occur,
|
|
|
|
|
# because it does, sometimes.
|
|
|
|
|
edge_angle = 0
|
2019-09-26 07:37:08 +10:00
|
|
|
if edge_angle > 30 and edge_angle < 160:
|
|
|
|
|
newedge = self.build_new_edge(edge, zmax+0.01)
|
|
|
|
|
if newedge:
|
|
|
|
|
self.background_elements.append({
|
|
|
|
|
'raw': product,
|
|
|
|
|
'geometry': newedge,
|
|
|
|
|
'type': 'line',
|
|
|
|
|
'z': zmax+0.01
|
|
|
|
|
})
|
|
|
|
|
exp2.Next()
|
|
|
|
|
|
|
|
|
|
def get_angle_between_faces(self, f1, f2):
|
|
|
|
|
return self.convert_dot_product_to_angle(
|
|
|
|
|
self.get_dot_product_of_normals(
|
|
|
|
|
self.get_normal(f1), self.get_normal(f2)))
|
|
|
|
|
|
|
|
|
|
def get_normal(self, face):
|
|
|
|
|
surface = OCC.Geom.Handle_Geom_Surface(OCC.BRep.BRep_Tool.Surface(face))
|
|
|
|
|
props = OCC.GeomLProp.GeomLProp_SLProps(surface, 0, 0, 1, .001)
|
|
|
|
|
return props.Normal()
|
|
|
|
|
|
|
|
|
|
def get_dot_product_of_normals(self, n1, n2):
|
|
|
|
|
return n1.X() * n2.X() + n1.Y() * n2.Y() + n1.Z() * n2.Z()
|
|
|
|
|
|
|
|
|
|
def convert_dot_product_to_angle(self, dp):
|
|
|
|
|
return math.acos(dp)
|
|
|
|
|
|
|
|
|
|
def is_same_point(self, p1, p2):
|
|
|
|
|
return p1.X() == p2.X() \
|
|
|
|
|
and p1.Y() == p2.Y() \
|
|
|
|
|
and p1.Z() == p2.Z()
|
|
|
|
|
|
|
|
|
|
def build_new_edge(self, edge, zpos):
|
|
|
|
|
exp = OCC.TopExp.TopExp_Explorer(edge, OCC.TopAbs.TopAbs_VERTEX)
|
|
|
|
|
new_vertices = []
|
|
|
|
|
while exp.More():
|
|
|
|
|
current_vertex = topods.Vertex(exp.Current())
|
|
|
|
|
current_point = OCC.BRep.BRep_Tool.Pnt(current_vertex)
|
|
|
|
|
current_point.SetZ(zpos)
|
|
|
|
|
new_vertices.append(OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex(current_point).Vertex())
|
|
|
|
|
exp.Next()
|
|
|
|
|
try:
|
|
|
|
|
return OCC.BRepBuilderAPI.BRepBuilderAPI_MakeEdge(
|
|
|
|
|
new_vertices[0], new_vertices[1]
|
|
|
|
|
).Edge()
|
|
|
|
|
except:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def build_new_face(self, face, zpos, product):
|
|
|
|
|
exp = OCC.TopExp.TopExp_Explorer(face, OCC.TopAbs.TopAbs_WIRE)
|
|
|
|
|
while exp.More():
|
|
|
|
|
wireexp = OCC.BRepTools.BRepTools_WireExplorer(topods.Wire(exp.Current()))
|
|
|
|
|
new_wire_builder = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeWire()
|
|
|
|
|
first_vertex = None
|
|
|
|
|
previous_vertex = None
|
|
|
|
|
while wireexp.More():
|
|
|
|
|
current_vertex = wireexp.CurrentVertex()
|
|
|
|
|
current_point = OCC.BRep.BRep_Tool.Pnt(current_vertex)
|
|
|
|
|
# Dodgy technique to squash in Z axis
|
|
|
|
|
current_point.SetZ(zpos)
|
|
|
|
|
current_vertex = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex(current_point).Vertex()
|
|
|
|
|
if not first_vertex:
|
|
|
|
|
first_vertex = current_vertex
|
|
|
|
|
if not previous_vertex:
|
|
|
|
|
previous_vertex = current_vertex
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
new_wire_builder.Add(topods.Edge(
|
|
|
|
|
OCC.BRepBuilderAPI.BRepBuilderAPI_MakeEdge(
|
|
|
|
|
previous_vertex, current_vertex
|
|
|
|
|
).Edge()))
|
|
|
|
|
previous_vertex = current_vertex
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
wireexp.Next()
|
|
|
|
|
|
|
|
|
|
# make last edge
|
|
|
|
|
if not wireexp.More():
|
|
|
|
|
try:
|
|
|
|
|
new_wire_builder.Add(topods.Edge(
|
|
|
|
|
OCC.BRepBuilderAPI.BRepBuilderAPI_MakeEdge(
|
|
|
|
|
current_vertex, first_vertex
|
|
|
|
|
).Edge()))
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
try:
|
|
|
|
|
new_wire = new_wire_builder.Wire()
|
|
|
|
|
new_face = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeFace(new_wire).Face()
|
2019-11-02 20:08:34 +11:00
|
|
|
self.background_elements.append({
|
|
|
|
|
'raw': product,
|
|
|
|
|
'geometry': new_wire,
|
|
|
|
|
'geometry_face': new_face,
|
|
|
|
|
'type': 'polygon',
|
|
|
|
|
'z': zpos
|
|
|
|
|
})
|
2019-09-26 07:37:08 +10:00
|
|
|
except:
|
2019-10-28 20:59:16 +11:00
|
|
|
#print('Could not build face')
|
|
|
|
|
pass
|
2019-09-26 07:37:08 +10:00
|
|
|
exp.Next()
|
|
|
|
|
|
|
|
|
|
def get_area(self, shape):
|
|
|
|
|
gprops = OCC.GProp.GProp_GProps()
|
|
|
|
|
OCC.BRepGProp.brepgprop.SurfaceProperties(shape, gprops)
|
|
|
|
|
return gprops.Mass()
|
|
|
|
|
|
|
|
|
|
def get_booleaned_edges(self, shape):
|
|
|
|
|
edges = []
|
|
|
|
|
exp = OCC.TopExp.TopExp_Explorer(shape, OCC.TopAbs.TopAbs_EDGE)
|
|
|
|
|
while exp.More():
|
|
|
|
|
edges.append(topods.Edge(exp.Current()))
|
|
|
|
|
exp.Next()
|
|
|
|
|
return edges
|
|
|
|
|
|
|
|
|
|
def get_cut_polygons(self):
|
2019-10-28 20:59:16 +11:00
|
|
|
total_product_shapes = len(self.product_shapes)
|
|
|
|
|
n = 0
|
2019-09-26 07:37:08 +10:00
|
|
|
for product, shape in self.product_shapes:
|
2019-10-28 20:59:16 +11:00
|
|
|
print('{}/{} cut elements processed ...'.format(n, total_product_shapes), end='\r', flush=True)
|
|
|
|
|
n += 1
|
2019-09-26 07:37:08 +10:00
|
|
|
section = OCC.BRepAlgoAPI.BRepAlgoAPI_Section(self.section_box['face'], shape).Shape()
|
|
|
|
|
section_edges = self.get_booleaned_edges(section)
|
|
|
|
|
if len(section_edges) <= 0:
|
|
|
|
|
continue
|
|
|
|
|
wires = self.connect_edges_into_wires(section_edges)
|
|
|
|
|
for i in range(wires.Length()):
|
|
|
|
|
wire_shape = wires.Value(i+1)
|
|
|
|
|
|
|
|
|
|
transformed_wire = OCC.BRepBuilderAPI.BRepBuilderAPI_Transform(
|
|
|
|
|
wire_shape, self.transformation)
|
|
|
|
|
wire_shape = transformed_wire.Shape()
|
|
|
|
|
|
|
|
|
|
wire = topods.Wire(wire_shape)
|
2019-12-08 21:14:04 +11:00
|
|
|
face = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeFace(wire).Face()
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
self.cut_polygons.append({
|
|
|
|
|
'raw': product,
|
2019-12-08 21:14:04 +11:00
|
|
|
'geometry': wire,
|
|
|
|
|
'geometry_face': face
|
2019-09-26 07:37:08 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
def connect_edges_into_wires(self, unconnected_edges):
|
|
|
|
|
edges = OCC.TopTools.TopTools_HSequenceOfShape()
|
|
|
|
|
edges_handle = OCC.TopTools.Handle_TopTools_HSequenceOfShape(edges)
|
|
|
|
|
wires = OCC.TopTools.TopTools_HSequenceOfShape()
|
|
|
|
|
wires_handle = OCC.TopTools.Handle_TopTools_HSequenceOfShape(wires)
|
|
|
|
|
|
|
|
|
|
for edge in unconnected_edges:
|
|
|
|
|
edges.Append(edge)
|
|
|
|
|
|
|
|
|
|
OCC.ShapeAnalysis.ShapeAnalysis_FreeBounds.ConnectEdgesToWires(edges_handle, 1e-5, True, wires_handle)
|
|
|
|
|
return wires_handle.GetObject()
|
|
|
|
|
|
|
|
|
|
class IfcCutterDebug(IfcCutter):
|
|
|
|
|
def cut(self):
|
|
|
|
|
self.occ_display = ifcopenshell.geom.utils.initialize_display()
|
|
|
|
|
super().cut()
|
|
|
|
|
|
|
|
|
|
def create_section_box(self):
|
|
|
|
|
super().create_section_box()
|
|
|
|
|
self.display_everything_with_section_plane()
|
|
|
|
|
|
|
|
|
|
def get_cut_polygons(self):
|
|
|
|
|
super().get_cut_polygons()
|
|
|
|
|
self.display_cut_polygons()
|
|
|
|
|
|
|
|
|
|
def get_background_elements(self):
|
|
|
|
|
super().get_background_elements()
|
|
|
|
|
self.display_background_elements()
|
|
|
|
|
|
|
|
|
|
def display_everything_with_section_plane(self):
|
|
|
|
|
section_face_display = ifcopenshell.geom.utils.display_shape(self.section_box['face'])
|
2019-10-28 20:59:16 +11:00
|
|
|
ifcopenshell.geom.utils.set_shape_transparency(section_face_display, 0.8)
|
2019-09-26 07:37:08 +10:00
|
|
|
section_box_display = ifcopenshell.geom.utils.display_shape(self.section_box['shape'])
|
2019-10-28 20:59:16 +11:00
|
|
|
ifcopenshell.geom.utils.set_shape_transparency(section_box_display, 0.5)
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
transformed_box = OCC.BRepBuilderAPI.BRepBuilderAPI_Transform(
|
|
|
|
|
self.section_box['shape'], self.transformation)
|
|
|
|
|
box_display = ifcopenshell.geom.utils.display_shape(transformed_box.Shape())
|
|
|
|
|
ifcopenshell.geom.utils.set_shape_transparency(box_display, 0.2)
|
|
|
|
|
|
|
|
|
|
for shape in self.product_shapes:
|
|
|
|
|
ifcopenshell.geom.utils.display_shape(shape[1])
|
|
|
|
|
input('Debug: showing everything with section plane.')
|
|
|
|
|
|
|
|
|
|
def display_cut_polygons(self):
|
|
|
|
|
self.occ_display.EraseAll()
|
|
|
|
|
for polygon in self.cut_polygons:
|
|
|
|
|
ifcopenshell.geom.utils.display_shape(polygon['geometry'], clr='BLACK')
|
|
|
|
|
face = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeFace(polygon['geometry']).Face()
|
|
|
|
|
face_display = ifcopenshell.geom.utils.display_shape(face)
|
|
|
|
|
ifcopenshell.geom.utils.set_shape_transparency(face_display, 0.5)
|
|
|
|
|
input('Debug: showing cut polygons.')
|
|
|
|
|
|
|
|
|
|
def display_background_elements(self):
|
|
|
|
|
self.occ_display.EraseAll()
|
|
|
|
|
for element in self.background_elements:
|
|
|
|
|
if element['type'] == 'line':
|
|
|
|
|
ifcopenshell.geom.utils.display_shape(element['geometry'], clr='PURPLE')
|
|
|
|
|
elif element['type'] == 'polyline':
|
|
|
|
|
ifcopenshell.geom.utils.display_shape(element['geometry_face'], clr='RED')
|
|
|
|
|
elif element['type'] == 'polygon':
|
|
|
|
|
ifcopenshell.geom.utils.display_shape(element['geometry_face'])
|
|
|
|
|
input('Debug: showing background elements.')
|
|
|
|
|
|
|
|
|
|
class SvgWriter():
|
|
|
|
|
def __init__(self, ifc_cutter):
|
|
|
|
|
self.ifc_cutter = ifc_cutter
|
|
|
|
|
self.scale = 1 / 100 # 1:100
|
|
|
|
|
|
|
|
|
|
def write(self):
|
|
|
|
|
self.calculate_scale()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.svg = svgwrite.Drawing('output.svg',
|
|
|
|
|
debug=False,
|
2019-09-26 07:37:08 +10:00
|
|
|
size=('{}mm'.format(self.ifc_cutter.section_box['x'] * self.scale),
|
|
|
|
|
'{}mm'.format(self.ifc_cutter.section_box['y'] * self.scale)),
|
|
|
|
|
viewBox=('0 0 {} {}'.format(
|
|
|
|
|
self.ifc_cutter.section_box['x'] * self.scale,
|
|
|
|
|
self.ifc_cutter.section_box['y'] * self.scale)))
|
2019-10-28 20:59:16 +11:00
|
|
|
self.add_stylesheet()
|
2019-09-26 07:37:08 +10:00
|
|
|
self.draw_background_elements()
|
|
|
|
|
self.draw_cut_polygons()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.svg.save(pretty=True)
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
def calculate_scale(self):
|
|
|
|
|
# TODO: properly handle units
|
|
|
|
|
if self.ifc_cutter.unit.Name == 'METRE':
|
|
|
|
|
self.scale *= 1000
|
|
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
def add_stylesheet(self):
|
|
|
|
|
with open('styles/default.css', 'r') as stylesheet:
|
|
|
|
|
self.svg.defs.add(self.svg.style(stylesheet.read()))
|
|
|
|
|
|
2019-09-26 07:37:08 +10:00
|
|
|
def draw_background_elements(self):
|
|
|
|
|
for element in self.ifc_cutter.background_elements:
|
|
|
|
|
if element['type'] == 'polygon':
|
2019-10-28 20:59:16 +11:00
|
|
|
self.draw_polygon(element, 'background')
|
2019-09-26 07:37:08 +10:00
|
|
|
elif element['type'] == 'polyline':
|
2019-10-28 20:59:16 +11:00
|
|
|
self.draw_polyline(element, 'background')
|
2019-09-26 07:37:08 +10:00
|
|
|
elif element['type'] == 'line':
|
2019-10-28 20:59:16 +11:00
|
|
|
self.draw_line(element, 'background')
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
def draw_cut_polygons(self):
|
|
|
|
|
for polygon in self.ifc_cutter.cut_polygons:
|
2019-10-28 20:59:16 +11:00
|
|
|
self.draw_polygon(polygon, 'cut')
|
2019-09-26 07:37:08 +10:00
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
def draw_polyline(self, element, position):
|
|
|
|
|
classes = self.get_classes(element['raw'], position)
|
2019-09-26 07:37:08 +10:00
|
|
|
exp = OCC.BRepTools.BRepTools_WireExplorer(element['geometry'])
|
|
|
|
|
points = []
|
|
|
|
|
while exp.More():
|
|
|
|
|
point = OCC.BRep.BRep_Tool.Pnt(exp.CurrentVertex())
|
|
|
|
|
points.append((point.X() * self.scale, -point.Y() * self.scale))
|
|
|
|
|
exp.Next()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.svg.add(self.svg.polyline(points=points, class_=' '.join(classes)))
|
2019-09-26 07:37:08 +10:00
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
def draw_line(self, element, position):
|
|
|
|
|
classes = self.get_classes(element['raw'], position)
|
2019-09-26 07:37:08 +10:00
|
|
|
exp = OCC.TopExp.TopExp_Explorer(element['geometry'], OCC.TopAbs.TopAbs_VERTEX)
|
|
|
|
|
points = []
|
|
|
|
|
while exp.More():
|
|
|
|
|
point = OCC.BRep.BRep_Tool.Pnt(topods.Vertex(exp.Current()))
|
|
|
|
|
points.append((point.X() * self.scale, -point.Y() * self.scale))
|
|
|
|
|
exp.Next()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.svg.add(self.svg.line(start=points[0], end=points[1], class_=' '.join(classes)))
|
2019-09-26 07:37:08 +10:00
|
|
|
|
2019-10-28 20:59:16 +11:00
|
|
|
def draw_polygon(self, polygon, position):
|
|
|
|
|
classes = self.get_classes(polygon['raw'], position)
|
2019-09-26 07:37:08 +10:00
|
|
|
exp = OCC.BRepTools.BRepTools_WireExplorer(polygon['geometry'])
|
|
|
|
|
points = []
|
|
|
|
|
while exp.More():
|
|
|
|
|
point = OCC.BRep.BRep_Tool.Pnt(exp.CurrentVertex())
|
|
|
|
|
points.append((point.X() * self.scale, -point.Y() * self.scale))
|
|
|
|
|
exp.Next()
|
2019-10-28 20:59:16 +11:00
|
|
|
self.svg.add(self.svg.polygon(points=points, class_=' '.join(classes)))
|
|
|
|
|
|
|
|
|
|
def get_classes(self, element, position):
|
|
|
|
|
classes = [position, element.is_a()]
|
|
|
|
|
for association in element.HasAssociations:
|
|
|
|
|
if association.is_a('IfcRelAssociatesMaterial'):
|
|
|
|
|
classes.append('material-{}'.format(association.RelatingMaterial.Name))
|
|
|
|
|
classes.append('globalid-{}'.format(element.GlobalId))
|
|
|
|
|
return classes
|
2019-09-26 07:37:08 +10:00
|
|
|
|
|
|
|
|
print('# Starting process')
|
|
|
|
|
ifc_cutter = IfcCutter()
|
|
|
|
|
#ifc_cutter = IfcCutterDebug()
|
|
|
|
|
svg_writer = SvgWriter(ifc_cutter)
|
|
|
|
|
ifc_cutter.cut()
|
2019-10-28 20:59:16 +11:00
|
|
|
start_time = time.time()
|
2019-09-26 07:37:08 +10:00
|
|
|
svg_writer.write()
|
2019-10-28 20:59:16 +11:00
|
|
|
print('# SVG writing finished in {:.2f} seconds'.format(time.time() - start_time))
|