parse.py

changeset 4
8eb83f200486
parent 3
1dc58f44d556
child 6
6da1e81c5652
equal deleted inserted replaced
3:1dc58f44d556 4:8eb83f200486
1 import linetypes 1 import linetypes
2 import re 2 import re
3 from geometry import * 3 from geometry import *
4 from ldraw import Color 4 from ldraw import Colour
5 5
6 class BadLdrawLine(Exception): 6 class BadLdrawLine(Exception):
7 pass 7 pass
8 8
9 def parse_ldraw_code(line): 9 def parse_ldraw_code(line):
32 match = re.search(pattern, line) 32 match = re.search(pattern, line)
33 if not match: 33 if not match:
34 raise BadLdrawLine('unable to parse') 34 raise BadLdrawLine('unable to parse')
35 groups = list(match.groups()) 35 groups = list(match.groups())
36 indices = { 36 indices = {
37 'color_index': 0, 37 'colour_index': 0,
38 'anchor': slice(1, 4), 38 'anchor': slice(1, 4),
39 'matrix': slice(4, 13), 39 'matrix': slice(4, 13),
40 'subfile_path': 13 40 'subfile_path': 13
41 } 41 }
42 try: 42 try:
43 color = Color(groups[indices['color_index']]) 43 colour = Colour(groups[indices['colour_index']])
44 vertex_values = [float(x) for x in groups[indices['anchor']]] 44 vertex_values = [float(x) for x in groups[indices['anchor']]]
45 matrix_values = [float(x) for x in groups[indices['matrix']]] 45 matrix_values = [float(x) for x in groups[indices['matrix']]]
46 except ValueError: 46 except ValueError:
47 raise BadLdrawLine('bad numeric values') 47 raise BadLdrawLine('bad numeric values')
48 return linetypes.SubfileReference( 48 return linetypes.SubfileReference(
49 color = color, 49 colour = colour,
50 anchor = Vertex(*vertex_values), 50 anchor = Vertex(*vertex_values),
51 matrix = TransformationMatrix(matrix_values), 51 matrix = TransformationMatrix(matrix_values),
52 subfile_path = groups[indices['subfile_path']] 52 subfile_path = groups[indices['subfile_path']]
53 ) 53 )
54 54
71 coordinates = [float(x) for x in coordinates] 71 coordinates = [float(x) for x in coordinates]
72 except ValueError: 72 except ValueError:
73 raise BadLdrawLine('bad numeric values') 73 raise BadLdrawLine('bad numeric values')
74 vertices.append(Vertex(*coordinates)) 74 vertices.append(Vertex(*coordinates))
75 return { 75 return {
76 'color': Color(match.group(1)), 76 'colour': Colour(match.group(1)),
77 'vertices': vertices, 77 'vertices': vertices,
78 } 78 }
79 79
80 def parse_ldraw_line(line): 80 def parse_ldraw_line(line):
81 parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2) 81 parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2)
82 return linetypes.LineSegment( 82 return linetypes.LineSegment(
83 color = parse_result['color'], 83 colour = parse_result['colour'],
84 geometry = LineSegment(*parse_result['vertices']), 84 geometry = LineSegment(*parse_result['vertices']),
85 ) 85 )
86 86
87 def parse_ldraw_triangle(line): 87 def parse_ldraw_triangle(line):
88 parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3) 88 parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3)
89 return linetypes.Triangle( 89 return linetypes.Triangle(
90 color = parse_result['color'], 90 colour = parse_result['colour'],
91 geometry = Polygon(parse_result['vertices']), 91 geometry = Polygon(parse_result['vertices']),
92 ) 92 )
93 93
94 def parse_ldraw_quadrilateral(line): 94 def parse_ldraw_quadrilateral(line):
95 parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4) 95 parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4)
96 return linetypes.Quadrilateral( 96 return linetypes.Quadrilateral(
97 color = parse_result['color'], 97 colour = parse_result['colour'],
98 geometry = Polygon(parse_result['vertices']), 98 geometry = Polygon(parse_result['vertices']),
99 ) 99 )
100 100
101 def parse_ldraw_contour(line): 101 def parse_ldraw_contour(line):
102 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4) 102 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
103 return linetypes.Contour( 103 return linetypes.Contour(
104 color = parse_result['color'], 104 colour = parse_result['colour'],
105 geometry = LineSegment(*parse_result['vertices'][0:2]), 105 geometry = LineSegment(*parse_result['vertices'][0:2]),
106 control_points = parse_result['vertices'][2:], 106 control_points = parse_result['vertices'][2:],
107 ) 107 )

mercurial