threadlib/design/metric_thread.awk
Adrian Schlatter 827723d1b1 Eliminate threads.xlsx
Replaces threads.xlsx by one .csv file and one awk script per thread
type. The script calculates threadlib specs from the data in the .csv
file. Finally and as before, autogenerate.awk generates the OpenSCAD
code (i.e. THREAD_TABLE.scad).

A new Makefile automates this process: make does what is needed. It
also provides a target 'test' to run the (existing) tests.

To add a new thread type:

- provide newthread.csv
- provide newthread.awk
- modify Makefile to generate your new threads
- add appropriate tests

Additionally:

- fixed bug in Rrot when DRmax is too large.
- reduced number of significant figures to a resolution of 0.1 um
  (while not extremely generous for the very finest threads, it
  should be enough by far for any 3D-prints).
- increased test limit for thread angle deviations (necessary due to
  reduced number of significant figures)
- added test counter: 'make test' now prints number of tests run +
  number of threads tested.
- For unknown reasons (MacOS awk?) final match-all rule in
  test_table.awk does not match with double-quotes. Work-around: Match
  without double-quotes.
2019-05-05 22:29:09 +02:00

72 lines
1.8 KiB
Awk

function tan(x) {
return(sin(x) / cos(x))
}
function min(x, y) {
if (x < y) return(x)
else return(y)
}
function calculateThreadlibSpecs() {
Designator = $3;
P = $4;
H = P / 2 / tan(phi / 2);
DPitchExt = ($8 + $9) / 2;
DSupportExt = ($10 + $11) / 2;
DValleyExt = $11;
DCrestExt = ($6 + $7) / 2;
ZValleyExt = (DPitchExt + H - DValleyExt) / 2 * tan(phi / 2);
ZCrestExt = (DPitchExt + H - DCrestExt) / 2 * tan(phi / 2);
DPitchInt = ($15 + $16) / 2;
DSupportInt = ($17 + $18) / 2;
DValleyInt = $18;
DCrestInt = ($13 + $14) / 2;
# for some reason, the data sometimes has dRMax > H. We have to fix this:
dRMax = min((DValleyInt - DPitchInt + H) / 2, 0.99 * H);
DValleyInt = DPitchInt - H + 2 * dRMax;
# now, continue as usual
ZValleyInt = dRMax * tan(phi / 2);
ZCrestInt = (DCrestInt - DPitchInt + H) / 2 * tan(phi / 2);
}
BEGIN {
FS = "\t";
pi = atan2(0, -1);
deg = pi / 180;
phi = 60 * deg;
}
/^[^#]/ {
calculateThreadlibSpecs();
# External thread:
printf Designator "-ext," # designator
printf P "," # pitch
printf "%.4f,", DValleyExt / 2 # Rrot
printf "%.4f,", DSupportExt # Dsupport
printf 0 "," # r0
printf "%.4f,", -ZValleyExt # z0
printf 0 "," # r1
printf "%.4f,", +ZValleyExt # z1
printf "%.4f,", (DCrestExt - DValleyExt) / 2 # r2
printf "%.4f,", +ZCrestExt # z2
printf "%.4f,", (DCrestExt - DValleyExt) / 2 # r3
printf "%.4f\n", -ZCrestExt; # z3
# Internal thread:
printf Designator "-int," # designator
printf P "," # pitch
printf "%.4f,", -DValleyInt / 2 # Rrot
printf "%.4f,", DSupportInt # Dsupport
printf 0 "," # r0
printf "%.4f,", +ZValleyInt # z0
printf 0 "," # r1
printf "%.4f,", -ZValleyInt # z1
printf "%.4f,", -(DCrestInt - DValleyInt) / 2 # r2
printf "%.4f,", -ZCrestInt # z2
printf "%.4f,", -(DCrestInt - DValleyInt) / 2 # r3
printf "%.4f\n", +ZCrestInt; # z3
}