linetypes.py

Mon, 11 Dec 2017 00:46:35 +0200

author
Santeri Piippo
date
Mon, 11 Dec 2017 00:46:35 +0200
changeset 3
1dc58f44d556
parent 1
5411a25cfca7
child 4
8eb83f200486
permissions
-rw-r--r--

Can now write dat files, added direct color handling

3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
1 def ldraw_str(value):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
2 ' Like str() except removes unneeded ".0"-suffixes '
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
3 rep = str(value)
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
4 if isinstance(value, float):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
5 if rep.endswith('.0'):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
6 rep = rep[:-2]
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
7 if rep == '-0':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
8 rep = '0'
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
9 return rep
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
10
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
11 class EmptyLine:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
12 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
13 return 'linetypes.EmptyLine()'
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
14 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
15 return ''
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
16
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
17 class Comment:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
18 def __init__(self, text, style = 'old'):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
19 if style == 'old' and text.startswith('//'):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
20 self.text = text[2:].strip()
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
21 self.style = 'new'
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
22 else:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
23 self.text = text
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
24 self.style = style
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
25 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
26 return str.format('linetypes.Comment({text!r}, {style!r})',
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
27 text = self.text,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
28 style = self.style,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
29 )
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
30 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
31 if self.style == 'old':
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
32 return '0 ' + self.text
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
33 else:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
34 return '0 // ' + self.text
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
35
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
36 class SubfileReference:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
37 def __init__(self, *, color, subfile_path, anchor, matrix):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
38 self.color, self.subfile_path, = color, subfile_path
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
39 self.anchor, self.matrix = anchor, matrix
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
40 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
41 return str.format('linetypes.SubfileReference(' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
42 'color = {color!r}, ' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
43 'subfile_path = {subfile_path!r}, ' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
44 'anchor = {anchor!r}, ' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
45 'matrix = {matrix!r})', **self.__dict__)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
46 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
47 args = [
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
48 1,
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
49 self.color,
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
50 *self.anchor.coordinates,
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
51 *self.matrix.values,
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
52 self.subfile_path,
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
53 ]
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
54 return ' '.join(ldraw_str(arg) for arg in args)
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
55
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
56 class BasePolygon:
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
57 def __init__(self, *, color, geometry):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
58 self.color, self.geometry = color, geometry
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
59 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
60 return str.format('linetypes.{typename}(' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
61 'color = {color!r}, ' \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
62 'geometry = {geometry!r})',
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
63 typename = type(self).__name__,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
64 color = self.color,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
65 geometry = self.geometry,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
66 )
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
67 def base_textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
68 args = [self.color]
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
69 for vertex in self.geometry.vertices:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
70 args += vertex.coordinates
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
71 return ' '.join(ldraw_str(arg) for arg in args)
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
72
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
73 class LineSegment(BasePolygon):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
74 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
75 return '2 ' + self.base_textual_representation()
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
76
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
77 class Triangle(BasePolygon):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
78 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
79 return '3 ' + self.base_textual_representation()
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
80
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
81 class Quadrilateral(BasePolygon):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
82 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
83 return '4 ' + self.base_textual_representation()
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
84
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
85 class Contour(LineSegment):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
86 def __init__(self, *, color, geometry, control_points):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
87 super().__init__(color = color, geometry = geometry)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
88 self.control_points = control_points
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
89 assert(len(self.control_points) == 2)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
90 def __repr__(self):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
91 return str.format('linetypes.Contour(' \
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
92 'color = {color!r}, ' \
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
93 'geometry = {geometry!r}, ' \
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
94 'control_points = {control_points!r})', **self.__dict__)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
95 def textual_representation(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
96 result = '5 ' + self.base_textual_representation()
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
97 for control_point in self.control_points:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
98 strings = (ldraw_str(value) for value in control_point.coordinates)
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
99 result += ' '
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
100 result += ' '.join(strings)
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
101 return result

mercurial