Skip to content

Commit f20ae8e

Browse files
committed
create a setting to produce pythonOCC shapes for ifcopenshell.geom.iterator and -create_shape()
1 parent 8f853ac commit f20ae8e

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

src/ifcopenshell-python/ifcopenshell/geom/__init__.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,33 @@
2222

2323
from .. import ifcopenshell_wrapper
2424

25-
settings = ifcopenshell_wrapper.settings
25+
def has_occ():
26+
try: import OCC.BRepTools
27+
except: return False
28+
return True
29+
30+
31+
has_occ = has_occ()
32+
wrap_shape_creation = lambda settings, shape: shape
33+
if has_occ:
34+
import occ_utils as utils
35+
wrap_shape_creation = lambda settings, shape: utils.create_shape_from_serialization(shape) if getattr(settings, 'use_python_opencascade', False) else shape
36+
37+
38+
# Subclass the settings module to provide an additional
39+
# setting to enable pythonOCC when available
40+
class settings(ifcopenshell_wrapper.settings):
41+
if has_occ:
42+
USE_PYTHON_OPENCASCADE = -1
43+
def set(self, *args):
44+
setting, value = args
45+
if setting == settings.USE_PYTHON_OPENCASCADE:
46+
self.set(settings.USE_BREP_DATA, value)
47+
self.set(settings.DISABLE_TRIANGULATION, value)
48+
self.use_python_opencascade = value
49+
else:
50+
ifcopenshell_wrapper.settings.set(self, *args)
51+
2652

2753
# Hide templating precision to the user by choosing based on Python's
2854
# internal float type. This is probably always going to be a double.
@@ -34,11 +60,15 @@
3460
# Make sure people are able to use python's platform agnostic paths
3561
class iterator(_iterator):
3662
def __init__(self, settings, filename):
63+
self.settings = settings
3764
_iterator.__init__(self, settings, os.path.abspath(filename))
65+
if has_occ:
66+
def get(self):
67+
return wrap_shape_creation(self.settings, _iterator.get(self))
3868

3969

4070
def create_shape(settings, inst):
41-
return ifcopenshell_wrapper.create_shape(settings, inst.wrapped_data)
71+
return wrap_shape_creation(settings, ifcopenshell_wrapper.create_shape(settings, inst.wrapped_data))
4272

4373

4474
def iterate(settings, filename):
@@ -47,3 +77,5 @@ def iterate(settings, filename):
4777
while True:
4878
yield it.get()
4979
if not it.next(): break
80+
81+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import random
2+
from collections import namedtuple
3+
4+
import OCC.gp
5+
import OCC.V3d
6+
import OCC.Quantity
7+
import OCC.BRepTools
8+
import OCC.Display.SimpleGui
9+
10+
tuple = namedtuple('shape', ('data', 'geometry'))
11+
12+
handle, main_loop, add_menu, add_function_to_menu = None, None, None, None
13+
14+
def initialize_display():
15+
global handle, main_loop, add_menu, add_function_to_menu
16+
handle, main_loop, add_menu, add_function_to_menu = OCC.Display.SimpleGui.init_display()
17+
18+
def setup():
19+
viewer_handle = handle.GetViewer()
20+
viewer = viewer_handle.GetObject()
21+
while True:
22+
viewer.InitActiveLights()
23+
try: active_light = viewer.ActiveLight()
24+
except: break
25+
viewer.DelLight(active_light)
26+
viewer.NextActiveLights()
27+
for dir in [(1,2,-3), (-2,-1,1)]:
28+
light = OCC.V3d.V3d_DirectionalLight(viewer_handle)
29+
light.SetDirection(*dir)
30+
viewer.SetLightOn(light.GetHandle())
31+
setup()
32+
return handle
33+
34+
35+
def display_shape(shape, clr=None):
36+
if not clr:
37+
r = lambda: random.random() * 0.3 + 0.7
38+
clr = OCC.Quantity.Quantity_Color(r(), r(), r(), OCC.Quantity.Quantity_TOC_RGB)
39+
return handle.DisplayShape(shape, color=clr, update=True)
40+
41+
42+
def set_shape_transparency(ais, t):
43+
handle.Context.SetTransparency(ais, t)
44+
45+
46+
def get_bounding_box_center(bbox):
47+
bbmin = [0.]*3; bbmax = [0.]*3
48+
bbmin[0], bbmin[1], bbmin[2], bbmax[0], bbmax[1], bbmax[2] = bbox.Get()
49+
return OCC.gp.gp_Pnt(*map(lambda xy: (xy[0]+xy[1])/2., zip(bbmin, bbmax)))
50+
51+
52+
def create_shape_from_serialization(brep_object):
53+
brep_data, occ_shape = None, None
54+
55+
try: brep_data = brep_object.geometry.brep_data
56+
except: pass
57+
if not brep_data: return tuple(brep_object, None)
58+
59+
try:
60+
ss = OCC.BRepTools.BRepTools_ShapeSet()
61+
ss.ReadFromString(brep_data)
62+
occ_shape = ss.Shape(ss.NbShapes())
63+
except: pass
64+
65+
return tuple(brep_object, occ_shape)
66+

0 commit comments

Comments
 (0)