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")
Example #2
0
    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 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")
Example #5
0
  def makeHelixOnCyl(self):
    bas = BRepAdaptor_Surface(self.face)
    cyl = bas.Cylinder()

    delta_v = bas.LastVParameter() - bas.FirstVParameter()
    v_final = bas.LastVParameter()

    dv_du = self.pitch / (2*pi)
    l = delta_v / sin(atan(dv_du))

    aLine2d = gp_Lin2d(gp_Pnt2d(bas.FirstUParameter(),bas.FirstVParameter()), gp_Dir2d(1,dv_du))
    if not self.reverse_helix:
      aSegment = GCE2d_MakeSegment(aLine2d, 0.0, l)
    else:
      aSegment = GCE2d_MakeSegment(aLine2d, l, 0.0)

    helix_edge = BRepBuilderAPI_MakeEdge(aSegment.Value(), Geom_CylindricalSurface(cyl)).Edge()

    #self.aLine2d = aLine2d
    #self.aSegment = aSegment
    self.helix_edge = helix_edge
    return helix_edge
Example #6
0
    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()])
Example #7
0
def detect_through_holes(shape: TopoDS_Shape, removeHoles: bool = False):
    holes = 0
    cones = 0
    re = BRepTools_ReShape()
    faces = TopologyExplorer(shape).number_of_faces()
    shells = TopologyExplorer(shape).number_of_shells()
    dxShape, dyShape, dzShape = get_dx_dy_dz(shape)
    for shell in TopologyExplorer(shape).shells():
        for face in TopologyExplorer(shell).faces():
            dxFace, dyFace, dzFace = get_dx_dy_dz(face)
            surf = BRepAdaptor_Surface(face, True)
            surf_type = surf.GetType()
            # if surf_type == GeomAbs_Plane:
            #     # print("Identified Plane Geometry")
            #     gp_pln = surf.Plane()
            #     location = gp_pln.Location()  # a point of the plane
            #     normal = gp_pln.Axis().Direction()  # the plane normal
            #     # print("--> Location (global coordinates)", location.X(), location.Y(), location.Z())
            #     # print("--> Normal (global coordinates)", normal.X(), normal.Y(), normal.Z())
            if 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())

                holes = holes + 1

                # for edge in TopologyExplorer(shape).edges_from_face(face):
                #     re.Remove(edge)
                # re.Remove(face)
                # re.Replace(shell, re.Apply(shell))
                # shape = re.Apply(shell)

            if surf_type == GeomAbs_Cone:
                print("Identified Cone Geometry")
                # look for the properties of the cylinder
                # first get the related gp_Cyl
                gp_cone = surf.Cone()
                location = gp_cone.Location()  # a point of the axis
                axis = gp_cone.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())

                cones = cones + 1

                # for edge in TopologyExplorer(shape).edges_from_face(face):
                #     re.Remove(edge)
                # re.Remove(face)
                # re.Replace(shell, re.Apply(shell))
                # shape = re.Apply(shell)
            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")

    return cones, holes, shape
shape = reader.OneShape()

# Explore the faces of the shape (these are known to be named)
exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
    s = exp.Current()
    exp.Next()

    # Converting TopoDS_Shape to TopoDS_Face
    face = topods.Face(s)

    # Working on the geometry
    face_adaptor = BRepAdaptor_Surface(face)
    face_type = face_adaptor.GetType()
    if face_type == GeomAbs_Cylinder:
        cylinder = face_adaptor.Cylinder()
        entity = {}
        entity['type'] = 'cylinder'
        entity['location'] = cylinder.Axis().Location().Coord()
        entity['direction'] = cylinder.Axis().Direction().Coord()
        entity['radius'] = cylinder.Radius()
        entity['coefficients'] = cylinder.Coefficients()
        print('cylinder:', entity)

    # Working on the name
    item = tr.EntityFromShapeResult(s, 1)
    # if item.IsNull():
    #    continue
    item = StepRepr_RepresentationItem.DownCast(item)
    name = item.Name().ToCString()
    if name: