ldraw.py

changeset 1
5411a25cfca7
parent 0
55b4c97d44c5
child 2
50d3086070df
equal deleted inserted replaced
0:55b4c97d44c5 1:5411a25cfca7
8 8
9 def parse_ldraw_code(line): 9 def parse_ldraw_code(line):
10 line = line.strip() 10 line = line.strip()
11 if not line: 11 if not line:
12 return linetypes.EmptyLine() 12 return linetypes.EmptyLine()
13 if line == '0': 13 elif line == '0':
14 return linetypes.Comment('') 14 return linetypes.Comment('')
15 if line.startswith('0 '): 15 elif line.startswith('0 '):
16 return linetypes.Comment(line[2:].strip()) 16 return linetypes.Comment(line[2:].strip())
17 if line.startswith('1 '): 17 elif line.startswith('1 '):
18 return parse_ldraw_subfile_reference(line) 18 return parse_ldraw_subfile_reference(line)
19 if line.startswith('2 '): 19 elif line.startswith('2 '):
20 return parse_ldraw_line(line) 20 return parse_ldraw_line(line)
21 ... 21 elif line.startswith('3 '):
22 return parse_ldraw_triangle(line)
23 elif line.startswith('4 '):
24 return parse_ldraw_quadrilateral(line)
25 elif line.startswith('5 '):
26 return parse_ldraw_contour(line)
27 else:
28 raise BadLdrawLine('unknown line type')
22 29
23 def parse_ldraw_subfile_reference(line): 30 def parse_ldraw_subfile_reference(line):
24 pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$' 31 pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
25 match = re.search(pattern, line) 32 match = re.search(pattern, line)
26 if not match: 33 if not match:
70 'vertices': vertices, 77 'vertices': vertices,
71 } 78 }
72 79
73 def parse_ldraw_line(line): 80 def parse_ldraw_line(line):
74 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)
75 line_segment = LineSegment(*parse_result['vertices'])
76 return linetypes.LineSegment( 82 return linetypes.LineSegment(
77 color = parse_result['color'], 83 color = parse_result['color'],
78 geometry = line_segment) 84 geometry = LineSegment(*parse_result['vertices']),
85 )
86
87 def parse_ldraw_triangle(line):
88 parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3)
89 return linetypes.Triangle(
90 color = parse_result['color'],
91 geometry = Polygon(parse_result['vertices']),
92 )
93
94 def parse_ldraw_quadrilateral(line):
95 parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4)
96 return linetypes.Quadrilateral(
97 color = parse_result['color'],
98 geometry = Polygon(parse_result['vertices']),
99 )
100
101 def parse_ldraw_contour(line):
102 parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
103 return linetypes.Contour(
104 color = parse_result['color'],
105 geometry = LineSegment(*parse_result['vertices'][0:2]),
106 control_points = parse_result['vertices'][2:],
107 )
79 108
80 def read_ldraw(file, *, libraries): 109 def read_ldraw(file, *, libraries):
81 result = list() 110 result = list()
82 for line in file: 111 for line in file:
83 result.append(parse_ldraw_code(line)) 112 result.append(parse_ldraw_code(line))

mercurial