Esempio n. 1
0
def test_convert_stl_file_binary():
    r"""Test the conversion of a binary STL file"""
    path_stl = join(dirname(__file__), "tests/in/stl/box_binary.stl")
    target_folder = join(dirname(__file__), "tests/out/box_binary")
    convert_stl_file(path_stl, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder, basename(path_stl)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 2
0
def test_convert_iges_file():
    r"""Test the conversion of a IGES file"""
    path_iges = join(dirname(__file__), "tests/in/iges/aube_pleine.iges")
    target_folder = join(dirname(__file__), "tests/out/aube_pleine")
    convert_iges_file(path_iges, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder, basename(path_iges)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 3
0
def test_convert_brep_file():
    r"""Test the conversion of a BREP file"""
    path_brep = join(dirname(__file__), "tests/in/brep/cylinder_head.brep")
    target_folder = join(dirname(__file__), "tests/out/cylinder_head")
    convert_brep_file(path_brep, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder, basename(path_brep)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 4
0
def convert_brep_file(brep_filename, target_folder, remove_original=True):
    r"""Convert a BREP file (.brep, .brp) for web display

    Parameters
    ----------
    brep_filename : str
        Full path to the BREP file
    target_folder : str
        Full path to the target folder for the conversion
    remove_original : bool
        Should the input file be deleted after conversion?
        It should be deleted on a web platform to save disk space, but, for
        testing, it might be useful not to delete it.

    Returns
    -------
    Nothing, it is a procedure

    """
    if not isdir(target_folder):
        mkdir(target_folder)
    shape = BrepImporter(brep_filename).shape
    converted_filename = _conversion_filename(brep_filename, target_folder, 0)
    converted_basenames = [basename(converted_filename)]
    _convert_shape(shape, converted_filename)

    _write_descriptor(
        BoundingBox(shape).max_dimension, converted_basenames,
        _descriptor_filename(target_folder, basename(brep_filename)))
    if remove_original is True:
        remove(brep_filename)
Esempio n. 5
0
def test_convert_step_file():
    r"""Test the conversion of a STEP file"""
    path_step = join(dirname(__file__), "tests/in/step/bottle.stp")
    target_folder = join(dirname(__file__), "tests/out/bottle")
    convert_step_file(path_step, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder, basename(path_step)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 6
0
def test_convert_step_file():
    r"""Test the conversion of a STEP file"""
    relpath = "tests/in/py/sample_projects/car/shelf/wheel/CAR-WHEEL-RIM-D416_l174_mm---.stepzip"
    path_stepzip = join(dirname(__file__), relpath)
    target_folder = join(dirname(__file__), "tests/out/wheel_rim_stepzip")
    convert_stepzip_file(path_stepzip, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder, basename(path_stepzip)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 7
0
def test_convert_py_file_part():
    r"""Test the conversion of a PY file that contains
    the definition of a part"""
    relpath = "tests/in/py/sample_projects/test_project/py_scripts/plate_with_holes.py"
    path_py_file_part = join(dirname(__file__), relpath)
    target_folder = join(dirname(__file__), "tests/out/plate_with_holes_py")
    convert_py_file_part(path_py_file_part, target_folder, remove_original=False)
    assert isfile(_descriptor_filename(target_folder,
                                       basename(path_py_file_part)))
    rmtree(target_folder, ignore_errors=True)
Esempio n. 8
0
def convert_py_file_part(py_filename, target_folder, remove_original=True):
    r"""Convert an OsvCad Python file that contains a part for web display

    The Python file contains the definition of a part.

    Parameters
    ----------
    py_filename : str
        Full path to the Python file
    target_folder : str
        Full path to the target folder for the conversion
    remove_original : bool
        Should the input file be deleted after conversion?
        It should be deleted on a web platform to save disk space, but, for
        testing, it might be useful not to delete it.

    Returns
    -------
    Nothing, it is a procedure

    Raises
    ------
    ValueError if not a part definition

    """
    if not isdir(target_folder):
        mkdir(target_folder)
    part = Part.from_py_script(py_filename)
    shape = part.node_shape.shape
    converted_filename = _conversion_filename(py_filename,
                                              target_folder,
                                              0)
    converted_basenames = [basename(converted_filename)]
    _convert_shape(shape, converted_filename)

    _write_descriptor(BoundingBox(shape).max_dimension,
                      converted_basenames,
                      _descriptor_filename(target_folder,
                                           basename(py_filename)))
    if remove_original is True:
        remove(py_filename)
Esempio n. 9
0
def convert_stepzip_file(stepzip_filename,
                         target_folder,
                         remove_original=True):
    r"""Convert an OsvCad Stepzip file for web display

    A Stepzip file contains a STEP geometry file and an anchors definition file
    zipped together

    Parameters
    ----------
    stepzip_filename : str
        Full path to the Stepzip file
    target_folder : str
        Full path to the target folder for the conversion
    remove_original : bool
        Should the input file be deleted after conversion?
        It should be deleted on a web platform to save disk space, but, for
        testing, it might be useful not to delete it.

    Returns
    -------
    Nothing, it is a procedure

    """
    if not isdir(target_folder):
        mkdir(target_folder)
    part = Part.from_stepzip(stepzip_filename)
    shape = part.node_shape.shape
    converted_filename = _conversion_filename(stepzip_filename, target_folder,
                                              0)
    converted_basenames = [basename(converted_filename)]
    _convert_shape(shape, converted_filename)

    _write_descriptor(
        BoundingBox(shape).max_dimension, converted_basenames,
        _descriptor_filename(target_folder, basename(stepzip_filename)))
    if remove_original is True:
        remove(stepzip_filename)
Esempio n. 10
0
def convert_iges_file(iges_filename, target_folder, remove_original=True):
    r"""Convert an IGES file (.iges, .igs) for web display

    Parameters
    ----------
    iges_filename : str
        Full path to IGES file
    target_folder : str
        Full path to the target folder for the conversion
    remove_original : bool
        Should the input file be deleted after conversion?
        It should be deleted on a web platform to save disk space, but, for
        testing, it might be useful not to delete it.

    Returns
    -------
    Nothing, it is a procedure

    """
    if not isdir(target_folder):
        mkdir(target_folder)
    converted_basenames = []
    importer = IgesImporter(iges_filename)
    shapes = importer.shapes
    max_dim = BoundingBox(importer.compound).max_dimension
    for i, shape in enumerate(shapes):
        converted_filename = _conversion_filename(iges_filename, target_folder,
                                                  i)
        _convert_shape(shape, converted_filename)
        converted_basenames.append(basename(converted_filename))

    _write_descriptor(
        max_dim, converted_basenames,
        _descriptor_filename(target_folder, basename(iges_filename)))
    if remove_original is True:
        remove(iges_filename)
Esempio n. 11
0
def convert_py_file_assembly(py_filename,
                             target_folder,
                             clone_url,
                             branch,
                             remove_original=True):
    r"""

    **** WORK IN PROGRESS ****

    Parameters
    ----------
    py_filename
    target_folder
    clone_url
    branch

    Returns
    -------

    """
    if not isdir(target_folder):
        mkdir(target_folder)

    logger.info("Dealing with a Python file that is supposed to "
                "define an assembly")
    # -1- change working dir to converted_files
    working_dir_initial = getcwd()
    chdir(target_folder)
    # 0 - Git clone
    logger.info("Git cloning %s into %s" % (clone_url, target_folder))

    # from subprocess import call
    # call(["cd", target_folder, "&&", "git", "clone", clone_url])
    system("cd %s && git clone %s" % (target_folder, clone_url))

    project = clone_url.split("/")[-1]

    # 1 - Git checkout the right branch/commit
    logger.info("Git checkout %s of %s" % (branch, project))
    system("cd %s/%s && git checkout %s" % (target_folder,
                                               project,
                                               branch))

    # 2 - Alter sys.path
    logger.info("Git checkout %s of %s" % (branch, project))
    sys_path_initial = sys.path
    path_extra = "%s/%s" % (target_folder, project)
    logger.info("Appending sys.path with %s" % path_extra)
    sys.path.append(path_extra)

    # Useless : adding converted_files to sys.path
    # logger.info("Appending sys.path with %s" % dirname(py_filename))
    # sys.path.append(dirname(py_filename))

    # 3 - Run osvcad functions
    converted_basenames = []
    # TODO : THE PROBLEM IS THAT WE ARE IMPORTING THE FILE OUTSIDE OF THE
    # CONTEXT OF ITS PROJECT
    module_ = imp.load_source(splitext(basename(py_filename))[0],
                              py_filename)
    assembly = getattr(module_, "assembly")

    for i, node in enumerate(assembly.nodes()):
        shape = node.node_shape.shape
        converted_filename = _conversion_filename(py_filename,
                                                  target_folder,
                                                  i)
        converted_basenames.append(basename(converted_filename))
        _convert_shape(shape, converted_filename)
    # TODO : max_dim
    _write_descriptor(1000,
                      converted_basenames,
                      _descriptor_filename(target_folder,
                                           basename(py_filename)))
    remove(py_filename)

    # 4 - Put sys.path back to where it was
    sys.path = sys_path_initial
    # 5 - Set back the working dir
    chdir(working_dir_initial)
    # 6 - Cleanup
    if remove_original is True:
        pass
Esempio n. 12
0
def convert_freecad_file(freecad_filename,
                         target_folder,
                         remove_original=True):
    r"""Convert a FreeCAD file (.fcstd) for web display

    Parameters
    ----------
    freecad_filename : str
        Full path to FreeCAD file
    target_folder : str
        Full path to the target folder for the conversion
    remove_original : bool
        Should the input file be deleted after conversion?
        It should be deleted on a web platform to save disk space, but, for
        testing, it might be useful not to delete it.

    Returns
    -------
    Nothing, it is a procedure

    """
    logger.info("Starting FreeCAD conversion")

    folder_unzipping = join(target_folder,
                            splitext(basename(freecad_filename))[0])

    extract_fcstd(freecad_filename, folder_unzipping)
    name_file_visibility_tuples = \
        name_file_visibility_from_unzipping_folder(folder_unzipping)

    extremas = []
    converted_basenames = []

    for i, (n, f, v) in enumerate(name_file_visibility_tuples):
        if v is True:
            brep_filename = "%s/%s" % (folder_unzipping, f)
            converted_filename = _conversion_filename(brep_filename,
                                                      target_folder,
                                                      i)
            converted_basenames.append(basename(converted_filename))
            try:
                importer = BrepImporter(brep_filename)
                extremas.append(BoundingBox(importer.shape).as_tuple)
                _convert_shape(importer.shape, converted_filename)
            except (RuntimeError, AssertionError):
                # An AssertionError is raised if one of the BREPs contained
                # in the FCSTD file contains a NULL shape
                logger.error("RuntimeError for %s" % brep_filename)

    x_min = min([extrema[0] for extrema in extremas])
    y_min = min([extrema[1] for extrema in extremas])
    z_min = min([extrema[2] for extrema in extremas])
    x_max = max([extrema[3] for extrema in extremas])
    y_max = max([extrema[4] for extrema in extremas])
    z_max = max([extrema[5] for extrema in extremas])

    max_dim = max([x_max - x_min, y_max - y_min, z_max - z_min])

    _write_descriptor(max_dim,
                      converted_basenames,
                      _descriptor_filename(target_folder,
                                           basename(freecad_filename)))

    # Cleanup of files that are not needed anymore
    if remove_original is True:
        remove(freecad_filename)
    rmtree(folder_unzipping, ignore_errors=True)