ldraw.py

changeset 1
5411a25cfca7
parent 0
55b4c97d44c5
child 2
50d3086070df
--- a/ldraw.py	Sun Dec 10 15:37:26 2017 +0200
+++ b/ldraw.py	Sun Dec 10 15:45:50 2017 +0200
@@ -10,15 +10,22 @@
     line = line.strip()
     if not line:
         return linetypes.EmptyLine()
-    if line == '0':
+    elif line == '0':
         return linetypes.Comment('')
-    if line.startswith('0 '):
+    elif line.startswith('0 '):
         return linetypes.Comment(line[2:].strip())
-    if line.startswith('1 '):
+    elif line.startswith('1 '):
         return parse_ldraw_subfile_reference(line)
-    if line.startswith('2 '):
+    elif line.startswith('2 '):
         return parse_ldraw_line(line)
-    ...
+    elif line.startswith('3 '):
+        return parse_ldraw_triangle(line)
+    elif line.startswith('4 '):
+        return parse_ldraw_quadrilateral(line)
+    elif line.startswith('5 '):
+        return parse_ldraw_contour(line)
+    else:
+        raise BadLdrawLine('unknown line type')
 
 def parse_ldraw_subfile_reference(line):
     pattern = r'^1\s+(\d+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$'
@@ -72,10 +79,32 @@
 
 def parse_ldraw_line(line):
     parse_result = generic_parse_polygon(line, type_code = 2, vertex_count = 2)
-    line_segment = LineSegment(*parse_result['vertices'])
     return linetypes.LineSegment(
         color = parse_result['color'],
-        geometry = line_segment)
+        geometry = LineSegment(*parse_result['vertices']),
+    )
+
+def parse_ldraw_triangle(line):
+    parse_result = generic_parse_polygon(line, type_code = 3, vertex_count = 3)
+    return linetypes.Triangle(
+        color = parse_result['color'],
+        geometry = Polygon(parse_result['vertices']),
+    )
+
+def parse_ldraw_quadrilateral(line):
+    parse_result = generic_parse_polygon(line, type_code = 4, vertex_count = 4)
+    return linetypes.Quadrilateral(
+        color = parse_result['color'],
+        geometry = Polygon(parse_result['vertices']),
+    )
+
+def parse_ldraw_contour(line):
+    parse_result = generic_parse_polygon(line, type_code = 5, vertex_count = 4)
+    return linetypes.Contour(
+        color = parse_result['color'],
+        geometry = LineSegment(*parse_result['vertices'][0:2]),
+        control_points = parse_result['vertices'][2:],
+    )
 
 def read_ldraw(file, *, libraries):
     result = list()

mercurial