aboutsummaryrefslogtreecommitdiff
path: root/clef/nix/model.nix
blob: da8be8296830291027765796164a629b8f2242bd (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
{
  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 "kicad_pro")

      (nix-filter.matchExt "stp")
      (nix-filter.matchExt "STP")
      (nix-filter.matchExt "step")
      (nix-filter.matchExt "STEP")
      (nix-filter.matchExt "wrl")

      (_args: path: type: type == "directory")
    ];
  };

  allowedRequisites = [];

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

  export HOME=$(mktemp -d)

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

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

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