forked from macdylan/SMFix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smfix.go
106 lines (92 loc) · 2.12 KB
/
smfix.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
package main
import (
"bufio"
"bytes"
"flag"
"log"
"os"
"runtime"
"github.com/macdylan/SMFix/fix"
)
var (
OutputPath string
noTrim bool
noShutoff bool
noPreheat bool
noReinforceTower bool
)
func init() {
flag.StringVar(&OutputPath, "o", "", "output path, default is input path")
flag.BoolVar(&noTrim, "notrim", false, "do not trim spaces in the gcode")
flag.BoolVar(&noShutoff, "noshutoff", false, "do not shutoff nozzles that are no longer in use")
flag.BoolVar(&noPreheat, "nopreheat", false, "do not pre-heat nozzles")
flag.BoolVar(&noReinforceTower, "noreinforcetower", false, "do not reinforce the prime tower")
flag.Parse()
}
func main() {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
var (
in *os.File
err error
)
if len(flag.Args()) > 0 {
in, err = os.OpenFile(flag.Arg(0), os.O_RDONLY, 0666)
if err != nil {
log.Fatalln(err)
}
defer in.Close()
}
if in == nil {
flag_usage()
}
var headers [][]byte
if headers, err = fix.ExtractHeader(in); err != nil {
log.Fatalf("Parse params failed: %s", err)
}
gcodes := make([]string, 0, fix.Params.TotalLines+len(headers)+128)
sc := bufio.NewScanner(in)
for sc.Scan() {
gcodes = append(gcodes, sc.Text())
}
in.Close()
if err := sc.Err(); err != nil {
log.Fatalf("Read input file error: %s", err)
}
// fix gcodes
funcs := make([]func([]string) []string, 0, 4)
if !noTrim {
funcs = append(funcs, fix.GcodeTrimLines)
}
if !noShutoff {
funcs = append(funcs, fix.GcodeFixShutoff)
}
if !noPreheat {
funcs = append(funcs, fix.GcodeFixPreheat)
}
if !noReinforceTower {
funcs = append(funcs, fix.GcodeReinforceTower)
}
for _, fn := range funcs {
gcodes = fn(gcodes)
}
// prepare for output file
if len(OutputPath) == 0 {
OutputPath = flag.Arg(0)
}
out, err := os.Create(OutputPath)
if err != nil {
log.Fatalln(err)
}
defer out.Close()
// write headers
if _, err := out.Write(bytes.Join(headers, []byte("\n"))); err != nil {
log.Fatalln(err)
}
// write gcodes
for _, gcode := range gcodes {
if _, err := out.WriteString(gcode + "\n"); err != nil {
log.Fatalln(err)
}
}
}