Monday, April 7, 2025

Bash Script for Machining Hexagons

 I rewrote this to be clearer.

#!/bin/bash
if [[ $# -lt 8 ]];
   then echo "$0 xstart ystart zstart zstep zend xyfeed zfeed height millradius"
   exit 2;
fi
cat prolog.nc >hexagon.nc
xstart=$1
ystart=$2
zstart=$3
zstep=$4
zend=$5
xyfeed=$6
zfeed=$7
height=$8
millradius=$9
halfheight=`echo "$height/2" | bc -l`
x1=`echo "$xstart+$millradius" | bc -l`
y1=`echo "$ystart+$millradius" |bc -l`
sqrt3div2=`echo "($height * .866)/2" | bc -l`
x2=$x1
y2=`echo "$ystart+$height-$millradius" | bc -l`
x3=`echo "$x2+(.5*$height)-$millradius" | bc -l`
y3=`echo "$y2+$sqrt3div2-$millradius" | bc -l`
x4=`echo "$x2+$height-$millradius" | bc -l`
y4=`echo "$y2-$millradius" | bc -l`
x5=$x4
y5=$y1
x6=$x3
y6=`echo "$y1-$sqrt3div2+$millradius" | bc -l`
cat prolog.nc >hexagon.nc
echo "g1 z1 f25" >>hexagon.nc
echo "g1 z.1 f25" >>hexagon.nc
for z in $(seq $zstart $zstep $zend)
do
    echo "(point 1)" >>hexagon.nc
    echo "g1 x$x1 y$y1 f$xyfeed" >>hexagon.nc
    echo "g1 z$z f$zfeed" >>hexagon.nc
    echo "(point 2)" >>hexagon.nc
    echo "g1 x$x2 y$y2 f$xyfeed" >>hexagon.nc
    echo "(point 3)" >>hexagon.nc
    echo "g1 x$x3 y$y3 f$xyfeed" >>hexagon.nc
    echo "(point 4)" >>hexagon.nc
    echo "g1 x$x4 y$y4 f$xyfeed" >>hexagon.nc
    echo "(point 5)" >>hexagon.nc
    echo "g1 x$x5 y$y5 f$xyfeed" >>hexagon.nc
    echo "(point 6)" >>hexagon.nc
    echo "g1 x$x6 y$y6 f$xyfeed" >>hexagon.nc
done
echo "g1 z1 f25" >>hexagon.nc
cat epilog.nc >>hexagon.nc

It only cuts the outline right now.  I need a hole for hex nuts to slide into, not an outline.  The next step is to write a wrapper that cuts the outline, then a series of progressively smaller hexagons inside it.  You may recall the Larry Niven story "Converging Series" with Satan continuously forced to reappear inside a pentagram drawn on his abdomen.  That is the idea, except I am not trying to avoid loss of my soul.

No comments:

Post a Comment