-
Notifications
You must be signed in to change notification settings - Fork 14
/
stlinfo.py
100 lines (93 loc) · 3.19 KB
/
stlinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#! /usr/bin/env python
# file: stlinfo.py
# vim:fileencoding=utf-8:fdm=marker:ft=python
#
# Copyright © 2020 R.F. Smith <[email protected]>. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause
# Created: 2011-12-22T16:48:45+01:00
# Last modified: 2020-10-04T16:18:51+0200
"""
Read an STL file and print information about the object.
Optionally print a text representation of the object. It can also write a binary
STL version of the object.
"""
import argparse
import logging
import sys
import time
from stltools import stl, bbox, utils, __version__
def main(argv):
"""
Entry point for stlinfo.
Arguments:
argv: command line arguments (without program name!)
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-t",
"--text",
action="store_true",
help="print text representation of the file",
)
parser.add_argument(
"-b",
"--binary",
action="store_true",
help="write binary representation of the file",
)
parser.add_argument(
"-e",
"--encoding",
type=str,
help="encoding for the name of the STL object (default utf-8)",
default="utf-8",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument("file", nargs="*", help="one or more file names")
args = parser.parse_args(argv)
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
if not args.file:
parser.print_help()
sys.exit(0)
for fn in args.file:
if not fn.lower().endswith(".stl"):
w = f'The file "{fn}" is probably not an STL file, skipping.'
logging.warning(w)
continue
try:
vertices, name = stl.readstl(fn, args.encoding)
if args.text or args.binary:
facets, points = stl.toindexed(vertices)
normals, vectors = stl.normals(facets, points)
except ValueError as e:
logging.error(f"{fn}: {e}")
continue
print("# Information for:", fn)
print(f"# Generated by stlinfo {__version__}")
print(f"# on {time.asctime()}.")
print(f'# Name: "{name}"')
print(f"# Number of facets: {len(vertices)/3:.0f}")
minx, maxx, miny, maxy, minz, maxz = bbox.makebb(vertices)
print("# Bounding box:")
print(f"# {minx:.3f} ≤ x ≤ {maxx:.3f}")
print(f"# {miny:.3f} ≤ y ≤ {maxy:.3f}")
print(f"# {minz:.3f} ≤ z ≤ {maxz:.3f}")
if args.text:
print("# Text representation:")
print(stl.text(name, facets, points, normals, vectors))
if args.binary:
on = utils.outname(fn, ".stl", "_bin")
print(f'# Writing binary represtation to "{on}".')
with open(on, "w+b") as of:
of.write(stl.binary(name, facets, points, normals, vectors))
if __name__ == "__main__":
main(sys.argv[1:])