~cypheon/kicad2spice

ref: 96e390bc4f914314da98ecdfd7bd775e620563e2 kicad2spice/lib/schematic.ml -rw-r--r-- 1.9 KiB
96e390bc — Johann Rudloff Implement correct pin order according to field "Spice_Node_Sequence" 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
93
94
95
96
97
98
99
100
include Geometry

module Fields = struct
  include Map.Make(String)

  module Keys = struct
    let spice_netlist_enabled = "Spice_Netlist_Enabled"
    let spice_primitive = "Spice_Primitive"
    let spice_node_sequence = "Spice_Node_Sequence"
  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]

  let number {number;_} = number
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]

  let find_pin_opt {part;_} pin_number = List.find_opt (fun p -> p.Pin.number = pin_number) part.pins
  let find_pin comp pin_number = match find_pin_opt comp pin_number with
    | Some pin -> pin
    | None -> raise Not_found
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)