Skip to content

Commit

Permalink
improve argument parsing and remove --file (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
aligator authored Aug 23, 2020
1 parent 3b95897 commit 5eb9839
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 34 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)"

## build: Compile the binary.
build:
@echo compile
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) GOARM=$(GOARM) CGO_CPPFLAGS=$(CGO_CPPFLAGS) CGO_CFLAGS=$(CGO_CFLAGS) CGO_CXXFLAGS=$(CGO_CXXFLAGS) CGO_LDFLAGS=$(CGO_LDFLAGS) go build $(LDFLAGS) -o $(TARGET)/$(PROJECTNAME) $(GOFILES)

## clear the build folder
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ https://github.com/aligator/GoSlice/releases
Unpack the executable and run it in the commandline.
linux / mac:
```
./goslice --file /path/to/stl/file.stl
./goslice /path/to/stl/file.stl
```

windows:
```
goslice.exe --file /path/to/stl/file.stl`
goslice.exe /path/to/stl/file.stl`
```

If you need the usage of all possible flags, run it with the `--help` flag:
Expand All @@ -44,7 +44,7 @@ If you need the usage of all possible flags, run it with the `--help` flag:
## Try it out - for developers
Minimal usage:
```
go run . --file /path/to/stl/file.stl
go run . /path/to/stl/file.stl
```
To get help for all possible flags take a look at /data/option.go or just run:
```
Expand Down
50 changes: 28 additions & 22 deletions data/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package data
import (
"errors"
"fmt"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -255,14 +256,6 @@ type PrinterOptions struct {
type GoSliceOptions struct {
// PrintVersion indicates if the GoSlice version should be printed.
PrintVersion bool
}

// Options contains all GoSlice options.
type Options struct {
Printer PrinterOptions
Filament FilamentOptions
Print PrintOptions
GoSlice GoSliceOptions

// MeldDistance is the distance which two points have to be
// within to count them as one point.
Expand All @@ -281,6 +274,14 @@ type Options struct {
InputFilePath string
}

// Options contains all GoSlice options.
type Options struct {
Printer PrinterOptions
Filament FilamentOptions
Print PrintOptions
GoSlice GoSliceOptions
}

func DefaultOptions() Options {
return Options{
Print: PrintOptions{
Expand Down Expand Up @@ -331,11 +332,13 @@ func DefaultOptions() Options {
0,
),
},
GoSlice: GoSliceOptions{},

MeldDistance: 30,
JoinPolygonSnapDistance: 160,
FinishPolygonSnapDistance: 1000,
GoSlice: GoSliceOptions{
PrintVersion: false,
MeldDistance: 30,
JoinPolygonSnapDistance: 160,
FinishPolygonSnapDistance: 1000,
InputFilePath: "",
},
}
}

Expand All @@ -344,11 +347,16 @@ func DefaultOptions() Options {
func ParseFlags() Options {
options := DefaultOptions()

flag.StringVar(&options.InputFilePath, "file", "", "The path to the input stl file.")
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage of goslice: goslice STL_FILE [flags]\n")
flag.PrintDefaults()
}

flag.Var(&options.MeldDistance, "meld-distance", "The distance which two points have to be within to count them as one point.")
flag.Var(&options.JoinPolygonSnapDistance, "join-polygon-snap-distance", "The distance used to check if two open polygons can be snapped together to one bigger polygon. Checked by the start and endpoints of the polygons.")
flag.Var(&options.FinishPolygonSnapDistance, "finish-polygon-snap-distance", "The max distance between start end endpoint of a polygon used to check if a open polygon can be closed.")
// GoSlice options
flag.BoolVarP(&options.GoSlice.PrintVersion, "version", "v", false, "Print the GoSlice version.")
flag.Var(&options.GoSlice.MeldDistance, "meld-distance", "The distance which two points have to be within to count them as one point.")
flag.Var(&options.GoSlice.JoinPolygonSnapDistance, "join-polygon-snap-distance", "The distance used to check if two open polygons can be snapped together to one bigger polygon. Checked by the start and endpoints of the polygons.")
flag.Var(&options.GoSlice.FinishPolygonSnapDistance, "finish-polygon-snap-distance", "The max distance between start end endpoint of a polygon used to check if a open polygon can be closed.")

// print options
flag.Var(&options.Print.IntialLayerSpeed, "initial-layer-speed", "The speed only for the first layer in mm per second.")
Expand Down Expand Up @@ -399,15 +407,13 @@ func ParseFlags() Options {
}
flag.Var(&center, "center", "The point where the model is finally placed.")

// GoSlice options
flag.BoolVarP(&options.GoSlice.PrintVersion, "version", "v", false, "Print the GoSlice version.")

flag.Parse()

options.Printer.Center = &center

if !options.GoSlice.PrintVersion && options.InputFilePath == "" {
panic("you have to pass a filename using the --file flag")
// Use the first arg as path.
if flag.NArg() > 0 {
options.GoSlice.InputFilePath = flag.Args()[0]
}

return options
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"io"
"os"

flag "github.com/spf13/pflag"
)

var Version = "UNDEFINED"
var Version = "unknown development version"

func main() {
o := data.ParseFlags()
Expand All @@ -17,6 +19,12 @@ func main() {
os.Exit(0)
}

if o.GoSlice.InputFilePath == "" {
_, _ = fmt.Fprintf(os.Stderr, "the STL_FILE path has to be specified\n")
flag.Usage()
os.Exit(1)
}

p := NewGoSlice(o)
err := p.Process()

Expand Down
4 changes: 2 additions & 2 deletions optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ FacesLoop:
currentPoint := face.Points()[j]
// create hash for the pos
// points which are within the meldDistance fall into the same category of the indices map
meldDistanceHash := pointHash(o.options.MeldDistance)
meldDistanceHash := pointHash(o.options.GoSlice.MeldDistance)
hash := ((pointHash(currentPoint.X()) + meldDistanceHash/2) / meldDistanceHash) ^
(((pointHash(currentPoint.Y()) + meldDistanceHash/2) / meldDistanceHash) << 10) ^
(((pointHash(currentPoint.Z()) + meldDistanceHash/2) / meldDistanceHash) << 20)
Expand All @@ -72,7 +72,7 @@ FacesLoop:
// is smaller (or same) than the currently tested pos
for _, index := range indices[hash] {
differenceVec := om.points[index].pos.Sub(currentPoint)
if differenceVec.ShorterThanOrEqual(o.options.MeldDistance) {
if differenceVec.ShorterThanOrEqual(o.options.GoSlice.MeldDistance) {
// if true for any of the points with the same hash,
// do not add the current pos to the indices map
// but save the indices of the already existing duplicate
Expand Down
4 changes: 2 additions & 2 deletions slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *GoSlice) Process() error {
startTime := time.Now()

// 1. Load model
models, err := s.reader.Read(s.options.InputFilePath)
models, err := s.reader.Read(s.options.GoSlice.InputFilePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (s *GoSlice) Process() error {
return err
}

err = s.writer.Write(finalGcode, s.options.InputFilePath+".gcode")
err = s.writer.Write(finalGcode, s.options.GoSlice.InputFilePath+".gcode")
fmt.Println("full processing time:", time.Now().Sub(startTime))

return err
Expand Down
2 changes: 1 addition & 1 deletion slicer/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (l *layer) makePolygons(om data.OptimizedModel, joinPolygonSnapDistance, fi
p1 := l.segments[touchingSegmentIndex].start
diff := p0.Sub(p1)

if diff.ShorterThanOrEqual(l.options.MeldDistance) {
if diff.ShorterThanOrEqual(l.options.GoSlice.MeldDistance) {
if touchingSegmentIndex == startSegmentIndex {
canClose = true
}
Expand Down
2 changes: 1 addition & 1 deletion slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (s slicer) Slice(m data.OptimizedModel) ([]data.PartitionedLayer, error) {
c := clip.NewClipper()

for i, layer := range layers {
layer.makePolygons(m, s.options.JoinPolygonSnapDistance, s.options.FinishPolygonSnapDistance)
layer.makePolygons(m, s.options.GoSlice.JoinPolygonSnapDistance, s.options.GoSlice.FinishPolygonSnapDistance)
lp, ok := c.GenerateLayerParts(layer)

if !ok {
Expand Down
2 changes: 1 addition & 1 deletion slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestWholeSlicer(t *testing.T) {

for _, testCase := range tests {
t.Log("slice " + testCase.path)
s.options.InputFilePath = folder + testCase.path
s.options.GoSlice.InputFilePath = folder + testCase.path
err := s.Process()
test.Ok(t, err)
}
Expand Down

0 comments on commit 5eb9839

Please sign in to comment.