Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import OCC.BRepPrimAPI as BRepPrimAPI
- import OCC.TopExp as TopExp
- import OCC.TopAbs as TopAbs
- import OCC.TopTools as TopTools
- import OCC.BRep as BRep
- import OCC.TopoDS as TopoDS
- import OCC.Geom as Geom
- import OCC.TopLoc as TopLoc
- import OCC.BRepAlgoAPI as BRepAlgoAPI
- from OCC.gp import gp_Pnt, gp_Dir, gp_Ax2
- def showBox(box):
- # get a blank IndexedMapOfShape to pass to MapShapes
- faces = TopTools.TopTools_IndexedMapOfShape()
- # stores the faces in faces variable. So c++ish...
- TopExp.topexp_MapShapes(box, TopAbs.TopAbs_FACE, faces)
- # loop over all the faces. Must add one to Extent b/c 1-based array
- for i in range(1, faces.Extent()+1):
- # grab the Shape at this index
- face = faces.FindKey(i)
- # 'convert' that TopoDS_Shape to a TopoDS_Face, since we know it's a face
- face = TopoDS.topods_Face(face)
- # Get the underlying geometrical surface corresponding to that face
- surface = BRep.BRep_Tool_Surface(face)
- # DownCast that surface into a plane, so that we can get the location
- # NOTE: the GetObject call here is necessary to convert the handle to an actual
- # object. May come in handy in other places
- plane = Geom.Handle_Geom_Plane.DownCast(surface).GetObject()
- location = plane.Location()
- direction = plane.Axis().Direction()
- # direction is (x, y, z), just like in math class. it's a vector
- direction = '({}, {}, {})'.format(direction.X(), direction.Y(), direction.Z())
- print('for i = {}, x = {}'.format(i, location.X()))
- print('for i = {}, y = {}'.format(i, location.Y()))
- print('for i = {}, z = {}'.format(i, location.Z()))
- print('for i = {}, dir = {}'.format(i, direction))
- print('-'*40)
- my_box = BRepPrimAPI.BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
- print('-'*40)
- print('Orig Box, no mods')
- print('-'*40)
- showBox(my_box)
- # We need to create an axis for our 'cut box' so that it's not located at the origin. This
- # will require a point and a direction vector
- cut_loc = gp_Pnt(7, 5, 30)
- cut_dir = gp_Dir(0, 0, -1)
- cut_ax2 = gp_Ax2(cut_loc, cut_dir)
- # create our 'cut box' using the axis that we created
- cut_box = BRepPrimAPI.BRepPrimAPI_MakeBox(cut_ax2, 5., 5., 5.).Shape()
- print('-'*40)
- print('Orig cut_box, no mods')
- print('-'*40)
- showBox(cut_box)
- new_box = BRepAlgoAPI.BRepAlgoAPI_Cut(my_box, cut_box).Shape()
- print('-'*40)
- print('Cut box')
- print('-'*40)
- showBox(new_box)
Advertisement
Add Comment
Please, Sign In to add comment