parse.py

changeset 145
fde18c4d6784
parent 127
97de6058109e
equal deleted inserted replaced
144:eb4c767522ac 145:fde18c4d6784
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
56 ): 56 ):
57 if transformation_matrix is None: 57 if transformation_matrix is None:
58 transformation_matrix = complete_matrix(Matrix3x3(), Vertex(0, 0, 0)) 58 transformation_matrix = complete_matrix(Matrix3x3(), Vertex(0, 0, 0))
59 if file_cache is None: 59 if file_cache is None:
60 import filecache 60 import filecache
61 file_cache = filecache.SubfileCache(model.ldraw_directories) 61 file_cache = filecache.SubfileCache(ldraw_directories = model.context.libraries)
62 for element in model.body: 62 for element in model.body:
63 if isinstance(element, linetypes.BasePolygon): 63 if isinstance(element, linetypes.BasePolygon):
64 for point in element.geometry.vertices: 64 for point in element.geometry.vertices:
65 yield point @ transformation_matrix 65 yield point @ transformation_matrix
66 if isinstance(element, linetypes.ConditionalLine): 66 if isinstance(element, linetypes.ConditionalLine):
71 for point in subfile.vertices: 71 for point in subfile.vertices:
72 matrix_4x4 = complete_matrix(element.matrix, element.anchor) 72 matrix_4x4 = complete_matrix(element.matrix, element.anchor)
73 point @= matrix_4x4 73 point @= matrix_4x4
74 yield point @ transformation_matrix 74 yield point @ transformation_matrix
75 75
76 def read_ldraw(file, *, ldraw_directories, name = ''): 76 def read_ldraw(file, *, context, name = ''):
77 line_ending_errors = { 77 line_ending_errors = {
78 'count': 0, 78 'count': 0,
79 'first-at': None, 79 'first-at': None,
80 } 80 }
81 model_body = [] 81 model_body = []
98 end = 0 98 end = 0
99 model = Model( 99 model = Model(
100 header = header_object, 100 header = header_object,
101 body = model_body, 101 body = model_body,
102 header_size = end, 102 header_size = end,
103 ldraw_directories = ldraw_directories, 103 context = context,
104 line_ending_errors = line_ending_errors, 104 line_ending_errors = line_ending_errors,
105 ) 105 )
106 model.name = name 106 model.name = name
107 return model 107 return model
108 108

mercurial