Mon, 11 Dec 2017 00:46:35 +0200
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 | 11 | class EmptyLine: |
12 | def __repr__(self): | |
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 | 16 | |
17 | class Comment: | |
18 | def __init__(self, text, style = 'old'): | |
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 | 21 | self.style = 'new' |
22 | else: | |
23 | self.text = text | |
24 | self.style = style | |
25 | def __repr__(self): | |
26 | return str.format('linetypes.Comment({text!r}, {style!r})', | |
27 | text = self.text, | |
28 | style = self.style, | |
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 | 35 | |
36 | class SubfileReference: | |
37 | def __init__(self, *, color, subfile_path, anchor, matrix): | |
38 | self.color, self.subfile_path, = color, subfile_path | |
39 | self.anchor, self.matrix = anchor, matrix | |
40 | def __repr__(self): | |
41 | return str.format('linetypes.SubfileReference(' \ | |
42 | 'color = {color!r}, ' \ | |
43 | 'subfile_path = {subfile_path!r}, ' \ | |
44 | 'anchor = {anchor!r}, ' \ | |
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 | 55 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
56 | class BasePolygon: |
0 | 57 | def __init__(self, *, color, geometry): |
58 | self.color, self.geometry = color, geometry | |
59 | def __repr__(self): | |
60 | return str.format('linetypes.{typename}(' \ | |
61 | 'color = {color!r}, ' \ | |
62 | 'geometry = {geometry!r})', | |
63 | typename = type(self).__name__, | |
64 | color = self.color, | |
65 | geometry = self.geometry, | |
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 | 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 | 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 | 84 | |
85 | class Contour(LineSegment): | |
86 | def __init__(self, *, color, geometry, control_points): | |
87 | super().__init__(color = color, geometry = geometry) | |
88 | self.control_points = control_points | |
89 | assert(len(self.control_points) == 2) | |
90 | def __repr__(self): | |
91 | return str.format('linetypes.Contour(' \ | |
92 | 'color = {color!r}, ' \ | |
93 | 'geometry = {geometry!r}, ' \ | |
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 |