def main(): vertices = [gp_Pnt(p[0], p[1], p[2]) for p in mesh['vertices']] oFaces = [] builder = BRep_Builder() shell = TopoDS_Shell() builder.MakeShell(shell) for face in mesh['faces']: edges = [] face.reverse() for i in range(len(face)): cur = face[i] nxt = face[(i + 1) % len(face)] segment = GC_MakeSegment(vertices[cur], vertices[nxt]) edges.append(BRepBuilderAPI_MakeEdge(segment.Value())) wire = BRepBuilderAPI_MakeWire() for edge in edges: wire.Add(edge.Edge()) oFace = BRepBuilderAPI_MakeFace(wire.Wire()) builder.Add(shell, oFace.Shape()) write_stl_file(shell, "./cube_binding.stl")
def test_memory_handle_getobject(self): """ See https://github.com/tpaviot/pythonocc-generator/pull/24 This commit tries to fix the issue tpaviot/pythonocc-core#292. When we got a handle from ann API function and called GetObject() the lifetime of the object was determined only by the handle. This lead to crashes, when only a reference to the object was stored. The commit registers the handle with its object to prevent premature destruction. This test case ensures everything is ok. Following lines used to crash on pythonocc-0.16.5 """ a = gp_Pnt(0., 0., 0.) b = gp_Pnt(100., 100., 100.) line3 = GC_MakeSegment(a, b).Value().GetObject() assert line3.FirstParameter() == 0. assert GC_MakeSegment(a, b).Value().GetObject().FirstParameter() == 0. assert GC_MakeSegment(a, b).Value().GetObject().GetHandle().GetObject( ).GetHandle().GetObject().FirstParameter() == 0. assert b.IsEqual(line3.EndPoint(), 0.01) assert b.IsEqual( GC_MakeSegment(a, b).Value().GetObject().EndPoint(), 0.01) assert b.IsEqual( GC_MakeSegment(a, b).Value().GetObject().GetHandle().GetObject(). GetHandle().GetObject().EndPoint(), 0.01)
def makeSqProfile(self, size): # points and segments need to be in CW sequence to get W pointing along Z p1 = gp_Pnt(-size, size, 0).Transformed(self.Trsf) p2 = gp_Pnt(size, size, 0).Transformed(self.Trsf) p3 = gp_Pnt(size, -size, 0).Transformed(self.Trsf) p4 = gp_Pnt(-size, -size, 0).Transformed(self.Trsf) seg1 = GC_MakeSegment(p1, p2).Value() seg2 = GC_MakeSegment(p2, p3).Value() seg3 = GC_MakeSegment(p3, p4).Value() seg4 = GC_MakeSegment(p4, p1).Value() e1 = BRepBuilderAPI_MakeEdge(seg1).Edge() e2 = BRepBuilderAPI_MakeEdge(seg2).Edge() e3 = BRepBuilderAPI_MakeEdge(seg3).Edge() e4 = BRepBuilderAPI_MakeEdge(seg4).Edge() aWire_mkr = BRepBuilderAPI_MakeWire(e1, e2, e3, e4) myWireProfile = aWire_mkr.Wire() return myWireProfile # TopoDS_Wire
def test_pipes(self): """Test: pipes""" a1 = [] a1.append(gp_Pnt(-4, 0, 2)) a1.append(gp_Pnt(-5, 1, 0)) a1.append(gp_Pnt(-6, 2, -2)) a1.append(gp_Pnt(-5, 4, -7)) a1.append(gp_Pnt(-3, 5, -12)) xxx = point_list_to_TColgp_Array1OfPnt(a1) SPL1 = GeomAPI_PointsToBSpline(xxx).Curve() aPipe = GeomFill_Pipe(SPL1, True) aPipe.Perform(False, False) aSurface = aPipe.Surface() self.assertIsNotNone(aSurface) E = GC_MakeEllipse(gp_XOY(), 2, 1).Value() aPipe2 = GeomFill_Pipe(SPL1, E, GeomFill_IsConstantNormal) aPipe2.Perform(False, False) aSurface2 = aPipe2.Surface() aSurface2.Translate(gp_Vec(5, 0, 0)) TC1 = GC_MakeSegment(gp_Pnt(1, 1, 1), gp_Pnt(2, 2, 2)).Value() TC2 = GC_MakeSegment(gp_Pnt(1, 1, 0), gp_Pnt(3, 2, 1)).Value() aPipe3 = GeomFill_Pipe(SPL1, TC1, TC2) aPipe3.Perform(False, False) aSurface3 = aPipe3.Surface() aSurface3.Translate(gp_Vec(10, 0, 0)) for _, mode in enumerate([ GeomFill_IsConstantNormal, GeomFill_IsCorrectedFrenet, GeomFill_IsDarboux, GeomFill_IsFrenet, GeomFill_IsGuideAC, GeomFill_IsGuideACWithContact, GeomFill_IsGuidePlan, GeomFill_IsGuidePlanWithContact, ]): E = GC_MakeEllipse(gp_XOY(), 2, 1).Value() aPipe2 = GeomFill_Pipe(SPL1, TC1, TC2, mode) aPipe2.Perform(False, False) aSurface2 = aPipe2.Surface() aSurface2.Translate(gp_Vec(5, 5, 0))
def line(self, pnt1, pnt2): """Create a line between two end points.""" # Two 2d end points x1, y1 = pnt1 x2, y2 = pnt2 p1 = gp_Pnt(x1, y1, 0).Transformed(self.Trsf) p2 = gp_Pnt(x2, y2, 0).Transformed(self.Trsf) seg = GC_MakeSegment(p1, p2).Value() # Geom_TrimmedCurve # Build the edge edge = BRepBuilderAPI_MakeEdge(seg).Edge() # TopoDS_Edge self.edgeList.append(edge)
def rect(self, pnt1, pnt2): """Create a rectangle from two diagonally opposite corners.""" # 2 diagonally opposite corners x1, y1 = pnt1 x2, y2 = pnt2 # 4 corners of rectangle p1 = gp_Pnt(x1, y1, 0).Transformed(self.Trsf) p2 = gp_Pnt(x2, y1, 0).Transformed(self.Trsf) p3 = gp_Pnt(x2, y2, 0).Transformed(self.Trsf) p4 = gp_Pnt(x1, y2, 0).Transformed(self.Trsf) # 4 sides (segments) of rectangle seg1 = GC_MakeSegment(p1, p2).Value() # Geom_TrimmedCurve seg2 = GC_MakeSegment(p2, p3).Value() seg3 = GC_MakeSegment(p3, p4).Value() seg4 = GC_MakeSegment(p4, p1).Value() # Build the edges e1 = BRepBuilderAPI_MakeEdge(seg1).Edge() # TopoDS_Edge e2 = BRepBuilderAPI_MakeEdge(seg2).Edge() e3 = BRepBuilderAPI_MakeEdge(seg3).Edge() e4 = BRepBuilderAPI_MakeEdge(seg4).Edge() edges = (e1, e2, e3, e4) for edge in edges: self.edgeList.append(edge)
def makeLines(): global aEdge1, aEdge2, aEdge3 # Make type 'Geom_TrimmedCurve' from type 'gp_Pnt' aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4) aSegment1 = GC_MakeSegment(aPnt1, aPnt2) aSegment2 = GC_MakeSegment(aPnt4, aPnt5) # Make type 'TopoDS_Edge' from type 'Geom_TrimmedCurve' aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value()) aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value()) aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value()) return (aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge())
def makeLines(event=None): global aEdge1, aEdge2, aEdge3 aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4) aSegment1 = GC_MakeSegment(aPnt1, aPnt2) aSegment2 = GC_MakeSegment(aPnt4, aPnt5) # Display lines aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value()) aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value()) aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value()) display.DisplayColoredShape(aEdge1.Edge(), 'RED') display.DisplayColoredShape(aEdge2.Edge(), 'RED') display.DisplayColoredShape(aEdge3.Edge(), 'RED') display.Repaint() win.statusBar().showMessage('Make lines complete')
def startBottle(startOnly=True): # minus the neck fillet, shelling & threads partName = "Bottle-start" # The points we'll use to create the profile of the bottle's body aPnt1 = gp_Pnt(-width / 2.0, 0, 0) aPnt2 = gp_Pnt(-width / 2.0, -thickness / 4.0, 0) aPnt3 = gp_Pnt(0, -thickness / 2.0, 0) aPnt4 = gp_Pnt(width / 2.0, -thickness / 4.0, 0) aPnt5 = gp_Pnt(width / 2.0, 0, 0) aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4) aSegment1 = GC_MakeSegment(aPnt1, aPnt2) aSegment2 = GC_MakeSegment(aPnt4, aPnt5) # Could also construct the line edges directly using the points # instead of the resulting line. aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value()) aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value()) aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value()) # Create a wire out of the edges aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge()) # Quick way to specify the X axis xAxis = gp_OX() # Set up the mirror aTrsf = gp_Trsf() aTrsf.SetMirror(xAxis) # Apply the mirror transformation aBRespTrsf = BRepBuilderAPI_Transform(aWire.Wire(), aTrsf) # Get the mirrored shape back out of the transformation # and convert back to a wire aMirroredShape = aBRespTrsf.Shape() # A wire instead of a generic shape now aMirroredWire = topods.Wire(aMirroredShape) # Combine the two constituent wires mkWire = BRepBuilderAPI_MakeWire() mkWire.Add(aWire.Wire()) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() # The face that we'll sweep to make the prism myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile) # We want to sweep the face along the Z axis to the height aPrismVec = gp_Vec(0, 0, height) myBody = BRepPrimAPI_MakePrism(myFaceProfile.Face(), aPrismVec) # Add fillets to all edges through the explorer mkFillet = BRepFilletAPI_MakeFillet(myBody.Shape()) anEdgeExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_EDGE) while anEdgeExplorer.More(): anEdge = topods.Edge(anEdgeExplorer.Current()) mkFillet.Add(thickness / 12.0, anEdge) anEdgeExplorer.Next() myBody = mkFillet.Shape() # Create the neck of the bottle neckLocation = gp_Pnt(0, 0, height) neckAxis = gp_DZ() neckAx2 = gp_Ax2(neckLocation, neckAxis) myNeckRadius = thickness / 4.0 myNeckHeight = height / 10.0 mkCylinder = BRepPrimAPI_MakeCylinder(neckAx2, myNeckRadius, myNeckHeight) myBody = BRepAlgoAPI_Fuse(myBody, mkCylinder.Shape()) if startOnly: # quit here uid = win.getNewPartUID(myBody.Shape(), name=partName) win.redraw() return partName = "Bottle-complete" # Our goal is to find the highest Z face and remove it faceToRemove = None zMax = -1 # We have to work our way through all the faces to find the highest Z face aFaceExplorer = TopExp_Explorer(myBody.Shape(), TopAbs_FACE) while aFaceExplorer.More(): aFace = topods.Face(aFaceExplorer.Current()) if face_is_plane(aFace): aPlane = geom_plane_from_face(aFace) # We want the highest Z face, so compare this to the previous faces aPnt = aPlane.Location() aZ = aPnt.Z() if aZ > zMax: zMax = aZ faceToRemove = aFace aFaceExplorer.Next() facesToRemove = TopTools_ListOfShape() facesToRemove.Append(faceToRemove) myBody = BRepOffsetAPI_MakeThickSolid(myBody.Shape(), facesToRemove, -thickness / 50.0, 0.001) # Set up our surfaces for the threading on the neck neckAx2_Ax3 = gp_Ax3(neckLocation, gp_DZ()) aCyl1 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 0.99) aCyl2 = Geom_CylindricalSurface(neckAx2_Ax3, myNeckRadius * 1.05) # Set up the curves for the threads on the bottle's neck aPnt = gp_Pnt2d(2.0 * math.pi, myNeckHeight / 2.0) aDir = gp_Dir2d(2.0 * math.pi, myNeckHeight / 4.0) anAx2d = gp_Ax2d(aPnt, aDir) aMajor = 2.0 * math.pi aMinor = myNeckHeight / 10.0 anEllipse1 = Geom2d_Ellipse(anAx2d, aMajor, aMinor) anEllipse2 = Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4.0) anArc1 = Geom2d_TrimmedCurve(anEllipse1, 0, math.pi) anArc2 = Geom2d_TrimmedCurve(anEllipse2, 0, math.pi) anEllipsePnt1 = anEllipse1.Value(0) anEllipsePnt2 = anEllipse1.Value(math.pi) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2) # Build edges and wires for threading anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1) anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl1) anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2) anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), aCyl2) threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1.Edge(), anEdge2OnSurf1.Edge()) threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2.Edge(), anEdge2OnSurf2.Edge()) # Compute the 3D representations of the edges/wires breplib.BuildCurves3d(threadingWire1.Shape()) breplib.BuildCurves3d(threadingWire2.Shape()) # Create the surfaces of the threading aTool = BRepOffsetAPI_ThruSections(True) aTool.AddWire(threadingWire1.Wire()) aTool.AddWire(threadingWire2.Wire()) aTool.CheckCompatibility(False) myThreading = aTool.Shape() # Build the resulting compound aRes = TopoDS_Compound() aBuilder = BRep_Builder() aBuilder.MakeCompound(aRes) aBuilder.Add(aRes, myBody.Shape()) aBuilder.Add(aRes, myThreading) uid = win.getNewPartUID(aRes, name=partName) win.redraw()
def test_distances(self): '''Test: distances''' array1 = [] array1.append(gp_Pnt(-5, 1, 2)) array1.append(gp_Pnt(-5, 2, 2)) array1.append(gp_Pnt(-5.3, 3, 1)) array1.append(gp_Pnt(-5, 4, 1)) array1.append(gp_Pnt(-5, 5, 2)) SPL1 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array1)).Curve() array2 = [] array2.append(gp_Pnt(4, 1, 2)) array2.append(gp_Pnt(4, 2, 2)) array2.append(gp_Pnt(3.7, 3, 1)) array2.append(gp_Pnt(4, 4, 1)) array2.append(gp_Pnt(4, 5, 2)) SPL2 = GeomAPI_PointsToBSpline( point_list_to_TColgp_Array1OfPnt(array2)).Curve() aGeomFill1 = GeomFill_BSplineCurves(SPL1, SPL2, GeomFill_StretchStyle) aSurf1 = aGeomFill1.Surface() array3 = TColgp_Array2OfPnt(1, 5, 1, 5) array3.SetValue(1, 1, gp_Pnt(-4, -4, 5)) array3.SetValue(1, 2, gp_Pnt(-4, -2, 5)) array3.SetValue(1, 3, gp_Pnt(-4, 0, 4)) array3.SetValue(1, 4, gp_Pnt(-4, 2, 5)) array3.SetValue(1, 5, gp_Pnt(-4, 4, 5)) array3.SetValue(2, 1, gp_Pnt(-2, -4, 4)) array3.SetValue(2, 2, gp_Pnt(-2, -2, 4)) array3.SetValue(2, 3, gp_Pnt(-2, 0, 4)) array3.SetValue(2, 4, gp_Pnt(-2, 2, 4)) array3.SetValue(2, 5, gp_Pnt(-2, 5, 4)) array3.SetValue(3, 1, gp_Pnt(0, -4, 3.5)) array3.SetValue(3, 2, gp_Pnt(0, -2, 3.5)) array3.SetValue(3, 3, gp_Pnt(0, 0, 3.5)) array3.SetValue(3, 4, gp_Pnt(0, 2, 3.5)) array3.SetValue(3, 5, gp_Pnt(0, 5, 3.5)) array3.SetValue(4, 1, gp_Pnt(2, -4, 4)) array3.SetValue(4, 2, gp_Pnt(2, -2, 4)) array3.SetValue(4, 3, gp_Pnt(2, 0, 3.5)) array3.SetValue(4, 4, gp_Pnt(2, 2, 5)) array3.SetValue(4, 5, gp_Pnt(2, 5, 4)) array3.SetValue(5, 1, gp_Pnt(4, -4, 5)) array3.SetValue(5, 2, gp_Pnt(4, -2, 5)) array3.SetValue(5, 3, gp_Pnt(4, 0, 5)) array3.SetValue(5, 4, gp_Pnt(4, 2, 6)) array3.SetValue(5, 5, gp_Pnt(4, 5, 5)) aSurf2 = GeomAPI_PointsToBSplineSurface(array3).Surface() ESS = GeomAPI_ExtremaSurfaceSurface(aSurf1, aSurf2) dist = ESS.LowerDistance() self.assertGreater(dist, 1.25) self.assertLess(dist, 1.26) a, b = gp_Pnt(), gp_Pnt() ESS.NearestPoints(a, b) NbExtrema = ESS.NbExtrema() for k in range(1, NbExtrema + 1): P3, P4 = gp_Pnt(), gp_Pnt() ESS.Points(k, P3, P4) aCurve = GC_MakeSegment(P3, P4).Value() self.assertFalse(aCurve is None)
height = 70 width = 50 thickness = 30 print("creating bottle") # The points we'll use to create the profile of the bottle's body aPnt1 = gp_Pnt(-width / 2.0, 0, 0) aPnt2 = gp_Pnt(-width / 2.0, -thickness / 4.0, 0) aPnt3 = gp_Pnt(0, -thickness / 2.0, 0) aPnt4 = gp_Pnt(width / 2.0, -thickness / 4.0, 0) aPnt5 = gp_Pnt(width / 2.0, 0, 0) aArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4) aSegment1 = GC_MakeSegment(aPnt1, aPnt2) aSegment2 = GC_MakeSegment(aPnt4, aPnt5) # Could also construct the line edges directly using the points instead of the resulting line aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value()) aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle.Value()) aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2.Value()) # Create a wire out of the edges aWire = BRepBuilderAPI_MakeWire(aEdge1.Edge(), aEdge2.Edge(), aEdge3.Edge()) # Quick way to specify the X axis xAxis = gp_OX() # Set up the mirror aTrsf = gp_Trsf()