Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
from stltools import vecops as vo

logging.basicConfig(level='INFO', format='%(levelname)s: %(message)s')
logging.info('creating facet data')
vertices = np.array(
    [[0, 0, 1], [1, 0, 1], [1, 0, 0], [0, 0, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0], [0, 1, 0]],
    np.float32
)
facets = np.array(
    [
        [0, 1, 4], [1, 5, 4], [0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7], [3, 7, 2], [7, 6, 2],
        [1, 2, 5], [5, 2, 6], [0, 4, 3], [3, 4, 7]
    ], np.uint16
)
logging.info('calculating normals')
ni, normals = stl.normals(facets, vertices)

logging.info('vertices:\n{}'.format(vertices))
logging.info('facets:\n{}'.format(facets))
logging.info('normals:\n{}'.format(normals))
logging.info('normal indices:\n{}'.format(ni))

logging.info('creating text STL data')
txtdata = stl.text('cube-txt', facets, vertices, ni, normals)
try:
    with open('cube-txt.stl', 'w') as stlf:
        stlf.write(txtdata)
    logging.info('wrote text STL file cube-txt.stl')
except IOError as e:
    logging.error('unable to write cube-txt.stl; {}'.format(e))
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
import numpy as np
from stltools import matrix as mx
from stltools import stl
from stltools import vecops as vo

logging.basicConfig(level='INFO', format='%(levelname)s: %(message)s')
logging.info('creating facet data')
vertices = np.array([[0, 0, 1], [1, 0, 1], [1, 0, 0], [0, 0, 0], [0, 1, 1],
                     [1, 1, 1], [1, 1, 0], [0, 1, 0]], np.float32)
facets = np.array(
    [[0, 1, 4], [1, 5, 4], [0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7],
     [3, 7, 2], [7, 6, 2], [1, 2, 5], [5, 2, 6], [0, 4, 3], [3, 4, 7]],
    np.uint16)
logging.info('calculating normals')
ni, normals = stl.normals(facets, vertices)

logging.info('vertices:\n{}'.format(vertices))
logging.info('facets:\n{}'.format(facets))
logging.info('normals:\n{}'.format(normals))
logging.info('normal indices:\n{}'.format(ni))

logging.info('creating text STL data')
txtdata = stl.text('cube-txt', facets, vertices, ni, normals)
try:
    with open('cube-txt.stl', 'w') as stlf:
        stlf.write(txtdata)
    logging.info('wrote text STL file cube-txt.stl')
except IOError as e:
    logging.error('unable to write cube-txt.stl; {}'.format(e))