Add Python tests for the hole cutouts to generate images.

* These are not (yet) triggered/run by the CI/CD system.
This commit is contained in:
Arthur Moore 2024-04-27 00:31:19 -04:00
parent ca28aed898
commit 0649f10802
4 changed files with 142 additions and 2 deletions

6
.gitignore vendored
View file

@ -6,3 +6,9 @@ stl/
batch/ batch/
site/ site/
*.json *.json
# From https://github.com/github/gitignore/blob/main/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

View file

@ -263,12 +263,17 @@ module block_base_hole(hole_options, o=0) {
//$fa = 8; //$fa = 8;
//$fs = 0.25; //$fs = 0.25;
if(!is_undef(test_options)){
block_base_hole(test_options);
}
//block_base_hole(bundle_hole_options( //block_base_hole(bundle_hole_options(
// refined_hole=false, // refined_hole=false,
// magnet_hole=true, // magnet_hole=true,
// screw_hole=true, // screw_hole=true,
// supportless=true, // supportless=true,
// crush_ribs=true, // crush_ribs=false,
// chamfer=true // chamfer=false
//)); //));
//make_hole_printable(1, 3, 0); //make_hole_printable(1, 3, 0);

44
tests/openscad_runner.py Normal file
View file

@ -0,0 +1,44 @@
"""
Helpful classes for running OpenScad from Python.
@Copyright Arthur Moore 2024 MIT License
"""
from typing import NamedTuple
class Vec3(NamedTuple):
'''Simple 3d Vector (x, y, z)'''
x: float
y: float
z: float
class CameraArguments(NamedTuple):
"""
Controls the camera position when outputting to png format.
@see `openscad -h`.
"""
translate: Vec3
rotate: Vec3
distance: float
def as_argument(self):
return '--camera=' \
f'{",".join(map(str,self.translate))},{",".join(map(str,self.rotate))},{self.distance}'
def set_variable_argument(var: str, val) -> [str, str]:
"""
Allows setting a variable to a particular value.
@warning value **can** be a function, but this is called for every file, so may generate 'undefined' warnings.
"""
return ['-D', f'{var}={str(val)}']
openscad_binary_windows = 'C:\Program Files\OpenSCAD\openscad.exe'
common_arguments = [
#'--hardwarnings', // Does not work when setting variables by using functions
'--enable=fast-csg',
'--enable=predictible-output',
'--imgsize=1280,720',
'--view=axes',
'--projection=ortho',
] + set_variable_argument('$fa', 8) + set_variable_argument('$fs', 0.25)
top_angle_camera = CameraArguments(Vec3(0,0,0),Vec3(45,0,45),50)

85
tests/test_holes.py Normal file
View file

@ -0,0 +1,85 @@
"""
Functions for testing hole cutouts.
@Copyright Arthur Moore 2024 MIT License
"""
from pathlib import Path
from openscad_runner import *
import subprocess
import unittest
class TestHoles(unittest.TestCase):
"""
Test Hole Cutouts.
Currently only makes sure code runs, and outputs pictures for manual verification.
"""
scad_file_path = Path('../gridfinity-rebuilt-holes.scad')
image_folder_base = Path('../images/hole_cutouts/')
def run_image(self, camera_args: CameraArguments, test_args: [str], image_file_name: str):
"""
Run the code, to create an image.
@Important The only verification is that no errors occured.
There is no verification if the image was created, or the image contents.
"""
assert(self.scad_file_path.exists())
image_path = self.image_folder_base.joinpath(image_file_name)
command_arguments = [openscad_binary_windows] + common_arguments + \
[camera_args.as_argument()] + test_args + \
[f'-o{str(image_path)}', str(self.scad_file_path)]
print(command_arguments)
return subprocess.run(command_arguments, check=True)
def test_refined_hole(self):
"""
refined_hole() is special, since top_angle_camera is not appropriate for it.
"""
camera_args = CameraArguments(Vec3(0,0,0),Vec3(225,0,225),50)
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=true, magnet_hole=false, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(camera_args, test_args, Path('refined_hole.png'))
def test_plain_magnet_hole(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole.png'))
def test_plain_screw_hole(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=false, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('screw_hole.png'))
def test_magnet_and_screw_hole(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole.png'))
def test_chamfered_magnet_hole(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=true, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('chamfered_magnet_hole.png'))
def test_magnet_hole_crush_ribs(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=true, chamfer=false, supportless=false)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole_crush_ribs.png'))
def test_magnet_hole_supportless(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('magnet_hole_supportless.png'))
def test_magnet_and_screw_hole_supportless(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole_supportless.png'))
def test_all_hole_options(self):
test_args = set_variable_argument('test_options',
'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=true, chamfer=true, supportless=true)')
self.run_image(top_angle_camera, test_args, Path('all_hole_options.png'))
if __name__ == '__main__':
unittest.main()