Skip to content

Commit

Permalink
fix: paper spaces and bump version (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarikjabiri authored May 18, 2023
1 parent e4e9b71 commit 13885fa
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 42 deletions.
9 changes: 9 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ const layerTest = dxf.addLayer('test', Colors.Blue)
const line = paperSpace.addLine(point3d(), point3d(100, 100));
line.layerName = layerTest.name;

const paperSpace0 = dxf.addPaperSpace();
const l = paperSpace0.addLine(point3d(), point3d(100, 100));
l.layerName = layerTest.name;

dxf.addPaperSpace().addLine(point3d(), point3d(100, 100)); // paperSpace1
dxf.addPaperSpace().addLine(point3d(), point3d(100, 100)); // paperSpace2
dxf.addPaperSpace(); // paperSpace3 (empty)


const _str = dxf.stringify();
writeFileSync('examples/example.dxf', _str);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarikjabiri/dxf",
"version": "2.8.5",
"version": "2.8.6",
"description": "A JavaScript interface to Dxf written in TypeScript.",
"main": "./lib/index.cjs.js",
"module": "./lib/index.esm.js",
Expand Down
52 changes: 26 additions & 26 deletions specs/__snapshots__/DxfWriter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ CIRCLE
5
23
330
21
20
100
AcDbEntity
8
Expand All @@ -884,7 +884,7 @@ LWPOLYLINE
5
24
330
21
20
100
AcDbEntity
8
Expand Down Expand Up @@ -938,7 +938,7 @@ POINT
5
1A
330
D
C
100
AcDbEntity
8
Expand All @@ -956,7 +956,7 @@ LWPOLYLINE
5
1B
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -996,7 +996,7 @@ LINE
5
1C
330
D
C
100
AcDbEntity
8
Expand All @@ -1020,7 +1020,7 @@ LWPOLYLINE
5
1E
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1064,7 +1064,7 @@ LWPOLYLINE
5
1F
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1128,7 +1128,7 @@ INSERT
5
25
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1166,7 +1166,7 @@ SPLINE
5
26
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1258,7 +1258,7 @@ ARC
5
27
330
D
C
100
AcDbEntity
8
Expand All @@ -1284,7 +1284,7 @@ ELLIPSE
5
28
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1314,7 +1314,7 @@ AcDbEllipse
5
29
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1352,7 +1352,7 @@ CIRCLE
5
2A
330
D
C
100
AcDbEntity
8
Expand All @@ -1372,7 +1372,7 @@ POLYLINE
5
2B
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1478,7 +1478,7 @@ IMAGE
5
31
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1542,7 +1542,7 @@ TEXT
5
37
330
D
C
100
AcDbEntity
8
Expand All @@ -1568,7 +1568,7 @@ DIMENSION
5
3B
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1598,7 +1598,7 @@ DIMENSION
5
3C
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1644,7 +1644,7 @@ DIMENSION
5
3D
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1674,7 +1674,7 @@ DIMENSION
5
3E
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1706,7 +1706,7 @@ DIMENSION
5
3F
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1738,7 +1738,7 @@ DIMENSION
5
40
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1772,7 +1772,7 @@ DIMENSION
5
41
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1828,7 +1828,7 @@ DIMENSION
5
42
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1878,7 +1878,7 @@ DIMENSION
5
43
330
D
C
100
AcDbEntity
8
Expand Down Expand Up @@ -1914,7 +1914,7 @@ DIMENSION
5
44
330
D
C
100
AcDbEntity
8
Expand Down
9 changes: 9 additions & 0 deletions src/DxfWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ export class DxfWriter {
return this.blocks.addBlock(name, this.document.objects)
}

/**
* Add new paper space block to the blocks tables.
* @returns The added block.
*/
addPaperSpace() {
return this.blocks.addPaperSpace()
}


/**
* Add a header variable to the dxf if not exist.
* If exist it will update values.
Expand Down
8 changes: 5 additions & 3 deletions src/Sections/BlocksSection/DxfBlock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { point3d, vec3_t } from 'Internals/Helpers'
import { DxfBlockRecord } from 'TablesSection/Tables/Records/DxfBlockRecord'
import { DxfEndBlk } from './DxfEndBlk'
import { DxfObjectsSection } from 'ObjectsSection/DxfObjectsSection'
import { Dxfier } from 'Internals/Dxfier'
Expand Down Expand Up @@ -35,11 +36,12 @@ export class DxfBlock extends EntitiesManager {
return this.isModelSpace || this.isPaperSpace
}

constructor(name: string, objects: DxfObjectsSection) {
super(objects, '0')
constructor(name: string, blockRecord: DxfBlockRecord, objects: DxfObjectsSection) {
super(objects, blockRecord, '0')
this.name = name
this.flags = BlockFlags.None
this.endBlk = new DxfEndBlk()
this.ownerObjectHandle = blockRecord.handle
this.basePoint = point3d(0, 0, 0)
this.xrefPathName = ''
}
Expand All @@ -60,7 +62,7 @@ export class DxfBlock extends EntitiesManager {
dx.point3d(this.basePoint)
dx.name(this.name, 3)
dx.push(1, this.xrefPathName)
if (!this.isModelOrPaperSpace) super.dxfy(dx)
if (!this.isModelSpace && this.name !== '*Paper_Space') super.dxfy(dx)
this.endBlk.dxfy(dx)
}
}
3 changes: 1 addition & 2 deletions src/Sections/BlocksSection/DxfBlocksSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export class DxfBlocksSection implements DxfInterface {
): DxfBlock {
if (removeSpecialChars) name = name.replace(specialCharsRegex, '')
const blockRecord = this.tables.addBlockRecord(name)
const block = new DxfBlock(name, objects)
block.ownerObjectHandle = blockRecord.handle
const block = new DxfBlock(name, blockRecord, objects)
this.blocks.push(block)
return block
}
Expand Down
11 changes: 3 additions & 8 deletions src/Sections/EntitiesSection/DxfEntitiesSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { Dxfier } from 'Internals/Dxfier'
export class DxfEntitiesSection implements DxfInterface {
readonly blocks: DxfBlocksSection
readonly modelSpace: DxfBlock
readonly paperSpace: DxfBlock

constructor(blocks: DxfBlocksSection) {
this.blocks = blocks
this.modelSpace = blocks.modelSpace
this.paperSpace = blocks.paperSpace
}

setLayerName(layerName: string) {
Expand All @@ -17,15 +19,8 @@ export class DxfEntitiesSection implements DxfInterface {

dxfy(dx: Dxfier) {
dx.start('ENTITIES')
this.paperSpace.entities.forEach((e) => e.dxfy(dx))
this.modelSpace.entities.forEach((e) => e.dxfy(dx))
this.blocks.blocks.forEach((b) => {
if (b.isPaperSpace) {
b.entities.forEach((e) => {
e.inPaperSpace = true
e.dxfy(dx)
})
}
})
dx.end()
}
}
8 changes: 6 additions & 2 deletions src/Sections/EntitiesSection/EntitiesManager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { BoundingBox, boundingBox_t } from 'Internals/BoundingBox'
import Entity, { CommonEntityOptions } from './Entity'
import { RectangleOptions, bulge, point2d, vec2_t, vec3_t } from 'Internals/Helpers'
import { DxfBlockRecord } from 'TablesSection/Tables/Records/DxfBlockRecord'
import { DxfInterface } from 'Internals/Interfaces'
import { DxfObjectsSection } from 'ObjectsSection/DxfObjectsSection'
import { Dxfier } from 'Internals/Dxfier'
import Handle from 'Internals/Handle'
import * as Entities from './Entities'

export abstract class EntitiesManager implements DxfInterface {
readonly blockRecord: DxfBlockRecord
readonly entities: Entity[] = []
readonly handle: string
private readonly objects: DxfObjectsSection
layerName: string

constructor(objects: DxfObjectsSection, layerName: string) {
constructor(objects: DxfObjectsSection, blockRecord: DxfBlockRecord, layerName: string) {
this.handle = Handle.next()
this.objects = objects
this.blockRecord = blockRecord
this.layerName = layerName
}

Expand All @@ -35,7 +38,8 @@ export abstract class EntitiesManager implements DxfInterface {
}

addEntity<T extends Entity>(entity: T): T {
entity.ownerBlockRecord = this.handle
entity.ownerBlockRecord = this.blockRecord.handle
if(this.blockRecord.isPaperSpace) entity.inPaperSpace = true
if (entity.layerName == null) entity.layerName = this.layerName
this.entities.push(entity)
return entity
Expand Down
4 changes: 4 additions & 0 deletions src/Sections/TablesSection/Tables/Records/DxfBlockRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class DxfBlockRecord extends DxfRecord {
scalability: number
layoutObject?: string

get isPaperSpace() {
return this.name.startsWith('*Paper_Space')
}

constructor(name: string) {
super('BLOCK_RECORD')
this.name = name
Expand Down

0 comments on commit 13885fa

Please sign in to comment.