A Library for loading and parsing a LDraw files.
LDraw http://www.ldraw.org
File Format 1.0.2: http://www.ldraw.org/article/218.html
npm install ldraw
Loads a model from the base path. If it doesn't find it in the file system, it will attempt to load it from the internets. It looks in the following places in the following order:
- (base path)/parts
- (base path)/p
- (base path)/models
- www.ldraw.org/library/official/parts/
- www.ldraw.org/library/official/p/
varldraw=require('ldraw')('/path/to/ldraw/files');ldraw.loadModel('car.dat',function(err,model){model.commands.forEach(function(cmd){console.log(cmd);});});Commands can be one of the following:
// 1 - subfilevarsubfile={type: 1,color: 2,x: 0,y: 0,z: 0,a: 0,b: 0,c: 0,d: 0,e: 0,f: 0,g: 0,h: 0,i: 0,file: '3001.dat'}// 2 - linevarline={type: 2,color: 2,x1: 0,y1: 0,z1: 0,// first pointx2: 1,y2: 1,z2: 1// second point};// 3 - trianglevarline={type: 3,color: 2,x1: 0,y1: 0,z1: 0,// first pointx2: 1,y2: 1,z2: 1,// second pointx3: 2,y3: 2,z3: 2// third point};// 4 - quadvarline={type: 4,color: 2,x1: 0,y1: 0,z1: 0,// first pointx2: 1,y2: 1,z2: 1,// second pointx3: 2,y3: 2,z3: 2,// third pointx4: 3,y4: 3,z4: 3// fourth point};// 5 - optional linevarline={type: 5,color: 24,x1: 0,y1: 0,z1: 0,// first pointx2: 1,y2: 1,z2: 1,// second pointx3: 2,y3: 2,z3: 2,// first control pointx4: 3,y4: 3,z4: 3// second control point};Loads the colors from the LDConfig.ldr file in the base directory.
varldraw=require('ldraw')('/path/to/ldraw/files');ldraw.loadColors(function(err,colors){console.log(colors[0].NAME);});varblack={"NAME": "Black","CODE": 0,"VALUE": "#05131D","EDGE": "#595959","MATERIAL": "SOLID","LEGOID": "26 - Black"}assert.equals(black.NAME,ldraw.colors[0].NAME);assert.equals(black.CODE,ldraw.colors[0].CODE);assert.equals(black.VALUE,ldraw.colors[0].VALUE);Given a string, it parses the text and returns a list of commands. loadModel calls this function.
varldraw=require('ldraw')('/path/to/ldraw/files');varfilename='messy.ldr';varcontent=fs.readFileSync(filename,{"encoding": "utf8"});varmodel=parseModel(content);Given a string, it returns an object of all the colors indexed by their code. loadColors calls this function
varldraw=require('ldraw')('/path/to/ldraw/files');varcontent=fs.readFileSync('...LDConfig.ldr',{"encoding": "utf8"});varcolors=ldraw.parseColors(content);assert.equals(colors[0].CODE===0);assert.equals(colors[0].NAME==='Black');This function takes ugly values and if the difference between the number and an integer is less than a small epsilon, it turns them into the integer. This is useful for reducing the size of ldraw files.
varldraw=require('ldraw')('/path/to/ldraw/files');varcmd='1 0 9.99999904632568359375 -7.99999904632568359375 10 0 0 -1 0 0.999999940395355224609375 0 1 0 0 3623.dat';varcleaned=ldraw.cleanFile(cmd);assert.equal(cleaned,'1 0 10 -8 10 0 0 -1 0 1 0 1 0 0 3623.dat');