script improvements

This commit is contained in:
cpu
2025-12-12 00:29:57 +01:00
parent 7c752054c5
commit 92cf4ba22b
26 changed files with 3471 additions and 3304 deletions

View File

@@ -1,66 +1,155 @@
#!/bin/bash
# --- CONFIGURATION ---
# Dimensions (Millimeters)
# Set the desired width in mm (e.g. 55.15).
# Leave as "0" to keep the original size from the SVG.
TARGET_WIDTH_MM="45.08"
# Thickness of the 3D model in mm
# --- DEFAULTS ---
THICKNESS_MM="0.1"
EXPORT_DPI="1494"
PADDING_MM="0.0" # Default padding
# Print resolution
# DPI based on the 3D printer resolution (e.g. 1494 for Anycubic Photon Mono 4)
EXPORT_DPI=1494
# --- ARGUMENT PARSING ---
INVERT_SUFFIXES=()
PROJECT_PREFIX=""
# Check for at least one input file
if [ "$#" -lt 1 ]; then
echo "Error: No input files provided."
echo "Usage: ./svg2stl.sh <file1.svg> [file2.svg ...]"
# Helper function to show usage
show_usage() {
echo "Usage: ./svg2stl.sh [options] <Project_Path_Prefix>"
echo ""
echo "Options:"
echo " -t, --thickness <mm> Final 3D model thickness (Default: $THICKNESS_MM)"
echo " -p, --padding <mm> Extra margin around inverted models (Default: $PADDING_MM)"
echo " -d, --printer-lcd-dpi <dpi> DPI for Inkscape export (Default: $EXPORT_DPI)"
echo " -i, --invert <Suffix> Generate inverted (negative) model for this suffix"
echo ""
echo "Example:"
echo " ./svg2stl.sh --thickness 0.2 --padding 5.0 -i B_Mask examples/MyBoard"
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
-t|--thickness)
if [[ -n "$2" && "$2" != -* ]]; then
THICKNESS_MM="$2"
shift; shift
else
echo "Error: --thickness requires a value (mm)."
exit 1
fi
;;
-p|--padding)
if [[ -n "$2" && "$2" != -* ]]; then
PADDING_MM="$2"
shift; shift
else
echo "Error: --padding requires a value (mm)."
exit 1
fi
;;
-d|--printer-lcd-dpi)
if [[ -n "$2" && "$2" != -* ]]; then
EXPORT_DPI="$2"
shift; shift
else
echo "Error: --printer-lcd-dpi requires a value."
exit 1
fi
;;
-i|--invert)
if [[ -n "$2" && "$2" != -* ]]; then
INVERT_SUFFIXES+=("$2")
shift; shift
else
echo "Error: --invert requires a suffix argument."
exit 1
fi
;;
-h|--help)
show_usage
;;
*)
if [[ -z "$PROJECT_PREFIX" ]]; then
PROJECT_PREFIX="$1"
shift
else
echo "Error: Unknown argument '$1' or Project Prefix already set."
show_usage
fi
;;
esac
done
if [[ -z "$PROJECT_PREFIX" ]]; then
echo "Error: No Project Prefix provided."
show_usage
fi
# Loop over all input files
for INPUT in "$@"; do
INPUT_FILE="$(readlink -f "$INPUT")"
# Find files matching the prefix
shopt -s nullglob
FILES=(${PROJECT_PREFIX}*.svg)
shopt -u nullglob
echo "Processing: $INPUT_FILE"
if [ ${#FILES[@]} -eq 0 ]; then
echo "Error: No files found starting with '${PROJECT_PREFIX}' ending in .svg"
exit 1
fi
# Setup Absolute Paths
WORK_DIR="$(dirname "$INPUT_FILE")"
BASENAME="$(basename "$INPUT_FILE" .svg)"
TEMP_SVG="$WORK_DIR/${BASENAME}_temp_processed.svg"
OUTPUT_STL="$WORK_DIR/$BASENAME.stl"
PREFIX_BASENAME="$(basename "$PROJECT_PREFIX")"
# Inkscape Processing
# Note: We do NOT resize here. We export high-res geometry and resize in Blender.
echo "========================================"
echo "Project Name: $PREFIX_BASENAME"
echo "Thickness: $THICKNESS_MM mm"
echo "Padding: $PADDING_MM mm (Inverted only)"
echo "Printer DPI: $EXPORT_DPI"
echo "Invert Suffixes: ${INVERT_SUFFIXES[*]}"
echo "========================================"
for INPUT_FILE in "${FILES[@]}"; do
# Resolve paths
INPUT_FILE="$(readlink -f "$INPUT_FILE")"
DIR="$(dirname "$INPUT_FILE")"
FILENAME="$(basename "$INPUT_FILE")"
BASENAME="${FILENAME%.*}"
# --- Suffix Extraction ---
SUFFIX_TEMP="${BASENAME#$PREFIX_BASENAME}"
SUFFIX="${SUFFIX_TEMP#-}"
# Check invert mode
INVERT_MODE="0"
for i in "${INVERT_SUFFIXES[@]}"; do
if [[ "$i" == "$SUFFIX" ]]; then
INVERT_MODE="1"
break
fi
done
echo "Processing: $FILENAME"
if [ "$INVERT_MODE" == "1" ]; then
echo " -> Mode: INVERTED (Negative) + ${PADDING_MM}mm Padding"
OUTPUT_STL="$DIR/${BASENAME}_inverted.stl"
else
echo " -> Mode: STANDARD (Positive)"
OUTPUT_STL="$DIR/${BASENAME}.stl"
fi
TEMP_SVG="$DIR/${BASENAME}_temp_proc.svg"
# --- Step 1: Inkscape ---
ACTIONS="select-by-element:text;delete;select-all;object-stroke-to-path;path-union;export-plain-svg;export-filename:$TEMP_SVG;export-do"
INK_ARGS="--export-dpi=$EXPORT_DPI --actions=$ACTIONS"
echo "------------------------------------------------"
echo "Step 1: Inkscape Processing"
echo "Input: $INPUT_FILE"
echo "------------------------------------------------"
inkscape "$INPUT_FILE" $INK_ARGS
inkscape "$INPUT_FILE" $INK_ARGS > /dev/null 2>&1
if [ ! -f "$TEMP_SVG" ]; then
echo "ERROR: Inkscape failed to create the temp file."
exit 1
echo " ERROR: Inkscape failed to process $FILENAME"
continue
fi
echo "------------------------------------------------"
echo "Step 2: Blender Processing"
echo "Target Width: ${TARGET_WIDTH_MM} mm"
echo "Target Thickness: ${THICKNESS_MM} mm"
echo "------------------------------------------------"
# --- Step 2: Blender ---
# Passing PADDING_MM as the 5th argument
blender --background --python pcb_to_stl.py -- "$TEMP_SVG" "$OUTPUT_STL" "$THICKNESS_MM" "$INVERT_MODE" "$PADDING_MM" | grep -v "Blender" | grep -v "Read prefs"
# Pass Width and Thickness to Python
blender --background --python pcb_to_stl.py -- "$TEMP_SVG" "$OUTPUT_STL" "$THICKNESS_MM" "$TARGET_WIDTH_MM"
# Cleanup
rm "$TEMP_SVG"
echo "Done! Saved to $OUTPUT_STL"
# ---------------------
echo " -> Done."
echo "----------------------------------------"
done