linetypes.py

changeset 3
1dc58f44d556
parent 1
5411a25cfca7
child 4
8eb83f200486
--- a/linetypes.py	Sun Dec 10 15:46:47 2017 +0200
+++ b/linetypes.py	Mon Dec 11 00:46:35 2017 +0200
@@ -1,11 +1,23 @@
+def ldraw_str(value):
+    ' Like str() except removes unneeded ".0"-suffixes '
+    rep = str(value)
+    if isinstance(value, float):
+        if rep.endswith('.0'):
+            rep = rep[:-2]
+        if rep == '-0':
+            rep = '0'
+    return rep
+
 class EmptyLine:
     def __repr__(self):
         return 'linetypes.EmptyLine()'
+    def textual_representation(self):
+        return ''
 
 class Comment:
     def __init__(self, text, style = 'old'):
         if style == 'old' and text.startswith('//'):
-            self.text = text[3:].strip()
+            self.text = text[2:].strip()
             self.style = 'new'
         else:
             self.text = text
@@ -15,6 +27,11 @@
             text = self.text,
             style = self.style,
         )
+    def textual_representation(self):
+        if self.style == 'old':
+            return '0 ' + self.text
+        else:
+            return '0 // ' + self.text
 
 class SubfileReference:
     def __init__(self, *, color, subfile_path, anchor, matrix):
@@ -26,8 +43,17 @@
             'subfile_path = {subfile_path!r}, ' \
             'anchor = {anchor!r}, ' \
             'matrix = {matrix!r})', **self.__dict__)
+    def textual_representation(self):
+        args = [
+            1,
+            self.color,
+            *self.anchor.coordinates,
+            *self.matrix.values,
+            self.subfile_path,
+        ]
+        return ' '.join(ldraw_str(arg) for arg in args)
 
-class LineSegment:
+class BasePolygon:
     def __init__(self, *, color, geometry):
         self.color, self.geometry = color, geometry
     def __repr__(self):
@@ -38,12 +64,23 @@
             color = self.color,
             geometry = self.geometry,
         )
+    def base_textual_representation(self):
+        args = [self.color]
+        for vertex in self.geometry.vertices:
+            args += vertex.coordinates
+        return ' '.join(ldraw_str(arg) for arg in args)
 
-class Triangle(LineSegment):
-    pass
+class LineSegment(BasePolygon):
+    def textual_representation(self):
+        return '2 ' + self.base_textual_representation()
 
-class Quadrilateral(LineSegment):
-    pass
+class Triangle(BasePolygon):
+    def textual_representation(self):
+        return '3 ' + self.base_textual_representation()
+
+class Quadrilateral(BasePolygon):
+    def textual_representation(self):
+        return '4 ' + self.base_textual_representation()
 
 class Contour(LineSegment):
     def __init__(self, *, color, geometry, control_points):
@@ -55,3 +92,10 @@
             'color = {color!r}, ' \
             'geometry = {geometry!r}, ' \
             'control_points = {control_points!r})', **self.__dict__)
+    def textual_representation(self):
+        result = '5 ' + self.base_textual_representation()
+        for control_point in self.control_points:
+            strings = (ldraw_str(value) for value in control_point.coordinates)
+            result += ' '
+            result += ' '.join(strings)
+        return result

mercurial