Mon, 22 Jan 2018 01:01:10 +0200
Condensed transform_to_xy
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 '' |
7 | 16 | def typename(self): |
17 | return 'empty line' | |
0 | 18 | |
19 | class Comment: | |
20 | def __init__(self, text, style = 'old'): | |
21 | if style == 'old' and text.startswith('//'): | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
22 | self.text = text[2:].strip() |
0 | 23 | self.style = 'new' |
24 | else: | |
25 | self.text = text | |
26 | self.style = style | |
27 | def __repr__(self): | |
28 | return str.format('linetypes.Comment({text!r}, {style!r})', | |
29 | text = self.text, | |
30 | style = self.style, | |
31 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
32 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
33 | if self.style == 'old': |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
34 | return '0 ' + self.text |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
35 | else: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
36 | return '0 // ' + self.text |
7 | 37 | def typename(self): |
38 | return 'comment' | |
0 | 39 | |
40 | class SubfileReference: | |
4 | 41 | def __init__(self, *, colour, subfile_path, anchor, matrix): |
42 | self.colour, self.subfile_path, = colour, subfile_path | |
0 | 43 | self.anchor, self.matrix = anchor, matrix |
44 | def __repr__(self): | |
45 | return str.format('linetypes.SubfileReference(' \ | |
4 | 46 | 'colour = {colour!r}, ' \ |
0 | 47 | 'subfile_path = {subfile_path!r}, ' \ |
48 | 'anchor = {anchor!r}, ' \ | |
49 | 'matrix = {matrix!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
50 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
51 | args = [ |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
52 | 1, |
4 | 53 | self.colour, |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
54 | *self.anchor.coordinates, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
55 | *self.matrix.values, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
56 | self.subfile_path, |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
57 | ] |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
58 | return ' '.join(ldraw_str(arg) for arg in args) |
7 | 59 | def typename(self): |
60 | return 'subfile reference' | |
0 | 61 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
62 | class BasePolygon: |
4 | 63 | def __init__(self, *, colour, geometry): |
64 | self.colour, self.geometry = colour, geometry | |
0 | 65 | def __repr__(self): |
66 | return str.format('linetypes.{typename}(' \ | |
4 | 67 | 'colour = {colour!r}, ' \ |
0 | 68 | 'geometry = {geometry!r})', |
69 | typename = type(self).__name__, | |
4 | 70 | colour = self.colour, |
0 | 71 | geometry = self.geometry, |
72 | ) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
73 | def base_textual_representation(self): |
4 | 74 | args = [self.colour] |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
75 | for vertex in self.geometry.vertices: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
76 | args += vertex.coordinates |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
77 | return ' '.join(ldraw_str(arg) for arg in args) |
0 | 78 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
79 | class LineSegment(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
80 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
81 | return '2 ' + self.base_textual_representation() |
7 | 82 | def typename(self): |
83 | return 'line segment' | |
0 | 84 | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
85 | class Triangle(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
86 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
87 | return '3 ' + self.base_textual_representation() |
7 | 88 | def typename(self): |
89 | return 'triangle' | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
90 | |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
91 | class Quadrilateral(BasePolygon): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
92 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
93 | return '4 ' + self.base_textual_representation() |
7 | 94 | def typename(self): |
95 | return 'quadrilateral' | |
1 | 96 | |
97 | class Contour(LineSegment): | |
4 | 98 | def __init__(self, *, colour, geometry, control_points): |
99 | super().__init__(colour = colour, geometry = geometry) | |
1 | 100 | self.control_points = control_points |
101 | assert(len(self.control_points) == 2) | |
102 | def __repr__(self): | |
103 | return str.format('linetypes.Contour(' \ | |
4 | 104 | 'colour = {colour!r}, ' \ |
1 | 105 | 'geometry = {geometry!r}, ' \ |
106 | 'control_points = {control_points!r})', **self.__dict__) | |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
107 | def textual_representation(self): |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
108 | result = '5 ' + self.base_textual_representation() |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
109 | for control_point in self.control_points: |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
110 | 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
|
111 | result += ' ' |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
112 | result += ' '.join(strings) |
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
1
diff
changeset
|
113 | return result |
7 | 114 | def typename(self): |
115 | return 'contour line segment' |