def draft_angle(event=None): S = BRepPrimAPI_MakeBox(200., 300., 150.).Shape() adraft = BRepOffsetAPI_DraftAngle(S) topExp = TopExp_Explorer() topExp.Init(S, TopAbs_FACE) while topExp.More(): face = topods_Face(topExp.Current()) surf = Handle_Geom_Plane_DownCast(BRep_Tool_Surface(face)).GetObject() dirf = surf.Pln().Axis().Direction() ddd = gp_Dir(0, 0, 1) if dirf.IsNormal(ddd, precision_Angular()): adraft.Add(face, ddd, math.radians(15), gp_Pln(gp_Ax3(gp_XOY()))) topExp.Next() adraft.Build() display.DisplayShape(adraft.Shape(), update=True)
def sample_point(face): # randomly choose a point from F u_min, u_max, v_min, v_max = breptools_UVBounds(face) u = random.uniform(u_min, u_max) v = random.uniform(v_min, v_max) itool = IntTools_FClass2d(face, 1e-6) while itool.Perform(gp_Pnt2d(u,v)) != 0: print('outside') u = random.uniform(u_min, u_max) v = random.uniform(v_min, v_max) P = BRepAdaptor_Surface(face).Value(u, v) # the normal surf = BRep_Tool_Surface(face) D = GeomLProp_SLProps(surf,u,v,1,0.01).Normal() if face.Orientation() == TopAbs_REVERSED: D.Reverse() return P, D
def brepfeat_prism(event=None): box = BRepPrimAPI_MakeBox(400, 250, 300).Shape() faces = TopologyExplorer(box).faces() for i in range(5): face = next(faces) srf = BRep_Tool_Surface(face) c = gp_Circ2d(gp_Ax2d(gp_Pnt2d(200, 130), gp_Dir2d(1, 0)), 75) circle = Geom2d_Circle(c) wire = BRepBuilderAPI_MakeWire() wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, 0., pi).Edge()) wire.Add(BRepBuilderAPI_MakeEdge(circle, srf, pi, 2. * pi).Edge()) wire.Build() display.DisplayShape(wire.Wire()) mkf = BRepBuilderAPI_MakeFace() mkf.Init(srf, False, 1e-6) mkf.Add(wire.Wire()) mkf.Build() new_face = mkf.Face() breplib_BuildCurves3d(new_face) display.DisplayShape(new_face) prism = BRepFeat_MakeDPrism(box, mkf.Face(), face, 100, True, True) prism.Perform(400) assert prism.IsDone() display.EraseAll() display.DisplayShape(prism.Shape()) display.DisplayColoredShape(wire.Wire(), 'RED') display.FitAll()
def surface(self): if self._srf is None or self.is_dirty: self._h_srf = BRep_Tool_Surface(self) self._srf = self._h_srf.GetObject() return self._srf
class Face(TopoDS_Face, BaseObject): """high level surface API object is a Face if part of a Solid otherwise the same methods do apply, apart from the topology obviously """ def __init__(self, face): ''' ''' assert isinstance(face, TopoDS_Face), 'need a TopoDS_Face, got a %s' % face.__class__ assert not face.IsNull() super(Face, self).__init__() BaseObject.__init__(self, 'face') # we need to copy the base shape using the following three # lines assert self.IsNull() self.TShape(face.TShape()) self.Location(face.Location()) self.Orientation(face.Orientation()) assert not self.IsNull() # cooperative classes self.DiffGeom = DiffGeomSurface(self) # STATE; whether cooperative classes are yet initialized self._curvature_initiated = False self._geometry_lookup_init = False #=================================================================== # properties #=================================================================== self._h_srf = None self._srf = None self._adaptor = None self._adaptor_handle = None self._classify_uv = None # cache the u,v classifier, no need to rebuild for every sample self._topo = None # aliasing of useful methods def is_u_periodic(self): return self.adaptor.IsUPeriodic() def is_v_periodic(self): return self.adaptor.IsVPeriodic() def is_u_closed(self): return self.adaptor.IsUClosed() def is_v_closed(self): return self.adaptor.IsVClosed() def is_u_rational(self): return self.adaptor.IsURational() def is_v_rational(self): return self.adaptor.IsVRational() def u_degree(self): return self.adaptor.UDegree() def v_degree(self): return self.adaptor.VDegree() def u_continuity(self): return self.adaptor.UContinuity() def v_continuity(self): return self.adaptor.VContinuity() def domain(self): '''the u,v domain of the curve :return: UMin, UMax, VMin, VMax ''' return breptools_UVBounds(self) def mid_point(self): """ :return: the parameter at the mid point of the face, and its corresponding gp_Pnt """ u_min, u_max, v_min, v_max = self.domain() u_mid = (u_min + u_max) / 2. v_mid = (v_min + v_max) / 2. return ((u_mid, v_mid), self.adaptor.Value(u_mid, v_mid)) @property def topo(self): if self._topo is not None: return self._topo else: self._topo = Topo(self) return self._topo @property def surface(self): if self._srf is None or self.is_dirty: self._h_srf = BRep_Tool_Surface(self) self._srf = self._h_srf.GetObject() return self._srf @property def surface_handle(self): if self._h_srf is None or self.is_dirty: self.surface # force building handle return self._h_srf @property def adaptor(self): if self._adaptor is not None and not self.is_dirty: pass else: self._adaptor = BRepAdaptor_Surface(self) self._adaptor_handle = BRepAdaptor_HSurface() self._adaptor_handle.Set(self._adaptor) return self._adaptor @property def adaptor_handle(self): if self._adaptor_handle is not None and not self.is_dirty: pass else: self.adaptor return self._adaptor_handle def is_closed(self): sa = ShapeAnalysis_Surface(self.surface_handle) # sa.GetBoxUF() return sa.IsUClosed(), sa.IsVClosed() def is_planar(self, tol=TOLERANCE): '''checks if the surface is planar within a tolerance :return: bool, gp_Pln ''' print(self.surface_handle) is_planar_surface = GeomLib_IsPlanarSurface(self.surface_handle, tol) return is_planar_surface.IsPlanar() def is_trimmed(self): """ :return: True if the Wire delimiting the Face lies on the bounds of the surface if this is not the case, the wire represents a contour that delimits the face [ think cookie cutter ] and implies that the surface is trimmed """ _round = lambda x: round(x, 3) a = map(_round, breptools_UVBounds(self)) b = map(_round, self.adaptor.Surface().Surface().GetObject().Bounds()) if a != b: print('a,b', a, b) return True return False def on_trimmed(self, u, v): '''tests whether the surface at the u,v parameter has been trimmed ''' if self._classify_uv is None: self._classify_uv = BRepTopAdaptor_FClass2d(self, 1e-9) uv = gp_Pnt2d(u, v) if self._classify_uv.Perform(uv) == TopAbs_IN: return True else: return False def parameter_to_point(self, u, v): '''returns the coordinate at u,v ''' return self.surface.Value(u, v) def point_to_parameter(self, pt): ''' returns the uv value of a point on a surface @param pt: ''' sas = ShapeAnalysis_Surface(self.surface_handle) uv = sas.ValueOfUV(pt, self.tolerance) return uv.Coord() def continuity_edge_face(self, edge, face): """ compute the continuity between two faces at :edge: :param edge: an Edge or TopoDS_Edge from :face: :param face: a Face or TopoDS_Face :return: bool, GeomAbs_Shape if it has continuity, otherwise False, None """ bt = BRep_Tool() if bt.HasContinuity(edge, self, face): continuity = bt.Continuity(edge, self, face) return True, continuity else: return False, None #=========================================================================== # Surface.project # project curve, point on face #=========================================================================== def project_vertex(self, pnt, tol=TOLERANCE): '''projects self with a point, curve, edge, face, solid method wraps dealing with the various topologies if other is a point: returns uv, point ''' if isinstance(pnt, TopoDS_Vertex): pnt = BRep_Tool.Pnt(pnt) proj = GeomAPI_ProjectPointOnSurf(pnt, self.surface_handle, tol) uv = proj.LowerDistanceParameters() proj_pnt = proj.NearestPoint() return uv, proj_pnt def project_curve(self, other): # this way Geom_Circle and alike are valid too if (isinstance(other, TopoDS_Edge) or isinstance(other, Geom_Curve) or issubclass(other, Geom_Curve)): # convert edge to curve first, last = topexp.FirstVertex(other), topexp.LastVertex(other) lbound, ubound = BRep_Tool().Parameter(first, other), BRep_Tool().Parameter(last, other) other = BRep_Tool.Curve(other, lbound, ubound).GetObject() return geomprojlib.Project(other, self.surface_handle) def project_edge(self, edg): if hasattr(edg, 'adaptor'): return self.project_curve(self, self.adaptor) return self.project_curve(self, to_adaptor_3d(edg)) def iso_curve(self, u_or_v, param): """ get the iso curve from a u,v + parameter :param u_or_v: :param param: :return: """ uv = 0 if u_or_v == 'u' else 1 iso = Adaptor3d_IsoCurve(self.adaptor_handle.GetHandle(), uv, param) return iso def edges(self): return [Edge(i) for i in WireExplorer(next(self.topo.wires())).ordered_edges()] def __repr__(self): return self.name def __str__(self): return self.__repr__()
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 surface(self): if self._srf is None or self.is_dirty: self._srf = BRep_Tool_Surface(self) return self._srf
def surf_dev(face, dx=50, dy=50): geom = BRep_Tool_Surface(face).GetObject() p0, v0_x, v0_y, v0_z = normal(geom, 0.0, 0.0) p1, v1_x, v1_y, v1_z = normal(geom, dx, 0.0) p2, v2_x, v2_y, v2_z = normal(geom, 0.0, dy)
my_renderer = JupyterRenderer() # Generation of a box # In[4]: S = BRepPrimAPI_MakeBox(20., 30., 15.).Shape() # Apply a draft angle. # In[5]: adraft = BRepOffsetAPI_DraftAngle(S) topExp = TopExp_Explorer() topExp.Init(S, TopAbs_FACE) while topExp.More(): face = topods_Face(topExp.Current()) surf = Geom_Plane.DownCast(BRep_Tool_Surface(face)) dirf = surf.Pln().Axis().Direction() ddd = gp_Dir(0, 0, 1) if dirf.IsNormal(ddd, precision_Angular()): adraft.Add(face, ddd, math.radians(15), gp_Pln(gp_Ax3(gp_XOY()))) topExp.Next() adraft.Build() shp = adraft.Shape() # In[6]: my_renderer.DisplayShape(shp, render_edges=True, update=True)
def geom_plane_from_face(aFace): """ Returns the geometric plane entity from a planar surface """ return Handle_Geom_Plane.DownCast(BRep_Tool_Surface(aFace)).GetObject()
# obj.show_ball() axs = gp_Ax3(gp_Pnt(0, 0, 0), gp_Dir(1, 1, 1)) elp = gen_ellipsoid(axs, [10, 20, 30]) obj.display.DisplayShape(elp, transparency=0.7, color="BLUE") obj.show_axs_pln(axs, scale=20) axs = gp_Ax3(gp_Pnt(30, 0, 0), gp_Dir(1, 1, 0)) elp = gen_ellipsoid(axs, [10, 20, 30]) obj.display.DisplayShape(elp, transparency=0.7, color="BLUE") obj.show_axs_pln(axs, scale=20) #elp = gen_ellipsoid_geom(axs, [10, 20, 30]) top_api = Topo(elp) print(top_api.number_of_faces()) for face in top_api.faces(): elp_face = face print(elp_face) elp_surf = BRep_Tool_Surface(elp_face) print(elp_surf) lin = Geom_Line(gp_Ax1(axs.Location(), gp_Dir(0, 1, 1))) api = GeomAPI_IntCS(lin, elp_surf) obj.display.DisplayShape(lin) print(api.Point(1)) print(api.Point(2)) obj.show()