Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down
27 changes: 25 additions & 2 deletions src/denormalise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions test/unit/denormalise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})