kiCad2JlcSMT/bom.go

76 lines
2.0 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func transBom(namei string) {
if namei == "" {
fmt.Println("Please input a KiCAD BOM CSV file.")
fmt.Scanln(&namei)
}
fmt.Println("Input from", namei)
namei = filepath.ToSlash(namei)
bi, e := os.ReadFile(namei)
if e != nil {
panic(e)
}
content := string(bi)
content = strings.ReplaceAll(content, "\r", "")
commentId, designatorId, footprintId, contents := findBomContents(content)
outStr := "\"Comment\",\"Designator\",\"Footprint\"\n"
outStr += transBomFormat(commentId, designatorId, footprintId, contents)
oname := filepath.Join(filepath.Dir(namei), "JLCBom.csv")
fmt.Println("Output to", oname)
os.WriteFile(oname, []byte(outStr), 0664)
fmt.Println("Done.")
fmt.Println("Done. Press any key to continue.")
fmt.Scanln(&namei)
}
func transBomFormat(commentId, designatorId, footprintId int, contents []string) string {
ostr := ""
for _, line := range contents {
lines := strings.Split(line, "\",")
if commentId < len(lines) && designatorId < len(lines) && footprintId < len(lines) {
footprint := transBomFootprint(lines[footprintId])
ostr += lines[commentId] + "\"," + lines[designatorId] + "\"," + footprint + "\"\n"
}
}
return ostr
}
func transBomFootprint(footprint string) string {
f, e := FootprintTransMap[footprint]
if e {
return f
}
return footprint
}
func findBomContents(content string) (commentId, designatorId, footprintId int, contents []string) {
contents = strings.Split(content, "\n")
for i, line := range contents {
if (strings.Contains(line, "\"Reference\"")) &&
(strings.Contains(line, "\"Value\"")) &&
(strings.Contains(line, "\"MaterialFootprint\"")) {
lines := strings.Split(line, ",")
for j, item := range lines {
if item == "\"Reference\"" {
designatorId = j
} else if item == "\"Value\"" {
commentId = j
} else if item == "\"MaterialFootprint\"" {
footprintId = j
}
}
contents = contents[i+1:]
return commentId, designatorId, footprintId, contents
}
}
return 0, 0, 0, nil
}