diff --git a/src/Helper.js b/src/Helper.js index f5d6fc1..d61a999 100644 --- a/src/Helper.js +++ b/src/Helper.js @@ -6,13 +6,14 @@ import toPolylines from './toPolylines' import groupEntitiesByLayer from './groupEntitiesByLayer' export default class Helper { - constructor(contents) { + constructor(contents, options = {}) { if (!(typeof contents === 'string')) { throw Error('Helper constructor expects a DXF string') } this._contents = contents this._parsed = null this._denormalised = null + this._options = options } parse() { @@ -28,8 +29,9 @@ export default class Helper { return this._parsed } - denormalise() { - this._denormalised = denormalise(this.parsed) + denormalise(options = this._options) { + this._denormalised = denormalise(this.parsed, options) + this._groups = null logger.info('denormalised:', this._denormalised) return this._denormalised } diff --git a/src/denormalise.js b/src/denormalise.js index cead51c..5bae2c4 100644 --- a/src/denormalise.js +++ b/src/denormalise.js @@ -2,7 +2,26 @@ import cloneDeep from 'lodash/cloneDeep' import logger from './util/logger' -export default (parseResult) => { +const DEFAULT_LAYER = '0' + +const resolveInsertedEntityLayer = ( + entityLayer, + insertLayer, + preserveBlockEntityLayers, +) => { + if (!preserveBlockEntityLayers) { + return insertLayer + } + + const layer = entityLayer ?? DEFAULT_LAYER + const resolvedInsertLayer = insertLayer ?? DEFAULT_LAYER + + return layer === DEFAULT_LAYER ? resolvedInsertLayer : layer +} + +export default (parseResult, options = {}) => { + const { preserveBlockEntityLayers = false } = options + const blocksByName = parseResult.blocks.reduce((acc, b) => { acc[b.name] = b return acc @@ -59,7 +78,11 @@ export default (parseResult) => { // Use the insert layer const blockEntities = block.entities.map((be) => { const be2 = cloneDeep(be) - be2.layer = insert.layer + be2.layer = resolveInsertedEntityLayer( + be2.layer, + insert.layer, + preserveBlockEntityLayers, + ) // https://github.com/bjnortier/dxf/issues/52 // See Issue 52. If we don't modify the // entity coordinates here it creates an issue with the diff --git a/test/unit/denormalise.test.js b/test/unit/denormalise.test.js index 6a8ac94..ec1d777 100644 --- a/test/unit/denormalise.test.js +++ b/test/unit/denormalise.test.js @@ -112,4 +112,107 @@ describe('Denormalise', () => { rotation: 120, }) }) + + it('uses the insert layer by default', () => { + const parsed = { + blocks: [ + { + name: 'DESK', + x: 0, + y: 0, + entities: [ + { + type: 'LINE', + layer: 'DESK_A', + start: { x: 0, y: 0 }, + end: { x: 10, y: 0 }, + }, + ], + }, + ], + entities: [ + { + type: 'INSERT', + block: 'DESK', + layer: 'FURNITURE', + x: 100, + y: 200, + }, + ], + } + + const entities = denormalise(parsed) + + expect(entities[0].layer).toEqual('FURNITURE') + }) + + it('preserves explicit block entity layers when preserveBlockEntityLayers is true', () => { + const parsed = { + blocks: [ + { + name: 'DESK', + x: 0, + y: 0, + entities: [ + { + type: 'LINE', + layer: 'DESK_A', + start: { x: 0, y: 0 }, + end: { x: 10, y: 0 }, + }, + ], + }, + ], + entities: [ + { + type: 'INSERT', + block: 'DESK', + layer: 'FURNITURE', + x: 100, + y: 200, + }, + ], + } + + const entities = denormalise(parsed, { + preserveBlockEntityLayers: true, + }) + + expect(entities[0].layer).toEqual('DESK_A') + }) + + it('uses the insert layer for layer 0 block entities when preserving block layers', () => { + const parsed = { + blocks: [ + { + name: 'DESK', + x: 0, + y: 0, + entities: [ + { + type: 'LINE', + layer: '0', + start: { x: 0, y: 0 }, + end: { x: 10, y: 0 }, + }, + ], + }, + ], + entities: [ + { + type: 'INSERT', + block: 'DESK', + layer: 'FURNITURE', + x: 100, + y: 200, + }, + ], + } + + const entities = denormalise(parsed, { + preserveBlockEntityLayers: true, + }) + + expect(entities[0].layer).toEqual('FURNITURE') + }) })