change to my current prefered DK resistor part numbers
[hw/teleterra] / scheme / gnet-partslist-csv.scm
1 ; Copyright (C) 2001-2010 MIYAMOTO Takanori
2 ; gnet-partslist-csv.scm
3
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation; either version 2 of the License, or
7 ; (at your option) any later version.
8
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ; GNU General Public License for more details.
13
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program; if not, write to the Free Software
16 ; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 ; The /'s may not work on win32
19 (load (string-append gedadata "/scheme/gnet-partslist-common.scm"))
20
21 (define partslist-csv:write-top-header
22   (lambda (port)
23     (display "device,value,footprint,vendor,vendor_part_number,quantity,refdes\n" port)))
24
25 (define (partslist-csv:write-partslist ls port)
26   (if (null? ls)
27       '()
28       (begin (write-one-row (cdar ls) "," "," port)
29              (write-one-row (caar ls) " " "\n" port)
30              (partslist-csv:write-partslist (cdr ls) port))))
31
32 (define (count-same-parts ls)
33   (if (null? ls)
34       (append ls)
35       (let* ((parts-table-no-uref (let ((result '()))
36                                     (for-each (lambda (l) (set! result (cons (cdr l) result))) (reverse ls))
37                                     (append result)))
38              (first-ls (car parts-table-no-uref))
39              (match-length (length (member first-ls (reverse parts-table-no-uref))))
40              (rest-ls (list-tail ls match-length))
41              (match-ls (list-tail (reverse ls) (- (length ls) match-length)))
42              (uref-ls (let ((result '()))
43                         (for-each (lambda (l) (set! result (cons (car l) result))) match-ls)
44                         (append result))))
45         (cons (cons uref-ls (append first-ls  (list match-length))) (count-same-parts rest-ls)))))
46
47 (define get-vendor
48    (lambda (package)
49       (gnetlist:get-package-attribute package "vendor")))
50
51 (define get-vendor-part-number
52    (lambda (package)
53       (gnetlist:get-package-attribute package "vendor_part_number")))
54
55 (define get-footprint
56    (lambda (package)
57       (gnetlist:get-package-attribute package "footprint")))
58
59 (define get-loadstatus
60   (lambda (package)
61     (gnetlist:get-package-attribute package "loadstatus")))
62
63 (define (get-parts-table-csv packages)
64   (if (null? packages)
65       '()
66       (let ((package (car packages)))
67         (if (string=? (get-device package) "include")
68             (get-parts-table-csv (cdr packages))
69             (if (string=? (get-loadstatus package) "smt")
70                 (cons (list package
71                             (get-device package)
72                             (get-value package)
73                             (get-footprint package)
74                             (get-vendor package)
75                             (get-vendor-part-number package)) ;; sdb change
76                       (get-parts-table-csv (cdr packages)))
77                 (get-parts-table-csv (cdr packages)))))))
78
79 (define partslist-csv
80   (lambda (output-filename)
81     (let ((port (open-output-file output-filename))
82           (parts-table (marge-sort-with-multikey (get-parts-table-csv packages) '(1 2 3 0))))
83       (set! parts-table (count-same-parts parts-table))
84       (partslist-csv:write-top-header port)
85       (partslist-csv:write-partslist parts-table port)
86       (close-output-port port))))