Compare commits
3 Commits
3e94c65cf2
...
0649f10802
Author | SHA1 | Date |
---|---|---|
Arthur Moore | 0649f10802 | |
Arthur Moore | ca28aed898 | |
Arthur Moore | 1a50807295 |
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 ===== //
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
|
@ -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()
|
Loading…
Reference in New Issue