-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibldrparser.py
441 lines (362 loc) · 15.8 KB
/
libldrparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"""Simple LDraw Model Parser.
Copyright (c) 2015 Tribex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import print_function
import os
import re
import fnmatch
__all__ = ("LDRParser")
class LDRParser:
version = ("0", "1", "0")
ControlCodes = ("COMMENT", "SUBPART", "LINE", "TRI", "QUAD", "OPTLINE")
options = {
"skip": [],
"logLevel": 0
}
def __init__(self, libraryLocation, options={}):
self.options.update(options)
self.libraryLocation = libraryLocation
self.modelFile = None
self.__parts = {}
self.__searchPaths = None
@staticmethod
def __locate(pattern, root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
@staticmethod
def __convert(vals):
for i in range(0, len(vals)):
line = vals[i]
# Floats
if bool(re.search("\d\.\d+", line)):
vals[i] = float(line)
else:
try:
# Integers
vals[i] = int(line)
# Strings
except ValueError:
pass
return vals
def __getSearchPaths(self):
"""Generate the search path for traversing the LDraw library.
@return {List} A spec-compliant search path.
"""
# Proper search order when looking for parts
# * Files relative to the main file
# * Models folder
# * Unofficial/parts
# * Unofficial/p/48 or Unofficial/p/8 (optional)
# * Unofficial/p
# * Parts folder
# * p/48 or p/8 folder (optional)
# * p folder
paths = [
os.path.join(os.path.dirname(os.path.abspath(self.modelFile))),
os.path.join(self.libraryLocation, "models"),
os.path.join(self.libraryLocation, "Unofficial", "parts"),
os.path.join(self.libraryLocation, "Unofficial", "p"),
os.path.join(self.libraryLocation, "parts"),
os.path.join(self.libraryLocation, "p")
]
return paths
def log(self, string, level=0):
"""Log debug messages to the console.
@param {String} string The message to be displayed.
@param {Number} level Specify the message verbosity level.
If the specified level is less than
or equal to the level given in the options,
the message will be printed.
"""
if self.options["logLevel"] >= level:
print("[LDRParser] {0}".format(string))
def parse(self, modelFile):
# Set the model to be read
self.modelFile = modelFile
# Set the search paths if they have not already been set.
if self.__searchPaths is None:
self.__searchPaths = self.__getSearchPaths()
# Display the line types we are going to skip parsing.
if len(self.options["skip"]) > 0:
self.log("Skip line type(s): {0}".format(
", ".join(self.options["skip"])), 5)
# This can load any valid file on the LDraw path
# with the specified name, not just a full path.
filePath = self.findFile(self.modelFile)
# The file could not be found.
if filePath is None:
self.log("Critical Error - File Not Found: {0}".format(
self.modelFile), 0)
return None
# Create root object.
root = None
# Begin parsing the model and all the parts.
self.log("Reading Initial File: {0}".format(filePath), 3)
with open(filePath, "rt") as f:
# Split input file by any file comments, to support MPD files.
splitFile = f.read().split("0 FILE ")
rootFile = None
# If there is only one result, use that as the root file. (Non MPD)
if len(splitFile) == 1:
rootFile = splitFile[0]
# If there are multiple results, take the second (First is blank)
# result as the root file.
else:
# Add file comment back in.
rootFile = "0 FILE " + splitFile[1]
# Populate the additional models as subparts.
for subFile in splitFile[2:]:
fileName = subFile.splitlines()[0].lower().strip()
# Add file comment back in.
fileBody = "0 FILE " + subFile
self.__parts[fileName] = self.buildPartData(fileBody)
# Build root model.
root = self.buildPartData(rootFile)
root["parts"] = self.__parts
self.log("Completed Parsing File: {0}".format(filePath), 3)
return root
def buildPartData(self, ldrString):
definition = {}
isBFCEnabled = False
baseBFCDir = None
curBFCMask = None
lines = ldrString.splitlines()
for line in lines:
# Determine the type of line this is.
ctrl = int(line.lstrip()[0] if line.lstrip() else -1)
# Ignore invalid line types
if ctrl == -1:
continue
# Determine the control code
code = self.ControlCodes[ctrl]
# Always parse comments so we can get the part type
comment = self.parseComment(line)
if comment is not None:
if comment.startswith("!LDRAW_ORG"):
definition["partType"] = self.getPartType(comment)
# Potentially dangerous if a malformed file has
# BFC declarations after the first command.
elif comment.startswith("BFC") and not isBFCEnabled:
isBFCEnabled = True
if comment.find("CCW") > -1 or comment.find("CW") == -1:
baseBFCDir = 0
else:
baseBFCDir = 1
# Set base BFC
definition['bfc'] = baseBFCDir
# Set initial BFC number
curBFCMask = self.getBFCBitmask(comment, curBFCMask)
# Parse all BFC instructions into bitmask.
elif comment.startswith("BFC") and isBFCEnabled:
# Set the current BFC mask, updating the old one.
curBFCMask = self.getBFCBitmask(comment, curBFCMask)
# We are not skipping this line type.
if code not in self.options["skip"]:
# Include the comments.
if code == "COMMENT":
if "comments" not in definition:
definition["comments"] = []
# Make sure there is a comment to add
if comment is not None:
definition["comments"].append(comment)
# Parse the subparts.
elif code == "SUBPART":
if "subparts" not in definition:
definition["subparts"] = []
definition["subparts"].append(self.parsePart(line))
# Add BFC Info
if isBFCEnabled:
definition["subparts"][-1]["bfc"] = curBFCMask
# Reset invertnext to 0, as it only applies for a single subpart line.
curBFCMask = self.toggleBit(curBFCMask, 0, 2)
# Parse the straight lines.
elif code == "LINE":
if "lines" not in definition:
definition["lines"] = []
definition["lines"].append(self.parseLine(line))
# Add BFC Info
if isBFCEnabled:
definition["lines"][-1]["bfc"] = curBFCMask
# Parse the triangles.
elif code == "TRI":
if "tris" not in definition:
definition["tris"] = []
definition["tris"].append(self.parseTri(line))
# Add BFC Info
if isBFCEnabled:
definition["tris"][-1]["bfc"] = curBFCMask
# Parse the quadrilaterals.
elif code == "QUAD":
if "quads" not in definition:
definition["quads"] = []
definition["quads"].append(self.parseQuad(line))
# Add BFC Info
if isBFCEnabled:
definition["quads"][-1]["bfc"] = curBFCMask
# Parse the optional lines.
elif code == "OPTLINE":
if "optlines" not in definition:
definition["optlines"] = []
definition["optlines"].append(self.parseOptLine(line))
# Add BFC Info
if isBFCEnabled:
definition["optlines"][-1]["bfc"] = curBFCMask
return definition
def toggleBit(self, val, state, bitIndex):
if state == 1:
return val | (1 << bitIndex)
elif state == 0:
return val & ~(1 << bitIndex)
else:
return val
def getBFCBitmask(self, bfcComment, bfcNum):
if bfcNum is None:
bfcNum = 0
if bfcComment.find('INVERTNEXT') > -1:
# Add invert flag.
bfcNum = self.toggleBit(bfcNum, 1, 2)
# Invertnext cannot have any other flags, so return here.
return bfcNum
if bfcComment.find('NOCLIP') > -1:
# Remove CLIP flag.
bfcNum = self.toggleBit(bfcNum, 0, 1)
elif bfcComment.find('CLIP') > -1:
# Set CLIP flag.
bfcNum = self.toggleBit(bfcNum, 1, 1)
if bfcComment.find('CCW') > -1:
bfcNum = self.toggleBit(bfcNum, 0, 0)
elif bfcComment.find('CW') > -1:
bfcNum = self.toggleBit(bfcNum, 1, 0)
return bfcNum
def getPartType(self, line):
return line.split()[1]
def parsePart(self, line):
myDef = {}
splitLine = self.__convert(line.split())
myDef["color"] = splitLine[1]
myDef["matrix"] = (
splitLine[5], splitLine[6], splitLine[7], splitLine[2],
splitLine[8], splitLine[9], splitLine[10], splitLine[3],
splitLine[11], splitLine[12], splitLine[13], splitLine[4],
0, 0, 0, 1,
)
myDef["partId"] = self.formatPartName(" ".join(
[str(i) for i in splitLine[14:]]
))
# The part is not in the cache.
if myDef["partId"] not in self.__parts:
filePath = self.findFile(myDef["partId"])
# We have a file path.
if filePath is not None:
self.log("Caching Part: {0}".format(filePath), 4)
# Read and cache the part contents.
with open(filePath, "rt") as f:
self.__parts[myDef["partId"]] = \
self.buildPartData(f.read())
return myDef
def parseComment(self, comment):
"""Perform basic comment processing, including
minor filtering and cleanup.
@param {String} comment The comment to process.
@return {!String} The cleaned comment, or None if it was filtered.
"""
# Strip the prefix
comment = comment.strip().lstrip("0 ")
# Filter out META commands, blank lines, and comments
# See http://www.ldraw.org/article/218/#meta
lineFilter = ("", "STEP", "WRITE", "PRINT", "CLEAR", "PAUSE", "SAVE")
if comment in lineFilter or comment.startswith("//"):
return None
return comment
def parseLine(self, line):
splitLine = self.__convert(line.split())
myDef = {
"color": splitLine[1],
"pos1": (splitLine[2], splitLine[3], splitLine[4]),
"pos2": (splitLine[5], splitLine[6], splitLine[7]),
}
return myDef
def parseTri(self, line):
splitLine = self.__convert(line.split())
myDef = {
"color": splitLine[1],
"pos1": (splitLine[2], splitLine[3], splitLine[4]),
"pos2": (splitLine[5], splitLine[6], splitLine[7]),
"pos3": (splitLine[8], splitLine[9], splitLine[10]),
}
return myDef
def parseQuad(self, line):
splitLine = self.__convert(line.split())
myDef = {
"color": splitLine[1],
"pos1": (splitLine[2], splitLine[3], splitLine[4]),
"pos2": (splitLine[5], splitLine[6], splitLine[7]),
"pos3": (splitLine[8], splitLine[9], splitLine[10]),
"pos4": (splitLine[11], splitLine[12], splitLine[13]),
}
return myDef
def parseOptLine(self, line):
splitLine = self.__convert(line.split())
myDef = {
"color": splitLine[1],
"pos1": (splitLine[2], splitLine[3], splitLine[4]),
"pos2": (splitLine[5], splitLine[6], splitLine[7]),
"ctl1": (splitLine[8], splitLine[9], splitLine[10]),
"ctl2": (splitLine[11], splitLine[12], splitLine[13]),
}
return myDef
def findFile(self, partPath):
"""Locate a part in the LDraw parts installation.
@param {String} partPath The part name that needs to be located.
@return {String} The full file path to the given part.
"""
locatedFile = None
# Try the current directory.
if os.path.isfile(partPath):
locatedFile = partPath
# Revise the search path just a little bit
# to append the current part name.
paths = [os.path.join(path, partPath) for path in self.__searchPaths]
# Now check the search path for the file.
if not locatedFile:
for path in paths:
if os.path.isfile(path):
locatedFile = path
break
# Failing that, recursively search through every directory
# in the library folder for the file.
if not locatedFile:
for f in self.__locate(os.path.basename(partPath),
self.libraryLocation):
locatedFile = f
break
# We are totally unable to find that part.
if not locatedFile:
self.log("Error: File not found: {0}".format(partPath), 1)
return locatedFile
def formatPartName(self, partName):
"""Clean up any path seperators
to be consistent with the platform
and convert the file name to lowercase.
@param {String} The part path to clean up.
@return {String}
"""
return partName.lower().replace("\\",
os.path.sep).replace("/",
os.path.sep)