-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslicer.go
More file actions
110 lines (90 loc) · 2.79 KB
/
Copy pathslicer.go
File metadata and controls
110 lines (90 loc) · 2.79 KB
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
package slicer
import (
"fmt"
"io"
"github.com/siherrmann/slicer/core"
"github.com/siherrmann/slicer/model"
)
type Slicer struct {
Config *model.SliceConfig // Slicing configuration
Model *model.BaseModel // Loaded model (STL or 3MF)
}
func NewSlicer() *Slicer {
return &Slicer{
Config: model.NewSliceConfig(), // Default config
}
}
// NewSlicerWithConfig creates a slicer with custom configuration
func NewSlicerWithConfig(config *model.SliceConfig) *Slicer {
return &Slicer{
Config: config,
}
}
func (s *Slicer) LoadSTLModel(r io.Reader, modelName string) (*model.BaseModel, error) {
// Load the STL model
stl, err := model.LoadSTL(r, modelName)
if err != nil {
return nil, fmt.Errorf("failed to load STL model: %w", err)
}
s.Model = &stl.BaseModel
return s.Model, nil
}
// Load3mfFile loads a 3MF file from a reader
func (s *Slicer) Load3mfFile(r io.Reader, modelName string) (*model.BaseModel, error) {
// Open the 3MF XML file via reader
bm, err := model.Load3MF(r, modelName)
if err != nil {
return nil, fmt.Errorf("failed to load 3MF file: %w", err)
}
s.Model = bm
if bm.SliceConfig != nil {
s.Config = bm.SliceConfig
}
return s.Model, nil
}
// Slice slices a model into layers
func (s *Slicer) Slice(bm *model.BaseModel) ([]*model.Slice, error) {
if bm == nil {
return nil, fmt.Errorf("cannot slice nil model")
}
// Perform the slicing
err := bm.Slice(s.Config)
if err != nil {
return nil, fmt.Errorf("failed to slice model: %w", err)
}
return bm.Slices, nil
}
// GeneratePrintPaths processes all slices to generate perimeters, infill, and classify layers
func (s *Slicer) GeneratePrintPaths(config *model.SliceConfig) ([]model.ContinuousPath, error) {
if s.Model == nil {
return nil, fmt.Errorf("no model loaded")
}
// Step 1: Clean the model to ensure it's ready for slicing
s.Model = s.Model.CleanBounds()
s.Model.SliceConfig = config
// Step 2: If the model hasn't been sliced yet, slice it now
if len(s.Model.Slices) == 0 {
err := s.Model.Slice(config)
if err != nil {
return nil, err
}
}
s.Model.ClassifyTopBottomLayers(config)
// Step 3: Generate full print paths for all layers (perimeters, infill, etc.)
paths := core.GenerateFullSTLPath(s.Model, *config)
// Step 4: Globally simplify all geometry to prevent massive JSON payload bloats.
// RDP simplification annihilates collinear/dense points preserving rendering and print quality.
var cleanPaths []model.ContinuousPath
for _, p := range paths {
if p.PathType == model.PathExtrusion && len(p.Segments) > 3 {
// Convert single path to array and apply RDP
merged := core.CleanFullPaths([]model.ContinuousPath{p}, 0.05)
if len(merged.Segments) > 0 {
cleanPaths = append(cleanPaths, merged)
}
} else {
cleanPaths = append(cleanPaths, p)
}
}
return cleanPaths, nil
}