diff --git a/Makefile b/Makefile index 8327f8e..6bf7d3e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index c55a6c1..e4a4aed 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: ``` diff --git a/data/option.go b/data/option.go index bfb6098..94a23bf 100644 --- a/data/option.go +++ b/data/option.go @@ -5,6 +5,7 @@ package data import ( "errors" "fmt" + "os" "strconv" "strings" @@ -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. @@ -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{ @@ -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: "", + }, } } @@ -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.") @@ -399,15 +407,13 @@ func ParseFlags() Options { } flag.Var(¢er, "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 = ¢er - 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 diff --git a/main.go b/main.go index 9ea83fa..21f38de 100644 --- a/main.go +++ b/main.go @@ -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() @@ -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() diff --git a/optimizer/optimizer.go b/optimizer/optimizer.go index aa256a2..45d09a4 100644 --- a/optimizer/optimizer.go +++ b/optimizer/optimizer.go @@ -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) @@ -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 diff --git a/slicer.go b/slicer.go index 1b46cdd..93e69e6 100644 --- a/slicer.go +++ b/slicer.go @@ -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 } @@ -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 diff --git a/slicer/layer.go b/slicer/layer.go index 78979f4..f67c65a 100644 --- a/slicer/layer.go +++ b/slicer/layer.go @@ -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 } diff --git a/slicer/slicer.go b/slicer/slicer.go index 703dcf8..4599e85 100644 --- a/slicer/slicer.go +++ b/slicer/slicer.go @@ -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 { diff --git a/slicer_test.go b/slicer_test.go index 9fc596e..deafd79 100644 --- a/slicer_test.go +++ b/slicer_test.go @@ -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) }