aboutsummaryrefslogtreecommitdiff
path: root/clef/nix/svg.nix
blob: fc1a404e2ba690f8b00b2ce135237ce75d0591ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
  runCommand,
  kicad,

  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
  ];

  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

  ${if withMirrors then ''
    kicad-cli pcb export svg \
      "$infile" \
      -m \
      -l "$layers" \
      -o "$outfile.mirror.svg" \
      --page-size-mode 2 \
      --exclude-drawing-sheet
  '' 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
''