def fillet(event=None): Box = BRepPrimAPI_MakeBox(gp_Pnt(-400, 0, 0), 200, 230, 180).Shape() fillet = BRepFilletAPI_MakeFillet(Box) # Add fillet on each edge for e in Topo(Box).edges(): fillet.Add(20, e) blendedBox = fillet.Shape() P1 = gp_Pnt(250, 150, 75) S1 = BRepPrimAPI_MakeBox(300, 200, 200).Shape() S2 = BRepPrimAPI_MakeBox(P1, 120, 180, 70).Shape() Fuse = BRepAlgoAPI_Fuse(S1, S2) FusedShape = Fuse.Shape() fill = BRepFilletAPI_MakeFillet(FusedShape) for e in Topo(FusedShape).edges(): fill.Add(e) for i in range(1, fill.NbContours() + 1): length = fill.Length(i) Rad = 0.15 * length fill.SetRadius(Rad, i, 1) blendedFusedSolids = fill.Shape() display.EraseAll() display.DisplayShape(blendedBox) display.DisplayShape(blendedFusedSolids) display.FitAll()
def extrusion(event=None): # Make a box Box = BRepPrimAPI_MakeBox(400., 250., 300.) S = Box.Shape() # Choose the first Face of the box F = next(Topo(S).faces()) surf = BRep_Tool_Surface(F) # Make a plane from this face Pl = Handle_Geom_Plane_DownCast(surf) Pln = Pl.GetObject() # Get the normal of this plane. This will be the direction of extrusion. D = Pln.Axis().Direction() # Inverse normal #D.Reverse() # Create the 2D planar sketch MW = BRepBuilderAPI_MakeWire() p1 = gp_Pnt2d(200., -100.) p2 = gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge1 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge1.Edge()) p1 = p2 p2 = gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge2 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge2.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge3 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge3.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge4 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge4.Edge()) # Build Face from Wire. NB: a face is required to generate a solid. MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) MKP = BRepFeat_MakePrism(S, FP, F, D, False, True) MKP.Perform(200.) # TODO MKP completes, seeing a split operation but no extrusion assert MKP.IsDone() res1 = MKP.Shape() display.EraseAll() display.DisplayColoredShape(res1, 'BLUE') display.DisplayColoredShape(FP, 'YELLOW') display.FitAll()
def fuse(event=None): display.EraseAll() box1 = BRepPrimAPI_MakeBox(2, 1, 1).Shape() box2 = BRepPrimAPI_MakeBox(2, 1, 1).Shape() box1 = translate_topods_from_vector(box1, gp_Vec(.5, .5, 0)) fuse_shp = BRepAlgoAPI_Fuse(box1, box2).Shape() display.DisplayShape(fuse_shp) display.FitAll()
def glue_solids_edges(event=None): display.EraseAll() display.Context.RemoveAll() # With common edges S3 = BRepPrimAPI_MakeBox(500., 400., 300.).Shape() S4 = BRepPrimAPI_MakeBox(gp_Pnt(0., 0., 300.), gp_Pnt(200., 200., 500.)).Shape() faces_S3 = get_faces(S3) faces_S4 = get_faces(S4) # tagging allows to visually find the right faces to glue tag_faces(faces_S3, "BLUE", "s3") tag_faces(faces_S4, "GREEN", "s4") F3, F4 = faces_S3[5], faces_S4[4] glue2 = BRepFeat_Gluer(S4, S3) glue2.Bind(F4, F3) glue2.Build() shape = glue2.Shape() # move the glued shape, such to be able to inspect input and output # of glueing operation trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(750, 0, 0)) shape.Move(TopLoc_Location(trsf)) common_edges = LocOpe_FindEdges(F4, F3) common_edges.InitIterator() n = 0 while common_edges.More(): edge_from = common_edges.EdgeFrom() edge_to = common_edges.EdgeTo() center_pt_edge_to = center_boundingbox(edge_to) center_pt_edge_from = center_boundingbox(edge_from) red = (1, 0, 0) display.DisplayMessage(center_pt_edge_from, "edge_{0}_from".format(n), message_color=red) display.DisplayMessage(center_pt_edge_to, "edge_{0}_to".format(n), message_color=red) glue2.Bind(edge_from, edge_to) common_edges.Next() n += 1 tag_faces(get_faces(shape), "BLACK", "") display.FitAll()
def testTopoDS_ShapeOperators(self): shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shape_3 = BRepPrimAPI_MakeSphere(20).Shape() self.assertFalse(shape_1 == shape_2) self.assertFalse(shape_2 == shape_3) self.assertFalse(shape_3 == shape_1) self.assertFalse(shape_2 == "some_string") self.assertTrue(shape_1 != shape_2) self.assertTrue(shape_2 != shape_3) self.assertTrue(shape_3 != shape_1) self.assertTrue(shape_2 != "some_string")
def make_reference(self, **parameters): l = parameters['length'] w = parameters['width'] h = parameters['height'] box_shape = BRepPrimAPI_MakeBox(l, w, h) transform = gp.gp_Trsf() transform.SetTranslation(gp.gp_Vec(-l / 2, -w / 2, -h / 2)) shape_transform = BRepTransform(box_shape.Shape(), transform, False) shape_transform.Build() shape = shape_transform.Shape() return shape
def test_neq_operator(self): # test Standard h1 = Handle_Standard_Transient() s = Standard_Transient() h2 = s.GetHandle() self.assertFalse(h1 != h1) self.assertTrue(h1 != h2) self.assertTrue(h1 != 10) self.assertFalse(h2 != s) shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shape_2 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() self.assertTrue(shape_1 != shape_2) self.assertFalse(shape_1 != shape_1) self.assertTrue(shape_1 != "some_string")
def test_X3DExporter(): from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox box_shp = BRepPrimAPI_MakeBox(10, 20, 30).Shape() # loop over faces x3d_exporter = X3DExporter(box_shp) x3d_exporter.compute() x3d_exporter.write_to_file("popo.x3d")
def test_step_write_shape_to_file_stp(self): shp = BRepPrimAPI_MakeBox(1., 1., 1.).Shape() path = 'tests/test_datasets/x.stp' step_handler = sh.StepHandler() step_handler.write_shape_to_file(shp, path) self.assertTrue(os.path.exists(path)) self.addCleanup(os.remove, path)
def get_front_surface(stl_file, p1, p2): show_log("get_front_surface", "001") stl_reader = StlAPI_Reader() stl_shape = TopoDS_Shape() show_log("get_front_surface", "005") # Read takes 5 minutes stl_reader.Read(stl_shape, stl_file) #pickle.dump(stl_shape, open( "get_front_surface 001.tmp", "wb" ) ) # stl_shape = pickle.load( open( "./get_front_surface 001.tmp", "rb" ) ) # t = Topo(stl_shape) # print("number of faces: %i" % (t.number_of_faces())) #OCC.RWStl.rwstl().ReadFile(stl_file) #time.sleep(10) #cos theta should be less than 0.5, theta is the angle between 1,0,0 and the face normal show_log("get_front_surface", "010") box = BRepPrimAPI_MakeBox(p1, p2).Shape() show_log("get_front_surface", "012") # BRepAlgoAPI_Common takes 3 minutes CommonSurface = BRepAlgoAPI_Common(box, stl_shape).Shape() ais_stl_shape = display.DisplayShape(stl_shape) # ais_box = display.DisplayShape(box) # ais_common = display.DisplayShape(CommonSurface) # display.Context.SetTransparency(ais_box, 0.8) display.Context.SetTransparency(ais_stl_shape, 0.8) # orig = OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex(gp_Pnt(0,0,0)) # display.DisplayShape(orig.Shape()) return CommonSurface
def test_export_to_x3d_IndexedFaceSet(self): """ 3rd test : export a sphere to an X3D IndexedFaceSet triangle mesh """ a_sphere = BRepPrimAPI_MakeBox(10., 10., 10.).Shape() ifs = Tesselator(a_sphere).ExportShapeToX3DIndexedFaceSet() self.assert_(ifs.startswith("<IndexedFaceSet")) self.assert_("0 10 0" in ifs) # a vertex self.assert_("0 0 1" in ifs) # a normal
def test_tesselate_box(self): """ 1st test : tesselation of a box """ a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() tess = Tesselator(a_box, atNormal, 1.0, 1, 0.01, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.) self.assert_(tess.ObjGetTriangleCount() == 12) self.assert_(tess.ObjGetNormalCount() == 24)
def create_AirconicsShape(): """Populates a basic airconics shape with a unit cube striding by 1 in x y and z from the origin, and a unit sphere centered at the origin""" cube = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 1, 1, 1).Shape() sphere = BRepPrimAPI_MakeSphere(gp_Pnt(0, 0, 0), 1).Shape() shape = AirconicsShape(components={'cube': cube, 'sphere': sphere}) return shape
def test_repr_overload(self): """ Test if repr string is properly returned """ p = gp_Pnt(1, 2, 3) assert str(p) == "class<'gp_Pnt'>" shp = BRepPrimAPI_MakeBox(10, 20, 30).Shape() assert "class<'TopoDS_Shape'; Type:Solid; id:" in str(shp)
def test_iges_write_shape_to_file_igs(self): ihp = BRepPrimAPI_MakeBox(1., 1., 1.).Shape() path = 'tests/test_datasets/x.igs' iges_handler = ih.IgesHandler() iges_handler.write_shape_to_file(ihp, path) self.assertTrue(os.path.exists(path)) self.addCleanup(os.remove, path)
def test_three_jsshaders(self): '''Test: point from curve''' torus_shp = BRepPrimAPI_MakeBox(20., 10., 30.).Shape() vertex_shader = """ attribute vec3 center; varying vec3 vCenter; void main() { vCenter = center; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } """ fragment_shader = """ #extension GL_OES_standard_derivatives : enable varying vec3 vCenter; float edgeFactorTri() { vec3 d = fwidth( vCenter.xyz ); vec3 a3 = smoothstep( vec3( 0.0 ), d * 1.5, vCenter.xyz ); return min( min( a3.x, a3.y ), a3.z ); } void main() { gl_FragColor.rgb = mix( vec3( 1.0 ), vec3( 0.2 ), edgeFactorTri() ); gl_FragColor.a = 1.0; } """ my_renderer = threejs_renderer.ThreejsRenderer("#123345", vertex_shader, fragment_shader, path="./test_io") js_file, html_file = my_renderer.create_files(torus_shp) assert os.path.isfile(js_file) assert os.path.isfile(html_file)
def test_write_step_file(self): ''' Exports a colored box into a STEP file ''' ### initialisation h_doc = Handle_TDocStd_Document() assert (h_doc.IsNull()) # Create the application app = XCAFApp_Application.GetApplication().GetObject() app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc) # Get root assembly doc = h_doc.GetObject() h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()) l_Colors = XCAFDoc_DocumentTool_ColorTool(doc.Main()) shape_tool = h_shape_tool.GetObject() colors = l_Colors.GetObject() ### create the shape to export test_shape = BRepPrimAPI_MakeBox(100., 100., 100.).Shape() ### add shape shp_label = shape_tool.AddShape(test_shape) ### set a color for this shape r = 1 g = b = 0.5 red_color = Quantity_Color(r, g, b, 0) colors.SetColor(shp_label, red_color, XCAFDoc_ColorGen) # write file WS = XSControl_WorkSession() writer = STEPCAFControl_Writer(WS.GetHandle(), False) writer.Transfer(h_doc, STEPControl_AsIs) status = writer.Write("./test_io/test_ocaf_generated.stp") assert status assert os.path.isfile("./test_io/test_ocaf_generated.stp")
def brep_feat_local_revolution(event=None): S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = list(Topo(S).faces()) F1 = faces[2] surf = BRep_Tool_Surface(F1) D = gp_OX() MW1 = BRepBuilderAPI_MakeWire() p1 = gp_Pnt2d(100., 100.) p2 = gp_Pnt2d(200., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) p1 = gp_Pnt2d(200., 100.) p2 = gp_Pnt2d(150., 200.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) p1 = gp_Pnt2d(150., 200.) p2 = gp_Pnt2d(100., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW1.Add(BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)).Edge()) MKF1 = BRepBuilderAPI_MakeFace() MKF1.Init(surf, False, 1e-6) MKF1.Add(MW1.Wire()) FP = MKF1.Face() breplib_BuildCurves3d(FP) MKrev = BRepFeat_MakeRevol(S, FP, F1, D, 1, True) F2 = faces[4] MKrev.Perform(F2) display.EraseAll() display.DisplayShape(MKrev.Shape()) display.FitAll()
def offset_cube(event=None): S2 = BRepPrimAPI_MakeBox(gp_Pnt(300, 0, 0), 220, 140, 180).Shape() offsetB = BRepOffsetAPI_MakeOffsetShape(S2, -20, 0.01, BRepOffset_Skin, False, False, GeomAbs_Arc) offB = display.DisplayColoredShape(S2, 'BLUE') display.Context.SetTransparency(offB, 0.3) display.DisplayColoredShape(offsetB.Shape(), 'GREEN') display.FitAll()
def BBox_FromExtents(xmin, ymin, zmin, xmax, ymax, zmax): """Generates the Wire Edges defining the Bounding Box defined in the input arguments: Can be used to display the bounding box""" s = BRepPrimAPI_MakeBox(gp_Pnt(xmin, ymin, zmin), gp_Pnt(xmax, ymax, zmax)).Shape() ais_bbox = AIS_Shape(s) ais_bbox.SetDisplayMode(AIS_WireFrame) return ais_bbox.GetHandle()
def test_build_bounding_box_1(self): origin = np.array([0., 0., 0.]) tops = np.array([1., 1., 1.]) cube = BRepPrimAPI_MakeBox(*tops).Shape() params = ffdp.FFDParameters() params.build_bounding_box(cube) np.testing.assert_array_almost_equal(params.lenght_box, tops, decimal=5)
def test_calculate_bb_dimensions(self): min_vals = np.zeros(3) max_vals = np.ones(3) cube = BRepPrimAPI_MakeBox(1, 1, 1).Shape() params = ffdp.FFDParameters() xyz_min, xyz_max = params._calculate_bb_dimension(cube) np.testing.assert_almost_equal(xyz_min, min_vals, decimal=5) np.testing.assert_almost_equal(xyz_max, max_vals, decimal=5)
def make_box(*args): p1, p2 = None, None if isinstance(args, (list, tuple)): if len(args) == 2: p1 = make_pnt(args[0]) p2 = make_pnt(args[1]) elif len(args) == 6: p1 = gp_Pnt(*args[0:3]) p2 = gp_Pnt(*args[3:]) if p1 is None: return box = BRepPrimAPI_MakeBox(p1, p2) box.Build() with assert_isdone(box, 'failed to built a cube...'): return box.Shape()
def test_x3dom_random_boxes(self): """ Test: threejs 10 random boxes """ my_x3dom_renderer = x3dom_renderer.X3DomRenderer() for i in range(10): box_shp = BRepPrimAPI_MakeBox(random.random()*20, random.random()*20, random.random()*20).Shape() rnd_color = (random.random(), random.random(), random.random()) rnd_export_edges = bool(random.random() > 0.5) my_x3dom_renderer.DisplayShape(box_shp, export_edges=True, color=rnd_color, transparency=random.random())
def test_extract_shape_vertices(self): clear() occ_shape = BRepPrimAPI_MakeBox(Point(0, 0, 0), Point(1, 1, 1)).Shape() display.DisplayShape(occ_shape) temp_points = extract_shape_vertices(occ_shape) self.assertTrue(len(temp_points) > 0) surf = Surface() surf.shape = occ_shape surf.points = temp_points surf.approximate()
def compute_minimal_distance_between_cubes(): """ compute the minimal distance between 2 cubes the line between the 2 points is rendered in cyan """ b1 = BRepPrimAPI_MakeBox(gp_Pnt(100, 0, 0), 10., 10., 10.).Shape() b2 = BRepPrimAPI_MakeBox(gp_Pnt(45, 45, 45), 10., 10., 10.).Shape() display.DisplayShape([b1, b2]) dss = BRepExtrema_DistShapeShape() dss.LoadS1(b1) dss.LoadS2(b2) dss.Perform() assert dss.IsDone() edg = make_edge(dss.PointOnShape1(1), dss.PointOnShape2(1)) display.DisplayColoredShape([edg], color="CYAN")
def testDumpToString(self): ''' Checks if the pickle python module works for TopoDS_Shapes ''' print 'Test: pickling of TopoDS_Shapes' from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox import pickle # Create the shape box_shape = BRepPrimAPI_MakeBox(100, 200, 300).Shape() pickle.dumps(box_shape)
def simple_mesh(): # # Create the shape # shape = BRepPrimAPI_MakeBox(200, 200, 200).Shape() theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape() theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape() shape = BRepAlgoAPI_Fuse(theSphere, theBox).Shape() # # Mesh the shape # BRepMesh_IncrementalMesh(shape, 0.8) builder = BRep_Builder() comp = TopoDS_Compound() builder.MakeCompound(comp) bt = BRep_Tool() ex = TopExp_Explorer(shape, TopAbs_FACE) while ex.More(): face = topods_Face(ex.Current()) location = TopLoc_Location() facing = (bt.Triangulation(face, location)).GetObject() tab = facing.Nodes() tri = facing.Triangles() for i in range(1, facing.NbTriangles()+1): trian = tri.Value(i) index1, index2, index3 = trian.Get() for j in range(1, 4): if j == 1: m = index1 n = index2 elif j == 2: n = index3 elif j == 3: m = index2 me = BRepBuilderAPI_MakeEdge(tab.Value(m), tab.Value(n)) if me.IsDone(): builder.Add(comp, me.Edge()) ex.Next() display.EraseAll() display.DisplayShape(shape) display.DisplayShape(comp, update=True)
def test_export_to_3js_JSON(self): a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() tess = Tesselator(a_box) tess.Compute() # get the JSON string JSON_str = tess.ExportShapeToThreejsJSONString("myshapeid") # check the python JSON parser can decode the string # i.e. the JSON string is well formed dico = json.loads(JSON_str) # after that, check that the number of vertices is ok assert len(dico["data"]["attributes"]["position"]["array"]) == 36 * 3
def test_dump_to_string(self): ''' Checks if the pickle python module works for TopoDS_Shapes ''' # Create the shape box_shape = BRepPrimAPI_MakeBox(100, 200, 300).Shape() shp_dump = pickle.dumps(box_shape) # write to file output = open("box_shape.brep", "wb") output.write(shp_dump) output.close()