def recognize_face(a_face): """ Takes a TopoDS shape and tries to identify its nature whether it is a plane a cylinder a torus etc. if a plane, returns the normal if a cylinder, returns the radius """ surf = BRepAdaptor_Surface(a_face, True) surf_type = surf.GetType() if surf_type == GeomAbs_Plane: print("--> plane") # look for the properties of the plane # first get the related gp_Pln gp_pln = surf.Plane() location = gp_pln.Location() # a point of the plane normal = gp_pln.Axis().Direction() # the plane normal # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Normal (global coordinates)", normal.X(), normal.Y(), normal.Z()) elif surf_type == GeomAbs_Cylinder: print("--> cylinder") # look for the properties of the cylinder # first get the related gp_Cyl gp_cyl = surf.Cylinder() location = gp_cyl.Location() # a point of the axis axis = gp_cyl.Axis().Direction() # the cylinder axis # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Axis (global coordinates)", axis.X(), axis.Y(), axis.Z()) else: # TODO there are plenty other type that can be checked # see documentation for the BRepAdaptor class # https://www.opencascade.com/doc/occt-6.9.1/refman/html/class_b_rep_adaptor___surface.html print("not implemented")
def recognize_face(self, a_face): """ Takes a TopoDS shape and tries to identify its nature whether it is a plane a cylinder a torus etc. if a plane, returns the normal if a cylinder, returns the radius """ surf = BRepAdaptor_Surface(a_face, True) surf_type = surf.GetType() if surf_type == GeomAbs_Plane: print("--> plane") # look for the properties of the plane # first get the related gp_Pln gp_pln = surf.Plane() location = gp_pln.Location() # a point of the plane normal = gp_pln.Axis().Direction() # the plane normal x_axis = gp_pln.XAxis().Direction() # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Normal (global coordinates)", normal.X(), normal.Y(), normal.Z()) print("--> X_axis", x_axis.X(), x_axis.Y(), x_axis.Z()) centre = [location.X(), location.Y(), location.Z()] normal = [normal.X(), normal.Y(), normal.Z()] point_coords = [ centre[0] + normal[0], centre[1] + normal[1], centre[2] + normal[2] ] pnt = gp_Pnt(point_coords[0], point_coords[1], point_coords[2]) _in_solid = BRepClass3d_SolidClassifier(self.frame, pnt, 1e-5) if _in_solid.State() == 0: normal = [-normal[0], -normal[1], -normal[2]] print(point_coords) X_axis = [x_axis.X(), x_axis.Y(), x_axis.Z()] return (centre, normal, X_axis) elif surf_type == GeomAbs_Cylinder: print("--> cylinder") # look for the properties of the cylinder # first get the related gp_Cyl gp_cyl = surf.Cylinder() location = gp_cyl.Location() # a point of the axis axis = gp_cyl.Axis().Direction() # the cylinder axis # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Axis (global coordinates)", axis.X(), axis.Y(), axis.Z()) else: # TODO there are plenty other type that can be checked # see documentation for the BRepAdaptor class # https://www.opencascade.com/doc/occt-6.9.1/refman/html/class_b_rep_adaptor___surface.html print("not implemented")
def recognize_face(topods_face): """returns True if the TopoDS_Face is a planar surface""" if not isinstance(topods_face, TopoDS_Face): return "Not a face", None, None surf = BRepAdaptor_Surface(topods_face, True) surf_type = surf.GetType() if surf_type == GeomAbs_Plane: kind = "Plane" # look for the properties of the plane # first get the related gp_Pln gp_pln = surf.Plane() location = gp_pln.Location() # a point of the plane normal = gp_pln.Axis().Direction() # the plane normal tuple_to_return = (kind, location, normal) elif surf_type == GeomAbs_Cylinder: kind = "Cylinder" # look for the properties of the cylinder # first get the related gp_Cyl gp_cyl = surf.Cylinder() location = gp_cyl.Location() # a point of the axis axis = gp_cyl.Axis().Direction() # the cylinder axis # then export location and normal to the console output tuple_to_return = (kind, location, axis) elif surf_type == GeomAbs_Cone: kind = "Cone" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_Sphere: kind = "Sphere" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_Torus: kind = "Torus" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_BezierSurface: kind = "Bezier" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_BSplineSurface: kind = "BSpline" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_SurfaceOfRevolution: kind = "Revolution" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_SurfaceOfExtrusion: kind = "Extrusion" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_OffsetSurface: kind = "Offset" tuple_to_return = (kind, None, None) elif surf_type == GeomAbs_OtherSurface: kind = "Other" tuple_to_return = (kind, None, None) else: tuple_to_return = ("Unknwon", None, None) return tuple_to_return
def pln_on_face(self, face=TopoDS_Face()): face_adaptor = BRepAdaptor_Surface(face) face_trf = face_adaptor.Trsf() face_pln = face_adaptor.Plane() #face_dir = face_adaptor.Direction() face_umin = face_adaptor.FirstUParameter() face_vmin = face_adaptor.FirstVParameter() face_umax = face_adaptor.LastUParameter() face_vmax = face_adaptor.LastVParameter() face_u = (face_umax + face_umin) / 2 face_v = (face_vmax + face_vmin) / 2 face_pnt = face_adaptor.Value(face_u, face_v) return face_pln
def recognize_face(a_face): """ Takes a TopoDS shape and tries to identify its nature whether it is a plane a cylinder a torus etc. if a plane, returns the normal if a cylinder, returns the radius """ if not type(a_face) is TopoDS_Face: print("Please hit the 'G' key to switch to face selection mode") return False surf = BRepAdaptor_Surface(a_face, True) surf_type = surf.GetType() if surf_type == GeomAbs_Plane: print("Identified Plane Geometry") # look for the properties of the plane # first get the related gp_Pln gp_pln = surf.Plane() location = gp_pln.Location() # a point of the plane normal = gp_pln.Axis().Direction() # the plane normal # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Normal (global coordinates)", normal.X(), normal.Y(), normal.Z()) elif surf_type == GeomAbs_Cylinder: print("Identified Cylinder Geometry") # look for the properties of the cylinder # first get the related gp_Cyl gp_cyl = surf.Cylinder() location = gp_cyl.Location() # a point of the axis axis = gp_cyl.Axis().Direction() # the cylinder axis # then export location and normal to the console output print("--> Location (global coordinates)", location.X(), location.Y(), location.Z()) print("--> Axis (global coordinates)", axis.X(), axis.Y(), axis.Z()) elif surf_type == GeomAbs_BSplineSurface: print("Identified BSplineSurface Geometry") #gp_bsrf = surf.Surface() #degree = gp_bsrf.NbUKnots() # TODO use a model that provided BSplineSurfaces, as1_pe_203.stp only contains # planes and cylinders else: # TODO there are plenty other type that can be checked # see documentation for the BRepAdaptor class # https://www.opencascade.com/doc/occt-6.9.1/refman/html/class_b_rep_adaptor___surface.html print(surf_type, "recognition not implemented")
def find_faces(solid): # search the solid for the bottom-most horizontal planes result = defaultdict(list) # downward-facing XY plane d = gp.DZ().Reversed() # loop over all faces (only faces) for f in TopologyExplorer(solid).faces(): surf = BRepAdaptor_Surface(f, True) # skip non-planar faces if surf.GetType() != GeomAbs_Plane: continue # get surface attributes pln = surf.Plane() location = pln.Location() normal = pln.Axis().Direction() # if face is reversed, reverse the surface normal if f.Orientation() == TopAbs_REVERSED: normal.Reverse() # add face if it's parallel (opposite) and coincident with a slicing plane if d.IsEqual(normal, 0.1): result[location.Z()].append(f) # display the face # display.DisplayShape(f, update=True) # display the face normal # display.DisplayVector(gp_Vec(normal), location, update=True) # sort the dict by keys (z-height), and return the lowest lowest = sorted(result.keys())[0] for f in result[lowest]: print( f'z: {lowest}, orientation: {"FORWARD" if f.Orientation() == TopAbs_FORWARD else "REVERSED"}' ) # return all faces associated with the lowest value print(f'number of faces: {len(result[lowest])}') return result[lowest]
def selected_shape_info(self): if self.selectMode == 'Face': print(self.shape_selected) surf = BRepAdaptor_Surface(self.shape_selected, True) if surf.GetType() == GeomAbs_Plane: gp_pln = surf.Plane() normal = gp_pln.Axis().Direction() print('plane normal: (%.3f, %.3f, %.3f)' % (normal.X(), normal.Y(), normal.Z())) elif surf.GetType() == GeomAbs_Cylinder: gp_cyl = surf.Cylinder() axis = gp_cyl.Axis().Direction() location = gp_cyl.Location() print('cylinder axis direction: (%.3f, %.3f, %.3f)' % (axis.X(), axis.Y(), axis.Z())) print('cylinder axis location: (%.3f, %.3f, %.3f)' % (location.X(), location.Y(), location.Z())) else: typeList = ['Plane', 'Cylinder', 'Cone', 'Sphere', 'Torus', 'BezierSurface', 'BSplineSurface', 'SurfaceOfRevolution', 'SurfaceOfExtrusion', 'OffsetSurface', 'OtherSurface'] print('This surface type "%s" is not implemented !!' % typeList[surf.GetType()]) elif self.selectMode == 'Edge': print(self.shape_selected) edge = BRepAdaptor_Curve(self.shape_selected) curveType = edge.GetType() if curveType == GeomAbs_Line: gp_lin = edge.Line() direction = gp_lin.Direction() print('Line direction: (%.3f, %.3f, %.3f)' % (direction.X(), direction.Y(), direction.Z())) elif curveType == GeomAbs_Circle: gp_circ = edge.Circle() center = gp_circ.Location() print('Center of circle: (%.3f, %.3f, %.3f)' % (center.X(), center.Y(), center.Z())) print('Radius of circle:', gp_circ.Radius()) else: typeList = ['Line', 'Circle', 'Ellipse', 'Parabola', 'BezierCurve', 'BSplineCurve', 'OffsetCurve or OtherCurve?', 'OtherCurve'] print('This edge type is not implemented !!') print('This surface type "%s" is not implemented !!' % typeList[surf.GetType()])