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)
def test_convert_shape_cache():
    my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
    target = join(dirname(__file__), "tests/out/box.json")
    assert not isfile(target)
    t0 = time()
    _convert_shape(my_box, target)
    t1 = time()
    assert isfile(target)
    t2 = time()
    _convert_shape(my_box, target)  # this call should use cached target
    t3 = time()
    assert isfile(target)
    assert t3 - t2 < t1 - t0
    remove(target)
Exemple #3
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)
Exemple #4
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)
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)
Exemple #6
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
Exemple #7
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)
def test_convert_shape():
    my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
    target = join(dirname(__file__), "tests/out/box.json")
    _convert_shape(my_box, target)
    assert isfile(target)
    remove(target)