-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_main.go
107 lines (95 loc) · 2.61 KB
/
generator_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"encoding/csv"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
func getOpenSCADPath() string {
switch runtime.GOOS {
case "windows":
return `C:\Program Files\OpenSCAD\openscad.exe`
case "linux":
return "openscad"
case "darwin":
defaultPath := "/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD"
if _, err := os.Stat(defaultPath); err == nil {
return defaultPath
}
userPath := os.ExpandEnv("$HOME/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD")
if _, err := os.Stat(userPath); err == nil {
return userPath
}
_, err := exec.LookPath("openscad")
if err != nil {
fmt.Println("OpenSCAD not found. Please install or add it to your PATH.")
os.Exit(1)
}
return "openscad"
default:
fmt.Printf("Unsupported platform: %s\n", runtime.GOOS)
os.Exit(1)
return ""
}
}
func genSamples(filePath string) {
outputDir := filepath.Join(filepath.Dir(filePath), "stl")
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
fmt.Println("Failed to create output directory:", err)
return
}
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Failed to open CSV file:", err)
return
}
defer file.Close()
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err != nil {
break
}
if len(record) == 0 || strings.HasPrefix(record[0], "#") || strings.TrimSpace(record[0]) == "" {
continue // Skip comments or empty lines
}
filename := filepath.Join(outputDir, strings.Join(record, "_")+".stl")
args := []string{
"-o", filename,
"-D", fmt.Sprintf(`BRAND="%s"`, record[0]),
"-D", fmt.Sprintf(`TYPE="%s"`, record[1]),
"-D", fmt.Sprintf(`COLOR="%s"`, record[2]),
"-D", fmt.Sprintf(`TEMP_HOTEND="%s"`, record[3]),
"-D", fmt.Sprintf(`TEMP_BED="%s"`, record[4]),
}
if len(record) > 5 && record[5] != "" {
args = append(args, "-D", fmt.Sprintf("BRAND_SIZE=%s", record[5]))
}
if len(record) > 6 && record[6] != "" {
args = append(args, "-D", fmt.Sprintf("TYPE_SIZE=%s", record[6]))
}
if len(record) > 7 && record[7] != "" {
args = append(args, "-D", fmt.Sprintf("COLOR_SIZE=%s", record[7]))
}
// Get the OpenSCAD path
openscadPath := getOpenSCADPath()
args = append(args, filepath.Join(filepath.Dir(filePath), "FilamentSamples.scad"))
fmt.Println("Running:", openscadPath, strings.Join(args, " "))
cmd := exec.Command(openscadPath, args...)
if err := cmd.Run(); err != nil {
fmt.Println("Failed to run OpenSCAD:", err)
return
}
}
}
func main() {
csvFile := "samples.csv"
if len(os.Args) > 1 {
csvFile = os.Args[1]
}
fmt.Println("Using CSV file:", csvFile)
genSamples(csvFile)
}