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

Python examples #365

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions SDK/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Bin
Bindings
Examples/*/*.dll
Examples/*/*.stl
.idea
cmake-build-*
95 changes: 95 additions & 0 deletions SDK/Examples/Python/3mf_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'''++

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.

This file has been generated by the Automatic Component Toolkit (ACT) version 1.6.0-develop.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these files autogenerated?

Copy link
Collaborator Author

@vijaiaeroastro vijaiaeroastro May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gangatp Sorry. Copy pasted from the existing example.


Abstract: An example to convert between 3MF and STL

Interface version: 2.3.1

'''


import sys
from lib3mf_common import *


def find_extension(filename):
idx = filename.rfind('.')
if idx != -1:
return filename[idx:]
return ""


def convert(filename):
# Get a wrapper object
wrapper = get_wrapper()

# Check version always
get_version(wrapper)

extension = find_extension(filename).lower()
reader_name, writer_name, new_extension = "", "", ""

if extension == ".stl":
reader_name = "stl"
writer_name = "3mf"
new_extension = ".3mf"
elif extension == ".3mf":
reader_name = "3mf"
writer_name = "stl"
new_extension = ".stl"

if not reader_name:
print(f"Unknown input file extension: {extension}")
return -1

output_filename = filename[:-len(extension)] + new_extension

model = wrapper.CreateModel()
reader = model.QueryReader(reader_name)
print(f"Reading {filename}...")
reader.ReadFromFile(filename)

writer = model.QueryWriter(writer_name)
print(f"Writing {output_filename}...")
writer.WriteToFile(output_filename)
print("Done")
return 0


if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage:")
print("Convert 3MF to STL: python3 3mf_convert.py model.3mf")
print("Convert STL to 3MF: python3 3mf_convert.py model.stl")
else:
try:
result = convert(sys.argv[1])
sys.exit(result)
except Exception as e:
print(str(e))
sys.exit(1)
59 changes: 59 additions & 0 deletions SDK/Examples/Python/add_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'''++

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.

This file has been generated by the Automatic Component Toolkit (ACT) version 1.6.0-develop.

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

Interface version: 2.3.1

'''

from lib3mf_common import *

# Get wrapper
wrapper = get_wrapper()

# Get version
get_version(wrapper)

# Create a model
model = wrapper.CreateModel()

# Initialize a mesh object
meshObject = model.AddMeshObject()

# Now create 3 vertices
p1 = create_vertex_and_return_index(meshObject, 0, 0, 0)
p2 = create_vertex_and_return_index(meshObject, 0, 1, 0)
p3 = create_vertex_and_return_index(meshObject, 0, 0, 1)

# Create a triangle with 3 positions
add_triangle(meshObject, p1, p2, p3)

# Get a 3MF writer and write the single triangle
writer = model.QueryWriter("3mf")
writer.WriteToFile("triangle.3mf")
99 changes: 99 additions & 0 deletions SDK/Examples/Python/beam_lattice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'''++

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.

This file has been generated by the Automatic Component Toolkit (ACT) version 1.6.0-develop.

Abstract: Beam Lattice example

Interface version: 2.3.1

'''


from lib3mf_common import *

# Get a wrapper object
wrapper = get_wrapper()

# Check version always
get_version(wrapper)

# Create a model and set name
model = wrapper.CreateModel()
mesh_object = model.AddMeshObject()
mesh_object.SetName("Beamlattice")

# Modifiable size
fSizeX = 100.0
fSizeY = 200.0
fSizeZ = 300.0

# Define vertices (creates an array of lib3mf position objects)
vertices = [
create_vertex(mesh_object, 0.0, 0.0, 0.0),
create_vertex(mesh_object, fSizeX, 0.0, 0.0),
create_vertex(mesh_object, fSizeX, fSizeY, 0.0),
create_vertex(mesh_object, 0.0, fSizeY, 0.0),
create_vertex(mesh_object, 0.0, 0.0, fSizeZ),
create_vertex(mesh_object, fSizeX, 0.0, fSizeZ),
create_vertex(mesh_object, fSizeX, fSizeY, fSizeZ),
create_vertex(mesh_object, 0.0, fSizeY, fSizeZ)
]

# Define beam variables
r0 = 1.0
r1 = 1.5
r2 = 2.0
r3 = 2.5

# Create a list of beams (strings are automatically converted to enums)
beams = [
create_beam(2, 1, r0, r0, 'Butt', 'Butt'),
create_beam(0, 3, r0, r1, 'Sphere', 'Butt'),
create_beam(4, 5, r0, r2, 'Sphere', 'Butt'),
create_beam(6, 7, r0, r3, 'HemiSphere', 'Butt'),
create_beam(0, 1, r1, r0, 'HemiSphere', 'Butt'),
create_beam(5, 4, r1, r1, 'Sphere', 'HemiSphere'),
create_beam(2, 3, r1, r2, 'Sphere', 'Sphere'),
create_beam(7, 6, r1, r3, 'Butt', 'Butt'),
create_beam(1, 2, r2, r2, 'Butt', 'Butt'),
create_beam(6, 5, r2, r3, 'HemiSphere', 'Butt'),
create_beam(3, 0, r3, r0, 'Butt', 'Sphere'),
create_beam(4, 7, r3, r1, 'HemiSphere', 'HemiSphere')
]

# Set geometry and beams
mesh_object.SetGeometry(vertices, [])
beam_lattice = mesh_object.BeamLattice()
beam_lattice.SetBeams(beams)
beam_lattice.SetMinLength(0.005)

# Add mesh object to the model
model.AddBuildItem(mesh_object, wrapper.GetIdentityTransform())

# Write it out
writer = model.QueryWriter("3mf")
writer.WriteToFile("beamlattice.3mf")
123 changes: 123 additions & 0 deletions SDK/Examples/Python/color_cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'''++

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.

This file has been generated by the Automatic Component Toolkit (ACT) version 1.6.0-develop.

Abstract: Color cube example

Interface version: 2.3.1

'''


from lib3mf_common import *

# Get wrapper
wrapper = get_wrapper()

# Get version
get_version(wrapper)

# Create a model
model = wrapper.CreateModel()

# Initialize a mesh object
mesh_object = model.AddMeshObject()
mesh_object.SetName("Colored Box")

# Define cube size
fSizeX, fSizeY, fSizeZ = 100.0, 200.0, 300.0

# Create vertices
vertices = [
create_vertex(mesh_object, 0.0, 0.0, 0.0),
create_vertex(mesh_object, fSizeX, 0.0, 0.0),
create_vertex(mesh_object, fSizeX, fSizeY, 0.0),
create_vertex(mesh_object, 0.0, fSizeY, 0.0),
create_vertex(mesh_object, 0.0, 0.0, fSizeZ),
create_vertex(mesh_object, fSizeX, 0.0, fSizeZ),
create_vertex(mesh_object, fSizeX, fSizeY, fSizeZ),
create_vertex(mesh_object, 0.0, fSizeY, fSizeZ)
]

# Define triangles
triangles = [
add_triangle(mesh_object, 2, 1, 0),
add_triangle(mesh_object, 0, 3, 2),
add_triangle(mesh_object, 4, 5, 6),
add_triangle(mesh_object, 6, 7, 4),
add_triangle(mesh_object, 0, 1, 5),
add_triangle(mesh_object, 5, 4, 0),
add_triangle(mesh_object, 2, 3, 7),
add_triangle(mesh_object, 7, 6, 2),
add_triangle(mesh_object, 1, 2, 6),
add_triangle(mesh_object, 6, 5, 1),
add_triangle(mesh_object, 3, 0, 4),
add_triangle(mesh_object, 4, 7, 3)
]

# Set geometry
mesh_object.SetGeometry(vertices, triangles)

# Define colors
color_group = model.AddColorGroup()
id_red = color_group.AddColor(wrapper.RGBAToColor(255, 0, 0, 255))
id_green = color_group.AddColor(wrapper.RGBAToColor(0, 255, 0, 255))
id_blue = color_group.AddColor(wrapper.RGBAToColor(0, 0, 255, 255))
id_orange = color_group.AddColor(wrapper.RGBAToColor(255, 128, 0, 255))
id_yellow = color_group.AddColor(wrapper.RGBAToColor(255, 255, 0, 255))

# Set triangle colors
sTriangleColorRed = create_triangle_color(color_group, id_red, id_red, id_red)
sTriangleColorGreen = create_triangle_color(color_group, id_green, id_green, id_green)
sTriangleColorBlue = create_triangle_color(color_group, id_blue, id_blue, id_blue)
sTriangleColor1 = create_triangle_color(color_group, id_orange, id_red, id_yellow)
sTriangleColor2 = create_triangle_color(color_group, id_yellow, id_green, id_orange)


# One-colored Triangles
mesh_object.SetTriangleProperties(0, sTriangleColorRed)
mesh_object.SetTriangleProperties(1, sTriangleColorRed)
mesh_object.SetTriangleProperties(2, sTriangleColorGreen)
mesh_object.SetTriangleProperties(3, sTriangleColorGreen)
mesh_object.SetTriangleProperties(4, sTriangleColorBlue)
mesh_object.SetTriangleProperties(5, sTriangleColorBlue)

# Gradient-colored Triangles
mesh_object.SetTriangleProperties(6, sTriangleColor1)
mesh_object.SetTriangleProperties(7, sTriangleColor2)
mesh_object.SetTriangleProperties(8, sTriangleColor1)
mesh_object.SetTriangleProperties(9, sTriangleColor2)
mesh_object.SetTriangleProperties(10, sTriangleColor1)
mesh_object.SetTriangleProperties(11, sTriangleColor2)

# Set object level property
mesh_object.SetObjectLevelProperty(sTriangleColorRed.ResourceID, sTriangleColorRed.PropertyIDs[0])

# Add build item and write to file
model.AddBuildItem(mesh_object, wrapper.GetIdentityTransform())
writer = model.QueryWriter("3mf")
writer.WriteToFile("colorcube.3mf")
Loading
Loading