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.GetHandle()) visible = [] visible_sharp_edges = hlr_shapes.VCompound() if not visible_sharp_edges.IsNull(): visible.append(visible_sharp_edges) visible_smooth_edges = hlr_shapes.Rg1LineVCompound() if not visible_smooth_edges.IsNull(): visible.append(visible_smooth_edges) visible_contour_edges = hlr_shapes.OutLineVCompound() if not visible_contour_edges.IsNull(): visible.append(visible_contour_edges) hidden = [] hidden_sharp_edges = hlr_shapes.HCompound() if not hidden_sharp_edges.IsNull(): hidden.append(hidden_sharp_edges) hidden_contour_edges = hlr_shapes.OutLineHCompound() if not hidden_contour_edges.IsNull(): 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
anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc1), Handle_Geom_Surface(aCyl1)) anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment.Value(), Handle_Geom_Surface(aCyl1)) anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(Handle_Geom2d_Curve(anArc2), Handle_Geom_Surface(aCyl2)) anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment.Value(), Handle_Geom_Surface(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)