ldcheck.py

Mon, 22 Jan 2018 01:01:10 +0200

author
Santeri Piippo
date
Mon, 22 Jan 2018 01:01:10 +0200
changeset 15
45b3aeb25003
parent 13
12d4ddc4bfd8
child 17
327da5d00360
permissions
-rwxr-xr-x

Condensed transform_to_xy

#!/usr/bin/env python3
from sys import version_info
if version_info < (3, 4):
    raise RuntimeError('Python 3.4 or newer required')

from parse import parse_ldraw_code
from colours import load_colours
from geometry import *
from pathlib import Path
import linetypes

from os.path import realpath
script_directory = Path(realpath(__file__)).parent

def load_config(filename):
    from configobj import ConfigObj
    from copy import deepcopy
    config = ConfigObj(filename, encoding = 'UTF8')
    read_config = deepcopy(config)
    if 'libraries' not in config:
        config['libraries'] = ['/path/to/ldraw']
    if config != read_config:
        config.write()
    return config

def read_ldraw(file, *, config):
    result = list()
    for line in file:
        result.append(parse_ldraw_code(line))
    return result

def library_paths(config):
    for library_path_string in config['libraries']:
        yield Path(library_path_string).expanduser()

def find_ldconfig_ldr_paths(config):
    for library_path in library_paths(config):
        ldconfig_paths = [
            library_path / 'LDConfig.ldr',
            library_path / 'ldconfig.ldr',
        ]
        for path in ldconfig_paths:
            print(path)
            if path.is_file():
                yield path

def hairline_score(smallest_angle):
    from math import log10
    return max(0, -log10(smallest_angle))

class Model:
	def __init__(self, body):
		self.body = body
	@property
	def quadrilaterals(self):
		yield from filter(
			lambda element: isinstance(element, linetypes.Quadrilateral),
			self.body)

if __name__ == '__main__':
    from sys import argv
    config = load_config('ldcheck.cfg')
    for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
        with ldconfig_ldr_path.open() as ldconfig_ldr:
            load_colours(ldconfig_ldr)
    with open(argv[1], 'r') as file:
        model_body = read_ldraw(file, config = config)
        model = Model(body = model_body)
        for line_number, entry in enumerate(model_body, 1):
            if hasattr(entry, 'colour'):
                print(repr(entry.colour))
            if hasattr(entry, 'geometry') and len(entry.geometry) >= 3:
                if hairline_score(entry.geometry.smallest_angle) >= 2.0:
                    print(str.format(
                        'Hairline {type} at line {line_number}',
                        type = entry.typename(),
                        line_number = line_number,
                    ))
                    print(entry.textual_representation())
                    print('-' * 25)

mercurial