~cypheon/kicad2spice

ref: 92c8156c1a874a8210631fdf9e15dceeac19cdcc kicad2spice/lib/schematic.ml -rw-r--r-- 1.6 KiB
92c8156c — Johann Rudloff Move write_* functions closer together. 3 years ago
                                                                                
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
83
84
85
86
87
88
89
90
91
92
include Geometry

module Fields = struct
  include Map.Make(String)

  module Keys = struct
    let spice_netlist_enabled = "Spice_Netlist_Enabled"
    let spice_primitive = "Spice_Primitive"
  end

  let pp pval fmt m = let open Format in
    pp_print_string fmt "{Fields.t";
    pp_open_hbox fmt ();
    let first = ref true in
    Seq.iter (fun (k,v) ->
      if not !first then pp_print_string fmt ","; first := false;
      pp_print_break fmt 1 2;
      fprintf fmt "\"%s\" => %a" k pval v;
    )
    (to_seq m);
    pp_close_box fmt ();
    pp_print_string fmt "}"

  let get fields k default_value =
    Stdlib.Option.value (find_opt k fields) ~default:default_value
end

module Wire = struct
  type t = {
    startp : point2i;
    endp : point2i;
  }
  [@@deriving show]
end

module Pin = struct
  type t = {
    name : string;
    number : string;
    pos : point2i;
  }
  [@@deriving show]
end

module CompDef = struct
  type t = {
    name : string;
    pins : Pin.t list;
    power : bool;
    aliases : string list;
  }
  [@@deriving show]
end

(* Resolved component *)
module Comp = struct
  type t = {
    name : string;
    ref : string;
    value : string;
    fields : string Fields.t;
    pos : point2i;
    transform : mat22i;
    part : CompDef.t;
  }
  [@@deriving show]
end

module Junction = struct
  type t = {
    pos : point2i;
  }
  [@@deriving show]
end

module Item = struct
  type t =
    | Wire of Wire.t
    | Comp of Comp.t
    | Junction of Junction.t
    | Pin of (Comp.t * Pin.t)
  [@@deriving show]
end

type value = {
  version : int;
  items : Item.t list;
}
[@@deriving show]

let print fmt c =
  Printf.fprintf fmt "%s" (show_value c)