kiCad2JlcSMT/pos.go

92 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func transPos(namei string) {
if namei == "" {
fmt.Println("Please input a KiCAD POS 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", "")
designatorId, xId, yId, layerId, rotationId, contents := findPosContents(content)
outStr := "\"Designator\",\"Mid X\",\"Mid Y\",\"Layer\",\"Rotation\"\n"
outStr += transPosFormat(designatorId, xId, yId, layerId, rotationId, contents)
oname := filepath.Join(filepath.Dir(namei), "JLCPos.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 transPosFormat(designatorId, xId, yId, layerId, rotationId int, contents []string) string {
ostr := ""
for _, line := range contents {
// 注意后面字段部分没有引号:"C1","0.1uF/10V","C_0603_1608Metric",154.498000,-107.311000,-90.000000,top
lines := strings.Split(line, "\",")
l := lines[len(lines)-1]
lines = lines[:len(lines)-1]
lines = append(lines, strings.Split(l, ",")...)
if designatorId < len(lines) &&
xId < len(lines) &&
yId < len(lines) &&
layerId < len(lines) &&
rotationId < len(lines) {
var layer string
switch lines[layerId] {
case "top":
layer = "T"
case "bottom":
layer = "B"
}
ostr += lines[designatorId] + "\"," +
"\"" + lines[xId] + "\"," +
"\"" + lines[yId] + "\"," +
"\"" + layer + "\"," +
"\"" + lines[rotationId] + "\"\n"
}
}
return ostr
}
func findPosContents(content string) (designatorId, xId, yId, layerId, rotationId int, contents []string) {
contents = strings.Split(content, "\n")
for i, line := range contents {
if (strings.Contains(line, "Ref")) &&
(strings.Contains(line, "PosX")) &&
(strings.Contains(line, "PosY")) &&
(strings.Contains(line, "Rot")) &&
(strings.Contains(line, "Side")) {
lines := strings.Split(line, ",")
for j, item := range lines {
if item == "Ref" {
designatorId = j
} else if item == "PosX" {
xId = j
} else if item == "PosY" {
yId = j
} else if item == "Side" {
layerId = j
} else if item == "Rot" {
rotationId = j
}
}
contents = contents[i+1:]
return designatorId, xId, yId, layerId, rotationId, contents
}
}
return 0, 0, 0, 0, 0, nil
}