def face_is_plane(face): ''' Returns True if the TopoDS_Shape is a plane, False otherwise ''' hs = BRep_Tool_Surface(face) downcast_result = Geom_Plane().DownCast(hs) # the handle is null if downcast failed or is not possible, # that is to say the face is not a plane if downcast_result.IsNull(): return False else: return True
def __init__(self, size, face=None, faceU=None, ax3=None): # gp_Ax3 of XYZ coord system origin = gp_Pnt(0, 0, 0) wDir = gp_Dir(0, 0, 1) uDir = gp_Dir(1, 0, 0) vDir = gp_Dir(0, 1, 0) xyzAx3 = gp_Ax3(origin, wDir, uDir) if (not face and not ax3): # create default wp (in XY plane at 0,0,0) axis3 = xyzAx3 gpPlane = gp_Pln(xyzAx3) self.gpPlane = gpPlane # type: gp_Pln self.plane = Geom_Plane(gpPlane) # type: Geom_Plane elif face: # create workplane on face, uDir defined by faceU wDir = face_normal(face) # from OCCUtils.Construct module props = GProp_GProps() brepgprop_SurfaceProperties(face, props) origin = props.CentreOfMass() ''' surface = BRep_Tool_Surface(face) # type: Handle_Geom_Surface plane = Handle_Geom_Plane.DownCast(surface).GetObject() # type: Geom_Plane gpPlane = plane.Pln() # type: gp_Pln origin = gpPlane.Location() # type: gp_Pnt ''' uDir = face_normal(faceU) # from OCCUtils.Construct module axis3 = gp_Ax3(origin, wDir, uDir) vDir = axis3.YDirection() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane elif ax3: axis3 = ax3 uDir = axis3.XDirection() vDir = axis3.YDirection() wDir = axis3.Axis().Direction() origin = axis3.Location() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane self.Trsf = gp_Trsf() self.Trsf.SetTransformation(axis3) self.Trsf.Invert() self.origin = origin self.uDir = uDir self.vDir = vDir self.wDir = wDir self.face = face self.size = size self.border = self.makeWpBorder(self.size) self.clList = [] # List of 'native' construction lines self.clineList = [] # List of pyOCC construction lines self.ccircList = [] # List of pyOCC construction circles self.wireList = [] # List of pyOCC wires self.wire = None self.hvcl((0, 0)) # Make H-V clines through origin self.accuracy = 0.001 # min distance between two points
def faircurve(event=None): pt1 = gp_Pnt2d(0., 0.) pt2 = gp_Pnt2d(0., 120.) height = 100. pl = Geom_Plane(gp_Pln()) for i in range(0, 40): # TODO: the parameter slope needs to be visualized slope = i / 100. bc = batten_curve(pt1, pt2, height, slope, math.radians(i), math.radians(-i)) display.EraseAll() edge = BRepBuilderAPI_MakeEdge(bc, pl.GetHandle()).Edge() display.DisplayShape(edge, update=True) time.sleep(0.21)
def get_deg(axs, vec): vx = dir_to_vec(axs.XDirection()) vy = dir_to_vec(axs.YDirection()) vz = dir_to_vec(axs.Direction()) pln_x = Geom_Plane(axs.Location(), axs.YDirection()) pln_y = Geom_Plane(axs.Location(), axs.XDirection()) vec_p = gp_Pnt((gp_Vec(axs.Location().XYZ()) + vec).XYZ()) pnt_x = GeomAPI_ProjectPointOnSurf(vec_p, pln_x).Point(1) pnt_y = GeomAPI_ProjectPointOnSurf(vec_p, pln_y).Point(1) vec_x = gp_Vec(axs.Location(), pnt_x) vec_y = gp_Vec(axs.Location(), pnt_y) deg_x = vec_x.AngleWithRef(vz, vy) deg_y = vec_y.AngleWithRef(vz, vx) print(np.rad2deg(deg_x), np.rad2deg(deg_y)) return deg_x, deg_y
def __init__(self, size, face=None, faceU=None, ax3=None): # gp_Ax3 of XYZ coord system origin = gp_Pnt(0, 0, 0) wDir = gp_Dir(0, 0, 1) uDir = gp_Dir(1, 0, 0) vDir = gp_Dir(0, 1, 0) xyzAx3 = gp_Ax3(origin, wDir, uDir) if (not face and not ax3): # create default wp (in XY plane at 0,0,0) axis3 = xyzAx3 gpPlane = gp_Pln(xyzAx3) self.gpPlane = gpPlane # type: gp_Pln self.plane = Geom_Plane(gpPlane) # type: Geom_Plane elif face: # create workplane on face, uDir defined by faceU wDir = face_normal(face) # from OCCUtils.Construct module props = GProp_GProps() brepgprop_SurfaceProperties(face, props) origin = props.CentreOfMass() uDir = face_normal(faceU) # from OCCUtils.Construct module axis3 = gp_Ax3(origin, wDir, uDir) vDir = axis3.YDirection() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane elif ax3: axis3 = ax3 uDir = axis3.XDirection() vDir = axis3.YDirection() wDir = axis3.Axis().Direction() origin = axis3.Location() self.gpPlane = gp_Pln(axis3) self.plane = Geom_Plane(self.gpPlane) # type: Geom_Plane self.Trsf = gp_Trsf() self.Trsf.SetTransformation(axis3) self.Trsf.Invert() self.origin = origin self.uDir = uDir self.vDir = vDir self.wDir = wDir self.wVec = gp_Vec(wDir) self.face = face self.size = size self.border = self.makeWpBorder(self.size) self.clines = set() # set of c-lines with (a, b, c) coefficients self.ccircs = set() # set of c-circs with (pc, r) coefficients self.edgeList = [] # List of profile lines type: <TopoDS_Edge> self.wire = None self.accuracy = 1e-6 # min distance between two points self.hvcl((0, 0)) # Make H-V clines through origin
def brep_feat_rib(event=None): mkw = BRepBuilderAPI_MakeWire() mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(0., 0., 0.), gp_Pnt(200., 0., 0.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(200., 0., 0.), gp_Pnt(200., 0., 50.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(200., 0., 50.), gp_Pnt(50., 0., 50.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 0., 50.), gp_Pnt(50., 0., 200.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 0., 200.), gp_Pnt(0., 0., 200.)).Edge()) mkw.Add(BRepBuilderAPI_MakeEdge(gp_Pnt(0., 0., 200.), gp_Pnt(0., 0., 0.)).Edge()) S = BRepPrimAPI_MakePrism(BRepBuilderAPI_MakeFace(mkw.Wire()).Face(), gp_Vec(gp_Pnt(0., 0., 0.), gp_Pnt(0., 100., 0.))) display.EraseAll() # display.DisplayShape(S.Shape()) W = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(gp_Pnt(50., 45., 100.), gp_Pnt(100., 45., 50.)).Edge()) aplane = Geom_Plane(0., 1., 0., -45.) aform = BRepFeat_MakeLinearForm(S.Shape(), W.Wire(), aplane, gp_Vec(0., 10., 0.), gp_Vec(0., 0., 0.), 1, True) aform.Perform() display.DisplayShape(aform.Shape()) display.FitAll()
def __init__(self): self.data = [] self.curHilightedObj = None self.ProjectOnCurve = Geom2dAPI_ProjectPointOnCurve() self.curCoordinateSystem = gp_Ax3(gp.XOY()) self.FirstEdge = TopoDS_Edge() self.SecondEdge = TopoDS_Edge() self.curPnt2d = gp.Origin2d() self.objectPnt2d = gp.Origin2d() self.bestPnt2d = gp.Origin2d() self.findbestPnt2d = False self.firstDisplay = True self.myGeom_Point = Geom_CartesianPoint(gp.Origin()) self.myAIS_Point = AIS_Point(self.myGeom_Point) self.myAIS_Point.SetColor(Quantity_Color(Quantity_NOC_YELLOW)) self.myAIS_Point.SetWidth(5.0) self.myAIS_Point.SetMarker(Aspect_TOM_O_POINT) self.minimumSnapDistance = MINIMUMSNAP self.minDistance = 0 self.curDistance = 0 self.curGeom2d_Point = Geom2d_CartesianPoint(self.curPnt2d) self.myPlane = Geom_Plane(self.curCoordinateSystem)
def extrusion(event=None): # Make a box Box = BRepPrimAPI_MakeBox(400., 250., 300.) S = Box.Shape() # Choose the first Face of the box F = next(TopologyExplorer(S).faces()) surf = BRep_Tool_Surface(F) # Make a plane from this face Pln = Geom_Plane.DownCast(surf) # Get the normal of this plane. This will be the direction of extrusion. D = Pln.Axis().Direction() # Inverse normal D.Reverse() # Create the 2D planar sketch MW = BRepBuilderAPI_MakeWire() p1 = gp_Pnt2d(200., -100.) p2 = gp_Pnt2d(100., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge1 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge1.Edge()) p1 = p2 p2 = gp_Pnt2d(100., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge2 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge2.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -200.) aline = GCE2d_MakeLine(p1, p2).Value() Edge3 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge3.Edge()) p1 = p2 p2 = gp_Pnt2d(200., -100.) aline = GCE2d_MakeLine(p1, p2).Value() Edge4 = BRepBuilderAPI_MakeEdge(aline, surf, 0., p1.Distance(p2)) MW.Add(Edge4.Edge()) # Build Face from Wire. NB: a face is required to generate a solid. MKF = BRepBuilderAPI_MakeFace() MKF.Init(surf, False, 1e-6) MKF.Add(MW.Wire()) FP = MKF.Face() breplib_BuildCurves3d(FP) MKP = BRepFeat_MakePrism(S, FP, F, D, False, True) MKP.Perform(200.) # TODO MKP completes, seeing a split operation but no extrusion assert MKP.IsDone() res1 = MKP.Shape() display.EraseAll() display.DisplayColoredShape(res1, 'BLUE') display.FitAll()
def face_is_plane(face): """ Returns True if the TopoDS_Shape is a plane, False otherwise """ hs = BRep_Tool_Surface(face) downcast_result = Geom_Plane.DownCast(hs) # The handle is null if downcast failed or is not possible, that is to say the face is not a plane if downcast_result is None: return False else: return True
def faircurve(self, event=None): pl = Geom_Plane(gp_Pln()) for i in range(0, 40): # TODO: the parameter slope needs to be visualized slope = i / 100. bc = self.batten_curve(slope, math.radians(i), math.radians(-i)) self.display.EraseAll() edge = BRepBuilderAPI_MakeEdge(bc, pl).Edge() self.display.DisplayShape(edge, update=True) time.sleep(0.21)
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 = 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() display.DisplayShape(adraft.Shape(), update=True)
def _unify_faces_array(input): ret = [] fset = {} for i in input: surface = BRep_Tool.Surface(i.Face()) adaptor_surface = BRepAdaptor_Surface(i.Face()) surface_type = adaptor_surface.GetType() if surface_type == GeomAbs_Plane: pln = Geom_Plane.DownCast(surface) pln0 = pln.Pln() found = False for key, arr in fset.items(): pnt = gp_Pnt() key.D0(0, 0, pnt) pln1 = key.Pln() dir0 = pln0.Axis().Direction() dir1 = pln1.Axis().Direction() if (dir0.IsEqual(dir1, 0.00001) and abs(pln0.Distance(pln1.Axis().Location())) < 0.0000001 and abs(pln1.Distance( pln0.Axis().Location())) < 0.0000001): found = True arr.append(i) break if found == False: fset[pln] = [i] else: ret.append(i) continue for key, arr in fset.items(): farr = _union(arr) ret.append(_unify_face(farr)) return ret
def beziercurve(): # the first bezier curve array = TColgp_Array1OfPnt(1, 5) array.SetValue(1, gp_Pnt(0, 0,-5)) array.SetValue(2, gp_Pnt(1, 2,1)) array.SetValue(3, gp_Pnt(2, 3,2)) array.SetValue(4, gp_Pnt(4, 3,-2)) array.SetValue(5, gp_Pnt(10, 5,-2)) beziercurve = Geom_BezierCurve(array) # the first bezier curve array1 = TColgp_Array1OfPnt(1, 5) array1.SetValue(1, gp_Pnt(0, 0, -5)) array1.SetValue(2, gp_Pnt(2, -2, 1)) array1.SetValue(3, gp_Pnt(2, -3, 2)) array1.SetValue(4, gp_Pnt(-4, 1, -2)) array1.SetValue(5, gp_Pnt(1, 5, -2)) beziercurve1 = Geom_BezierCurve(array1) array2 = TColgp_Array1OfPnt(1, 5) array2.SetValue(1, gp_Pnt(0, 0, -5)) array2.SetValue(2, gp_Pnt(-2, 2, 1)) array2.SetValue(3, gp_Pnt(2, -2, 2)) array2.SetValue(4, gp_Pnt(-4, 8, -2)) array2.SetValue(5, gp_Pnt(1, 5, -9)) beziercurve2 = Geom_BezierCurve(array2) plane = Geom_Plane(gp_Pnt(0.0, 0.0, 0.0), gp_Dir(0, 1, 1)) display.DisplayShape(plane, color='RED') surface1=GeomFill_BezierCurves(beziercurve,beziercurve1,beziercurve2,GeomFill_CurvedStyle) surface2 = GeomFill_BezierCurves(beziercurve, beziercurve1, GeomFill_StretchStyle) surface3 = GeomFill_BezierCurves(beziercurve, beziercurve1, GeomFill_CoonsStyle) bounds = True toldegen = 1e-6 face = BRepBuilderAPI_MakeFace() face.Init(surface1.Surface(), bounds, toldegen) face.Build() shapes = face.Shape() fix=AIS_FixRelation(shapes,plane) display.Context.Display(fix, True) display.DisplayShape(shapes, color="RED")
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 Geom_Plane.DownCast(BRep_Tool_Surface(aFace))
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()
from OCC.Core.TCollection import TCollection_ExtendedString from OCC.Core.Prs3d import * display, start_display, add_menu, add_function_to_menu = init_display() axis = Geom_Axis2Placement(gp.ZOX()) triehedron = AIS_Trihedron(axis) triehedron.SetXAxisColor(Quantity_Color(Quantity_NOC_RED)) triehedron.SetYAxisColor(Quantity_Color(Quantity_NOC_GREEN)) triehedron.SetAxisColor(Quantity_Color(Quantity_NOC_BLUE1)) context: AIS_InteractiveContext = display.Context drawer = triehedron.Attributes() context.Display(triehedron, 0, 3, True) plane = Geom_Plane(gp_Pnt(0.0, 0.0, 0.0), gp_Dir(0, 1, 0)) ais_plane1 = AIS_Plane(plane, True) ais_plane1.SetColor(Quantity_Color(Quantity_NOC_GRAY)) ais_plane1.SetTypeOfSensitivity(Select3D_TOS_INTERIOR) asp = Prs3d_LineAspect(Quantity_Color(Quantity_NOC_GREEN), 1, 10) ais_plane1.SetAspect(asp) display.Context.Display(ais_plane1, True) text = AIS_TextLabel() text.SetText(TCollection_ExtendedString("ARE YOUR SURE")) text.SetPosition(gp_Pnt(0, 10, 0)) text.SetColor(Quantity_Color(Quantity_NOC_GREEN)) display.Context.Display(text, True) start_display()
def __init__(self, p: numpy, v: numpy): self.plane gp = OccPoint(p) gv = OccVector(v) self.plane = Geom_Plane(gp, gv)