-
Notifications
You must be signed in to change notification settings - Fork 4
/
smfix.go
145 lines (126 loc) · 2.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"bytes"
"flag"
"log"
"os"
"runtime"
"strings"
"github.com/macdylan/SMFix/fix"
)
var (
OutputPath string
noTrim bool
noShutoff bool
noPreheat bool
noReinforceTower bool
noReplaceTool 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", true, "do not pre-heat nozzles")
flag.BoolVar(&noReinforceTower, "noreinforcetower", true, "do not reinforce the prime tower")
flag.BoolVar(&noReplaceTool, "noreplacetool", false, "do not replace the tool number")
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()
}
startCPUProfile()
defer func() {
writeMemProfile()
stopCPUProfile()
}()
// read gcodes form file
gcodes := []*fix.GcodeBlock{}
sc := bufio.NewScanner(in)
for sc.Scan() {
line := sc.Text()
if strings.HasPrefix(line, "; Postprocessed by smfix") {
log.Fatalln(fix.ErrIsFixed)
}
g, err := fix.ParseGcodeBlock(line)
if err == nil {
// ignore G4 S0
if g.Is("G4") {
var s int
if err := g.GetParam('S', &s); err == nil && s == 0 {
continue
}
}
gcodes = append(gcodes, g)
continue
}
if err != fix.ErrEmptyString {
log.Fatalf("Parse gcode error: %s", err)
}
}
in.Close()
if err := sc.Err(); err != nil {
log.Fatalf("Read input file error: %s", err)
}
// fix gcodes
funcs := make([]fix.GcodeModifier, 0, 6)
if !noTrim {
// funcs = append(funcs, fix.GcodeTrimLines)
}
if !noShutoff {
funcs = append(funcs, fix.GcodeFixShutoff)
}
if !noPreheat {
funcs = append(funcs, fix.GcodeFixPreheat)
}
if !noReplaceTool {
funcs = append(funcs, fix.GcodeReplaceToolNum)
}
if !noReinforceTower {
funcs = append(funcs, fix.GcodeReinforceTower)
}
funcs = append(funcs, fix.GcodeFixOrcaToolUnload)
for _, fn := range funcs {
gcodes = fn(gcodes)
}
// extract headers
var headers [][]byte
if headers, err = fix.ExtractHeader(gcodes); err != nil {
log.Fatalf("Parse params failed: %s", err)
}
// 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()
bufWriter := bufio.NewWriterSize(out, 64*1024)
defer bufWriter.Flush()
// write headers
if _, err := bufWriter.Write(bytes.Join(headers, []byte("\n"))); err != nil {
log.Fatalln(err)
}
// write gcodes
for _, gcode := range gcodes {
if _, err := bufWriter.WriteString(gcode.String() + "\n"); err != nil {
log.Fatalln(err)
}
}
}