Beispiel #1
0
def test_step_exporter_adding_not_a_shape(box_shape):
    r"""Adding something to the exporter
    that is not a TopoDS_Shape or a subclass"""
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    with pytest.raises(ValueError):
        exporter.add_shape(gp_Pnt(1, 1, 1))
Beispiel #2
0
def test_step_exporter_happy_path(box_shape):
    r"""Happy path"""
    filename = path_from_file(__file__, "./models_out/box.StP")
    exporter = StepExporter(filename)
    exporter.add_shape(box_shape)
    exporter.write_file()
    assert os.path.isfile(filename)
Beispiel #3
0
def test_step_exporter_happy_path_shape_subclass(box_shape):
    r"""Happy path with a subclass of TopoDS_Shape"""
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    solid = shape_to_topology(box_shape)
    assert isinstance(solid, TopoDS_Solid)
    exporter.add_shape(solid)
    exporter.write_file()
    assert os.path.isfile(filename)
#!/usr/bin/env python
# coding: utf-8
r"""Exporting a single shape to STL"""

import logging

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox

from aocxchange.step import StepExporter
from corelib.core.files import path_from_file

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s :: %(levelname)6s :: %(module)20s :: '
                    '%(lineno)3d :: %(message)s')

# First create a simple shape to export
box_shape = BRepPrimAPI_MakeBox(50, 50, 50).Shape()

# Export to STEP
filename = path_from_file(__file__, "./models_out/result_export_single.stp")
step_exporter = StepExporter(filename)
step_exporter.add_shape(box_shape)
step_exporter.write_file()
Beispiel #5
0
def export_step():
    """
    Exports a TopoDS_Shape to a STEP file.
    """
    test_shape = BRepPrimAPI_MakeBox(100., 100., 100.).Shape()
    # export to AP203 schema
    ap203_exporter = StepExporter('./models_out/box_203.stp', schema='AP203')
    ap203_exporter.add_shape(test_shape)
    ap203_exporter.write_file()
    # export AP214 schema
    ap214cd_exporter = StepExporter('./models_out/box_214CD.stp',
                                    schema='AP214CD')
    ap214cd_exporter.add_shape(test_shape)
    ap214cd_exporter.write_file()
Beispiel #6
0
def _generate_cad(output_folder, py_geometry_file, output_format):
    if output_format not in ["step", "stl", "html"]:
        raise ValueError
    py_geometry_module = imp.load_source(py_geometry_file, py_geometry_file)
    shape = py_geometry_module.__shape__
    part_id = splitext(basename(py_geometry_file))[0]
    part_id = str(part_id)  # Keeps the OCC STEP Writer happy !

    if output_format == "step":
        # part.to_step(join(output_folder, "%s.stp" % part_id))
        exporter = StepExporter(filename=join(output_folder, "%s.stp" % part_id))
        exporter.add_shape(shape)
        exporter.write_file()
    elif output_format == "stl":
        # part.to_stl(join(output_folder, "%s.stl" % part_id))
        exporter = StlExporter(filename=join(output_folder, "%s.stl" % part_id))
        exporter.set_shape(shape)
        exporter.write_file()
    # elif output_format == "html":
    #     part.to_html(join(output_folder, "%s.html" % part_id))
    else:
        msg = "Unknown export format"
        logger.error(msg)
        raise ValueError(msg)
Beispiel #7
0
def test_step_exporter_overwrite(box_shape):
    r"""Happy path with a subclass of TopoDS_Shape"""
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    solid = shape_to_topology(box_shape)
    assert isinstance(solid, TopoDS_Solid)
    exporter.add_shape(solid)
    exporter.write_file()
    initial_timestamp = os.path.getmtime(filename)
    assert os.path.isfile(filename)

    # read the written box.stp
    importer = StepImporter(filename)
    topo_compound = Topo(importer.compound, return_iter=False)
    assert topo_compound.number_of_faces == 6
    assert len(topo_compound.faces) == 6
    assert topo_compound.number_of_edges == 12

    # add a sphere and write again with same exporter
    sphere = BRepPrimAPI_MakeSphere(10)
    exporter.add_shape(sphere.Shape())
    exporter.write_file()  # this creates a file with a box and a sphere
    intermediate_timestamp = os.path.getmtime(filename)
    assert intermediate_timestamp > initial_timestamp

    # check that the file contains the box and the sphere
    importer = StepImporter(filename)

    # 6 from box + 1 from sphere
    assert len(Topo(importer.compound, return_iter=False).faces) == 7

    assert len(Topo(importer.compound, return_iter=False).solids) == 2

    # create a new exporter and overwrite with a box only
    filename = path_from_file(__file__, "./models_out/box.stp")
    exporter = StepExporter(filename)
    solid = shape_to_topology(box_shape)
    exporter.add_shape(solid)
    exporter.write_file()
    assert os.path.isfile(filename)
    last_timestamp = os.path.getmtime(filename)
    assert last_timestamp > intermediate_timestamp

    # check the file only contains a box
    importer = StepImporter(filename)

    # 6 from box
    assert len(Topo(importer.compound, return_iter=False).faces) == 6

    assert len(Topo(importer.compound, return_iter=False).solids) == 1
Beispiel #8
0
def test_step_exporter_wrong_schema(box_shape):
    r"""Schema is not AP203 or AP214CD"""
    filename = path_from_file(__file__, "./models_out/box.stp")
    with pytest.raises(StepUnknownSchemaException):
        StepExporter(filename, schema="48.3")
Beispiel #9
0
def test_step_exporter_wrong_extension(box_shape):
    r"""Trying to write a step file with the IgesExporter"""
    filename = path_from_file(__file__, "./models_out/box.igs")
    with pytest.raises(IncompatibleFileFormatException):
        StepExporter(filename)
Beispiel #10
0
def test_step_exporter_wrong_filename(box_shape):
    r"""Trying to write to a non-existent directory"""
    filename = path_from_file(__file__, "./nonexistent/box.stp")
    with pytest.raises(DirectoryNotFoundException):
        StepExporter(filename)
Beispiel #11
0
def handle_cad_file_save_as(shapes):
    r"""Handle the logic of cad file save as

    Parameters
    ----------
    shapes : iterable of OCC.TopoDS.TopoDS_Shape

    Returns
    -------
    tuple
        path, type (step, iges ...), extra_info(format, schema or None)

    """

    # assert len(shapes) > 0
    if len(shapes) == 0:
        msg = "shapes list does not contain any shape"
        raise ValueError(msg)

    with SaveAsCadDialog() as save_as_cad_dialog:
        if save_as_cad_dialog.ShowModal() == wx.ID_OK:
            path = str(save_as_cad_dialog.GetPath())
            extension = extract_file_extension(path)

            if extension.lower() in step_extensions:
                type_ = "step"
                with StepSchemaDialog() as step_schema_dialog:
                    if step_schema_dialog.ShowModal() == wx.ID_OK:
                        extra_info = schema = str(step_schema_dialog.selection.GetValue())
                        exporter = StepExporter(path, schema=schema)
                        for shape in shapes:
                            exporter.add_shape(shape)
                        exporter.write_file()
            elif extension.lower() in iges_extensions:
                type_ = "iges"
                with IgesFormatDialog() as iges_format_dialog:
                    if iges_format_dialog.ShowModal() == wx.ID_OK:
                        extra_info = format_ = str(iges_format_dialog.selection.GetValue())
                        exporter = IgesExporter(path, format_=format_)
                        for shape in shapes:
                            exporter.add_shape(shape)
                        exporter.write_file()
            elif extension.lower() in stl_extensions:
                type_ = "stl"
                with StlFormatDialog() as stl_format_dialog:
                    if stl_format_dialog.ShowModal() == wx.ID_OK:
                        extra_info = ascii_mode = True if stl_format_dialog.selection.GetValue() == "ASCII" else False
                        exporter = StlExporter(path, ascii_mode=ascii_mode)
                        # TODO : warning message if len(shapes) > 1
                        exporter.set_shape(shapes[0])
                        exporter.write_file()
            elif extension.lower() in brep_extensions:
                type_ = "brep"
                extra_info = None
                exporter = BrepExporter(path)
                # TODO : warning message if len(shapes) > 1
                exporter.set_shape(shapes[0])
                exporter.write_file()

            else:
                msg = "File extension indicates a file type " \
                      "that is not supported"
                raise ValueError(msg)

            return path, type_, extra_info
        else:
            return None, None, None