Guest User

Python-Occ Example

a guest
May 29th, 2016
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. import OCC.BRepPrimAPI as BRepPrimAPI
  2. import OCC.TopExp as TopExp
  3. import OCC.TopAbs as TopAbs
  4. import OCC.TopTools as TopTools
  5. import OCC.BRep as BRep
  6. import OCC.TopoDS as TopoDS
  7. import OCC.Geom as Geom
  8. import OCC.TopLoc as TopLoc
  9. import OCC.BRepAlgoAPI as BRepAlgoAPI
  10. from OCC.gp import gp_Pnt, gp_Dir, gp_Ax2
  11.  
  12. def showBox(box):
  13.     # get a blank IndexedMapOfShape to pass to MapShapes
  14.     faces = TopTools.TopTools_IndexedMapOfShape()
  15.  
  16.     # stores the faces in faces variable. So c++ish...
  17.     TopExp.topexp_MapShapes(box, TopAbs.TopAbs_FACE, faces)
  18.  
  19.     # loop over all the faces. Must add one to Extent b/c 1-based array
  20.     for i in range(1, faces.Extent()+1):
  21.         # grab the Shape at this index
  22.         face = faces.FindKey(i)
  23.         # 'convert' that TopoDS_Shape to a TopoDS_Face, since we know it's a face
  24.         face = TopoDS.topods_Face(face)
  25.         # Get the underlying geometrical surface corresponding to that face
  26.         surface = BRep.BRep_Tool_Surface(face)
  27.         # DownCast that surface into a plane, so that we can get the location
  28.         # NOTE: the GetObject call here is necessary to convert the handle to an actual
  29.         # object. May come in handy in other places
  30.         plane = Geom.Handle_Geom_Plane.DownCast(surface).GetObject()
  31.         location = plane.Location()
  32.         direction = plane.Axis().Direction()
  33.         # direction is (x, y, z), just like in math class. it's a vector
  34.         direction = '({}, {}, {})'.format(direction.X(), direction.Y(), direction.Z())
  35.  
  36.         print('for i = {}, x = {}'.format(i, location.X()))
  37.         print('for i = {}, y = {}'.format(i, location.Y()))
  38.         print('for i = {}, z = {}'.format(i, location.Z()))
  39.         print('for i = {}, dir = {}'.format(i, direction))
  40.         print('-'*40)
  41.  
  42. my_box = BRepPrimAPI.BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
  43.  
  44. print('-'*40)
  45. print('Orig Box, no mods')
  46. print('-'*40)
  47. showBox(my_box)
  48.  
  49. # We need to create an axis for our 'cut box' so that it's not located at the origin. This
  50. # will require a point and a direction vector
  51. cut_loc = gp_Pnt(7, 5, 30)
  52. cut_dir = gp_Dir(0, 0, -1)
  53. cut_ax2 = gp_Ax2(cut_loc, cut_dir)
  54. # create our 'cut box' using the axis that we created
  55. cut_box = BRepPrimAPI.BRepPrimAPI_MakeBox(cut_ax2, 5., 5., 5.).Shape()
  56. print('-'*40)
  57. print('Orig cut_box, no mods')
  58. print('-'*40)
  59. showBox(cut_box)
  60.  
  61. new_box = BRepAlgoAPI.BRepAlgoAPI_Cut(my_box, cut_box).Shape()
  62. print('-'*40)
  63. print('Cut box')
  64. print('-'*40)
  65. showBox(new_box)
Advertisement
Add Comment
Please, Sign In to add comment