7 class BadLdrawLine(Exception): |
7 class BadLdrawLine(Exception): |
8 pass |
8 pass |
9 |
9 |
10 class Model: |
10 class Model: |
11 def __init__( |
11 def __init__( |
12 self, header, body, *, ldraw_directories, \ |
12 self, header, body, *, context, \ |
13 header_size = 0, line_ending_errors = None |
13 header_size = 0, line_ending_errors = None |
14 ): |
14 ): |
15 self.header = header |
15 self.header = header |
16 self.body = body |
16 self.body = body |
17 self.header_size = header_size |
17 self.header_size = header_size |
18 self.ldraw_directories = ldraw_directories |
18 self.context = context # contains stuff like library paths |
19 self.line_ending_errors = line_ending_errors |
19 self.line_ending_errors = line_ending_errors |
20 def filter_by_type(self, type): |
20 def filter_by_type(self, type): |
21 yield from [ |
21 yield from [ |
22 element |
22 element |
23 for element in self.body |
23 for element in self.body |
37 yield from self.filter_by_type(linetypes.Quadrilateral) |
37 yield from self.filter_by_type(linetypes.Quadrilateral) |
38 @property |
38 @property |
39 def has_header(self): |
39 def has_header(self): |
40 return self.header and not isinstance(self.header, header.BadHeader) |
40 return self.header and not isinstance(self.header, header.BadHeader) |
41 def find_first_header_object(self, object_type): |
41 def find_first_header_object(self, object_type): |
42 return self.body[self.header.first_occurrence[object_type]] |
42 return self.find_header_object(object_type, 0) |
|
43 def find_header_object(self, object_type, n): |
|
44 try: |
|
45 return self.body[self.header.occurrences[object_type][n]] |
|
46 except IndexError: |
|
47 raise KeyError(str.format( |
|
48 '{type} not found in header', |
|
49 type = object_type |
|
50 )) |
43 |
51 |
44 def model_vertices( |
52 def model_vertices( |
45 model, |
53 model, |
46 transformation_matrix = None, |
54 transformation_matrix = None, |
47 file_cache = None, |
55 file_cache = None, |
48 ): |
56 ): |
49 if transformation_matrix is None: |
57 if transformation_matrix is None: |
50 transformation_matrix = complete_matrix(Matrix3x3(), Vertex(0, 0, 0)) |
58 transformation_matrix = complete_matrix(Matrix3x3(), Vertex(0, 0, 0)) |
51 if file_cache is None: |
59 if file_cache is None: |
52 import filecache |
60 import filecache |
53 file_cache = filecache.SubfileCache(model.ldraw_directories) |
61 file_cache = filecache.SubfileCache(ldraw_directories = model.context.libraries) |
54 for element in model.body: |
62 for element in model.body: |
55 if isinstance(element, linetypes.BasePolygon): |
63 if isinstance(element, linetypes.BasePolygon): |
56 for point in element.geometry.vertices: |
64 for point in element.geometry.vertices: |
57 yield point @ transformation_matrix |
65 yield point @ transformation_matrix |
58 if isinstance(element, linetypes.ConditionalLine): |
66 if isinstance(element, linetypes.ConditionalLine): |
63 for point in subfile.vertices: |
71 for point in subfile.vertices: |
64 matrix_4x4 = complete_matrix(element.matrix, element.anchor) |
72 matrix_4x4 = complete_matrix(element.matrix, element.anchor) |
65 point @= matrix_4x4 |
73 point @= matrix_4x4 |
66 yield point @ transformation_matrix |
74 yield point @ transformation_matrix |
67 |
75 |
68 def read_ldraw(file, *, ldraw_directories, name = ''): |
76 def read_ldraw(file, *, context, name = ''): |
69 line_ending_errors = { |
77 line_ending_errors = { |
70 'count': 0, |
78 'count': 0, |
71 'first-at': None, |
79 'first-at': None, |
72 } |
80 } |
73 model_body = [] |
81 model_body = [] |