def _calcTransforms(self): """Computes transformation matrices to convert between coordinates Computes transformation matrices to convert between local and global coordinates. """ # r is the forward transformation matrix from world to local coordinates # ok i will be really honest, i cannot understand exactly why this works # something bout the order of the translation and the rotation. # the double-inverting is strange, and I don't understand it. forward = Matrix() inverse = Matrix() forwardT = gp_Trsf() inverseT = gp_Trsf() global_coord_system = gp_Ax3() local_coord_system = gp_Ax3( gp_Pnt(*self.origin.toTuple()), gp_Dir(*self.zDir.toTuple()), gp_Dir(*self.xDir.toTuple()), ) forwardT.SetTransformation(global_coord_system, local_coord_system) forward.wrapped = gp_GTrsf(forwardT) inverseT.SetTransformation(local_coord_system, global_coord_system) inverse.wrapped = gp_GTrsf(inverseT) # TODO verify if this is OK self.lcs = local_coord_system self.rG = inverse self.fG = forward
def __init__(self, matrix=None): if matrix is None: self.wrapped = gp_GTrsf() elif isinstance(matrix, gp_GTrsf): self.wrapped = matrix elif isinstance(matrix, gp_Trsf): self.wrapped = gp_GTrsf(matrix) elif isinstance(matrix, (list, tuple)): # Validate matrix size & 4x4 last row value valid_sizes = all( (isinstance(row, (list, tuple)) and (len(row) == 4)) for row in matrix) and len(matrix) in (3, 4) if not valid_sizes: raise TypeError( "Matrix constructor requires 2d list of 4x3 or 4x4, but got: {!r}" .format(matrix)) elif (len(matrix) == 4) and (tuple(matrix[3]) != (0, 0, 0, 1)): raise ValueError( "Expected the last row to be [0,0,0,1], but got: {!r}". format(matrix[3])) # Assign values to matrix self.wrapped = gp_GTrsf() [ self.wrapped.SetValue(i + 1, j + 1, e) for i, row in enumerate(matrix[:3]) for j, e in enumerate(row) ] else: raise TypeError( "Invalid param to matrix constructor: {}".format(matrix))
def htransform2(tpnt, tvx, tvy, tvz, pnt, vx, vy, vz): tvx = tvy.Crossed(tvz) tvy = tvz.Crossed(tvx) tvx.Normalize() tvy.Normalize() tvz.Normalize() tpnt = gp_Pnt((gp_Vec(tpnt.XYZ()) + tvz * 0.1).XYZ()) vx = vy.Crossed(vz) vy = vz.Crossed(vx) vx.Normalize() vy.Normalize() vz.Normalize() Tt = gp_GTrsf( gp_Mat(tvx.X(), tvy.X(), tvz.X(), tvx.Y(), tvy.Y(), tvz.Y(), tvx.Z(), tvy.Z(), tvz.Z()), gp_XYZ(tpnt.X(), tpnt.Y(), tpnt.Z())) Tt.Invert() rott = gp_Mat(tvx.X(), tvy.X(), tvz.X(), tvx.Y(), tvy.Y(), tvz.Y(), tvx.Z(), tvy.Z(), tvz.Z()) rott.Transpose() quatt = gp_Quaternion(rott) dispt = gp_Vec(Tt.TranslationPart().X(), Tt.TranslationPart().Y(), Tt.TranslationPart().Z()) trsft = gp_Trsf() trsft.SetTransformation(quatt, dispt) rotp = gp_Mat(vx.X(), vy.X(), vz.X(), vx.Y(), vy.Y(), vz.Y(), vx.Z(), vy.Z(), vz.Z()) quatp = gp_Quaternion(rotp) dispp = gp_Vec(gp_Pnt(0, 0, 0), pnt) trsfp = gp_Trsf() trsfp.SetTransformation(quatp, dispp) trsfo = trsfp.Multiplied(trsft) loco = TopLoc_Location(trsfo) return loco
def _rotate(self, direction, angle): new = gp_Trsf() new.SetRotation(direction, angle) self.wrapped = self.wrapped * gp_GTrsf(new)
def gen_ellipsoid(axs=gp_Ax3(), rxyz=[10, 20, 30]): sphere = BRepPrimAPI_MakeSphere(gp_Ax2(), 1).Solid() loc = set_loc(gp_Ax3(), axs) mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2]) gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0)) ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape() ellips.Location(loc) return ellips
def __setstate__(self, dct): col1 = dct["col1"] col2 = dct["col2"] col3 = dct["col3"] tra = dct["transl"] mat = gp_Mat(col1, col2, col3) _gtrsf = gp_GTrsf() _gtrsf.SetVectorialPart(mat) _gtrsf.SetTranslation(*tra)
def gen_ellipsoid_geom(axs=gp_Ax3(), rxyz=[10, 20, 30]): sphere = gp_Sphere(axs, 1) api = Geom_SphericalSurface(sphere) api.Continuity() api.Transform() print(api.Area()) loc = set_loc(gp_Ax3(), axs) mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2]) gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0)) ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape() ellips.Location(loc) return ellips
def scale_shape(shape, fx, fy, fz): """Scale a shape along the 3 directions @param fx : scale factor in the x direction @param fy : scale factor in the y direction @param fz : scale factor in the z direction @return : the scaled shape """ assert_shape_not_null(shape) scale_trsf = gp_GTrsf() rot = gp_Mat(fx, 0.0, 0.0, 0.0, fy, 0.0, 0.0, 0.0, fz) scale_trsf.SetVectorialPart(rot) shp = BRepBuilderAPI_GTransform(shape, scale_trsf).Shape() return shp
def __init__(self): super().__init__() self.axs = gp_Ax3() self.radi = [500, 200] self.rxyz = [1.5, 1.2, 1.5] mat = gp_Mat(self.rxyz[0], 0, 0, 0, self.rxyz[1], 0, 0, 0, self.rxyz[2]) gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0)) #self.t = Geom_ToroidalSurface(self.axs, *self.radi) self.t = Geom_SphericalSurface(self.axs, 100.0) self.face = BRepBuilderAPI_MakeFace(self.t, 1e-6).Face() self.face = BRepBuilderAPI_GTransform(self.face, gtrf).Shape() self.surf = BRep_Tool.Surface(self.face) self.prop = GeomLProp_SLProps(self.surf, 0.0, 0.0, 1, 1.0) self.export_stp(self.face) print(self.t.UPeriod())
def uniform_scale(occtopology, tx, ty, tz, ref_pypt): """ This function uniformly scales an OCCtopology based on the reference point and tx,ty,tz factors. Parameters ---------- occtopology : OCCtopology The OCCtopology to be scaled. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex tx : float The scale factor in the X-axis. ty : float The scale factor in the Y-axis. tz : float The scale factor in the Z-axis. 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. """ moved_shape = move(ref_pypt, (0, 0, 0), occtopology) xform = gp_GTrsf() xform.SetVectorialPart(gp_Mat( tx, 0, 0, 0, ty, 0, 0, 0, tz, )) brep = BRepBuilderAPI_GTransform(xform) brep.Perform(moved_shape, True) trsfshape = brep.Shape() move_back_shp = move((0, 0, 0), ref_pypt, trsfshape) return move_back_shp
def rotated(self, rotate=(0, 0, 0)): """Returns a copy of this plane, rotated about the specified axes Since the z axis is always normal the plane, rotating around Z will always produce a plane that is parallel to this one. The origin of the workplane is unaffected by the rotation. Rotations are done in order x, y, z. If you need a different order, manually chain together multiple rotate() commands. :param rotate: Vector [xDegrees, yDegrees, zDegrees] :return: a copy of this plane rotated as requested. """ # NB: this is not a geometric Vector rotate = Vector(rotate) # Convert to radians. rotate = rotate.multiply(math.pi / 180.0) # Compute rotation matrix. T1 = gp_Trsf() T1.SetRotation( gp_Ax1(gp_Pnt(*(0, 0, 0)), gp_Dir(*self.xDir.toTuple())), rotate.x) T2 = gp_Trsf() T2.SetRotation( gp_Ax1(gp_Pnt(*(0, 0, 0)), gp_Dir(*self.yDir.toTuple())), rotate.y) T3 = gp_Trsf() T3.SetRotation( gp_Ax1(gp_Pnt(*(0, 0, 0)), gp_Dir(*self.zDir.toTuple())), rotate.z) T = Matrix(gp_GTrsf(T1 * T2 * T3)) # Compute the new plane. newXdir = self.xDir.transform(T) newZdir = self.zDir.transform(T) return Plane(self.origin, newXdir, newZdir)
def scaleXYZ(x, y, z): gtrsf = gp_GTrsf() gtrsf.SetVectorialPart(gp_Mat(x, 0, 0, 0, y, 0, 0, 0, z)) return GeneralTransformation(gtrsf)
def brep_feat_extrusion_protrusion(event=None): # Extrusion S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = TopologyExplorer(S).faces() F = next(faces) surf1 = BRep_Tool_Surface(F) Pl1 = Geom_Plane.DownCast(surf1) D1 = Pl1.Pln().Axis().Direction().Reversed() MW = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(200., -100.), gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -100.), gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(100., -200.), gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., -200.), gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() MW.Add(BRepBuilderAPI_MakeEdge(aline, surf1, 0., p1.Distance(p2)).Edge()) MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf1, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) display.EraseAll() MKP = BRepFeat_MakePrism(S, FP, F, D1, 0, True) MKP.PerformThruAll() res1 = MKP.Shape() display.DisplayShape(res1) # Protrusion next(faces) F2 = next(faces) surf2 = BRep_Tool_Surface(F2) Pl2 = Geom_Plane.DownCast(surf2) D2 = Pl2.Pln().Axis().Direction().Reversed() MW2 = BRepBuilderAPI_MakeWire() p1, p2 = gp_Pnt2d(100., 100.), gp_Pnt2d(200., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(200., 100.), gp_Pnt2d(150., 200.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) p1, p2 = gp_Pnt2d(150., 200.), gp_Pnt2d(100., 100.) aline = GCE2d_MakeLine(p1, p2).Value() MW2.Add(BRepBuilderAPI_MakeEdge(aline, surf2, 0., p1.Distance(p2)).Edge()) MKF2 = BRepBuilderAPI_MakeFace() MKF2.Init(surf2, False, 1e-6) MKF2.Add(MW2.Wire()) MKF2.Build() FP = MKF2.Face() breplib_BuildCurves3d(FP) MKP2 = BRepFeat_MakePrism(res1, FP, F2, D2, 0, True) MKP2.PerformThruAll() display.EraseAll() trf = gp_Trsf() trf.SetTranslation(gp_Vec(0, 0, 300)) gtrf = gp_GTrsf() gtrf.SetTrsf(trf) tr = BRepBuilderAPI_GTransform(MKP2.Shape(), gtrf, True) fused = BRepAlgoAPI_Fuse(tr.Shape(), MKP2.Shape()) fused.Build() display.DisplayShape(fused.Shape()) display.FitAll()
def getTrsf(self): kx, ky, kz = self.geometry trsf = gp_GTrsf() # todo SetAffinity trsf.SetScale(kx, ky, kz) return trsf
def utilShapeZScale(shape, scaleK): transform = gp_GTrsf() transform.SetAffinity(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1), gp_Dir(0, 1, 0)), scaleK) shape = BRepBuilderAPI_GTransform(shape, transform).Shape() return shape
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_GTransform from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Common from OCC.Display.SimpleGui import init_display display, start_display, add_menu, add_function_to_menu = init_display() orig = gp_Pnt(0., 0., 0.) sphere = BRepPrimAPI_MakeSphere(orig, 50.).Solid() # be careful that the following scale numbers are "not too big", # otherwise boolean operations can be buggy # see isue a1 = 17.1 a2 = 17.1 a3 = 3.5 gTrsf = gp_GTrsf(gp_Mat(a1, 0, 0, 0, a2, 0, 0, 0, a3), gp_XYZ(0., 112.2, 0.)) ellipsoid = BRepBuilderAPI_GTransform(sphere, gTrsf).Shape() # then perform a boolean intersection with a box box = BRepPrimAPI_MakeBox(gp_Pnt(-1000, -1000, -1000), gp_Pnt(1000, 112.2, 1000)).Shape() common = BRepAlgoAPI_Common(box, ellipsoid).Shape() if common.IsNull(): raise AssertionError("common result is Null.") display.DisplayShape(box, color="BLACK", transparency=0.8) display.DisplayShape(ellipsoid, color="BLACK", transparency=0.8) display.DisplayShape(common, color="BLACK", transparency=0.8, update=True) start_display()
def gen_ellipsoid(axs=gp_Ax3(), rxyz=[10, 20, 30]): mat = gp_Mat(rxyz[0], 0, 0, 0, rxyz[1], 0, 0, 0, rxyz[2]) gtrf = gp_GTrsf(mat, gp_XYZ(0, 0, 0)) sphere = BRepPrimAPI_MakeSphere(axs.Ax2(), 1).Solid() ellips = BRepBuilderAPI_GTransform(sphere, gtrf).Shape() return ellips