Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Golang examples #390

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ jobs:
cd SDK/CPackExamples/Cpp/build
cmake --build .
./Example_ExtractInfo ../../../Examples/Files/Helix.3mf
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Go Bindings
run: |
cd SDK/Examples/Go
go mod init lib3mfExamples
go get github.com/3MFConsortium/lib3mf.go/[email protected]
go build -o create_cube create_cube.go
./create_cube

deploy-windows:
runs-on: windows-2019
Expand Down Expand Up @@ -451,6 +462,18 @@ jobs:
cd SDK/CPackExamples/Cpp/build
cmake --build . --config Release
./Release/Example_ExtractInfo.exe ../../../Examples/Files/Helix.3mf
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Go Bindings
run: |
cd SDK/Examples/Go
go mod init lib3mfExamples
go get github.com/3MFConsortium/lib3mf.go/[email protected]
go build -o create_cube.exe create_cube.go
./create_cube.exe


deploy-macos:
runs-on: macos-latest
Expand Down Expand Up @@ -507,6 +530,17 @@ jobs:
cd SDK/CPackExamples/Cpp/build
cmake --build .
./Example_ExtractInfo ../../../Examples/Files/Helix.3mf
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Go Bindings
run: |
cd SDK/Examples/Go
go mod init lib3mfExamples
go get github.com/3MFConsortium/lib3mf.go/[email protected]
go build -o create_cube create_cube.go
./create_cube

deploy-source-code-with-submodules:
runs-on: ubuntu-20.04
Expand Down
138 changes: 138 additions & 0 deletions SDK/Examples/Go/3mf_convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
+++

Copyright (C) 2019 3MF Consortium (Vijai Kumar Suriyababu)

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Abstract: An example to convert between 3MF and STL

Interface version: 2.3.2
+++
*/


package main

import (
"fmt"
lib3mf "github.com/3MFConsortium/lib3mf.go/v2"
"log"
"os"
"strings"
)

// findExtension returns the file extension from a given filename.
func findExtension(filename string) string {
idx := strings.LastIndex(filename, ".")
if idx != -1 {
return filename[idx:]
}
return ""
}

// convert performs the conversion between 3MF and STL formats.
func convert(filename string) int {
// Get a wrapper object
wrapper, err := lib3mf.GetWrapper()
if err != nil {
log.Fatal("Error loading 3MF library:", err)
return -1
}

// Check the library version (optional, similar to get_version in Python)
nMajor, nMinor, nMicro, err := wrapper.GetLibraryVersion()
if err != nil {
log.Fatal("Error fetching lib3mf version:", err)
return -1
}
fmt.Printf("lib3mf version: %d.%d.%d\n", nMajor, nMinor, nMicro)

extension := strings.ToLower(findExtension(filename))
var readerName, writerName, newExtension string

switch extension {
case ".stl":
readerName = "stl"
writerName = "3mf"
newExtension = ".3mf"
case ".3mf":
readerName = "3mf"
writerName = "stl"
newExtension = ".stl"
default:
fmt.Printf("Unknown input file extension: %s\n", extension)
return -1
}

outputFilename := filename[:len(filename)-len(extension)] + newExtension

// Create a new 3MF model
model, err := wrapper.CreateModel()
if err != nil {
log.Fatal("Error creating 3MF model:", err)
return -1
}

// Read from the input file
reader, err := model.QueryReader(readerName)
if err != nil {
log.Fatal("Error querying reader for format:", readerName)
return -1
}
fmt.Printf("Reading %s...\n", filename)
err = reader.ReadFromFile(filename)
if err != nil {
log.Fatal("Error reading from file:", err)
return -1
}

// Write to the output file
writer, err := model.QueryWriter(writerName)
if err != nil {
log.Fatal("Error querying writer for format:", writerName)
return -1
}
fmt.Printf("Writing %s...\n", outputFilename)
err = writer.WriteToFile(outputFilename)
if err != nil {
log.Fatal("Error writing to file:", err)
return -1
}

fmt.Println("Done")
return 0
}

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage:")
fmt.Println("Convert 3MF to STL: go run 3mf_convert.go model.3mf")
fmt.Println("Convert STL to 3MF: go run 3mf_convert.go model.stl")
os.Exit(1)
} else {
if err := convert(os.Args[1]); err != 0 {
os.Exit(err)
}
}
}
110 changes: 110 additions & 0 deletions SDK/Examples/Go/add_triangle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
+++

Copyright (C) 2019 3MF Consortium (Vijai Kumar Suriyababu)

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Abstract: Simplest 3mf example that just includes a single triangle

Interface version: 2.3.2
+++
*/

package main

import (
"fmt"
lib3mf "github.com/3MFConsortium/lib3mf.go/v2"
"log"
)

// createVertexAndReturnIndex creates a vertex on the mesh object and returns its index.
func createVertexAndReturnIndex(mesh lib3mf.MeshObject, x, y, z float32) uint32 {
position := lib3mf.Position{
Coordinates: [3]float32{x, y, z},
}
vertexIndex, err := mesh.AddVertex(position)
if err != nil {
log.Fatalf("Error adding vertex at (%f, %f, %f): %v", x, y, z, err)
}
return vertexIndex
}

// addTriangle adds a triangle to the mesh object using three vertex indices.
func addTriangle(mesh lib3mf.MeshObject, p1, p2, p3 uint32) {
triangle := lib3mf.Triangle{
Indices: [3]uint32{p1, p2, p3},
}
_, err := mesh.AddTriangle(triangle)
if err != nil {
log.Fatalf("Error adding triangle with vertices %d, %d, %d: %v", p1, p2, p3, err)
}
}

func main() {
// Get a wrapper object
wrapper, err := lib3mf.GetWrapper()
if err != nil {
log.Fatalf("Error loading 3MF library: %v", err)
}

// Check the lib3mf version
nMajor, nMinor, nMicro, err := wrapper.GetLibraryVersion()
if err != nil {
log.Fatalf("Error fetching lib3mf version: %v", err)
}
fmt.Printf("lib3mf version: %d.%d.%d\n", nMajor, nMinor, nMicro)

// Create a new 3MF model
model, err := wrapper.CreateModel()
if err != nil {
log.Fatalf("Error creating 3MF model: %v", err)
}

// Initialize a mesh object
meshObject, err := model.AddMeshObject()
if err != nil {
log.Fatalf("Error adding mesh object: %v", err)
}

// Create 3 vertices
p1 := createVertexAndReturnIndex(meshObject, 0, 0, 0)
p2 := createVertexAndReturnIndex(meshObject, 0, 1, 0)
p3 := createVertexAndReturnIndex(meshObject, 0, 0, 1)

// Create a triangle with 3 positions
addTriangle(meshObject, p1, p2, p3)

// Get a 3MF writer and write the single triangle
writer, err := model.QueryWriter("3mf")
if err != nil {
log.Fatalf("Error querying writer for 3MF format: %v", err)
}
err = writer.WriteToFile("triangle.3mf")
if err != nil {
log.Fatalf("Error writing to file 'triangle.3mf': %v", err)
}

fmt.Println("3MF file with a single triangle written successfully to 'triangle.3mf'")
}
Loading
Loading