Skip to content

Commit

Permalink
Return some errors which were not returned, yet. Inject logger to be …
Browse files Browse the repository at this point in the history
…able to control the logs from the outside - better for usage as lib
  • Loading branch information
aligator committed Apr 2, 2021
1 parent eadd414 commit a7a245d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion clip/clip.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Pattern interface {
// Fill fills the given part.
// It returns the final infill pattern.
Fill(layerNr int, part data.LayerPart) data.Paths
Fill(layerNr int, part data.LayerPart) (data.Paths, error)
}

// OffsetResult is built the following way: [partNr][insetNr][insetPartsNr]data.LayerPart
Expand Down
18 changes: 10 additions & 8 deletions clip/linear.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package clip

import (
"fmt"
"errors"
"github.com/aligator/goslice/data"

clipper "github.com/aligator/go.clipper"
Expand Down Expand Up @@ -35,7 +35,7 @@ func NewLinearPattern(lineWidth data.Micrometer, lineDistance data.Micrometer, m
}

// Fill implements the Pattern interface by using simple linear lines as infill.
func (p linear) Fill(layerNr int, part data.LayerPart) data.Paths {
func (p linear) Fill(layerNr int, part data.LayerPart) (data.Paths, error) {
rotation := float64(p.degree)

// for rectlinear fill patterns rotate each 2nd layer by 90 degree.
Expand Down Expand Up @@ -73,13 +73,16 @@ func (p linear) Fill(layerNr int, part data.LayerPart) data.Paths {
smallerLines = p.lineWidth
}

resultInfill := p.getInfill(min, max, clipperPath(outline), clipperPaths(holes), 0, smallerLines)
resultInfill, err := p.getInfill(min, max, clipperPath(outline), clipperPaths(holes), 0, smallerLines)
if err != nil {
return nil, err
}

result := p.sortInfill(microPaths(resultInfill, false), p.zigZag, data.NewBasicLayerPart(outline, holes))

result.Rotate(-rotation)

return result
return result, nil
}

// sortInfill optimizes the order of the infill lines.
Expand Down Expand Up @@ -173,7 +176,7 @@ func (p linear) sortInfill(unsorted data.Paths, zigZag bool, part data.LayerPart
}

// getInfill fills a polygon (with holes)
func (p linear) getInfill(min data.MicroPoint, max data.MicroPoint, outline clipper.Path, holes clipper.Paths, overlap float32, smallerLines data.Micrometer) clipper.Paths {
func (p linear) getInfill(min data.MicroPoint, max data.MicroPoint, outline clipper.Path, holes clipper.Paths, overlap float32, smallerLines data.Micrometer) (clipper.Paths, error) {
var result clipper.Paths

// clip the paths with the lines using intersection
Expand Down Expand Up @@ -219,8 +222,7 @@ func (p linear) getInfill(min data.MicroPoint, max data.MicroPoint, outline clip

tree, ok := cl.Execute2(clipper.CtIntersection, clipper.PftEvenOdd, clipper.PftEvenOdd)
if !ok {
fmt.Println("getLinearFill failed")
return nil
return nil, errors.New("getLinearFill failed")
}

for _, c := range tree.Childs() {
Expand All @@ -242,5 +244,5 @@ func (p linear) getInfill(min data.MicroPoint, max data.MicroPoint, outline clip
}
}

return result
return result, nil
}
4 changes: 4 additions & 0 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"
"log"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -265,6 +266,8 @@ type GoSliceOptions struct {

// OutputFilePath specifies the path to the output gcode file.
OutputFilePath string

Logger *log.Logger
}

// SlicingOptions contains all options related to slice a model.
Expand Down Expand Up @@ -352,6 +355,7 @@ func DefaultOptions() Options {
PrintVersion: false,
InputFilePath: "",
OutputFilePath: "",
Logger: log.New(os.Stdout, "", 0),
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion gcode/renderer/infill.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func (i *Infill) Render(b *gcode.Builder, layerNr int, maxLayer int, layer data.
b.AddComment(c)
}

for _, path := range i.pattern.Fill(layerNr, part) {
infill, err := i.pattern.Fill(layerNr, part)
if err != nil {
return err
}
for _, path := range infill {
err := b.AddPolygon(layer, path, z, true)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions goslice.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goslice

import (
"fmt"
"github.com/aligator/goslice/clip"
"github.com/aligator/goslice/data"
"github.com/aligator/goslice/gcode"
Expand Down Expand Up @@ -174,7 +173,7 @@ func (s *GoSlice) Process() error {
}

err = s.Writer.Write(finalGcode, outputPath)
fmt.Println("full processing time:", time.Now().Sub(startTime))
s.Options.Logger.Println("full processing time:", time.Now().Sub(startTime))

return err
}
3 changes: 1 addition & 2 deletions optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package optimizer

import (
"fmt"
"github.com/aligator/goslice/data"
"github.com/aligator/goslice/handler"
)
Expand Down Expand Up @@ -147,7 +146,7 @@ FacesLoop:
om.faces[i] = face
}

fmt.Printf("Number of open faces: %v\n", openFaces)
o.options.GoSlice.Logger.Printf("Number of open faces: %v\n", openFaces)

min := m.Min()
max := m.Max()
Expand Down
2 changes: 1 addition & 1 deletion slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewSlicer(options *data.Options) handler.ModelSlicer {

func (s slicer) Slice(m data.OptimizedModel) ([]data.PartitionedLayer, error) {
layerCount := (m.Size().Z()-s.options.Print.InitialLayerThickness)/s.options.Print.LayerThickness + 1
fmt.Println("layer count:", layerCount)
s.options.GoSlice.Logger.Println("layer count:", layerCount)

layers := make([]*layer, layerCount)

Expand Down
3 changes: 1 addition & 2 deletions writer/writer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package writer

import (
"fmt"
"github.com/aligator/goslice/handler"
"os"
)
Expand All @@ -16,7 +15,7 @@ func Writer() handler.GCodeWriter {
func (w writer) Write(gcode string, filename string) error {
buf, err := os.Create(filename)
if err != nil {
fmt.Println(err)
return err
}

defer buf.Close()
Expand Down

0 comments on commit a7a245d

Please sign in to comment.