Ejemplo n.º 1
0
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")
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
    def by_face(cls, face, restrict=True):
        """
        Create by a face.

        :param afem.topology.entities.Face face: The face.
        :param bool restrict: Option to restruct the uv-domain of the surface
            to the boundary of the face.

        :return: The adaptor surface.
        :rtype: afem.adaptor.entities.FaceAdaptorSurface
        """
        adp_srf = BRepAdaptor_Surface(face.object, restrict)
        return cls(adp_srf)
Ejemplo n.º 5
0
    def cast_surface(cls, shape, expected_type=None):
        """ Attempt to cast the shape (a face) to a surface

        Parameters
        ----------
        shape: TopoDS_Face
            The shape to cast
        expected_type: GeomAbs_SurfaceType
            The type to restrict

        Returns
        -------
        surface: BRepAdaptor_Surface or None
            The surface or None if it could not be created or did not
            match the expected type (if given).
        """
        if isinstance(shape, TopoDS_Face):
            face = shape
        else:
            face = TopoDS.Face_(shape)
        surface = BRepAdaptor_Surface(face, True)
        if expected_type is not None and surface.GetType() != expected_type:
            return None
        return surface
# conversion to a nurbs representation
nurbs_converter = BRepBuilderAPI_NurbsConvert(base_shape, True)
#nurbs_converter.Perform()
converted_shape = nurbs_converter.Shape()

# now, all edges should be BSpline curves and surfaces BSpline surfaces
# see https://www.opencascade.com/doc/occt-7.4.0/refman/html/class_b_rep_builder_a_p_i___nurbs_convert.html#details

expl = TopologyExplorer(converted_shape)

# loop over faces
fc_idx = 1

for face in expl.faces():
    print("=== Face %i ===" % fc_idx)
    surf = BRepAdaptor_Surface(face, True)
    surf_type = surf.GetType()
    # check each of the is a BSpline surface
    # it should be, since we used the nurbs converter before
    if not surf_type == GeomAbs_BSplineSurface:
        raise AssertionError("the face was not converted to a GeomAbs_BSplineSurface")
    # get the nurbs
    bsrf = surf.BSpline()
    print("UDegree:", bsrf.UDegree())
    print("VDegree:", bsrf.VDegree())
    # uknots array
    uknots = bsrf.UKnots()
    print("Uknots:", end="")
    for i in range(bsrf.NbUKnots()):
        print(uknots.Value(i + 1), end=" ")
    # vknots array