def mesh2(name, vertices): """ Create a POV-ray mesh2 object from facet data. Arguments: name: The name of the object. vertices: An (N,3) numpy array containing the vertex data. Returns: A string representation of a POV-ray mesh2 object. """ ifacets, points = stl.toindexed(vertices) lines = [ "# declare m_{} = mesh2 {{".format(name), ' vertex_vectors {', ' {},'.format(len(points)) ] # The indices sequence 0, 2, 1 is used because of the difference between # the STL coordinate system and that used in POV-ray lines += [' <{0}, {2}, {1}>,'.format(*p) for p in points] lines[-1] = lines[-1][:-1] lines += [' }\n face_indices {', ' {},'.format(len(ifacets))] lines += [' <{0}, {1}, {2}>,'.format(*f) for f in ifacets] lines[-1] = lines[-1][:-1] lines += [' }', '}'] return '\n'.join(lines)
def mesh2(name, vertices): """ Create a POV-ray mesh2 object from facet data. Arguments: name: The name of the object. vertices: An (N,3) numpy array containing the vertex data. Returns: A string representation of a POV-ray mesh2 object. """ ifacets, points = stl.toindexed(vertices) lines = [ "# declare m_{} = mesh2 {{".format(name), ' vertex_vectors {', ' {},'.format(len(points)) ] # The indices sequence 1, 0, 2 is used because of the difference between # the STL coordinate system and that used in POV-ray lines += [' <{1}, {0}, {2}>,'.format(*p) for p in points] lines[-1] = lines[-1][:-1] lines += [' }\n face_indices {', ' {},'.format(len(ifacets))] lines += [' <{0}, {1}, {2}>,'.format(*f) for f in ifacets] lines[-1] = lines[-1][:-1] lines += [' }', '}'] return '\n'.join(lines)
def test_normals(): vertices, name = stl.readstl('test/data/cube-txt.stl') facets, pnts = stl.toindexed(vertices) ni, nv = stl.normals(facets, pnts) assert len(ni) == 12 # 6 faces, 2 facets/face. p = np.arange(6) assert np.all(ni == np.vstack((p, p)).T.reshape((1, -1))) assert nv.shape == (6, 3)
def test_normals(): vertices, name = stl.readstl('data/cube-txt.stl') facets, pnts = stl.toindexed(vertices) ni, nv = stl.normals(facets, pnts) assert len(ni) == 12 # 6 faces, 2 facets/face. p = np.arange(6) assert np.all(ni == np.vstack((p, p)).T.reshape((1, -1))) assert nv.shape == (6, 3)
def test_text(): vertices, name = stl.readstl('data/cube-txt.stl') facets, pnts = stl.toindexed(vertices) ni, nv = stl.normals(facets, pnts) res = stl.text('cube_txt', facets, pnts, ni, nv) res = [ln.strip() for ln in res.splitlines()] with open('data/cube-txt.stl') as inp: orig = [ln.strip() for ln in inp.readlines()] for a, b in zip(orig, res): assert a == b
def test_read_txt(): vertices, name = stl.readstl('test/data/cube-txt.stl') assert name == 'cube_txt' assert vertices.shape == (36, 3) _, pnts = stl.toindexed(vertices) assert pnts.shape == (8, 3) assert np.all(pnts[0] == np.array([0, 0, 1])) assert np.all(pnts[1] == np.array([1, 0, 1])) assert np.all(pnts[2] == np.array([0, 1, 1])) assert np.all(pnts[3] == np.array([1, 1, 1])) assert np.all(pnts[4] == np.array([1, 0, 0])) assert np.all(pnts[5] == np.array([0, 0, 0])) assert np.all(pnts[6] == np.array([1, 1, 0])) assert np.all(pnts[7] == np.array([0, 1, 0]))
def test_read_txt(): vertices, name = stl.readstl('data/cube-txt.stl') assert name == 'cube_txt' assert vertices.shape == (36, 3) _, pnts = stl.toindexed(vertices) assert pnts.shape == (8, 3) assert np.all(pnts[0] == np.array([0, 0, 1])) assert np.all(pnts[1] == np.array([1, 0, 1])) assert np.all(pnts[2] == np.array([0, 1, 1])) assert np.all(pnts[3] == np.array([1, 1, 1])) assert np.all(pnts[4] == np.array([1, 0, 0])) assert np.all(pnts[5] == np.array([0, 0, 0])) assert np.all(pnts[6] == np.array([1, 1, 0])) assert np.all(pnts[7] == np.array([0, 1, 0]))
def main(argv): """Main program. Keyword 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('-v', '--version', action='version', version=__version__) parser.add_argument('file', nargs='*', help='one or more file names') args = parser.parse_args(argv) if not args.file: parser.print_help() sys.exit(0) for fn in args.file: if not fn.lower().endswith('.stl'): w = 'The file "{}" is probably not an STL file, skipping.' print w.format(fn) continue try: vertices, name = stl.readstl(fn) if args.text or args.binary: facets, points = stl.toindexed(vertices) normals, vectors = stl.normals(facets, points) except ValueError as e: print fn + ':', e continue print "# Information for:", fn print "# Generated by stlinfo {}".format(__version__) print "# on {}.".format(time.asctime()) print '# Name: "{}"'.format(name) print '# Number of facets:', len(vertices)/3 minx, maxx, miny, maxy, minz, maxz = bbox.makebb(vertices) print '# Bounding box:' print '# {} ≤ x ≤ {}'.format(minx, maxx) print '# {} ≤ y ≤ {}'.format(miny, maxy) print '# {} ≤ z ≤ {}'.format(minz, maxz) if args.text: print '# Text representation:' print stl.text(name, facets, points, normals, vectors) if args.binary: on = utils.outname(fn, '.stl', '_bin') print '# Writing binary represtation to "{}".'.format(on) with open(on, 'w+b') as of: of.write(stl.binary(name, facets, points, normals, vectors))
def test_read_bin(): vertices, name = stl.readstl('test/data/cube-bin.stl') assert name == 'cube_bin' assert vertices.shape == (36, 3) _, pnts = stl.toindexed(vertices) assert pnts.shape == (8, 3)
def test_read_bin(): vertices, name = stl.readstl('data/cube-bin.stl') assert name == 'cube_bin' assert vertices.shape == (36, 3) _, pnts = stl.toindexed(vertices) assert pnts.shape == (8, 3)
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 = 'The file "{}" is probably not an STL file, skipping.' logging.warning(w.format(fn)) 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('{}: {}'.format(fn, e)) continue print("# Information for:", fn) print("# Generated by stlinfo {}".format(__version__)) print("# on {}.".format(time.asctime())) print('# Name: "{}"'.format(name)) print('# Number of facets: {:.0f}'.format(len(vertices) / 3)) minx, maxx, miny, maxy, minz, maxz = bbox.makebb(vertices) print('# Bounding box:') print('# {} ≤ x ≤ {}'.format(minx, maxx)) print('# {} ≤ y ≤ {}'.format(miny, maxy)) print('# {} ≤ z ≤ {}'.format(minz, maxz)) if args.text: print('# Text representation:') print(stl.text(name, facets, points, normals, vectors)) if args.binary: on = utils.outname(fn, '.stl', '_bin') print('# Writing binary represtation to "{}".'.format(on)) with open(on, 'w+b') as of: of.write(stl.binary(name, facets, points, normals, vectors))
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 = 'The file "{}" is probably not an STL file, skipping.' logging.warning(w.format(fn)) 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('{}: {}'.format(fn, e)) continue print("# Information for:", fn) print("# Generated by stlinfo {}".format(__version__)) print("# on {}.".format(time.asctime())) print('# Name: "{}"'.format(name)) print('# Number of facets: {:.0f}'.format(len(vertices) / 3)) minx, maxx, miny, maxy, minz, maxz = bbox.makebb(vertices) print('# Bounding box:') print('# {} ≤ x ≤ {}'.format(minx, maxx)) print('# {} ≤ y ≤ {}'.format(miny, maxy)) print('# {} ≤ z ≤ {}'.format(minz, maxz)) if args.text: print('# Text representation:') print(stl.text(name, facets, points, normals, vectors)) if args.binary: on = utils.outname(fn, '.stl', '_bin') print('# Writing binary represtation to "{}".'.format(on)) with open(on, 'w+b') as of: of.write(stl.binary(name, facets, points, normals, vectors))