~cypheon/kicad2spice

96e390bc4f914314da98ecdfd7bd775e620563e2 — Johann Rudloff 1 year, 10 months ago 92c8156
Implement correct pin order according to field "Spice_Node_Sequence"
2 files changed, 17 insertions(+), 1 deletions(-)

M lib/export_spice.ml
M lib/schematic.ml
M lib/export_spice.ml => lib/export_spice.ml +9 -1
@@ 32,10 32,18 @@ let make_unique used name =
    in helper 2
  end

let pin_order comp =
  let default_order = List.map Pin.number comp.Comp.part.CompDef.pins in
  Stdlib.Option.value ~default:default_order (
    Fields.find_opt Fields.Keys.spice_node_sequence comp.Comp.fields
    |> Stdlib.Option.map Lex_tokenize.tokenize
  )

let write_comp outch pin_cache comp name =
  let pins_in_order = List.map (Comp.find_pin comp) (pin_order comp) in
  let pin_nets = map (fun pin ->
    Hashtbl.find pin_cache (comp.Comp.ref, pin.Pin.number)
    ) comp.Comp.part.CompDef.pins in
    ) pins_in_order in
  output_string outch name;
  output_string outch (" " ^ (String.concat " " pin_nets));
  output_string outch (" " ^ (comp.Comp.value));

M lib/schematic.ml => lib/schematic.ml +8 -0
@@ 6,6 6,7 @@ module Fields = struct
  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


@@ 40,6 41,8 @@ module Pin = struct
    pos : point2i;
  }
  [@@deriving show]

  let number {number;_} = number
end

module CompDef = struct


@@ 64,6 67,11 @@ module Comp = struct
    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