Skip to content

Commit

Permalink
add WriteCloser
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jul 2, 2021
1 parent 8f9ae4d commit 57d8d54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ The 3D Manufacturing Format (3MF) is a 3D printing format that allows design app

## Features

* High parsing speed and moderate memory consumption
* Complete 3MF Core spec implementation.
* Clean API.
* STL importer
* Spec conformance validation
* Robust implementation with full coverage and validated against real cases.
* Extensions
* Support custom and private extensions.
* spec_production.
* spec_slice.
* spec_beamlattice.
* spec_materials, missing the display resources.
- High parsing speed and moderate memory consumption
- Complete 3MF Core spec implementation.
- Clean API.
- STL importer
- Spec conformance validation
- Robust implementation with full coverage and validated against real cases.
- Extensions
- Support custom and private extensions.
- spec_production.
- spec_slice.
- spec_beamlattice.
- spec_materials, missing the display resources.

## Examples

Expand Down Expand Up @@ -93,9 +93,10 @@ import (
)

func main() {
file := os.Create("/testdata/cube.3mf")
model := new(go3mf.Model)
go3mf.NewEncoder(file).Encode(model)
w, _ := go3mf.CreateWriter("/testdata/cube.3mf")
w.Encode(model)
w.Close()
}
```

Expand All @@ -121,7 +122,7 @@ func main() {
r, _ := go3mf.OpenReader("/testdata/cube.3mf")
r.Decode(model)
fmt.Println(production.GetBuildAttr(&model.Build).UUID)

model.Resources.Assets = append(model.Resources.Assets, &materials.ColorGroup{
ID: 10, Colors: []color.RGBA{{R: 255, G: 255, B: 255, A: 255}},
}
Expand Down
24 changes: 24 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"encoding/xml"
"io"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -93,6 +94,29 @@ func MarshalModel(m *Model) ([]byte, error) {
return b.Bytes(), nil
}

// WriteCloser wrapps an Encoder than can be closed.
type WriteCloser struct {
Encoder
f *os.File
}

// Close closes the 3MF file, rendering it unusable for I/O.
func (w *WriteCloser) Close() error {
return w.f.Close()
}

// CreateWriter will create the 3MF file specified by name and return a WriteCloser.
func CreateWriter(name string) (*WriteCloser, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &WriteCloser{
Encoder: *NewEncoder(f),
f: f,
}, nil
}

// An Encoder writes Model data to an output stream.
//
// See the documentation for strconv.FormatFloat for details about the FloatPrecision behaviour.
Expand Down
4 changes: 2 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type packageReader interface {

// ReadCloser wrapps a Decoder than can be closed.
type ReadCloser struct {
Decoder
f *os.File
*Decoder
}

// OpenReader will open the 3MF file specified by name and return a ReadCloser.
Expand All @@ -53,7 +53,7 @@ func OpenReader(name string) (*ReadCloser, error) {
f.Close()
return nil, err
}
return &ReadCloser{f: f, Decoder: NewDecoder(f, fi.Size())}, nil
return &ReadCloser{f: f, Decoder: *NewDecoder(f, fi.Size())}, nil
}

// Close closes the 3MF file, rendering it unusable for I/O.
Expand Down

0 comments on commit 57d8d54

Please sign in to comment.