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 move(orig_pypt, location_pypt, occtopology): """ This function moves an OCCtopology from the orig_pypt to the location_pypt. Parameters ---------- orig_pypt : tuple of floats The OCCtopology will move in reference to this point. A pypt is a tuple that documents the xyz coordinates of a pt e.g. (x,y,z) location_pypt : tuple of floats The destination of where the OCCtopology will be moved in relation to the orig_pypt. A pypt is a tuple that documents the xyz coordinates of a pt e.g. (x,y,z) occtopology : OCCtopology The OCCtopology to be moved. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex Returns ------- moved topology : OCCtopology (OCCshape) The moved OCCtopology. """ gp_ax31 = gp_Ax3(gp_Pnt(orig_pypt[0], orig_pypt[1], orig_pypt[2]), gp_DZ()) gp_ax32 = gp_Ax3( gp_Pnt(location_pypt[0], location_pypt[1], location_pypt[2]), gp_DZ()) aTrsf = gp_Trsf() aTrsf.SetTransformation(gp_ax32, gp_ax31) trsf_brep = BRepBuilderAPI_Transform(aTrsf) trsf_brep.Perform(occtopology, True) trsf_shp = trsf_brep.Shape() return trsf_shp
def utilGetZRotatedShape(aShape, angle): aTransform = gp_Trsf() rotationAxis = gp_OZ() aTransform.SetRotation(rotationAxis, angle) rotatedShape = BRepBuilderAPI_Transform(aShape, aTransform).Shape() return rotatedShape
def scale(occtopology, scale_factor, ref_pypt): """ This function uniformly scales an OCCtopology based on the reference point and the scale factor. Parameters ---------- occtopology : OCCtopology The OCCtopology to be scaled. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex scale_factor : float The scale factor. ref_pypt : tuple of floats The OCCtopology will scale in reference to this point. A pypt is a tuple that documents the xyz coordinates of a pt e.g. (x,y,z) Returns ------- scaled topology : OCCtopology (OCCshape) The scaled OCCtopology. """ xform = gp_Trsf() gp_pnt = construct.make_gppnt(ref_pypt) xform.SetScale(gp_pnt, scale_factor) brep = BRepBuilderAPI_Transform(xform) brep.Perform(occtopology, True) trsfshape = brep.Shape() return trsfshape
def TransformShape(self, X: float, Y: float, Z: float, RX: float, RY: float, RZ: float, Shape: TopoDS_Compound) -> TopoDS_Compound: trsf = self.__GetTransform(X, Y, Z, RX, RY, RZ) transform = BRepBuilderAPI_Transform(Shape, trsf, False) transform.Build() return transform.Shape()
def transform(self, trans): if isinstance(trans, Transformation): shp = BRepBuilderAPI_Transform( self._shp, trans._trsf, True).Shape() return Shape(shp) if isinstance(trans, GeneralTransformation): shp = BRepBuilderAPI_GTransform( self._shp, trans._gtrsf, True).Shape() return Shape(shp)
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 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 ''' trns = gp_Trsf() trns.SetTranslation(vec) brep_trns = BRepBuilderAPI_Transform(brep_or_iterable, trns, copy) brep_trns.Build() return 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 translate_topods_from_vector(brep, vec, copy=False): """Translate a brep over a vector. Args: brep (BRep): the Topo_DS to translate vec (gp_Vec): the vector defining the translation copy (bool): copies to brep if True """ trns = gp_Trsf() trns.SetTranslation(vec) brep_trns = BRepBuilderAPI_Transform(brep, trns, copy) 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 renderShapeObj(self, shape, transforms, styleName): color, transparency, materialName = self.getStyle(styleName) trsf = gp_Trsf() for tr in transforms: trsf *= tr.getTrsf() shape = BRepBuilderAPI_Transform(shape, trsf).Shape() ais = AIS_Shape(shape) r,g,b = color aisColor = Quantity_Color(r/256, g/256, b/256, Quantity_TypeOfColor(Quantity_TypeOfColor.Quantity_TOC_RGB)) ais.SetColor(aisColor) ais.SetTransparency(transparency/100) aspect = Graphic3d_MaterialAspect(getMaterialNameConst(materialName)) ais.SetMaterial(aspect) self.display.Context.Display(ais, False)
def _renderShapeObj(self, aShape): shapeTr = BRepBuilderAPI_Transform(aShape, self.aMove.getTrsf()).Shape() ais = AIS_Shape(shapeTr) r, g, b = self.aStyle.getNormedColor() aisColor = Quantity_Color( r, g, b, Quantity_TypeOfColor(Quantity_TypeOfColor.Quantity_TOC_RGB)) ais.SetColor(aisColor) ais.SetTransparency(self.aStyle.getNormedTransparency()) aspect = Graphic3d_MaterialAspect( MATERIAL_CONSTS[self.aStyle.getMaterial()]) ais.SetMaterial(aspect) self.display.Context.Display(ais, False)
def transformed_shape(self): r"""The shape of the part, placed in its final location Returns ------- an OCC shape, in its final location """ trsf = gp_Trsf() m = self.combined_matrix trsf.SetValues(m[0, 0], m[0, 1], m[0, 2], m[0, 3], m[1, 0], m[1, 1], m[1, 2], m[1, 3], m[2, 0], m[2, 1], m[2, 2], m[2, 3]) transformed = BRepBuilderAPI_Transform(self.shape, trsf) return transformed.Shape()
def rotate_shp_3_axis(shape, revolve_axis, rotation): """ Rotate a shape around a pre-defined rotation axis gp_Ax1. @param rotation : rotation in degrees around (gp_Ax1) @param shape : shape in question @param revolve_axis : rotation axis gp_Ax1 @return : the rotated shape. """ alpha = gp_Trsf() alpha.SetRotation(revolve_axis, np.deg2rad(rotation)) brep_trns = BRepBuilderAPI_Transform(shape, alpha, False) shp = brep_trns.Shape() return shp
def offset_shapes(shapes, height_mm): # Offset letters so they can be previewed properly from 2 directions tf = gp_Trsf() p1 = ORIGIN offset = 0 offset_increment = 1.1 * height_mm offset_letters = [] for shape in shapes: tf.SetTranslation(p1, gp_Pnt(offset, offset, 0)) offset_letter = BRepBuilderAPI_Transform(shape, tf).Shape() assert isinstance(offset_letter, TopoDS_Compound) offset_letters.append(offset_letter) offset += offset_increment return offset_letters
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 makeWholeWire(): global myWireProfile xAxis = gp_OX() # Set up the mirror aTrsf = gp_Trsf() aTrsf.SetMirror(xAxis) # Apply the mirror transform aBRepTrsf = BRepBuilderAPI_Transform(aWire, aTrsf) # Convert mirrored shape to a wire aMirroredShape = aBRepTrsf.Shape() aMirroredWire = topods_Wire(aMirroredShape) # Combine the two wires mkWire = BRepBuilderAPI_MakeWire() mkWire.Add(aWire) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() return myWireProfile
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 create_model(self, rotate_angle=None): edges = makeEdgesFromPoints(self.points) wire = makeWireFromEdges(edges) aFace = makeFaceFromWire(wire) extrudeDir = self.T * self.wDir # extrudeDir is a numpy array if rotate_angle == None: prism1 = makePrismFromFace(aFace, extrudeDir) else: prism = makePrismFromFace(aFace, extrudeDir) trns = gp_Trsf() # axis = numpy.array([1.0, 0.0, 0.0]) angle = radians(rotate_angle) trns.SetRotation(gp_OX(), angle) brep_trns = BRepBuilderAPI_Transform(prism, trns, False) brep_trns.Build() prism1 = brep_trns.Shape() return prism1
def rotate_shape(shape, axis, angle, unite="deg"): """Rotate a shape around an axis, with a given angle. @param shape : the shape to rotate @point : the origin of the axis @vector : the axis direction @angle : the value of the rotation @return: the rotated shape. """ assert_shape_not_null(shape) if unite == "deg": # convert angle to radians angle = radians(angle) trns = gp_Trsf() trns.SetRotation(axis, angle) brep_trns = BRepBuilderAPI_Transform(shape, trns, False) brep_trns.Build() shp = brep_trns.Shape() return shp
def makeWholeWire(event=None): global myWireProfile xAxis = gp_OX() # Set up the mirror aTrsf = gp_Trsf() aTrsf.SetMirror(xAxis) # Apply the mirror transform aBRepTrsf = BRepBuilderAPI_Transform(aWire, aTrsf) # Convert mirrored shape to a wire aMirroredShape = aBRepTrsf.Shape() aMirroredWire = topods_Wire(aMirroredShape) # Combine the two wires mkWire = BRepBuilderAPI_MakeWire() mkWire.Add(aWire) mkWire.Add(aMirroredWire) myWireProfile = mkWire.Wire() display.DisplayColoredShape(myWireProfile, 'BLUE') display.Repaint() win.statusBar().showMessage('Make whole wire complete')
def rotate_shp_3_axis(shape, rx, ry, rz, unity="deg"): """ Rotate a shape around (O,x), (O,y) and (O,z). @param rx_degree : rotation around (O,x) @param ry_degree : rotation around (O,y) @param rz_degree : rotation around (O,z) @return : the rotated shape. """ if unity == "deg": # convert angle to radians rx = radians(rx) ry = radians(ry) rz = radians(rz) alpha = gp_Trsf() alpha.SetRotation(gp_OX(), rx) beta = gp_Trsf() beta.SetRotation(gp_OY(), ry) gamma = gp_Trsf() gamma.SetRotation(gp_OZ(), rz) brep_trns = BRepBuilderAPI_Transform(shape, alpha * beta * gamma, False) shp = brep_trns.Shape() return shp
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 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 combine_faces(face1, face2, height_mm): assert isinstance(face1, TopoDS_Face) assert isinstance(face2, TopoDS_Face) face1_ = copy.deepcopy(face1) face2_ = copy.deepcopy(face2) # assuming both faces start in the XZ plane tf = gp_Trsf() # rotate from the XZ plane to the YZ plane tf.SetRotation(gp_Ax1(ORIGIN, DIR_Z), math.pi / 2) face2_ = BRepBuilderAPI_Transform(face2_, tf).Shape() # We assume characters are no wider than they are tall, but just in case # we extrude by twice the height to make sure to capture all features face1_extruded = make_extrusion(face1_, 2 * height_mm, gp_Vec(0, 1, 0)) face2_extruded = make_extrusion(face2_, 2 * height_mm, gp_Vec(1, 0, 0)) common = BRepAlgoAPI_Common(face1_extruded, face2_extruded) result = common.Shape() assert isinstance(result, TopoDS_Compound) return copy.deepcopy(result)
def _place(self): """ put the part where it belongs. This should be called after the shape has been initialized, so that the shape can be transformed """ assert (self._shape is not None) if self._parent is not None: trans = self._parent.shape.Location().Transformation() else: trans = gp_Trsf() translation = gp_Trsf() translation.SetTranslation(gp_Vec(*self._position)) trans = trans * translation rot = euler_to_gp_trsf(self._orientation) trans = trans * rot brep_trns = BRepBuilderAPI_Transform(self._shape, trans, False) brep_trns.Build() self._shape = brep_trns.Shape()
def get_wire(self): if (self.wire == None): occ_edges = [] for i, e in enumerate(self.edges): o = "Element %i/%i: " % (i, len(self.edges)) if (isinstance(e, Line3)): occ_edges.append(e.to_edge()) elif (isinstance(e, Fillet2)): if ((len(self.edges)) <= (i + 1)): error("Not enough elements for fillet") exit(0) else: occ_edges.append( e.to_edge(self.edges[i - 1], self.edges[i + 1])) o += repr(e) debug(o) if occ_edges == []: return None self.wire = make_wire(occ_edges) trsf_wire = BRepBuilderAPI_Transform(self.wire, self.trsf) trsf_shape = trsf_wire.Shape() self.wire = topods.Wire(trsf_shape) self.face = None return self.wire
def transform_shape(shape: Union[TopoDS_Shape], scale=None, transform: Union[Placement, tuple, list] = None, rotate: Rotation = None) -> TopoDS_Shape: trsf = gp_Trsf() if scale is not None: trsf.SetScaleFactor(scale) if transform is not None: if type(transform) is Placement: tra = transform.origin trsf.SetTranslation(gp_Vec(tra[0], tra[1], tra[2])) elif type(transform) in (tuple, list): trsf.SetTranslation( gp_Vec(transform[0], transform[1], transform[2])) else: raise ValueError( f'Unrecognized transform input type "{type(transform)}"') if rotate is not None: pt = gp_Pnt(*rotate.origin) dire = gp_Dir(*rotate.vector) revolve_axis = gp_Ax1(pt, dire) trsf.SetRotation(revolve_axis, math.radians(rotate.angle)) return BRepBuilderAPI_Transform(shape, trsf, True).Shape()