Files
hexapod-robot/2026-current/hardware/openscad/export_3mf.py
T

115 lines
5.3 KiB
Python
Raw Normal View History

"""Combine STL parts into one multi-part 3MF for OrcaSlicer / PrusaSlicer.
python export_3mf.py (from hardware/openscad, after the STL export)
Writes stl/remote_v2/remote_v2_top_inlays.3mf: the top shell with its
two inlays as ONE object with three parts, in the same frame, each part on
its own extruder/filament (1 = case, 2 = honeycomb, 3 = ON/OFF text). The
part list is in PARTS below. Uses the PrusaSlicer 3MF layout (one mesh, the
parts as triangle ranges in Metadata/Slic3r_PE_model.config), which
OrcaSlicer and PrusaSlicer both read.
"""
import os
import zipfile
HERE = os.path.dirname(os.path.abspath(__file__))
OUT_DIR = os.path.join(HERE, "stl", "remote_v2")
# (file, part name, extruder)
PARTS = [
("remote_v2_top.stl", "top shell (black)", 1),
("remote_v2_inlay.stl", "honeycomb inlay (red)", 2),
("remote_v2_text.stl", "ON-OFF inlay (white)", 3),
]
OUT = "remote_v2_top_inlays.3mf" # remote_v2_top_multicolor.3mf is the owner's OrcaSlicer project
def read_stl(path):
"""Triangles of an ASCII or binary STL as lists of 3 vertex tuples."""
data = open(path, "rb").read()
tris = []
if data[:5] == b"solid" and b"facet" in data[:400]:
verts = []
for line in data.decode("ascii", "replace").splitlines():
t = line.split()
if t and t[0] == "vertex":
verts.append(tuple(float(x) for x in t[1:4]))
if len(verts) == 3:
tris.append(verts)
verts = []
else:
import struct
n = struct.unpack_from("<I", data, 80)[0]
for i in range(n):
f = struct.unpack_from("<12f", data, 84 + 50 * i)
tris.append([tuple(f[3:6]), tuple(f[6:9]), tuple(f[9:12])])
return tris
def main():
vertices, index, triangles, ranges = [], {}, [], []
for fname, name, extruder in PARTS:
first = len(triangles)
for tri in read_stl(os.path.join(OUT_DIR, fname)):
ids = []
for v in tri:
key = (round(v[0], 5), round(v[1], 5), round(v[2], 5), len(ranges)) # no sharing between parts
if key not in index:
index[key] = len(vertices)
vertices.append(v)
ids.append(index[key])
if len(set(ids)) == 3:
triangles.append(ids)
ranges.append((first, len(triangles) - 1, name, extruder))
model = ['<?xml version="1.0" encoding="UTF-8"?>',
'<model unit="millimeter" xml:lang="en-US" '
'xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02" '
'xmlns:slic3rpe="http://schemas.slic3r.org/3mf/2017/06">',
' <metadata name="slic3rpe:Version3mf">1</metadata>',
' <metadata name="Title">remote_v2 top shell with inlays</metadata>',
' <resources>',
' <object id="1" type="model">',
' <mesh>',
' <vertices>']
model += [' <vertex x="%.5f" y="%.5f" z="%.5f"/>' % v for v in vertices]
model += [' </vertices>', ' <triangles>']
model += [' <triangle v1="%d" v2="%d" v3="%d"/>' % tuple(t) for t in triangles]
model += [' </triangles>', ' </mesh>', ' </object>', ' </resources>',
' <build>', ' <item objectid="1"/>', ' </build>', '</model>']
config = ['<?xml version="1.0" encoding="UTF-8"?>', '<config>',
' <object id="1" instances_count="1">',
' <metadata type="object" key="name" value="remote_v2_top_multicolor"/>']
for first, last, name, extruder in ranges:
config += [' <volume firstid="%d" lastid="%d">' % (first, last),
' <metadata type="volume" key="name" value="%s"/>' % name,
' <metadata type="volume" key="volume_type" value="ModelPart"/>',
' <metadata type="volume" key="extruder" value="%d"/>' % extruder,
' </volume>']
config += [' </object>', '</config>']
content_types = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">\n'
' <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>\n'
' <Default Extension="model" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml"/>\n'
' <Default Extension="config" ContentType="text/xml"/>\n'
'</Types>\n')
rels = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">\n'
' <Relationship Target="/3D/3dmodel.model" Id="rel0" '
'Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel"/>\n'
'</Relationships>\n')
path = os.path.join(OUT_DIR, OUT)
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z:
z.writestr("[Content_Types].xml", content_types)
z.writestr("_rels/.rels", rels)
z.writestr("3D/3dmodel.model", "\n".join(model) + "\n")
z.writestr("Metadata/Slic3r_PE_model.config", "\n".join(config) + "\n")
print("%s: %d vertices, %d triangles, parts %s" % (OUT, len(vertices), len(triangles),
", ".join("%s %d-%d" % (r[2], r[0], r[1]) for r in ranges)))
if __name__ == "__main__":
main()