def fillet(event=None): display.EraseAll() box = BRepPrimAPI_MakeBox(gp_Pnt(-400, 0, 0), 200, 230, 180).Shape() fillet = BRepFilletAPI_MakeFillet(box) # Add fillet on each edge for e in TopologyExplorer(box).edges(): fillet.Add(20, e) blended_box = fillet.Shape() p_1 = gp_Pnt(250, 150, 75) s_1 = BRepPrimAPI_MakeBox(300, 200, 200).Shape() s_2 = BRepPrimAPI_MakeBox(p_1, 120, 180, 70).Shape() fused_shape = BRepAlgoAPI_Fuse(s_1, s_2).Shape() fill = BRepFilletAPI_MakeFillet(fused_shape) for e in TopologyExplorer(fused_shape).edges(): fill.Add(e) for i in range(1, fill.NbContours() + 1): length = fill.Length(i) radius = 0.15 * length fill.SetRadius(radius, i, 1) blended_fused_solids = fill.Shape() display.DisplayShape(blended_box) display.DisplayShape(blended_fused_solids, update=True)
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 TopologyExplorer(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 TopologyExplorer(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 big_box(pt1, pt2): gp1 = gp_Pnt(pt1[0], pt1[1], pt1[2]) gp2 = gp_Pnt(pt2[0], pt2[1], pt2[2]) gpm = gp_Pnt(0.5 * (pt1[0] + pt2[0]), 0.5 * (pt1[1] + pt2[1]), 0.5 * (pt1[2] + pt2[2])) box = BRepPrimAPI_MakeBox(gp1, gp2) return box.Shape()
def create_model(self): box1 = BRepPrimAPI_MakeBox(getGpPt(self.a1), self.L, self.L, self.H).Shape() box2 = BRepPrimAPI_MakeBox(getGpPt(self.a2), self.L - self.T, self.L - self.T, self.H).Shape() prism = BRepAlgoAPI_Cut(box1, box2).Shape() return prism
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 extrusion(event=None): # Make a box Box = BRepPrimAPI_MakeBox(400., 250., 300.) S = Box.Shape() # Choose the first Face of the box F = next(TopologyExplorer(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.FitAll()
def fuse(event=None): 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)) fusedshp = BRepAlgoAPI_Fuse(box1, box2).Shape() rnd = JupyterRenderer() rnd.DisplayShape(fusedshp, render_edges=True) rnd.Display()
def test_NCollection_List(self) -> None: """Check that python proxy for NCollection_List is ok""" l = TopTools_ListOfShape() shp1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape() shp2 = BRepPrimAPI_MakeBox(20, 30, 40).Shape() l.Append(shp1) l.Append(shp2) self.assertEqual(l.Size(), 2) self.assertEqual(len(l), 2) l.RemoveFirst() self.assertEqual(len(l), 1)
def test_neq_operator(self): # test Standard s = Standard_Transient() s2 = Standard_Transient() self.assertTrue(s != s2) self.assertFalse(s != 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 _desk(self, scaleA, scaleB, deskDX, deskDY, deskDZ): style = self._deskStyle() scale = scaleA / scaleB xBox, yBox, zBox = 1500 * scale, 1000 * scale, 40 * scale desk = BRepPrimAPI_MakeBox( gp_Pnt(-xBox / 2 + deskDX, -yBox / 2 + deskDY, -zBox + deskDZ), xBox, yBox, zBox) self._drawShape(desk.Solid(), style) scalePoint = gp_Pnt(-xBox / 2 + deskDX, -yBox / 2 + deskDY, zBox / 3 + deskDZ) self._drawLabel(scalePoint, 'A0 M' + str(scaleB) + ':' + str(scaleA), style)
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 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, True) display.DisplayColoredShape(offsetB.Shape(), 'GREEN') display.FitAll()
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 getDaoCaseSurface(self): r = self.aBaseRadius r2 = r * 2 h = self.aCaseHeight h2 = h / 2 offset = self.aOffset gap = self.aCaseGap rTop = r + offset + gap rSphere = gp_Vec(0, rTop, h2).Magnitude() sphere = BRepPrimAPI_MakeSphere(rSphere).Shape() limit = BRepPrimAPI_MakeBox(gp_Pnt(-r2, -r2, -h2), gp_Pnt(r2, r2, h2)).Shape() step01Surface = BRepAlgoAPI_Common(sphere, limit).Shape() step02Surface = getShapeTranslate(step01Surface, 0, 0, -h2) cutIngSurface = self.getCached('getDaoIngSurface', offset - gap) cutYangSurface = self.getCached('getDaoYangSurface', offset - gap) step03Surface = BRepAlgoAPI_Cut(step02Surface, cutIngSurface).Shape() step04Surface = BRepAlgoAPI_Cut(step03Surface, cutYangSurface).Shape() step05Surface = getShapeTranslate(step04Surface, 0, 0, -h2) return step05Surface
def variable_filleting(event=None): a_pnt = gp_Pnt(350, 0, 0) box_2 = BRepPrimAPI_MakeBox(a_pnt, 200, 200, 200).Shape() a_fillet = BRepFilletAPI_MakeFillet(box_2) tab_point = TColgp_Array1OfPnt2d(1, 6) p_1 = gp_Pnt2d(0., 8.) p_2 = gp_Pnt2d(0.2, 16.) p_3 = gp_Pnt2d(0.4, 25.) p_4 = gp_Pnt2d(0.6, 55.) p_5 = gp_Pnt2d(0.8, 28.) p_6 = gp_Pnt2d(1., 20.) tab_point.SetValue(1, p_1) tab_point.SetValue(2, p_2) tab_point.SetValue(3, p_3) tab_point.SetValue(4, p_4) tab_point.SetValue(5, p_5) tab_point.SetValue(6, p_6) expl3 = list(TopologyExplorer(box_2).edges()) a_fillet.Add(tab_point, expl3[9]) a_fillet.Build() if a_fillet.IsDone(): law_evolved_box = a_fillet.Shape() display.DisplayShape(law_evolved_box) else: print("aFillet not done.") display.FitAll()
def test_write_step_file(self) -> None: """Exports a colored box into a STEP file""" ### initialisation doc = TDocStd_Document(TCollection_ExtendedString("pythonocc-doc")) self.assertTrue(doc is not None) # Get root assembly shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main()) colors = XCAFDoc_DocumentTool.ColorTool(doc.Main()) ### create the shape to export test_shape = BRepPrimAPI_MakeBox(100.0, 100.0, 100.0).Shape() ### add shape shp_label = shape_tool.AddShape(test_shape) ### set a color for this shape r = 1.0 g = b = 0.5 red_color = Quantity_Color(r, g, b, Quantity_TypeOfColor.Quantity_TOC_RGB) colors.SetColor(shp_label, red_color, XCAFDoc_ColorGen) # write file WS = XSControl_WorkSession() writer = STEPCAFControl_Writer(WS, False) writer.Transfer(doc, STEPControl_AsIs) status = writer.Write("./test_io/test_ocaf_generated.stp") self.assertTrue(status) self.assertTrue(os.path.isfile("./test_io/test_ocaf_generated.stp"))
def test_repr_overload(self): """ Test if repr string is properly returned """ p = gp_Pnt(1, 2, 3) self.assertEqual(str(p), "class<'gp_Pnt'>") shp = BRepPrimAPI_MakeBox(10, 20, 30).Shape() self.assertTrue("class<'TopoDS_Solid'; id:" in str(shp))
def brep_feat_local_revolution(event=None): S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = list(TopologyExplorer(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 New_shp(Xmax, Xmin, Ymax, Ymin, Zmax, Zmin, X, Y, Z): Index = random.randint(1, 4) position = cal_position(Xmax, Xmin, Ymax, Ymin, Zmax, Zmin) A = (X + Y + Z) / 5 if Index == 1: X1 = random.uniform(0.5 * A, A) Y1 = random.uniform(0.5 * A, A) Z1 = random.uniform(0.5 * A, A) nshp = BRepPrimAPI_MakeBox( gp_Pnt(-0.5 * X1 + position[0], -0.5 * Y1 + position[1], -0.5 * Z1 + position[2]), X1, Y1, Z1).Shape() if Index == 2: R = random.uniform(0.25 * A, 0.5 * A) nshp = BRepPrimAPI_MakeSphere( gp_Pnt(position[0], position[1], position[2]), R).Shape() if Index == 3: R2 = random.uniform(0.25 * A, 0.5 * A) H = random.uniform(0.5 * A, A) origin = gp_Ax2( gp_Pnt(position[0], position[1], -0.5 * H + position[2]), gp_Dir(0.0, 0.0, 1.0)) nshp = BRepPrimAPI_MakeCone(origin, R2, 0, H).Shape() if Index == 4: R = random.uniform(0.25 * A, 0.5 * A) H = random.uniform(0.5 * A, A) cylinder_origin = gp_Ax2( gp_Pnt(position[0], position[1], -0.5 * H + position[2]), gp_Dir(0.0, 0.0, 1.0)) nshp = BRepPrimAPI_MakeCylinder(cylinder_origin, R, H).Shape() return nshp
def test_topabs_orientation_byref(self): # see Issue https://github.com/tpaviot/pythonocc-core/issues/1038 box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() box_face = list(TopologyExplorer(box).faces())[0] facetopo = BRepClass_FaceExplorer(box_face) facetopo.InitWires() # get ready to explore wires/loops of this face edge_1 = BRepClass_Edge() edge_2 = BRepClass_Edge() edge_3 = BRepClass_Edge() edge_4 = BRepClass_Edge() facetopo.InitEdges() topabs_ori_1 = facetopo.CurrentEdge(edge_1) facetopo.NextEdge() topabs_ori_2 = facetopo.CurrentEdge(edge_2) facetopo.NextEdge() topabs_ori_3 = facetopo.CurrentEdge(edge_3) facetopo.NextEdge() topabs_ori_4 = facetopo.CurrentEdge(edge_4) self.assertEqual(topabs_ori_1, TopAbs_Orientation.TopAbs_REVERSED) self.assertEqual(topabs_ori_2, TopAbs_Orientation.TopAbs_REVERSED) self.assertEqual(topabs_ori_3, TopAbs_Orientation.TopAbs_FORWARD) self.assertEqual(topabs_ori_4, TopAbs_Orientation.TopAbs_FORWARD)
def test_tesselate_box(self): """ 1st test : tesselation of a box """ a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() tess = Tesselator(a_box) tess.Compute() self.assertEqual(tess.ObjGetTriangleCount(), 12) self.assertEqual(tess.ObjGetNormalCount(), 24)
def test_step_write_shape_to_file_stp(self): shp = BRepPrimAPI_MakeBox(1., 1., 1.).Shape() path = 'tests/test_datasets/x.stp' step_handler = StepHandler() step_handler.write_shape_to_file(shp, path) self.assertTrue(os.path.exists(path)) self.addCleanup(os.remove, path)
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 emmenthaler(event=None): init_time = time.time() scope = 200. nb_iter = 40 box = BRepPrimAPI_MakeBox(scope, scope, scope).Shape() def do_cyl(): axe = gp_Ax2() axe.SetLocation(gp_Pnt((random_vec() * scope).XYZ())) axe.SetDirection(gp_Dir(random_vec())) cyl = BRepPrimAPI_MakeCylinder(axe, random.uniform(8, 36), 5000.) return cyl.Shape() # perform a recursive fusszy cut # initialize the loop with the box shape shp = box for i in range(nb_iter): cyl = do_cyl() tA = time.time() shp = fuzzy_cut(shp, cyl, 1e-4) print('boolean cylinder:', i, 'took', time.time() - tA) total_time = time.time() - init_time print("Total time : %fs" % total_time) display.DisplayShape(shp, update=True) start_display()
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_repr_overload(self) -> None: """Test if repr string is properly returned""" p = gp_Pnt(1, 2, 3) self.assertEqual(repr(p), "<class 'gp_Pnt'>") empty_shape = TopoDS_Shape() self.assertEqual(repr(empty_shape), "<class 'TopoDS_Shape': Null>") shp = BRepPrimAPI_MakeBox(10, 20, 30).Shape() self.assertEqual(repr(shp), "<class 'TopoDS_Solid'>")
def glue_solids_edges(event=None): display.EraseAll() display.Context.RemoveAll(True) # 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() tag_edge(edge_from, "edge_{0}_from".format(n)) tag_edge(edge_to, "edge_{0}_to".format(n)) glue2.Bind(edge_from, edge_to) common_edges.Next() n += 1 tag_faces(get_faces(shape), "BLACK", "") display.FitAll()
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.) tess.Compute() self.assert_(tess.ObjGetTriangleCount() == 12) self.assert_(tess.ObjGetNormalCount() == 24)
def test_export_to_x3d_TriangleSet(self): """ 3rd test : export a sphere to an X3D TriangleSet triangle mesh """ a_sphere = BRepPrimAPI_MakeBox(10., 10., 10.).Shape() tess = Tesselator(a_sphere) tess.Compute() ifs = tess.ExportShapeToX3DIndexedFaceSet() self.assertTrue(ifs.startswith("<TriangleSet")) self.assertTrue("0 10 0" in ifs) # a vertex self.assertTrue("0 0 1" in ifs) # a normal
def test_export_to_x3d_TriangleSet(self): """3rd test : export a sphere to an X3D TriangleSet triangle mesh""" a_sphere = BRepPrimAPI_MakeBox(10.0, 10.0, 10.0).Shape() sphere_tess = ShapeTesselator(a_sphere) sphere_tess.Compute() sphere_triangle_set_string = sphere_tess.ExportShapeToX3DTriangleSet() self.assertTrue(sphere_triangle_set_string.startswith("<TriangleSet")) self.assertTrue("0 10 0" in sphere_triangle_set_string) # a vertex self.assertTrue("0 0 1" in sphere_triangle_set_string) # a normal