-
Notifications
You must be signed in to change notification settings - Fork 144
Home
This repository contains a sample dxf at samples/data/api-cw750-details.dxf
and basic sample code to demonstrate how to use this parser at samples/parse-sync.js
and samples/parse-stream.js
. To use them, clone the repository, cd into samples, and run
node parse-sync
// OR
node parse-stream
This will create a file called out.json
in the current directory. Open it to see the result of parsing the sample dxf mentioned above.
You may also see some example output here
Anyone is welcome to contribute. Simply clone this repository, make your changes and create a pull request.
We prefer it if changes come with tests. Tests are run using mocha in the BDD style. See the test directory for samples.
Most often, people want to contribute by adding the ability to read entity that is not yet covered by the parser, or to add more parsing support to an entity already being parsed by adding support for particular fields. The first place to look is in the lib/entities
folder of the project. Each file in that folder follows a similar flow for parsing an entity.
Below is a simplified version of the ARC entity code with some annotations to help you see the structure of one of these entity parser files.
var helpers = require('../ParseHelpers');
module.exports = EntityParser;
function EntityParser() {}
// The entity name according to the DXF reference.
EntityParser.ForEntityName = 'ARC';
EntityParser.prototype.parseEntity = function(scanner, curr) {
var entity = { type: curr.value };
curr = scanner.next(); // Get the next group
while(curr !== 'EOF') {
if(curr.code === 0) break; // Loop through groups until we come to a new entity (indicated by code 0)
switch(curr.code) { // The code determines the property we set. See the DXF reference to know what the codes mean
//
// Everything above this line is boilerplate code for all entity parsers
//
case 10: // indicates the X coordinate of a 2D or 3D point
entity.center = helpers.parsePoint(scanner); // parsePoint() will parse the next few codes for us to get a 2D or 3D point
break;
case 40: // radius
entity.radius = curr.value;
break;
default: // parses fields common to all entities
helpers.checkCommonEntityProperties(entity, curr);
break;
}
curr = scanner.next();
}
return entity;
};
You can copy the above code and modify it for any other entity you wish to add support for.
The last step is to add it to the registered entity parsers in lib/DxfParser.js
.
function registerDefaultEntityHandlers(dxfParser) {
// Supported entities here
dxfParser.registerEntityHandler(require('./entities/3dface'));
dxfParser.registerEntityHandler(require('./entities/arc'));
dxfParser.registerEntityHandler(require('./entities/attdef'));
dxfParser.registerEntityHandler(require('./entities/circle'));
// ...
}
It can be difficult debug the parser code for large files. To aid developmenet, you can uncomment a line near the top of lib/DxfParser.js
to enable debug logs.
//log.setLevel('debug'); // Uncomment for verbose tracing
//log.setLevel('info'); // Uncomment for regular output
The debug
logs will be a verbose tracing of the parser's progress. The output of these logs usually needs to be redirected to a file. For example node samples/parse-sync.js > out.log
. Some useful output is sent to console.error so it may be good to redirect that to out.log as well.
Some sample output of the debug logs
LINE {
}
MTEXT {
unhandled group 100:AcDbMText
unhandled group 46:0
unhandled group 7:FG-Note
unhandled group 73:1
unhandled group 44:1
}
The above output shows 2 entities that were parsed. First was a LINE entity. All it's properties were handled properly by the parser. The second is the MTEXT entity. It had 5 unhandled code:value pairs (called groups). You can read the AutoCad DXF Reference (pdf download) to determine what these codes mean and can edit the parser to include these values with logical names.
Some key items that we could use some help with:
- Writing tests for current code
- Writing a build script to compile the code for browsers (probably using browserify but we are open to ideas)
- See our issues page for other things to work on