open List
open Connectiongraph
open Schematic
let comp_ignored comp =
comp.Comp.ref.[0] = '#'
|| Fields.get comp.Comp.fields Fields.Keys.spice_netlist_enabled "Y" <> "Y"
let make_cache kf vf xs =
let tbl = Hashtbl.create (Enum.count xs) in
Enum.iter (fun x ->
Hashtbl.replace tbl (kf x) (vf x)
) xs;
tbl
let id x = x
let comp_name comp =
let comp_type_from_name = String.sub comp.Comp.ref 0 1 in
let comp_type = Fields.get comp.Comp.fields Fields.Keys.spice_primitive comp_type_from_name in
if comp_type_from_name = comp_type then comp.Comp.ref
else comp_type ^ comp.Comp.ref
let make_unique used name =
if not (Hashtbl.mem used name) then (Hashtbl.add used name 1; name)
else begin
let rec helper n =
let full_name = name ^ "_" ^ (string_of_int n) in
if not (Hashtbl.mem used full_name) then (Hashtbl.add used full_name 1; full_name)
else helper (n+1)
in helper 2
end
let write_comp outch pin_cache comp name =
let pin_nets = map (fun pin ->
Hashtbl.find pin_cache (comp.Comp.ref, pin.Pin.number)
) comp.Comp.part.CompDef.pins in
output_string outch name;
output_string outch (" " ^ (String.concat " " pin_nets));
output_string outch (" " ^ (comp.Comp.value));
output_string outch "\n"
let write_netlist outch schematic netlist =
let net_names = make_cache id ( fun net_name ->
Netlist.driver_net_name netlist net_name
) (ExtHashtbl.Hashtbl.keys netlist.Netlist.nets) in
let pin_cache = Hashtbl.create 512 in
Hashtbl.iter ( fun net_name subgraph -> iter (
function
| Item.Pin (comp, pin) -> Hashtbl.replace pin_cache (comp.ref, pin.number) (Hashtbl.find net_names net_name)
| _ -> failwith "invalid netlist"
) subgraph.Subgraph.items
) netlist.Netlist.nets;
output_string outch ".title KiCad schematic (by eeschema2netlist)\n";
let used_comp_names = Hashtbl.create 512 in
iter ( function
| Item.Comp comp when not (comp_ignored comp) ->
let comp_export_name = make_unique used_comp_names (comp_name comp) in
write_comp outch pin_cache comp comp_export_name
| _ -> ()
) schematic.items;
output_string outch ".end\n";
()