def clone_tooth(base_shape): clone = gp_Trsf() grouped_shape = base_shape # Find a divisor, between 1 and 8, for the number_of teeth multiplier = 1 max_multiplier = 1 for i in range(0, 8): if num_teeth % multiplier == 0: max_multiplier = i + 1 multiplier = max_multiplier for i in range(1, multiplier): clone.SetRotation(gp_OZ(), -i * tooth_angle) rotated_shape = BRepBuilderAPI_Transform(base_shape, clone, True).Shape() grouped_shape = BRepAlgoAPI_Fuse(grouped_shape, rotated_shape).Shape() # Rotate the basic tooth and fuse together aggregated_shape = grouped_shape for i in range(1, int(num_teeth / multiplier)): clone.SetRotation(gp_OZ(), - i * multiplier * tooth_angle) rotated_shape = BRepBuilderAPI_Transform(grouped_shape, clone, True).Shape() aggregated_shape = BRepAlgoAPI_Fuse(aggregated_shape, rotated_shape).Shape() cylinder = BRepPrimAPI_MakeCylinder(gp_XOY(), top_radius - roller_diameter, thickness) aggregated_shape = BRepAlgoAPI_Fuse(aggregated_shape, cylinder.Shape()).Shape() return aggregated_shape
def mirror_pnt_dir(brep, pnt, direction, copy=False): ''' @param brep: @param line: ''' trns = gp_Trsf() trns.SetMirror(gp_Ax1(pnt, direction)) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) with assert_isdone(brep_trns, 'could not produce mirror'): brep_trns.Build() return brep_trns.Shape()
def mirror_axe2(brep, axe2, copy=False): ''' @param brep: @param line: ''' trns = gp_Trsf() trns.SetMirror(axe2) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) with assert_isdone(brep_trns, 'could not produce mirror'): brep_trns.Build() return brep_trns.Shape()
def rotate(brep, axe, degree, copy=False): ''' @param brep: @param axe: @param degree: ''' from math import radians trns = gp_Trsf() trns.SetRotation(axe, radians(degree)) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) with assert_isdone(brep_trns, 'could not produce rotation'): brep_trns.Build() return ST(brep_trns.Shape())
def scale_uniformal(brep, pnt, factor, copy=False): ''' translate a brep over a vector @param brep: the Topo_DS to translate @param pnt: a gp_Pnt @param triple: scaling factor @param copy: copies to brep if True ''' trns = gp_Trsf() trns.SetScale(pnt, factor) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) brep_trns.Build() return brep_trns.Shape()
def create_shape(self): d = self.declaration if not d.source: return if os.path.exists(os.path.expanduser(d.source)): svg = etree.parse(os.path.expanduser(d.source)).getroot() else: svg = etree.fromstring(d.source) node = OccSvgDoc(element=svg) builder = BRep_Builder() shape = TopoDS_Compound() builder.MakeCompound(shape) shapes = node.create_shape() for s in shapes: builder.Add(shape, s) bbox = self.get_bounding_box(shape) cx, cy = bbox.dx / 2, bbox.dy / 2 # Move to position and align along direction axis t = gp_Trsf() axis = gp_Ax3() axis.SetDirection(d.direction.proxy) t.SetTransformation(axis) pos = d.position-(cx, cy, 0) t.SetTranslationPart(gp_Vec(*pos)) self.shape = BRepBuilderAPI_Transform(shape, t, False).Shape()
def translate_topods_from_vector(brep_or_iterable, vec, copy=False): ''' translate a brep over a vector @param brep: the Topo_DS to translate @param vec: the vector defining the translation @param copy: copies to brep if True ''' st = ShapeToTopology() trns = gp_Trsf() trns.SetTranslation(vec) if issubclass(brep_or_iterable.__class__, TopoDS_Shape): brep_trns = BRepBuilderAPI_Transform(brep_or_iterable, trns, copy) brep_trns.Build() return st(brep_trns.Shape()) else: return [translate_topods_from_vector(brep_or_iterable, vec, copy) for i in brep_or_iterable]
def boolean_cut(base): # Create a cylinder cylinder_radius = 0.25 cylinder_height = 2.0 cylinder_origin = gp_Ax2(gp_Pnt(0.0, 0.0, -cylinder_height / 2.0), gp_Dir(0.0, 0.0, 1.0)) cylinder = BRepPrimAPI_MakeCylinder(cylinder_origin, cylinder_radius, cylinder_height) # Repeatedly move and subtract it from the input shape move = gp_Trsf() boolean_result = base clone_radius = 1.0 for clone in range(8): angle = clone * pi / 4.0 # Move the cylinder move.SetTranslation( gp_Vec(cos(angle) * clone_radius, sin(angle) * clone_radius, 0.0)) moved_cylinder = BRepBuilderAPI_Transform(cylinder.Shape(), move, True).Shape() # Subtract the moved cylinder from the drilled sphere boolean_result = BRepAlgoAPI_Cut(boolean_result, moved_cylinder).Shape() return boolean_result
def mirror_shape(shape, pln): """ Mirror a shape about a plane. :param afem.topology.entities.Shape shape: The shape. :param afem.geometry.entities.Plane pln: The plane. :return: The mirrored shape. :rtype: afem.topology.entities.Shape :raise RuntimeError: If the transformation fails or is not done. """ trsf = gce_MakeMirror(pln.gp_pln).Value() builder = BRepBuilderAPI_Transform(shape.object, trsf, True) if not builder.IsDone(): raise RuntimeError('Failed to mirror the shape.') return Shape.wrap(builder.Shape())
def translate_shape(shape, vec): """ Translate a shape along a vector. :param afem.topology.entities.Shape shape: The shape. :param Union[afem.geometry.entitites.Vector, Sequence] vec: The vector. :return: The translated shape. :rtype: afem.topology.entities.Shape :raise RuntimeError: If the translation fails or is not done. """ trsf = gce_MakeTranslation(vec.gp_vec).Value() builder = BRepBuilderAPI_Transform(shape.object, trsf, True) if not builder.IsDone(): raise RuntimeError('Failed to mirror the shape.') return Shape.wrap(builder.Shape())
def update_shape(self, change=None): d = self.declaration #: Get the shape to apply the tranform to if d.shape: make_copy = True original = coerce_shape(d.shape) else: # Use the first child make_copy = False child = self.get_first_child() original = child.shape t = self.get_transform() transform = BRepBuilderAPI_Transform(original, t, make_copy) shape = transform.Shape() # Convert it back to the original type self.shape = Topology.cast_shape(shape)
def create_shape(self): d = self.declaration if not d.source: return if os.path.exists(os.path.expanduser(d.source)): svg = etree.parse(os.path.expanduser(d.source)).getroot() else: svg = etree.fromstring(d.source) node = self.doc = OccSvgDoc(element=svg) viewbox = svg.attrib.get('viewBox') x, y = (0, 0) sx, sy = (1, 1) if viewbox: ow = parse_unit(svg.attrib.get('width')) oh = parse_unit(svg.attrib.get('height')) x, y, iw, ih = map(parse_unit, viewbox.split()) sx = ow / iw sy = oh / ih builder = BRep_Builder() shape = TopoDS_Compound() builder.MakeCompound(shape) shapes = node.create_shape() for s in shapes: builder.Add(shape, s) bbox = self.get_bounding_box(shape) # Move to position and align along direction axis t = self.get_transform() if d.mirror: m = gp_Trsf() m.SetMirror(gp_Ax2(gp_Pnt(*bbox.center), gp_Dir(0, 1, 0))) t.Multiply(m) # Apply viewport scale s = gp_Trsf() s.SetValues(sx, 0, 0, x, 0, sy, 0, y, 0, 0, 1, 0) t.Multiply(s) self.shape = BRepBuilderAPI_Transform(shape, t, False).Shape()
def revolved_cut(base): # Define 7 points face_points = TColgp_Array1OfPnt(1, 7) face_inner_radius = 0.6 pts = [ gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius - 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, 0.025), gp_Pnt(face_inner_radius + 0.10, 0.0, -0.025), gp_Pnt(face_inner_radius + 0.05, 0.0, -0.05), gp_Pnt(face_inner_radius - 0.05, 0.0, -0.05), ] for n, i in enumerate(pts): face_points.SetValue(n + 1, i) # Use these points to create edges and add these edges to a wire hexwire = BRepBuilderAPI_MakeWire() for i in range(1, 7): hexedge = BRepBuilderAPI_MakeEdge(face_points.Value(i), face_points.Value(i + 1)).Edge() hexwire.Add(hexedge) # Turn the wire into a 6 sided face hexface = BRepBuilderAPI_MakeFace(hexwire.Wire()).Face() # Revolve the face around an axis revolve_axis = gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) revolved_shape = BRepPrimAPI_MakeRevol(hexface, revolve_axis).Shape() # Move the generated shape move = gp_Trsf() move.SetTranslation(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, sin(0.5))) moved_shape = BRepBuilderAPI_Transform(revolved_shape, move, False).Shape() # Remove the revolved shape cut = BRepAlgoAPI_Cut(base, moved_shape).Shape() return cut
def create_shape(self): d = self.declaration t = self.get_transform() w, h = d.width, d.height if d.rx or d.ry: rx, ry = d.rx, d.ry if not ry: ry = rx elif not rx: rx = ry # Clamp to the valid range rx = min(w / 2, rx) ry = min(h / 2, ry) # Bottom p1 = gp_Pnt(0 + rx, 0, 0) p2 = gp_Pnt(0 + w - rx, 0, 0) # Right p3 = gp_Pnt(w, ry, 0) p4 = gp_Pnt(w, h - ry, 0) # Top p5 = gp_Pnt(w - rx, h, 0) p6 = gp_Pnt(rx, h, 0) # Left p7 = gp_Pnt(0, h - ry, 0) p8 = gp_Pnt(0, ry, 0) shape = BRepBuilderAPI_MakeWire() e = d.tolerance # Bottom if not p1.IsEqual(p2, e): shape.Add(BRepBuilderAPI_MakeEdge(p1, p2).Edge()) # Arc bottom right c = make_ellipse((w - rx, ry, 0), rx, ry) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p2, p3, False).Value()).Edge()) # Right if not p3.IsEqual(p4, e): shape.Add(BRepBuilderAPI_MakeEdge(p3, p4).Edge()) # Arc top right c.SetLocation(gp_Pnt(w - rx, h - ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p4, p5, False).Value()).Edge()) # Top if not p5.IsEqual(p6, e): shape.Add(BRepBuilderAPI_MakeEdge(p5, p6).Edge()) # Arc top left c.SetLocation(gp_Pnt(rx, h - ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p6, p7, False).Value()).Edge()) # Left if not p7.IsEqual(p8, e): shape.Add(BRepBuilderAPI_MakeEdge(p7, p8).Edge()) # Arc bottom left c.SetLocation(gp_Pnt(rx, ry, 0)) shape.Add( BRepBuilderAPI_MakeEdge( GC_MakeArcOfEllipse(c, p8, p1, False).Value()).Edge()) shape = shape.Wire() shape.Closed(True) else: shape = BRepBuilderAPI_MakePolygon(gp_Pnt(0, 0, 0), gp_Pnt(w, 0, 0), gp_Pnt(w, h, 0), gp_Pnt(0, h, 0), True).Wire() wire = TopoDS.Wire_(BRepBuilderAPI_Transform(shape, t, False).Shape()) self.curve = BRepAdaptor_CompCurve(wire) self.shape = wire
def cut_out(base): outer = gp_Circ2d(gp_OX2d(), top_radius - 1.75 * roller_diameter) inner = gp_Circ2d(gp_OX2d(), center_radius + 0.75 * roller_diameter) geom_outer = GCE2d_MakeCircle(outer).Value() geom_inner = GCE2d_MakeCircle(inner).Value() geom_inner.Reverse() base_angle = (2. * M_PI) / mounting_hole_count hole_angle = atan(hole_radius / mounting_radius) correction_angle = 3 * hole_angle left = gp_Lin2d(gp_Origin2d(), gp_DX2d()) right = gp_Lin2d(gp_Origin2d(), gp_DX2d()) left.Rotate(gp_Origin2d(), correction_angle) right.Rotate(gp_Origin2d(), base_angle - correction_angle) geom_left = GCE2d_MakeLine(left).Value() geom_right = GCE2d_MakeLine(right).Value() inter_1 = Geom2dAPI_InterCurveCurve(geom_outer, geom_left) inter_2 = Geom2dAPI_InterCurveCurve(geom_outer, geom_right) inter_3 = Geom2dAPI_InterCurveCurve(geom_inner, geom_right) inter_4 = Geom2dAPI_InterCurveCurve(geom_inner, geom_left) if inter_1.Point(1).X() > 0: p1 = inter_1.Point(1) else: p1 = inter_1.Point(2) if inter_2.Point(1).X() > 0: p2 = inter_2.Point(1) else: p2 = inter_2.Point(2) if inter_3.Point(1).X() > 0: p3 = inter_3.Point(1) else: p3 = inter_3.Point(2) if inter_4.Point(1).X() > 0: p4 = inter_4.Point(1) else: p4 = inter_4.Point(2) trimmed_outer = GCE2d_MakeArcOfCircle(outer, p1, p2).Value() trimmed_inner = GCE2d_MakeArcOfCircle(inner, p4, p3).Value() plane = gp_Pln(gp_Origin(), gp_DZ()) arc1 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_outer, plane)).Edge() lin1 = BRepBuilderAPI_MakeEdge(gp_Pnt(p2.X(), p2.Y(), 0), gp_Pnt(p3.X(), p3.Y(), 0)).Edge() arc2 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_inner, plane)).Edge() lin2 = BRepBuilderAPI_MakeEdge(gp_Pnt(p4.X(), p4.Y(), 0), gp_Pnt(p1.X(), p1.Y(), 0)).Edge() cutout_wire = BRepBuilderAPI_MakeWire(arc1) cutout_wire.Add(lin1) cutout_wire.Add(arc2) cutout_wire.Add(lin2) # Turn the wire into a face cutout_face = BRepBuilderAPI_MakeFace(cutout_wire.Wire()) filleted_face = BRepFilletAPI_MakeFillet2d(cutout_face.Face()) explorer = BRepTools_WireExplorer(cutout_wire.Wire()) while explorer.More(): vertex = explorer.CurrentVertex() filleted_face.AddFillet(vertex, roller_radius) explorer.Next() cutout = BRepPrimAPI_MakePrism(filleted_face.Shape(), gp_Vec(0.0, 0.0, thickness)).Shape() result = base rotate = gp_Trsf() for i in range(0, mounting_hole_count): rotate.SetRotation(gp_OZ(), i * 2. * M_PI / mounting_hole_count) rotated_cutout = BRepBuilderAPI_Transform(cutout, rotate, True) result = BRepAlgoAPI_Cut(result, rotated_cutout.Shape()).Shape() return result
def create_shape(self): """ Create the shape by loading it from the given path. """ shape = self.load_shape() t = self.get_transform() loaded_shape = BRepBuilderAPI_Transform(shape, t, False) self.shape = loaded_shape.Shape()
def round_tooth(wedge): round_x = 2.6 round_z = 0.06 * pitch round_radius = pitch # Determine where the circle used for rounding has to start and stop p2d_1 = gp_Pnt2d(top_radius - round_x, 0) p2d_2 = gp_Pnt2d(top_radius, round_z) # Construct the rounding circle round_circle = GccAna_Circ2d2TanRad(p2d_1, p2d_2, round_radius, 0.01) if (round_circle.NbSolutions() != 2): sys.exit(-2) round_circle_2d_1 = round_circle.ThisSolution(1) round_circle_2d_2 = round_circle.ThisSolution(2) if (round_circle_2d_1.Position().Location().Coord()[1] >= 0): round_circle_2d = round_circle_2d_1 else: round_circle_2d = round_circle_2d_2 # Remove the arc used for rounding trimmed_circle = GCE2d_MakeArcOfCircle(round_circle_2d, p2d_1, p2d_2).Value() # Calculate extra points used to construct lines p1 = gp_Pnt(p2d_1.X(), 0, p2d_1.Y()) p2 = gp_Pnt(p2d_2.X(), 0, p2d_2.Y()) p3 = gp_Pnt(p2d_2.X() + 1, 0, p2d_2.Y()) p4 = gp_Pnt(p2d_2.X() + 1, 0, p2d_1.Y() - 1) p5 = gp_Pnt(p2d_1.X(), 0, p2d_1.Y() - 1) # Convert the arc and four extra lines into 3D edges plane = gp_Pln(gp_Ax3(gp_Origin(), gp_DY().Reversed(), gp_DX())) arc1 = BRepBuilderAPI_MakeEdge(geomapi_To3d(trimmed_circle, plane)).Edge() lin1 = BRepBuilderAPI_MakeEdge(p2, p3).Edge() lin2 = BRepBuilderAPI_MakeEdge(p3, p4).Edge() lin3 = BRepBuilderAPI_MakeEdge(p4, p5).Edge() lin4 = BRepBuilderAPI_MakeEdge(p5, p1).Edge() # Make a wire composed of the edges round_wire = BRepBuilderAPI_MakeWire(arc1) round_wire.Add(lin1) round_wire.Add(lin2) round_wire.Add(lin3) round_wire.Add(lin4) # Turn the wire into a face round_face = BRepBuilderAPI_MakeFace(round_wire.Wire()).Shape() # Revolve the face around the Z axis over the tooth angle rounding_cut_1 = BRepPrimAPI_MakeRevol(round_face, gp_OZ(), tooth_angle).Shape() # Construct a mirrored copy of the first cutting shape mirror = gp_Trsf() mirror.SetMirror(gp_XOY()) mirrored_cut_1 = BRepBuilderAPI_Transform(rounding_cut_1, mirror, True).Shape() # and translate it so that it ends up on the other side of the wedge translate = gp_Trsf() translate.SetTranslation(gp_Vec(0, 0, thickness)) rounding_cut_2 = BRepBuilderAPI_Transform(mirrored_cut_1, translate, False).Shape() # Cut the wedge using the first and second cutting shape cut_1 = BRepAlgoAPI_Cut(wedge, rounding_cut_1).Shape() cut_2 = BRepAlgoAPI_Cut(cut_1, rounding_cut_2).Shape() return cut_2
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)