aboutsummaryrefslogtreecommitdiff
path: root/nix/model.nix
blob: 04bf1b1c7b0d195749e95be8cc413935ea58ca1d (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
{
  kicad,
  runCommand,

  nix-filter,
  lib,

  withTracks ? false,
  withZones ? false,

  src,
  pcb_path,

  boardName ? (lib.removeSuffix ".kicad_pcb" (builtins.baseNameOf pcb_path)),
}: let
  zonesArg = if withZones then "--include-zones" else "";
  tracksArg = if withTracks then "--include-tracks" else "";
  sharePath = "share/npry/clef/model";

in runCommand "${boardName}.model" {
  nativeBuildInputs = [
    kicad
  ];

  src = nix-filter {
    root = src;

    include = [
      (nix-filter.matchExt "kicad_pcb")

      (nix-filter.matchExt "stp")
      (nix-filter.matchExt "step")
      (nix-filter.matchExt "wrl")
    ];
  };

  allowedRequisites = [];

  KICAD8_3DMODEL_DIR = "${kicad.libraries.packages3d}/share/kicad/3dmodels";
} ''
  set -e

  export HOME=$(mktemp -d)

  echo "board: '${boardName}'" >&2

  mkdir -p $out/${sharePath}
  cd "$out/${sharePath}"

  kicad-cli pcb export step --subst-models --no-dnp ${tracksArg} ${zonesArg} "$src/${pcb_path}" -o "${boardName}.step"
  kicad-cli pcb export glb  --subst-models --no-dnp ${tracksArg} ${zonesArg} "$src/${pcb_path}" -o "${boardName}.glb"
''