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 filling(self, name_body, coord_centres, coord_centres_2): ''' :param name_body: :param coord_centres: 0 - угол поворота x (градусы) 1 - угол поворота y (градусы) 2 - угол поворота z (градусы) 3 - смещенмие по x 4 - смещенмие по y 5 - смещенмие по z :return: ''' # print(name_body, coord_centres) # shape = self.modules[name_body] # print(body) cp = BRepBuilderAPI_Copy(self.reserv_models[name_body]) cp.Perform(self.reserv_models[name_body]) shape = cp.Shape() # print(shape) r = R.from_euler( 'xyz', [coord_centres[0], coord_centres[1], coord_centres[2]], degrees=True) Rot = r.as_dcm() trsf = gp_Trsf() Mat = gp_Mat(Rot[0][0], Rot[0][1], Rot[0][2], Rot[1][0], Rot[1][1], Rot[1][2], Rot[2][0], Rot[2][1], Rot[2][2]) trsf.SetRotation(gp_Quaternion(Mat)) shape.Move(TopLoc_Location(trsf)) r = R.from_euler( 'xyz', [coord_centres_2[0], coord_centres_2[1], coord_centres_2[2]], degrees=True) Rot = r.as_dcm() trsf = gp_Trsf() Mat = gp_Mat(Rot[0][0], Rot[0][1], Rot[0][2], Rot[1][0], Rot[1][1], Rot[1][2], Rot[2][0], Rot[2][1], Rot[2][2]) trsf.SetRotation(gp_Quaternion(Mat)) shape.Move(TopLoc_Location(trsf)) trsf = gp_Trsf() trsf.SetTranslation( gp_Vec(coord_centres[3], coord_centres[4], coord_centres[5])) shape.Move(TopLoc_Location(trsf)) self.modules[name_body] = shape
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 test_in_place_operators(self): # operator += a = gp_XYZ(1., 2., 3.) self.assertEqual(a.X(), 1.) self.assertEqual(a.Y(), 2.) self.assertEqual(a.Z(), 3.) a += gp_XYZ(4., 5., 6.) self.assertEqual(a.X(), 5.) self.assertEqual(a.Y(), 7.) self.assertEqual(a.Z(), 9.) # operator *= with a scalar b1 = gp_XYZ(2., 4., 5.) self.assertEqual(b1.X(), 2.) self.assertEqual(b1.Y(), 4.) self.assertEqual(b1.Z(), 5.) b1 *= 2 self.assertEqual(b1.X(), 4.) self.assertEqual(b1.Y(), 8.) self.assertEqual(b1.Z(), 10.) # operator *= with a gp_XYZ b2 = gp_XYZ(4., 5., 6.) self.assertEqual(b2.X(), 4.) self.assertEqual(b2.Y(), 5.) self.assertEqual(b2.Z(), 6.) b2 *= gp_XYZ(3., 6., 7.) self.assertEqual(b2.X(), 12.) self.assertEqual(b2.Y(), 30.) self.assertEqual(b2.Z(), 42.) # operator *= with a gp_Mat b3 = gp_XYZ(1., 2., 3.) self.assertEqual(b3.X(), 1.) self.assertEqual(b3.Y(), 2.) self.assertEqual(b3.Z(), 3.) m_ident = gp_Mat() m_ident.SetIdentity() b3 *= m_ident self.assertEqual(b3.X(), 1.) self.assertEqual(b3.Y(), 2.) self.assertEqual(b3.Z(), 3.) # operator -= c = gp_XYZ(3., 2., 1.) self.assertEqual(c.X(), 3.) self.assertEqual(c.Y(), 2.) self.assertEqual(c.Z(), 1.) c -= gp_XYZ(1., 0.5, 1.5) self.assertEqual(c.X(), 2.) self.assertEqual(c.Y(), 1.5) self.assertEqual(c.Z(), -0.5) # operator /= d = gp_XYZ(12., 14., 18.) self.assertEqual(d.X(), 12.) self.assertEqual(d.Y(), 14.) self.assertEqual(d.Z(), 18.) d /= 2. self.assertEqual(d.X(), 6.) self.assertEqual(d.Y(), 7.) self.assertEqual(d.Z(), 9.)
def test_in_place_operators(self) -> None: # operator += a = gp_XYZ(1.0, 2.0, 3.0) self.assertEqual(a.X(), 1.0) self.assertEqual(a.Y(), 2.0) self.assertEqual(a.Z(), 3.0) a += gp_XYZ(4.0, 5.0, 6.0) self.assertEqual(a.X(), 5.0) self.assertEqual(a.Y(), 7.0) self.assertEqual(a.Z(), 9.0) # operator *= with a scalar b1 = gp_XYZ(2.0, 4.0, 5.0) self.assertEqual(b1.X(), 2.0) self.assertEqual(b1.Y(), 4.0) self.assertEqual(b1.Z(), 5.0) b1 *= 2 self.assertEqual(b1.X(), 4.0) self.assertEqual(b1.Y(), 8.0) self.assertEqual(b1.Z(), 10.0) # operator *= with a gp_XYZ b2 = gp_XYZ(4.0, 5.0, 6.0) self.assertEqual(b2.X(), 4.0) self.assertEqual(b2.Y(), 5.0) self.assertEqual(b2.Z(), 6.0) b2 *= gp_XYZ(3.0, 6.0, 7.0) self.assertEqual(b2.X(), 12.0) self.assertEqual(b2.Y(), 30.0) self.assertEqual(b2.Z(), 42.0) # operator *= with a gp_Mat b3 = gp_XYZ(1.0, 2.0, 3.0) self.assertEqual(b3.X(), 1.0) self.assertEqual(b3.Y(), 2.0) self.assertEqual(b3.Z(), 3.0) m_ident = gp_Mat() m_ident.SetIdentity() b3 *= m_ident self.assertEqual(b3.X(), 1.0) self.assertEqual(b3.Y(), 2.0) self.assertEqual(b3.Z(), 3.0) # operator -= c = gp_XYZ(3.0, 2.0, 1.0) self.assertEqual(c.X(), 3.0) self.assertEqual(c.Y(), 2.0) self.assertEqual(c.Z(), 1.0) c -= gp_XYZ(1.0, 0.5, 1.5) self.assertEqual(c.X(), 2.0) self.assertEqual(c.Y(), 1.5) self.assertEqual(c.Z(), -0.5) # operator /= d = gp_XYZ(12.0, 14.0, 18.0) self.assertEqual(d.X(), 12.0) self.assertEqual(d.Y(), 14.0) self.assertEqual(d.Z(), 18.0) d /= 2.0 self.assertEqual(d.X(), 6.0) self.assertEqual(d.Y(), 7.0) self.assertEqual(d.Z(), 9.0)
def test_in_place_operators(self): # operator += a = gp_XYZ(1., 2., 3.) self.assertEqual(a.X(), 1.) self.assertEqual(a.Y(), 2.) self.assertEqual(a.Z(), 3.) a += gp_XYZ(4., 5., 6.) self.assertEqual(a.X(), 5.) self.assertEqual(a.Y(), 7.) self.assertEqual(a.Z(), 9.) # operator *= with a scalar b1 = gp_XYZ(2., 4., 5.) self.assertEqual(b1.X(), 2.) self.assertEqual(b1.Y(), 4.) self.assertEqual(b1.Z(), 5.) b1 *= 2 self.assertEqual(b1.X(), 4.) self.assertEqual(b1.Y(), 8.) self.assertEqual(b1.Z(), 10.) # operator *= with a gp_XYZ b2 = gp_XYZ(4., 5., 6.) self.assertEqual(b2.X(), 4.) self.assertEqual(b2.Y(), 5.) self.assertEqual(b2.Z(), 6.) b2 *= gp_XYZ(3., 6., 7.) self.assertEqual(b2.X(), 12.) self.assertEqual(b2.Y(), 30.) self.assertEqual(b2.Z(), 42.) # operator *= with a gp_Mat b3 = gp_XYZ(1., 2., 3.) self.assertEqual(b3.X(), 1.) self.assertEqual(b3.Y(), 2.) self.assertEqual(b3.Z(), 3.) m_ident = gp_Mat() m_ident.SetIdentity() b3 *= m_ident self.assertEqual(b3.X(), 1.) self.assertEqual(b3.Y(), 2.) self.assertEqual(b3.Z(), 3.) # operator -= c = gp_XYZ(3., 2., 1.) self.assertEqual(c.X(), 3.) self.assertEqual(c.Y(), 2.) self.assertEqual(c.Z(), 1.) c -= gp_XYZ(1., 0.5, 1.5) self.assertEqual(c.X(), 2.) self.assertEqual(c.Y(), 1.5) self.assertEqual(c.Z(), -0.5) # operator /= d = gp_XYZ(12., 14., 18.) self.assertEqual(d.X(), 12.) self.assertEqual(d.Y(), 14.) self.assertEqual(d.Z(), 18.) d /= 2. self.assertEqual(d.X(), 6.) self.assertEqual(d.Y(), 7.) self.assertEqual(d.Z(), 9.)
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 update(links, T, tool=None): for i in range(0, len(links)): display.Context.SetLocation(links[i], TopLoc_Location(T[i])) if tool != None and i == len(links) - 1: M = gp_Mat(0, 0, 1, 0, -1, 0, 1, 0, 0) P = gp_Pnt(100, 0, 0) T_ee = gp_Trsf() T_ee.SetTransformation(gp_Quaternion(M),\ gp_Vec(gp_Pnt(),P)) display.Context.SetLocation( tool, TopLoc_Location(T[i].Multiplied(T_ee))) display.Context.UpdateCurrentViewer()
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 FK(robot_model, joint_var): mod_joint_var = numpy.concatenate([[0], joint_var]) A = robot_model.forward_kinematics(mod_joint_var, True) # A = A[:-1] T = [] for a in A: M = gp_Mat(a[0][0],a[0][1],a[0][2],\ a[1][0],a[1][1],a[1][2],\ a[2][0],a[2][1],a[2][2]) P = gp_Pnt(a[0][3] * 1000, a[1][3] * 1000, a[2][3] * 1000) trsf = gp_Trsf() trsf.SetTransformation(gp_Quaternion(M),\ gp_Vec(gp_Pnt(),P)) T.append(trsf) return T
def reverse_tool_transform(points): M = gp_Mat(-0.955248, -0.295806, 0.0000000000,\ -0.295806, 0.955248, 0.0000000000,\ 0.0000000000, 0.0000000000, -1.00000) P = gp_Pnt(-20.5609, -11.9004, 251.300) tool_trsf = gp_Trsf() tool_trsf.SetTransformation(gp_Quaternion(M),\ gp_Vec(gp_Pnt(),P)) tool_trsf.Invert() M = gp_Mat(0, 0, 1, 0, -1, 0, 1, 0, 0) P = gp_Pnt(100, 0, 0) ee_trsf = gp_Trsf() ee_trsf.SetTransformation(gp_Quaternion(M),\ gp_Vec(gp_Pnt(),P)) ee_trsf.Invert() total_trsf = tool_trsf.Multiplied(ee_trsf) new_points = [] for point in points: new_point = [] M = gp_Mat(point[1].X(),point[2].X(),point[3].X(),\ point[1].Y(),point[2].Y(),point[3].Y(), point[1].Z(),point[2].Z(),point[3].Z()) P = point[0] point_trsf = gp_Trsf() point_trsf.SetTransformation(gp_Quaternion(M),\ gp_Vec(gp_Pnt(),P)) new_point_trsf = point_trsf.Multiplied(total_trsf) M = new_point_trsf.VectorialPart() new_point.append(gp_Pnt(new_point_trsf.TranslationPart())) new_point.append(gp_Vec(M.Column(1)).Normalized()) new_point.append(gp_Vec(M.Column(2)).Normalized()) new_point.append(gp_Vec(M.Column(3)).Normalized()) new_points.append(new_point) return new_points
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 htransform(shape, pnt, vx, vy, vz): vx = vy.Crossed(vz) vy = vz.Crossed(vx) vx.Normalize() vy.Normalize() vz.Normalize() tshape = copy.deepcopy(shape) rot = gp_Mat(vx.X(), vy.X(), vz.X(), vx.Y(), vy.Y(), vz.Y(), vx.Z(), vy.Z(), vz.Z()) quat = gp_Quaternion(rot) disp = gp_Vec(gp_Pnt(0, 0, 0), pnt) trsf = gp_Trsf() trsf.SetTransformation(quat, disp) aniloc = TopLoc_Location(trsf) loc = aniloc * tshape.Location() tshape.Location(loc) return aniloc, tshape
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
# for shpt_lbl_color in shapes_labels_colors: # label, c = shapes_labels_colors[shpt_lbl_color] # display.DisplayColoredShape(shpt_lbl_color, color=Quantity_Color(c.Red(), # c.Green(), # c.Blue(), # Quantity_TOC_RGB)) shp = read_step_file(os.path.join('part_of_sattelate', 'pribore', 'DAV_WS16.STEP')) #BRepOffsetAPI_MakeOffsetShape(shp, 10, 0.1) #kode to rotation trsf = gp_Trsf() # vX = gp_Vec(12, 0, 0) # vY = gp_Vec(0, 12, 0) Mat = gp_Mat(0.5, (0.75**0.5), 0, -(0.75**2), 0.5, 0, 0, 0, 1) trsf.SetRotation(gp_Quaternion(Mat)) shp.Move(TopLoc_Location(trsf)) def measure(shape): bbox = Bnd_Box() #bbox.SetGap(tol) #bbox.SetShape(shape) brepbndlib_Add(shape, bbox) xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() my_box = BRepPrimAPI_MakeBox(gp_Pnt(xmin, ymin, zmin), gp_Pnt(xmax, ymax, zmax)).Shape() #display.SetBackgroundImage('nevers-pont-de-loire.jpg', stretch=True)
def scaleXYZ(x, y, z): gtrsf = gp_GTrsf() gtrsf.SetVectorialPart(gp_Mat(x, 0, 0, 0, y, 0, 0, 0, z)) return GeneralTransformation(gtrsf)
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()