Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
"""The export interface, now that the writers live in the evaluator.
The formats themselves -- the object split, per-triangle colour, the exact
bytes of each writer -- are tested in openscad_cpp_evaluator's own suite,
which is where that code now is. What is worth testing here is the boundary:
that the handle reaches export_model, that every format the dialog offers
actually writes, and that warnings come back rather than being swallowed.
test_3mf_export.py and test_export_object_split.py were retired with the
Python writers they covered; their assertions live on in the C++ tests.
"""
import pytest
from belfryscad import exporters
from openscad_cpp_evaluator import Evaluator
def geometry_for(src, tmp_path):
path = tmp_path / "t.scad"
path.write_text(src)
ev = Evaluator(echo_fn=lambda _m: None)
ev.evaluate(str(path))
return ev.geometry
def test_evaluate_leaves_a_geometry_handle(tmp_path):
g = geometry_for("cube(10);", tmp_path)
assert g is not None
assert not g.is_empty()
assert len(g) == 1
def test_the_handle_is_not_the_body_list(tmp_path):
"""It is opaque on purpose: the point is that geometry never round-trips
through numpy just to be exported."""
g = geometry_for("cube(10);", tmp_path)
assert not isinstance(g, list)
assert not hasattr(g, "to_mesh")
@pytest.mark.parametrize("ext", [".3mf", ".stl", ".obj", ".off", ".ply", ".wrl", ".x3d"])
def test_every_format_writes_a_non_empty_file(ext, tmp_path):
g = geometry_for('color("red") cube(10);', tmp_path)
out = tmp_path / f"m{ext}"
assert exporters.export_model(str(out), g) == []
assert out.is_file() and out.stat().st_size > 0
def test_obj_writes_its_companion_mtl(tmp_path):
g = geometry_for('color("red") cube(10);', tmp_path)
out = tmp_path / "m.obj"
exporters.export_model(str(out), g)
assert (tmp_path / "m.mtl").is_file()
def test_format_can_override_the_extension(tmp_path):
g = geometry_for("cube(10);", tmp_path)
out = tmp_path / "named_wrong.dat"
exporters.export_model(str(out), g, format="ply")
assert out.read_bytes().startswith(b"ply\n")
def test_ascii_stl_is_opt_in(tmp_path):
g = geometry_for("cube(10);", tmp_path)
binary, ascii_ = tmp_path / "b.stl", tmp_path / "a.stl"
exporters.export_model(str(binary), g)
exporters.export_model(str(ascii_), g, ascii_stl=True)
assert not binary.read_bytes().startswith(b"solid")
# No trailing newline in the assertion: the text formats are written in
# text mode, so Windows gets CRLF here. That matches what the Python
# writer did before the port (open(path, "w") translates newlines too),
# so it is the existing behaviour rather than a regression -- but it
# does mean the text formats are not byte-identical across platforms.
# PLY is unaffected; its header rides on a binary-mode stream.
assert ascii_.read_bytes().startswith(b"solid OpenSCAD_Model")
assert b"endsolid OpenSCAD_Model" in ascii_.read_bytes()
def test_the_extension_list_matches_what_the_dialog_offers():
from belfryscad.window.main_window import _EXPORT_FORMATS
offered = {e for _f, e in _EXPORT_FORMATS}
assert offered <= set(exporters.export_extensions())
# --- warnings come back, they are not swallowed -----------------------
def test_an_open_shell_is_reported_and_still_written(tmp_path):
"""Nothing here refuses to write: a deliberately open surface is a
legitimate export, so it is warned about and written anyway."""
src = "polyhedron(points=[[0,0,0],[10,0,0],[0,10,0]], faces=[[0,1,2]]);"
g = geometry_for(src, tmp_path)
out = tmp_path / "m.3mf"
warnings = exporters.export_model(str(out), g)
assert any("not a closed solid" in w for w in warnings), warnings
assert out.is_file() and out.stat().st_size > 0
def test_a_sound_model_warns_about_nothing(tmp_path):
g = geometry_for("cube(10);", tmp_path)
assert exporters.export_model(str(tmp_path / "m.3mf"), g) == []
# --- failures are errors, not silence ---------------------------------
def test_an_unknown_format_raises(tmp_path):
g = geometry_for("cube(10);", tmp_path)
with pytest.raises(Exception, match="(?i)format"):
exporters.export_model(str(tmp_path / "m.xyz"), g)
def test_an_empty_model_raises_rather_than_writing_nothing(tmp_path):
g = geometry_for("// nothing here\n", tmp_path)
with pytest.raises(Exception, match="(?i)geometry"):
exporters.export_model(str(tmp_path / "m.3mf"), g)
# --- the union rule, end to end ---------------------------------------
def test_top_level_is_an_implicit_union(tmp_path):
"""The bug that started all of this: two overlapping top-level cubes are
one solid. Real OpenSCAD 2022.08.22 writes 20 vertices and 36 triangles
for this script; asserted here because it is the behaviour the GUI
depends on, whichever side of the boundary implements it."""
import re
import zipfile
g = geometry_for("cube(100, center=false); cube(100, center=true);", tmp_path)
out = tmp_path / "m.3mf"
exporters.export_model(str(out), g)
xml = zipfile.ZipFile(out).read("3D/3dmodel.model").decode()
assert len(re.findall(r"<object ", xml)) == 1
assert len(re.findall(r"<vertex ", xml)) == 20
assert len(re.findall(r"<triangle ", xml)) == 36
# --- the split option (issue #319) -------------------------------------
# The behaviour itself belongs to the evaluator and is tested there. What
# this file owns is the boundary: that the flag survives the trip from a
# caller, through exporters, into the writer -- which is exactly what broke
# once already, when the evaluator's own Python facade quietly dropped it.
def _objects_in(path):
import zipfile
with zipfile.ZipFile(path) as z:
return z.read("3D/3dmodel.model").decode().count("<object ")
def test_disjoint_pieces_are_one_object_by_default(tmp_path):
g = geometry_for("cube(10); translate([20,0,0]) cube(10); translate([40,0,0]) cube(10);", tmp_path)
out = tmp_path / "joined.3mf"
exporters.export_model(str(out), g)
assert _objects_in(out) == 1
def test_split_components_separates_them(tmp_path):
g = geometry_for("cube(10); translate([20,0,0]) cube(10); translate([40,0,0]) cube(10);", tmp_path)
out = tmp_path / "split.3mf"
exporters.export_model(str(out), g, split_components=True)
assert _objects_in(out) == 3
def test_cli_passes_the_flag_through(tmp_path):
"""--split-components reaches the writer, not just argparse."""
from belfryscad import headless
src = tmp_path / "pieces.scad"
src.write_text("cube(10); translate([20,0,0]) cube(10);")
for split, expected in ((False, 1), (True, 2)):
out = tmp_path / f"cli-{split}.3mf"
assert headless.render_and_export(str(src), str(out), quiet=True,
split_components=split) == 0
assert _objects_in(out) == expected
# --- SVG: the one 2D format (#367) ------------------------------------
def test_svg_export_writes_a_2d_model_at_true_size(tmp_path):
"""The boundary check the rest of this file makes for the mesh formats.
The bytes themselves are pinned against real OpenSCAD in the
evaluator's own suite."""
g = geometry_for("square([70.4, 25.3]);", tmp_path)
out = tmp_path / "flat.svg"
assert exporters.export_model(str(out), g) == [] # no mesh checks apply
text = out.read_text()
# mm, 1:1 -- that is what makes a print measure what the script says.
assert 'width="72mm" height="27mm"' in text
assert "<path" in text
def test_svg_export_refuses_a_3d_model(tmp_path):
"""OpenSCAD's rule: 2D export needs an all-2D top level. Refusing is
the honest answer -- a silhouette would be a different model."""
g = geometry_for("cube(10);", tmp_path)
with pytest.raises(Exception, match="not a 2D object"):
exporters.export_model(str(tmp_path / "solid.svg"), g)
# --- PDF: 2D on a fixed page (#368) -----------------------------------
def test_pdf_export_centres_the_drawing_on_a4_by_default(tmp_path):
g = geometry_for("square([70, 25]);", tmp_path)
out = tmp_path / "flat.pdf"
assert exporters.export_model(str(out), g) == []
data = out.read_bytes()
assert data.startswith(b"%PDF-1.4")
assert b"/MediaBox [0 0 595 842]" in data
# The ruler is the reason the format exists; base-14 Helvetica is why
# it needs no embedded font.
assert b"/BaseFont /Helvetica" in data
assert b"/FontFile" not in data
def test_pdf_options_reach_the_writer(tmp_path):
g = geometry_for("square([70, 25]);", tmp_path)
out = tmp_path / "letter.pdf"
exporters.export_model(str(out), g, pdf_options={"paper-size": "letter",
"orientation": "landscape"})
assert b"/MediaBox [0 0 792 612]" in out.read_bytes()
def test_pdf_export_refuses_a_3d_model(tmp_path):
g = geometry_for("cube(10);", tmp_path)
with pytest.raises(Exception, match="not a 2D object"):
exporters.export_model(str(tmp_path / "solid.pdf"), g)
def test_pdf_dialog_answers_become_export_arguments():
"""The export dialog's answers, turned into export_model() arguments."""
from belfryscad.window.export_options import export_kwargs
answers = {
"export/pdfPaperSize": "letter",
"export/pdfOrientation": "landscape",
"export/pdfShowScale": True,
"export/pdfShowGrid": False,
}
opts = export_kwargs(".pdf", answers, "scale-card.scad")["pdf_options"]
assert opts["paper-size"] == "letter"
assert opts["orientation"] == "landscape"
assert opts["design-filename"] == "scale-card.scad"
assert opts["show-filename"] is True
# No script name means nothing to draw, so the switch goes off with it.
assert export_kwargs(".pdf", answers)["pdf_options"]["show-filename"] is False
62c9a9e10This file is pinned to the selected release. Download the original to open it in your design tool.