Skip to content

Commit

Permalink
First batch of python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaiaeroastro committed May 14, 2024
1 parent 6c92731 commit 0098f2b
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 0 deletions.
61 changes: 61 additions & 0 deletions SDK/Examples/Python/3mf_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# An example to convert between 3MF and STL
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)
25 changes: 25 additions & 0 deletions SDK/Examples/Python/add_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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")
65 changes: 65 additions & 0 deletions SDK/Examples/Python/beam_lattice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Beam Lattice example
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")
89 changes: 89 additions & 0 deletions SDK/Examples/Python/color_cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Color cube example
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")
60 changes: 60 additions & 0 deletions SDK/Examples/Python/create_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# An example that create multiple components using transformations
from lib3mf_common import *

# Get wrapper
wrapper = get_wrapper()

# Get version
get_version(wrapper)

# Create a model
model = wrapper.CreateModel()
mesh_object = model.AddMeshObject()
mesh_object.SetName("Box")

# Define the size of the box
fSizeX, fSizeY, fSizeZ = 10.0, 20.0, 30.0

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

# Define triangles by vertices indices
triangle_indices = [
(2, 1, 0), (0, 3, 2), (4, 5, 6), (6, 7, 4),
(0, 1, 5), (5, 4, 0), (2, 3, 7), (7, 6, 2),
(1, 2, 6), (6, 5, 1), (3, 0, 4), (4, 7, 3)
]

# Create a list of triangles
triangles = []
for v0, v1, v2 in triangle_indices:
triangles.append(add_triangle(mesh_object, v0, v1, v2))

# Set geometry to the mesh object after creating vertices and triangles
mesh_object.SetGeometry(vertices, triangles)

# Adding components with different transformations
components_object = model.AddComponentsObject()
components_object.AddComponent(mesh_object, create_translation_matrix(0.0, 0.0, 0.0))
components_object.AddComponent(mesh_object, create_translation_matrix(40.0, 60.0, 80.0))
components_object.AddComponent(mesh_object, create_translation_matrix(120.0, 30.0, 70.0))

# Add the components object to the model as a build item
model.AddBuildItem(components_object, create_translation_matrix(0.0, 0.0, 0.0))

# Writing to files (3MF)
writer_3mf = model.QueryWriter("3mf")
writer_3mf.WriteToFile("components.3mf")

# Dump to a STL file
writer_stl = model.QueryWriter("stl")
writer_stl.WriteToFile("components.stl")
50 changes: 50 additions & 0 deletions SDK/Examples/Python/create_cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Simple cube creation example
from lib3mf_common import *

# Get a wrapper object
wrapper = get_wrapper()

# Check version always
get_version(wrapper)

# Create a model
model = wrapper.CreateModel()
mesh_object = model.AddMeshObject()
mesh_object.SetName("Box")

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

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

# Define triangles by vertices indices
triangle_indices = [
(2, 1, 0), (0, 3, 2), (4, 5, 6), (6, 7, 4),
(0, 1, 5), (5, 4, 0), (2, 3, 7), (7, 6, 2),
(1, 2, 6), (6, 5, 1), (3, 0, 4), (4, 7, 3)
]

# Create triangles
triangles = []
for v0, v1, v2 in triangle_indices:
triangles.append(add_triangle(mesh_object, v0, v1, v2))

# Set geometry to the mesh object after creating vertices and triangles
mesh_object.SetGeometry(vertices, triangles)

# Add build item with an identity transform
model.AddBuildItem(mesh_object, wrapper.GetIdentityTransform())

# Save the model to a 3MF file
writer = model.QueryWriter("3mf")
writer.WriteToFile("cube.3mf")
30 changes: 30 additions & 0 deletions SDK/Examples/Python/extract_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Extract info from a 3MF model
import sys
from lib3mf_common import *


def extract_info(file_path):
wrapper = get_wrapper()
model = wrapper.CreateModel()

# Initialize a 3MF reader
read_3mf_file_to_model(model, file_path)

# Print library version
get_version(wrapper)

# Show meta data info
show_metadata_information(model.GetMetaDataGroup())

# Show slice stack info
show_slice_stack_information(model)

# Show object info
show_object_information(model)


if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python extract_info.py model.3mf")
sys.exit()
extract_info(sys.argv[1])
Loading

0 comments on commit 0098f2b

Please sign in to comment.