|
| 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