-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPortedIOTests.test.ts
More file actions
235 lines (190 loc) · 6.96 KB
/
Copy pathPortedIOTests.test.ts
File metadata and controls
235 lines (190 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { beforeAll, describe, expect, it } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { TestVariables } from '../TestVariables.js';
import { getDwgFiles, getDxfAsciiFiles, readFileAsArrayBuffer, readFileAsUint8Array } from '../testHelpers.js';
import { CadDocument } from '../../src/CadDocument.js';
import { ACadVersion } from '../../src/ACadVersion.js';
import { DxfReader } from '../../src/IO/DXF/DxfReader.js';
import { DxfWriter } from '../../src/IO/DXF/DxfWriter.js';
import { DwgReader } from '../../src/IO/DWG/DwgReader.js';
import { DwgWriter } from '../../src/IO/DWG/DwgWriter.js';
class InMemoryAsciiStream {
private readonly chunks: string[] = [];
public write(value: string): void {
this.chunks.push(value);
}
public flush(): void {}
public close(): void {}
public toUint8Array(): Uint8Array {
return new TextEncoder().encode(this.chunks.join(''));
}
}
const dwgFiles = getDwgFiles();
const dxfAsciiFiles = getDxfAsciiFiles();
function isWritableDwgVersion(version: ACadVersion): boolean {
return version >= ACadVersion.AC1014;
}
function isAc1009Sample(filePath: string): boolean {
return filePath.toLowerCase().includes('ac1009');
}
function deleteIfExists(filePath: string): void {
if (fs.existsSync(filePath)) {
fs.rmSync(filePath, { force: true });
}
}
function expectFileWritten(filePath: string): void {
expect(fs.existsSync(filePath)).toBe(true);
expect(fs.statSync(filePath).size).toBeGreaterThan(0);
}
function readDwgDocument(filePath: string): CadDocument {
const reader = new DwgReader(readFileAsArrayBuffer(filePath));
return reader.read();
}
function readDxfDocument(filePath: string): CadDocument {
const reader = new DxfReader(readFileAsUint8Array(filePath));
return reader.read();
}
function transferEntities(source: CadDocument): CadDocument {
const transfer = new CadDocument();
transfer.header.version = source.header.version;
const entities = [...source.entities];
for (const entity of entities) {
const moved = source.entities.remove(entity);
if (moved) {
transfer.entities.add(moved);
}
}
return transfer;
}
function normalizeHeaderTablePointers(doc: CadDocument): void {
const { header } = doc;
if (doc.layers?.tryGetValue('0')) {
try {
doc.layers.get(header.currentLayerName);
} catch {
header.currentLayerName = '0';
}
}
if (doc.lineTypes?.tryGetValue('ByLayer')) {
try {
doc.lineTypes.get(header.currentLineTypeName);
} catch {
header.currentLineTypeName = 'ByLayer';
}
}
if (doc.textStyles?.tryGetValue('Standard')) {
try {
doc.textStyles.get(header.currentTextStyleName);
} catch {
header.currentTextStyleName = 'Standard';
}
}
if (doc.dimensionStyles?.tryGetValue('Standard')) {
try {
doc.dimensionStyles.get(header.currentDimensionStyleName);
} catch {
header.currentDimensionStyleName = 'Standard';
}
}
}
function writeDwgFile(outPath: string, doc: CadDocument): boolean {
if (!TestVariables.localEnv || !isWritableDwgVersion(doc.header.version)) {
return false;
}
deleteIfExists(outPath);
doc.createDefaults();
normalizeHeaderTablePointers(doc);
const buffer = new ArrayBuffer(16 * 1024 * 1024);
const writer = new DwgWriter(buffer, doc);
writer.write();
fs.writeFileSync(outPath, new Uint8Array(buffer, 0, writer.bytesWritten));
expectFileWritten(outPath);
return true;
}
function writeDxfFile(outPath: string, doc: CadDocument): void {
deleteIfExists(outPath);
const stream = new InMemoryAsciiStream();
const writer = new DxfWriter(stream as any, doc, false);
writer.write();
fs.writeFileSync(outPath, stream.toUint8Array());
expectFileWritten(outPath);
}
describe('Ported IOTests', () => {
beforeAll(() => {
fs.mkdirSync(TestVariables.outputSamplesFolder, { recursive: true });
});
describe.each(dwgFiles.map(file => [file.fileName, file] as const))('DWG sample %s', (_name, sample) => {
it('DwgEntitiesToDwgFile', () => {
const doc = readDwgDocument(sample.path);
if (!isWritableDwgVersion(doc.header.version)) {
return;
}
const transfer = transferEntities(doc);
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_moved_out.dwg`);
writeDwgFile(outPath, transfer);
});
it('DwgEntitiesToDxfFile', () => {
const doc = readDwgDocument(sample.path);
const transfer = transferEntities(doc);
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_moved_to.dxf`);
writeDxfFile(outPath, transfer);
});
it('DwgToDwg', () => {
const doc = readDwgDocument(sample.path);
if (!isWritableDwgVersion(doc.header.version)) {
return;
}
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_out.dwg`);
writeDwgFile(outPath, doc);
});
it('DwgToDxf', () => {
const doc = readDwgDocument(sample.path);
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_out.dxf`);
writeDxfFile(outPath, doc);
});
});
describe.each(dxfAsciiFiles.map(file => [file.fileName, file] as const))('DXF ASCII sample %s', (_name, sample) => {
it('DxfEntitiesToDwgFile', () => {
if (isAc1009Sample(sample.path)) {
return;
}
const doc = readDxfDocument(sample.path);
if (!isWritableDwgVersion(doc.header.version)) {
return;
}
const transfer = transferEntities(doc);
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_moved_to.dwg`);
writeDwgFile(outPath, transfer);
});
it('DxfToDwg', () => {
if (isAc1009Sample(sample.path)) {
return;
}
const doc = readDxfDocument(sample.path);
if (!isWritableDwgVersion(doc.header.version)) {
return;
}
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_dxf_to.dwg`);
writeDwgFile(outPath, transferEntities(doc));
});
it('DxfToDxf', () => {
if (isAc1009Sample(sample.path)) {
return;
}
const doc = readDxfDocument(sample.path);
if (doc.header.version < ACadVersion.AC1012) {
return;
}
const outPath = path.join(TestVariables.outputSamplesFolder, `${sample.noExtensionName}_rewrite_out.dxf`);
writeDxfFile(outPath, doc);
});
});
it('EmptyDwgToDxf', () => {
const inPath = path.join(TestVariables.samplesFolder, 'sample_base', 'empty.dwg');
expect(fs.existsSync(inPath)).toBe(true);
const doc = readDwgDocument(inPath);
const outPath = path.join(TestVariables.outputSamplesFolder, 'empty_out.dxf');
writeDxfFile(outPath, doc);
});
});