ldraw.py

Sun, 10 Dec 2017 15:37:26 +0200

author
Santeri Piippo
date
Sun, 10 Dec 2017 15:37:26 +0200
changeset 0
55b4c97d44c5
child 1
5411a25cfca7
permissions
-rwxr-xr-x

Initial commit with half-done parsing function

0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
1 #!/usr/bin/env python3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
2 import linetypes
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
3 import re
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
4 from geometry import *
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
5
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
6 class BadLdrawLine(Exception):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
7 pass
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
8
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
9 def parse_ldraw_code(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
10 line = line.strip()
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
11 if not line:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
12 return linetypes.EmptyLine()
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
13 if line == '0':
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
14 return linetypes.Comment('')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
15 if line.startswith('0 '):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
16 return linetypes.Comment(line[2:].strip())
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
17 if line.startswith('1 '):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
18 return parse_ldraw_subfile_reference(line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
19 if line.startswith('2 '):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
20 return parse_ldraw_line(line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
21 ...
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
22
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
23 def parse_ldraw_subfile_reference(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
24 pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
25 match = re.search(pattern, line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
26 if not match:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
27 raise BadLdrawLine('unable to parse')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
28 groups = list(match.groups())
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
29 indices = {
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
30 'color': 0,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
31 'anchor': slice(1, 4),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
32 'matrix': slice(4, 13),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
33 'subfile_path': 13
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
34 }
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
35 try:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
36 color = int(groups[indices['color']])
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
37 vertex_values = [float(x) for x in groups[indices['anchor']]]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
38 matrix_values = [float(x) for x in groups[indices['matrix']]]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
39 except ValueError:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
40 raise BadLdrawLine('bad numeric values')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
41 return linetypes.SubfileReference(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
42 color = color,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
43 anchor = Vertex(*vertex_values),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
44 matrix = TransformationMatrix(matrix_values),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
45 subfile_path = groups[indices['subfile_path']]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
46 )
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
47
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
48 def generic_parse_polygon(line, *, type_code, vertex_count):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
49 pattern = r'^' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
50 + str(type_code) \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
51 + '\s+(\d+)' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
52 + r'\s+([^ ]+)' * (vertex_count * 3) \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
53 + r'\s*$'
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
54 match = re.search(pattern, line)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
55 if not match:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
56 raise BadLdrawLine(str.format('cannot parse type-{} line', type_code))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
57 vertices = []
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
58 for vertex_index in range(vertex_count):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
59 slice_begin = 1 + vertex_index * 3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
60 slice_end = 1 + (vertex_index + 1) * 3
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
61 coordinates = match.groups()[slice_begin:slice_end]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
62 assert(len(coordinates) == 3)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
63 try:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
64 coordinates = [float(x) for x in coordinates]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
65 except ValueError:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
66 raise BadLdrawLine('bad numeric values')
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
67 vertices.append(Vertex(*coordinates))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
68 return {
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
69 'color': int(match.group(1)),
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
70 'vertices': vertices,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
71 }
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
72
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
73 def parse_ldraw_line(line):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
74 parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
75 line_segment = LineSegment(*parse_result['vertices'])
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
76 return linetypes.LineSegment(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
77 color = parse_result['color'],
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
78 geometry = line_segment)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
79
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
80 def read_ldraw(file, *, libraries):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
81 result = list()
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
82 for line in file:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
83 result.append(parse_ldraw_code(line))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
84 return result
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
85
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
86 if __name__ == '__main__':
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
87 from sys import argv
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
88 libraries = [{'path': '/home/teemu/ldraw', 'role': 'official'}]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
89 with open(argv[1], 'r') as file:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
90 model = read_ldraw(file, libraries = libraries)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
91 from pprint import pprint
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
92 pprint(model)

mercurial