aboutsummaryrefslogtreecommitdiff
path: root/hw/clef/nix/svg.nix
diff options
context:
space:
mode:
Diffstat (limited to 'hw/clef/nix/svg.nix')
-rw-r--r--hw/clef/nix/svg.nix92
1 files changed, 92 insertions, 0 deletions
diff --git a/hw/clef/nix/svg.nix b/hw/clef/nix/svg.nix
new file mode 100644
index 0000000..c93ba87
--- /dev/null
+++ b/hw/clef/nix/svg.nix
@@ -0,0 +1,92 @@
+{
+ runCommand,
+ kicad,
+ inkscape,
+
+ nix-filter,
+ lib,
+
+ pcb_path,
+ src,
+
+ withSilk ? true,
+ withEdgeCuts ? true,
+ withMirrors ? true,
+ nLayer ? 2,
+
+ boardName ? (lib.removeSuffix ".kicad_pcb" (builtins.baseNameOf pcb_path)),
+}: let
+ sharePath = "share/npry/clef/svg";
+
+in runCommand "${boardName}.svg" {
+ nativeBuildInputs = [
+ kicad
+ inkscape
+ ];
+
+ src = nix-filter {
+ root = src;
+
+ include = [
+ (nix-filter.matchExt "kicad_pcb")
+ ];
+ };
+
+ nInnerLayer = if nLayer < 2 then 0 else nLayer - 2;
+
+ allowedRequisites = [];
+} ''
+ set -e
+
+ export HOME=$(mktemp -d)
+
+ echo "board: '${boardName}'" >&2
+
+ mkdir -p $out/${sharePath}
+ cd "$out/${sharePath}"
+
+ mksvg() {
+ local infile=$1
+ local layers=$2
+ local outfile=$3
+
+ kicad-cli pcb export svg \
+ "$infile" \
+ -l "$layers" \
+ -o "$outfile.svg" \
+ --page-size-mode 2 \
+ --exclude-drawing-sheet
+
+ inkscape --actions 'select-all;fit-canvas-to-selection' \
+ -o "$outfile.trim.svg" \
+ "$outfile.svg"
+
+ ${if withMirrors then ''
+ kicad-cli pcb export svg \
+ "$infile" \
+ -m \
+ -l "$layers" \
+ -o "$outfile.mirror.svg" \
+ --page-size-mode 2 \
+ --exclude-drawing-sheet
+
+ inkscape --actions 'select-all;fit-canvas-to-selection' \
+ -o "$outfile.mirror.trim.svg" \
+ "$outfile.mirror.svg"
+ '' else ""}
+ }
+
+ mksvg "$src/${pcb_path}" \
+ "F.Cu,${if withSilk then "F.Silkscreen," else ""}${if withEdgeCuts then "Edge.Cuts," else ""}" \
+ front
+
+ mksvg "$src/${pcb_path}" \
+ "B.Cu,${if withSilk then "B.Silkscreen," else ""}${if withEdgeCuts then "Edge.Cuts," else ""}" \
+ back
+
+ for i in $(seq 1 $nInnerLayer); do
+ mksvg "$src/${pcb_path}" \
+ "In$i.Cu,${if withEdgeCuts then "Edge.Cuts," else ""}" \
+ "in$i"
+ done
+''