def transform_nonuniformal(brep, factors, vec=[0, 0, 0], copy=False): """Nonuniformly scale brep with respect to pnt by the x y z scaling factors provided in 'factors', and translate by vector 'vec' Parameters ---------- factors : List of factors [Fx, Fy, Fz] Scaling factors with respect to origin (0,0,0) vec : List of x,y,z or gp_Vec the translation vector (default is [0,0,0]) Notes ----- * Only tested on 3d shapes * Assumes factors are define with respect to the origin (0,0,0) """ assert (len(factors) == 3), \ ("factors should have [Fx, Fy, Fz] scaling factors: Found length ", len(factors)) M = np.diag(factors).flatten() trns_M = gp_Mat(*M) try: V = gp_XYZ(*vec) except NotImplementedError: V = gp_XYZ(vec.X(), vec.Y(), vec.Z()) trns = gp_GTrsf(trns_M, V) brep_trns = BRepBuilderAPI_GTransform(brep, trns, copy) brep_trns.Build() return brep_trns.Shape()
def make_ellipsoid(focus1, focus2, major_axis): """ @param focus1: length 3 sequence giving first focus location @param focus2: length 3 sequence giving second focus location @param path_length: major axis length """ f1 = numpy.asarray(focus1) f2 = numpy.asarray(focus2) direction = -(f1 - f2) centre = (f1 + f2)/2. sep = numpy.sqrt((direction**2).sum()) minor_axis = numpy.sqrt( major_axis**2 - (sep/2.)**2 ) sphere = BRepPrimAPI.BRepPrimAPI_MakeSphere(minor_axis) scale = gp.gp_GTrsf() scale.SetValue(3,3, major_axis/minor_axis) ellipse = BRepBuilderAPI.BRepBuilderAPI_GTransform(sphere.Shape(), scale) loc = gp.gp_Ax3() loc.SetLocation(gp.gp_Pnt(*centre)) loc.SetDirection(gp.gp_Dir(*direction)) tr = gp.gp_Trsf() tr.SetTransformation(loc, gp.gp_Ax3()) trans = BRepBuilderAPI.BRepBuilderAPI_Transform(ellipse.Shape(), tr) shape = toshape(trans) return shape
def transform_nonuniformal(brep, factors, vec=[0, 0, 0], copy=False): """Nonuniformly scale brep with respect to pnt by the x y z scaling factors provided in 'factors', and translate by vector 'vec' Parameters ---------- factors : List of factors [Fx, Fy, Fz] Scaling factors with respect to origin (0,0,0) vec : List of x,y,z or gp_Vec the translation vector (default is [0,0,0]) Notes ----- * Only tested on 3d shapes * Assumes factors are define with respect to the origin (0,0,0) """ assert(len(factors) == 3),\ ("factors should have [Fx, Fy, Fz] scaling factors: Found length ", len(factors)) M = np.diag(factors).flatten() trns_M = gp_Mat(*M) try: V = gp_XYZ(*vec) except NotImplementedError: V = gp_XYZ(vec.X(), vec.Y(), vec.Z()) trns = gp_GTrsf(trns_M, V) brep_trns = BRepBuilderAPI_GTransform(brep, trns, copy) brep_trns.Build() return brep_trns.Shape()
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 transformGeometry(self, tMatrix): """ tMatrix is a matrix object. returns a copy of the object, but with geometry transformed insetad of just rotated. WARNING: transformGeometry will sometimes convert lines and circles to splines, but it also has the ability to handle skew and stretching transformations. If your transformation is only translation and rotation, it is safer to use transformShape, which doesnt change the underlying type of the geometry, but cannot handle skew transformations """ r = Shape.cast( BRepBuilderAPI_GTransform(self.wrapped, gp_GTrsf(tMatrix.wrapped), True).Shape()) r.forConstruction = self.forConstruction return r
def uniform_scale(occshape, tx, ty, tz, ref_pypt): moved_shape = move(ref_pypt, (0, 0, 0), occshape) 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 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
from OCC.gp import gp_Pnt, gp_XYZ, gp_Mat, gp_GTrsf from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox from OCC.BRepBuilderAPI import BRepBuilderAPI_GTransform from OCC.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() assert not common.IsNull() 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 scale(sx, sy, sz): t = gp.gp_GTrsf() t.SetValue(1, 1, sx) t.SetValue(2, 2, sy) t.SetValue(3, 3, sz) return t
shape = BRepPrimAPI_MakeCylinder(20, 40).Shape() # This options creates unwanted polygons ''' xform = gp_Trsf() xform.SetValues( 1.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, Precision_Angular(), Precision_Confusion() ); brep = BRepBuilderAPI_GTransform(shape, gp_GTrsf(xform), False) ''' # This options works as desired xform = gp_GTrsf() xform.SetVectorialPart(gp_Mat( 1.5, 0, 0, 0, 1, 0, 0, 0, 1, )) brep = BRepBuilderAPI_GTransform(shape, xform, False) brep.Build() shape = brep.Shape()
def scale(sx, sy, sz): t = gp.gp_GTrsf() t.SetValue(1,1,sx) t.SetValue(2,2,sy) t.SetValue(3,3,sz) return t
from OCC import StlAPI shape = BRepPrimAPI_MakeCylinder(20,40).Shape() # This options creates unwanted polygons ''' xform = gp_Trsf() xform.SetValues( 1.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, Precision_Angular(), Precision_Confusion() ); brep = BRepBuilderAPI_GTransform(shape, gp_GTrsf(xform), False) ''' # This options works as desired xform = gp_GTrsf() xform.SetVectorialPart(gp_Mat( 1.5, 0, 0, 0, 1, 0, 0, 0, 1, )) brep = BRepBuilderAPI_GTransform(shape, xform, False) brep.Build() shape = brep.Shape() stl_writer = StlAPI.StlAPI_Writer() stl_writer.Write(shape, 'scaled-cylinder.stl')
def brep_feat_extrusion_protrusion(event=None): # Extrusion S = BRepPrimAPI_MakeBox(400., 250., 300.).Shape() faces = Topo(S).faces() F = next(faces) surf1 = BRep_Tool_Surface(F) Pl1 = Handle_Geom_Plane_DownCast(surf1).GetObject() 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 = Handle_Geom_Plane_DownCast(surf2).GetObject() 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.RefineEdges() fused.Build() print('Boolean operation error status:', fused.ErrorStatus()) display.DisplayShape(fused.Shape()) display.FitAll()