Skip to content

Commit

Permalink
Updated tests. Browserified DxfParser.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzuillsmith committed May 1, 2015
1 parent ea36c86 commit add30ed
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 173 deletions.
195 changes: 151 additions & 44 deletions dist/dxf-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ var DxfArrayScanner = require('./DxfArrayScanner.js'),
var log = require('loglevel');

//log.setLevel('trace');
//log.setLevel('debug');
log.setLevel('info');
log.setLevel('debug');
//log.setLevel('info');
//log.setLevel('warn');
//log.setLevel('error');
//log.setLevel('silent');
Expand Down Expand Up @@ -428,7 +428,11 @@ DxfParser.prototype.parseStream = function(stream, done) {
}

function onEnd() {
var dxf = self._parse(dxfString);
try {
var dxf = self._parse(dxfString);
}catch(err) {
return done(err);
}
done(null, dxf);
}

Expand Down Expand Up @@ -638,13 +642,11 @@ DxfParser.prototype._parse = function(dxfString) {

if(groupIs(0, 'TABLE')) {
curr = scanner.next();
if(groupIs(2, 'LAYER')) {
log.debug('LayerTable {');
tables.layer = parseLayerTable();
log.debug('}')
} else if(groupIs(2, 'LTYPE')) {
log.debug('LType Table {');
tables.lineType = parseLineTypeTable();

var tableDefinition = tableDefinitions[curr.value];
if(tableDefinition) {
log.debug(curr.value + ' Table {');
tables[tableDefinitions[curr.value].tableName] = parseTable();
log.debug('}');
} else {
log.debug('Unhandled Table ' + curr.value);
Expand All @@ -659,12 +661,16 @@ DxfParser.prototype._parse = function(dxfString) {
return tables;
};

var parseLayerTable = function() {
var table = {},
const END_OF_TABLE_VALUE = 'ENDTAB';

var parseTable = function() {
var tableDefinition = tableDefinitions[curr.value],
table = {},
expectedCount = 0,
actualCount;

curr = scanner.next();
while(!groupIs(0, 'ENDTAB')) {
while(!groupIs(0, END_OF_TABLE_VALUE)) {

switch(curr.code) {
case 5:
Expand All @@ -689,8 +695,8 @@ DxfParser.prototype._parse = function(dxfString) {
curr = scanner.next();
break;
case 0:
if(curr.value === 'LAYER') {
table.layers = parseLayers();
if(curr.value === tableDefinition.dxfSymbolName) {
table[tableDefinition.tableRecordsProperty] = tableDefinition.parseTableRecords();
} else {
logUnhandledGroup(curr);
curr = scanner.next();
Expand All @@ -701,58 +707,138 @@ DxfParser.prototype._parse = function(dxfString) {
curr = scanner.next();
}
}
actualCount = Object.keys(table.layers).length;
if(expectedCount !== actualCount) log.warn('Parsed ' + actualCount + ' LAYER\'s but expected ' + expectedCount);
var tableRecords = table[tableDefinition.tableRecordsProperty];
console.error(typeof(tableRecords));
if(tableRecords) {
if(tableRecords.constructor === Array){
actualCount = tableRecords.length;
} else if(typeof(tableRecords) === 'object') {
actualCount = Object.keys(tableRecords).length;
}
if(expectedCount !== actualCount) log.warn('Parsed ' + actualCount + ' ' + tableDefinition.dxfSymbolName + '\'s but expected ' + expectedCount);
}
curr = scanner.next();
return table;
};

var parseLineTypeTable = function() {
var table = {},
expectedCount = 0,
actualCount;
var parseViewPortRecords = function() {
var viewPorts = [], // Multiple table entries may have the same name indicating a multiple viewport configuration
viewPort = {};

log.debug('ViewPort {');
curr = scanner.next();
while(!groupIs(0, 'ENDTAB')) {
while(!groupIs(0, END_OF_TABLE_VALUE)) {

switch(curr.code) {
case 5:
table.handle = curr.value;
case 2: // layer name
viewPort.name = curr.value;
curr = scanner.next();
break;
case 330:
table.ownerHandle = curr.value;
case 10:
viewPort.lowerLeftCorner = parsePoint();
break;
case 11:
viewPort.upperRightCorner = parsePoint();
break;
case 12:
viewPort.center = parsePoint();
break;
case 13:
viewPort.snapBasePoint = parsePoint();
break;
case 14:
viewPort.snapSpacing = parsePoint();
break;
case 15:
viewPort.gridSpacing = parsePoint();
break;
case 16:
viewPort.viewDirectionFromTarget = parsePoint();
break;
case 17:
viewPort.viewTarget = parsePoint();
break;
case 42:
viewPort.lensLength = curr.value;
curr = scanner.next();
break;
case 100:
if(curr.value === 'AcDbSymbolTable') {
// ignore
curr = scanner.next();
}else{
logUnhandledGroup(curr);
curr = scanner.next();
}
case 43:
viewPort.frontClippingPlane = curr.value;
curr = scanner.next();
break;
case 70:
expectedCount = curr.value;
case 44:
viewPort.backClippingPlane = curr.value;
curr = scanner.next();
break;
case 45:
viewPort.viewHeight = curr.value;
curr = scanner.next();
break;
case 50:
viewPort.snapRotationAngle = curr.value;
curr = scanner.next();
break;
case 51:
viewPort.viewTwistAngle = curr.value;
curr = scanner.next();
break;
case 110:
viewPort.ucsOrigin = parsePoint();
break;
case 111:
viewPort.ucsXAxis = parsePoint();
break;
case 112:
viewPort.ucsYAxis = parsePoint();
break;
case 110:
viewPort.ucsOrigin = parsePoint();
break;
case 281:
viewPort.renderMode = curr.value;
curr = scanner.next();
break;
case 281:
// 0 is one distant light, 1 is two distant lights
viewPort.defaultLightingType = curr.value;
curr = scanner.next();
break;
case 292:
viewPort.defaultLightingOn = curr.value;
curr = scanner.next();
break;
case 330:
viewPort.ownerHandle = curr.value;
curr = scanner.next();
break;
case 63:
case 421:
case 431:
viewPort.ambientColor = curr.value;
curr = scanner.next();
break;
case 0:
if(curr.value === 'LTYPE') {
table.lineTypes = parseLineTypes();
} else {
logUnhandledGroup(curr);
// New ViewPort
if(curr.value === 'VPORT') {
log.debug('}');
viewPorts.push(viewPort);
log.debug('ViewPort {');
viewPort = {};
curr = scanner.next();
}
break;
default:
logUnhandledGroup(curr);
curr = scanner.next();
break;
}
}
actualCount = Object.keys(table.lineTypes).length;
if(expectedCount !== actualCount) log.warn('Parsed ' + actualCount + ' LTYPE\'s but expected ' + expectedCount);
curr = scanner.next();
return table;
// Note: do not call scanner.next() here,
// parseTable() needs the current group
log.debug('}');
viewPorts.push(viewPort);

return viewPorts;
};

var parseLineTypes = function() {
Expand Down Expand Up @@ -852,6 +938,27 @@ DxfParser.prototype._parse = function(dxfString) {
return layers;
};

var tableDefinitions = {
VPORT: {
tableRecordsProperty: 'viewPorts',
tableName: 'viewPort',
dxfSymbolName: 'VPORT',
parseTableRecords: parseViewPortRecords
},
LTYPE: {
tableRecordsProperty: 'lineTypes',
tableName: 'lineType',
dxfSymbolName: 'LTYPE',
parseTableRecords: parseLineTypes
},
LAYER: {
tableRecordsProperty: 'layers',
tableName: 'layer',
dxfSymbolName: 'LAYER',
parseTableRecords: parseLayers
}
};

/**
* Is called after the parser first reads the 0:ENTITIES group. The scanner
* should be on the start of the first entity already.
Expand Down
10 changes: 7 additions & 3 deletions lib/DxfParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var DxfArrayScanner = require('./DxfArrayScanner.js'),
var log = require('loglevel');

//log.setLevel('trace');
log.setLevel('debug');
//log.setLevel('info');
//log.setLevel('debug');
log.setLevel('info');
//log.setLevel('warn');
//log.setLevel('error');
//log.setLevel('silent');
Expand Down Expand Up @@ -40,7 +40,11 @@ DxfParser.prototype.parseStream = function(stream, done) {
}

function onEnd() {
var dxf = self._parse(dxfString);
try {
var dxf = self._parse(dxfString);
}catch(err) {
return done(err);
}
done(null, dxf);
}

Expand Down
39 changes: 26 additions & 13 deletions test/DxfParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var should = require('should');
var path = require('path');

describe('Parser', function() {

it('should parse the dxf header variables into an object', function(done) {
var file = fs.createReadStream(__dirname + '/data/header.dxf', { encoding: 'utf8' });
var parser = new DxfParser();
Expand All @@ -16,27 +17,36 @@ describe('Parser', function() {
});
});

it('should parse the dxf layers', function(done) {
var tables;

it('should parse the tables section without error', function(done) {
var file = fs.createReadStream(__dirname + '/data/tables.dxf', { encoding: 'utf8' });
var parser = new DxfParser();

parser.parseStream(file, function(err, result) {
should.not.exist(err);
result.tables.layers.should.eql({ '0': { name: '0', color: 16777215 }, 'Layer 1': { name: 'Layer 1', color: 16777215}});
tables = result.tables;
done();
});
});

it('should parse the dxf ltype table', function(done) {
var file = fs.createReadStream(__dirname + '/data/tables.dxf', { encoding: 'utf8' });
var parser = new DxfParser();
it('should parse the dxf layers', function() {
should.exist(tables);
tables.should.have.property('layer');

parser.parseStream(file, function(err, result) {
should.not.exist(err);
var expected = fs.readFileSync(__dirname + '/data/tables.parser.out', {encoding: 'utf8'})
result.tables.lineTypes.should.eql(JSON.parse(expected));
done();
});
//fs.writeFileSync(__dirname + '/data/layer-table.actual.json', JSON.stringify(tables.layer, null, 2));
var expected = fs.readFileSync(path.join(__dirname,'data','layer-table.expected.json'), {encoding: 'utf8'});
tables.layer.should.eql(JSON.parse(expected));
});

it('should parse the dxf ltype table', function() {
should.exist(tables);
tables.should.have.property('lineType');

//fs.writeFileSync(__dirname + '/data/ltype-table.actual.json', JSON.stringify(tables.lineType, null, 2));

var expected = fs.readFileSync(path.join(__dirname,'data','ltype-table.expected.json'), {encoding: 'utf8'});
tables.lineType.should.eql(JSON.parse(expected));
});

it('should parse the BLOCKS section', function() {
Expand All @@ -50,7 +60,10 @@ describe('Parser', function() {
should.not.exist(err);
}
should.exist(dxf);
var expected = require('./data/blocks.json');
dxf.should.eql(expected);

//fs.writeFileSync(__dirname + '/data/blocks.actual.json', JSON.stringify(dxf, null, 2));

var expected = fs.readFileSync(path.join(__dirname,'data','blocks.expected.json'), {encoding: 'utf8'});
dxf.should.eql(JSON.parse(expected));
});
});
1 change: 1 addition & 0 deletions test/data/blocks.expected.json

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions test/data/blocks.json

This file was deleted.

16 changes: 16 additions & 0 deletions test/data/layer-table.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"handle": "2",
"ownerHandle": "0",
"layers": {
"0": {
"name": "0",
"visible": false,
"color": 16777215
},
"Layer 1": {
"name": "Layer 1",
"visible": false,
"color": 16777215
}
}
}
Loading

0 comments on commit add30ed

Please sign in to comment.