Пример #1
0
def load_geometry_from_file_object(
        file: Union[BinaryIO, TextIO],
        extension: str,
        units: str,
        geometry: OFFGeometryNoNexus = OFFGeometryNoNexus(),
) -> OFFGeometry:
    """
    Loads geometry from a file object into an OFFGeometry instance

    Supported file types are OFF and STL.

    :param file: The file object to load the geometry from.
    :param units: A unit of length in the form of a string. Used to determine the multiplication factor.
    :param geometry: The optional OFFGeometry to load the geometry data into. If not provided, a new instance will be
    returned.
    :return: An OFFGeometry instance containing that file's geometry, or an empty instance if filename's extension is
    unsupported.
    """

    mult_factor = calculate_unit_conversion_factor(units, METRES)

    if extension == ".off":
        _load_off_geometry(file, mult_factor, geometry)
    elif extension == ".stl":
        _load_stl_geometry(file, mult_factor, geometry)
    else:
        geometry.faces = []
        geometry.vertices = []
        geometry.colors = []
        logging.error("geometry file extension not supported")

    return geometry
Пример #2
0
def _load_off_geometry(
        file: Union[BinaryIO, TextIO],
        mult_factor: float,
        geometry: OFFGeometryNoNexus = OFFGeometryNoNexus(),
) -> OFFGeometry:
    """
    Loads geometry from an OFF file into an OFFGeometry instance.

    :param file: The file containing an OFF geometry.
    :param mult_factor: The multiplication factor for unit conversion.
    :param geometry: The optional OFFGeometry to load the OFF data into. If not provided, a new instance will be
    returned.
    :return: An OFFGeometry instance containing that file's geometry.
    """
    vertices, faces, face_colors = parse_off_file(file)

    geometry.vertices = [
        QVector3D(x * mult_factor, y * mult_factor, z * mult_factor)
        for x, y, z in (vertex for vertex in vertices)
    ]
    geometry.faces = faces
    geometry.colors = face_colors
    logging.info("OFF loaded")
    return geometry