Compare commits

...

4 Commits

Author SHA1 Message Date
Arthur Moore f88f60446f
Merge 0649f10802 into 36345f3efb 2024-04-27 04:31:33 +00:00
Arthur Moore 0649f10802 Add Python tests for the hole cutouts to generate images.
* These are not (yet) triggered/run by the CI/CD system.
2024-04-27 00:31:19 -04:00
Arthur Moore ca28aed898 Allow multiple levels for supportless holes
Helps prevent / minimize issues with filament droop.
Expecially when Cura decides to start the top of hole in mid-air.

Visible Changes:
* Supportless screw holes have a 3rd layer.
* Supportless magnet holes without screw holes have a 3rd layer.

Backend Changes:
* Switched to a completely different generation strategy.
  * Previous strategy directly produced negative.
  * New strategy is to make a positive,
    then use that to create a negative.
* Algorithm for multiple layers is not perfect,
  but works within tollerances set.
2024-04-27 00:18:18 -04:00
Arthur Moore 1a50807295 Make Screw Holes Printable 2024-04-24 21:22:56 -04:00
6 changed files with 204 additions and 42 deletions

6
.gitignore vendored
View File

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

View File

@ -5,6 +5,8 @@
function clp(x,a,b) = min(max(x,a),b);
function is_even(number) = (number%2)==0;
module rounded_rectangle(length, width, height, rad) {
linear_extrude(height)
offset(rad)

View File

@ -92,10 +92,11 @@ screw_holes = false;
crush_ribs = false;
// Magnet holes will have a chamfer to ease insertion.
chamfer_magnet_holes = false;
// Allows printing screw holes with magnet holes without using supports.
printable_magnet_hole_top = false;
chamfer_magnet_holes = true;
// Screw holes and magnet holes will be printed so supports are not needed.
printable_hole_top = true;
hole_options = bundle_hole_options(refined_hole, magnet_holes, screw_holes, crush_ribs, chamfer_magnet_holes, printable_magnet_hole_top);
hole_options = bundle_hole_options(refined_hole, magnet_holes, screw_holes, crush_ribs, chamfer_magnet_holes, printable_hole_top);
// ===== IMPLEMENTATION ===== //

View File

@ -50,7 +50,6 @@ module ribbed_circle(outer_radius, inner_radius, ribs) {
polygon(wrapped_circle);
}
/**
* @brief A cylinder with crush ribs to give a tighter press fit.
* @details To be used as the negative for a hole.
@ -70,52 +69,64 @@ module ribbed_cylinder(outer_radius, inner_radius, height, ribs) {
);
}
/**
* @brief Make a hole printable without suports.
* @see https://www.youtube.com/watch?v=W8FbHTcB05w
* @param inner_radius Radius of the inner hole.
* @param outer_radius Radius of the outer hole.
* @param outer_depth Depth of the magnet hole.
* @param outer_height Height of the outer hole.
* @param layers Number of layers to make printable.
* @details This is the negative designed to be cut out of the magnet hole.
* Use it with `difference()`.
* Special handling is done to support a single layer,
* and because the last layer (unless there is only one) has a different shape.
*/
module make_hole_printable(inner_radius, outer_radius, outer_depth) {
module make_hole_printable(inner_radius, outer_radius, outer_height, layers=2) {
assert(inner_radius > 0, "inner_radius must be positive");
assert(outer_radius > 0, "outer_radius must be positive");
assert(outer_depth > 2*LAYER_HEIGHT, str("outer_depth must be at least ", 2*LAYER_HEIGHT));
tollerance = 0.001; // To make sure the top layer is fully removed
assert(layers > 0);
translation_matrix = affine_translate([
-outer_radius,
inner_radius,
outer_depth - 2*LAYER_HEIGHT
]);
second_translation_matrix = translation_matrix * affine_translate([0, 0, LAYER_HEIGHT]);
tollerance = 0.01; // Ensure everything is fully removed.
height_adjustment = outer_height - (layers * LAYER_HEIGHT);
cube_dimensions = [
outer_radius*2,
outer_radius - inner_radius,
LAYER_HEIGHT + tollerance
// Needed, since the last layer should not be used for calculations,
// unless there is a single layer.
calculation_layers = max(layers-1, 1);
cube_height = LAYER_HEIGHT + 2*tollerance;
inner_diameter = 2*(inner_radius+tollerance);
outer_diameter = 2*(outer_radius+tollerance);
per_layer_difference = (outer_diameter-inner_diameter) / calculation_layers;
initial_matrix = affine_translate([0, 0, cube_height/2-tollerance + height_adjustment]);
// Produces data in the form [affine_matrix, [cube_dimensions]]
// If layers > 1, the last item produced has an invalid "affine_matrix.y", because it is beyond calculation_layers.
// That is handled in a special case to avoid doing a check every loop.
cutout_information = [
for(i=0; i <= layers; i=i+1)
[
initial_matrix * affine_translate([0, 0, (i-1)*LAYER_HEIGHT]) *
affine_rotate([0, 0, is_even(i) ? 90 : 0]),
[outer_diameter-per_layer_difference*(i-1),
outer_diameter-per_layer_difference*i,
cube_height]
]
];
union(){
union() {
multmatrix(translation_matrix)
cube(cube_dimensions);
multmatrix(affine_rotate([0, 0, 180]) * translation_matrix)
cube(cube_dimensions);
difference() {
translate([0, 0, layers*cube_height/2 + height_adjustment])
cube([outer_diameter+tollerance, outer_diameter+tollerance, layers*cube_height], center = true);
for (i = [1 : calculation_layers]){
data = cutout_information[i];
multmatrix(data[0])
cube(data[1], center = true);
}
//2nd level
union() {
multmatrix(second_translation_matrix)
cube(cube_dimensions);
multmatrix(affine_rotate([0, 0, 90]) * second_translation_matrix)
cube(cube_dimensions);
multmatrix(affine_rotate([0, 0, 180]) * second_translation_matrix)
cube(cube_dimensions);
multmatrix(affine_rotate([0, 0, 270]) * second_translation_matrix)
cube(cube_dimensions);
if(layers > 1) {
data = cutout_information[len(cutout_information)-1];
multmatrix(data[0])
cube([data[1].x, data[1].x, data[1].z], center = true);
}
}
}
@ -209,10 +220,10 @@ module block_base_hole(hole_options, o=0) {
magnet_radius = MAGNET_HOLE_RADIUS - (o/2);
magnet_inner_radius = MAGNET_HOLE_CRUSH_RIB_INNER_RADIUS - (o/2);
screw_depth = h_base-o;
// If using supportless / printable mode, need to add two additional layers, so they can be removed later.
supportless_additional_depth = 2* LAYER_HEIGHT;
// If using supportless / printable mode, need to add additional layers, so they can be removed later.
supportless_additional_layers = screw_hole ? 2 : 3;
magnet_depth = MAGNET_HOLE_DEPTH - o +
(supportless ? supportless_additional_depth : 0);
(supportless ? supportless_additional_layers*LAYER_HEIGHT : 0);
union() {
if(refined_hole) {
@ -228,7 +239,8 @@ module block_base_hole(hole_options, o=0) {
}
if(supportless) {
make_hole_printable(screw_radius, magnet_radius, magnet_depth);
make_hole_printable(
screw_hole ? screw_radius : 1, magnet_radius, magnet_depth, supportless_additional_layers);
}
}
@ -238,18 +250,30 @@ module block_base_hole(hole_options, o=0) {
}
if(screw_hole) {
cylinder(h = screw_depth, r = screw_radius);
difference() {
cylinder(h = screw_depth, r = screw_radius);
if(supportless) {
rotate([0, 0, 90])
make_hole_printable(0.5, screw_radius, screw_depth, 3);
}
}
}
}
}
//$fa = 8;
//$fs = 0.25;
if(!is_undef(test_options)){
block_base_hole(test_options);
}
//block_base_hole(bundle_hole_options(
// refined_hole=false,
// magnet_hole=true,
// screw_hole=true,
// supportless=true,
// crush_ribs=true,
// chamfer=true
// crush_ribs=false,
// chamfer=false
//));
//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()