Thu, 21 Dec 2017 12:27:16 +0200
Added ldconfig.ldr support
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
1 | #!/usr/bin/env python3 |
8 | 2 | from sys import version_info |
3 | if version_info < (3, 4): | |
4 | raise RuntimeError('Python 3.4 or newer required') | |
5 | ||
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
6 | from parse import parse_ldraw_code |
8 | 7 | from colours import load_colours |
7 | 8 | from geometry import * |
8 | 9 | from pathlib import Path |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
10 | |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
11 | def read_ldraw(file, *, libraries): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
12 | result = list() |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
13 | for line in file: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
14 | result.append(parse_ldraw_code(line)) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
15 | return result |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
16 | |
8 | 17 | def find_ldconfig_ldr_paths(libraries): |
18 | for library in libraries: | |
19 | ldconfig_paths = [ | |
20 | library['path'] / 'LDConfig.ldr', | |
21 | library['path'] / 'ldconfig.ldr', | |
22 | ] | |
23 | for path in ldconfig_paths: | |
24 | if path.is_file(): | |
25 | yield path | |
26 | ||
7 | 27 | def hairline_score(smallest_angle): |
28 | from math import log10 | |
29 | return max(0, -log10(smallest_angle)) | |
30 | ||
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
31 | if __name__ == '__main__': |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
32 | from sys import argv |
8 | 33 | libraries = [{'path': Path('~/ldraw').expanduser(), 'role': 'official'}] |
34 | for ldconfig_ldr_path in find_ldconfig_ldr_paths(libraries): | |
35 | with ldconfig_ldr_path.open() as ldconfig_ldr: | |
36 | load_colours(ldconfig_ldr) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
37 | with open(argv[1], 'r') as file: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
38 | model = read_ldraw(file, libraries = libraries) |
7 | 39 | for line_number, entry in enumerate(model, 1): |
8 | 40 | if hasattr(entry, 'colour'): |
41 | print(repr(entry.colour)) | |
6
6da1e81c5652
Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents:
3
diff
changeset
|
42 | if hasattr(entry, 'geometry') and len(entry.geometry) >= 3: |
7 | 43 | if hairline_score(entry.geometry.smallest_angle) >= 2.0: |
44 | print(str.format( | |
45 | 'Hairline {type} at line {line_number}', | |
46 | type = entry.typename(), | |
47 | line_number = line_number, | |
48 | )) | |
49 | print(entry.textual_representation()) | |
50 | print('-' * 25) |