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 getSVG(shape, opts=None): """ Export a shape to SVG """ d = {"width": 800, "height": 240, "marginLeft": 200, "marginTop": 20} if opts: d.update(opts) # need to guess the scale and the coordinate center uom = guessUnitOfMeasure(shape) width = float(d["width"]) height = float(d["height"]) marginLeft = float(d["marginLeft"]) marginTop = float(d["marginTop"]) hlr = HLRBRep_Algo() hlr.Add(shape.wrapped) projector = HLRAlgo_Projector(gp_Ax2(gp_Pnt(), DEFAULT_DIR)) hlr.Projector(projector) hlr.Update() hlr.Hide() hlr_shapes = HLRBRep_HLRToShape(hlr) visible = [] visible_sharp_edges = hlr_shapes.VCompound() if visible_sharp_edges: visible.append(visible_sharp_edges) visible_smooth_edges = hlr_shapes.Rg1LineVCompound() if visible_smooth_edges: visible.append(visible_smooth_edges) visible_contour_edges = hlr_shapes.OutLineVCompound() if visible_contour_edges: visible.append(visible_contour_edges) hidden = [] hidden_sharp_edges = hlr_shapes.HCompound() if hidden_sharp_edges: hidden.append(hidden_sharp_edges) hidden_contour_edges = hlr_shapes.OutLineHCompound() if hidden_contour_edges: hidden.append(hidden_contour_edges) # Fix the underlying geometry - otherwise we will get segfaults for el in visible: breplib.BuildCurves3d(el, TOLERANCE) for el in hidden: breplib.BuildCurves3d(el, TOLERANCE) # convert to native CQ objects visible = list(map(Shape, visible)) hidden = list(map(Shape, hidden)) (hiddenPaths, visiblePaths) = getPaths(visible, hidden) # get bounding box -- these are all in 2-d space bb = Compound.makeCompound(hidden + visible).BoundingBox() # width pixels for x, height pixesl for y unitScale = min(width / bb.xlen * 0.75, height / bb.ylen * 0.75) # compute amount to translate-- move the top left into view (xTranslate, yTranslate) = ( (0 - bb.xmin) + marginLeft / unitScale, (0 - bb.ymax) - marginTop / unitScale, ) # compute paths ( again -- had to strip out freecad crap ) hiddenContent = "" for p in hiddenPaths: hiddenContent += PATHTEMPLATE % p visibleContent = "" for p in visiblePaths: visibleContent += PATHTEMPLATE % p svg = SVG_TEMPLATE % ( { "unitScale": str(unitScale), "strokeWidth": str(1.0 / unitScale), "hiddenContent": hiddenContent, "visibleContent": visibleContent, "xTranslate": str(xTranslate), "yTranslate": str(yTranslate), "width": str(width), "height": str(height), "textboxY": str(height - 30), "uom": str(uom), } ) # svg = SVG_TEMPLATE % ( # {"content": projectedContent} # ) return svg
def map(self, tcrv): mk = BRepBuilderAPI_MakeEdge(tcrv.Curve2(), self.Surface()) edge = mk.Edge() breplib.BuildCurves3d(edge) return Shape(edge)
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 bottle = TopoDS_Compound() aBuilder = BRep_Builder() aBuilder.MakeCompound(bottle) aBuilder.Add(bottle, myBody_step3.Shape()) aBuilder.Add(bottle, myThreading)
def _helix(r, h, step=None, pitch=None, angle=0, left=False): radius = r height = h if pitch: pitch = math.sin(pitch) * 2 * math.pi * r else: pitch = step if pitch < precision_Confusion(): raise Exception("Pitch of helix too small") if height < precision_Confusion(): raise Exception("Height of helix too small") cylAx2 = gp_Ax2(gp_Pnt(0.0, 0.0, 0.0), gp_DZ()) if abs(angle) < precision_Confusion(): # Cylindrical helix if radius < precision_Confusion(): raise Exception("Radius of helix too small") surf = Geom_CylindricalSurface(gp_Ax3(cylAx2), radius) isCylinder = True else: # Conical helix if abs(angle) < precision_Confusion(): raise Exception("Angle of helix too small") surf = Geom_ConicalSurface(gp_Ax3(cylAx2), angle, radius) isCylinder = False turns = height / pitch wholeTurns = math.floor(turns) partTurn = turns - wholeTurns aPnt = gp_Pnt2d(0, 0) aDir = gp_Dir2d(2. * math.pi, pitch) coneDir = 1.0 if left: aDir.SetCoord(-2. * math.pi, pitch) coneDir = -1.0 aAx2d = gp_Ax2d(aPnt, aDir) line = Geom2d_Line(aAx2d) beg = line.Value(0) mkWire = BRepBuilderAPI_MakeWire() for i in range(wholeTurns): if isCylinder: end = line.Value( math.sqrt(4.0 * math.pi * math.pi + pitch * pitch) * (i + 1)) else: u = coneDir * (i + 1) * 2.0 * math.pi v = ((i + 1) * pitch) / math.cos(angle) end = gp_Pnt2d(u, v) segm = GCE2d_MakeSegment(beg, end).Value() edgeOnSurf = BRepBuilderAPI_MakeEdge(segm, surf).Edge() mkWire.Add(edgeOnSurf) beg = end if partTurn > precision_Confusion(): if (isCylinder): end = line.Value( math.sqrt(4.0 * math.pi * math.pi + pitch * pitch) * turns) else: u = coneDir * turns * 2.0 * math.pi v = height / math.cos(angle) end = gp_Pnt2d(u, v) segm = GCE2d_MakeSegment(beg, end).Value() edgeOnSurf = BRepBuilderAPI_MakeEdge(segm, surf).Edge() mkWire.Add(edgeOnSurf) shape = mkWire.Wire() breplib.BuildCurves3d(shape) return Shape(shape)