-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathconvert2xkt.js
executable file
·254 lines (210 loc) · 9.27 KB
/
convert2xkt.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env node
const commander = require('commander');
const npmPackage = require('./package.json');
const {convert2xkt, XKT_INFO} = require("./dist/xeokit-convert.cjs.js");
const fs = require('fs');
const defaultConfigs = require(`./convert2xkt.conf.js`);
const WebIFC = require("web-ifc/web-ifc-api-node.js");
const path = require("path");
const {createValidator} = require("@typeonly/validator");
// const validator = createValidator({
// bundle: require("./types.to.json")
// });
const program = new commander.Command();
program.version(npmPackage.version, '-v, --version');
program
.option('-c, --configs [file]', 'optional path to JSON configs file; overrides convert2xkt.conf.js')
.option('-s, --source [file]', 'path to source file')
.option('-a, --sourcemanifest [file]', 'path to source manifest file (for converting split file output from ifcgltf -s)')
.option('-f, --format [string]', 'source file format (optional); supported formats are gltf, ifc, laz, las, pcd, ply, stl and cityjson')
.option('-m, --metamodel [file]', 'path to source metamodel JSON file (optional)')
.option('-i, --include [types]', 'only convert these types (optional)')
.option('-x, --exclude [types]', 'never convert these types (optional)')
.option('-r, --rotatex', 'rotate model 90 degrees about X axis (for las and cityjson)')
.option('-g, --disablegeoreuse', 'disable geometry reuse (optional)')
.option('-z, --minTileSize [number]', 'minimum diagonal tile size (optional, default 500)')
.option('-t, --disabletextures', 'ignore textures (optional)')
.option('-n, --disablenormals', 'ignore normals (optional)')
.option('-u, --uncompressBuffers', 'do not compress buffers, otherwise output xkt V10 with compressed buffers (optional)')
.option('-o, --output [file]', 'path to target .xkt file when -s option given, or JSON manifest for multiple .xkt files when source manifest file given with -a; creates directories on path automatically if not existing')
.option('-l, --log', 'enable logging (optional)');
program.on('--help', () => {
console.log(`\n\nXKT version: ${XKT_INFO.xktVersion}`);
});
program.parse(process.argv);
const options = program.opts();
let configs = defaultConfigs;
if (options.source === undefined && options.sourcemanifest === undefined) {
console.error('[convert2xkt] [ERROR]: Please specify path to source file or manifest.');
program.help();
process.exit(1);
}
if (options.source !== undefined && options.sourcemanifest !== undefined) {
console.error('[convert2xkt] [ERROR]: Can\'t specify path to source file AND manifest - only one of these params allowed.');
program.help();
process.exit(1);
}
if (options.output === undefined) {
console.error('[convert2xkt] [ERROR]: Please specify target xkt file path.');
program.help();
process.exit(1);
}
function log(msg) {
if (options.log) {
console.log(msg);
}
}
function getFileExtension(fileName) {
let ext = path.extname(fileName);
if (ext.charAt(0) === ".") {
ext = ext.substring(1);
}
return ext;
}
async function main() {
log(`[convert2xkt] Running convert2xkt v${npmPackage.version}...`);
if (options.configs !== undefined) {
log(`[convert2xkt] Using JSON configs file: ${options.configs}`);
try {
let configsData = fs.readFileSync(options.configs);
configs = JSON.parse(configsData);
} catch (e) {
console.error(`[convert2xkt] [ERROR]: Failed to load custom configs file (specified with -c or --configs) - ${e}`);
process.exit(1);
}
} else {
log(`[convert2xkt] Using configs in ./convert2xkt.conf.js`);
}
configs.sourceConfigs ||= {};
if (options.sourcemanifest) {
log(`[convert2xkt] Converting glTF files in manifest ${options.sourcemanifest}...`);
let manifestData = fs.readFileSync(options.sourcemanifest);
let manifest = JSON.parse(manifestData);
if (!manifest.gltfOutFiles) {
console.error(`[convert2xkt] [ERROR]: Input manifest invalid - missing field: gltfOutFiles`);
process.exit(1);
}
const numInputFiles = manifest.gltfOutFiles.length;
if (numInputFiles === 0) {
console.error(`[convert2xkt] [ERROR]: Input manifest invalid - gltfOutFiles is zero length`);
process.exit(1);
}
const outputDir = path.dirname(options.output);
if (outputDir !== "" && !fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, {recursive: true});
}
function formatDate(date) {
return date.toISOString();
}
const xktManifest = {
inputFile: options.sourcemanifest,
converterApplication: "convert2xkt",
converterApplicationVersion: `v${npmPackage.version}`,
conversionDate: formatDate(new Date()),
outputDir,
xktFiles: []
};
const sourceConfigs = configs.sourceConfigs || {};
const formatConfig = sourceConfigs["gltf"] || configs["glb"] || {};
const externalMetadata = (!!formatConfig.externalMetadata);
if (externalMetadata) {
xktManifest.metaModelFiles = [];
for (let i = 0, len = manifest.metadataOutFiles.length; i < len; i++) {
const metadataSource = manifest.metadataOutFiles[i];
xktManifest.metaModelFiles.push(path.basename(metadataSource));
}
}
let i = 0;
const convertNextFile = () => {
const source = manifest.gltfOutFiles[i];
const metaModelSource = (i < manifest.metadataOutFiles.length) ? manifest.metadataOutFiles[i] : null;
const outputFileName = getFileNameWithoutExtension(source);
const outputFileNameXKT = `${outputFileName}.xkt`;
const ext = getFileExtension(source);
let modelAABB;
// if (manifest.modelBoundsMin && manifest.modelBoundsMax) {
// modelAABB= [
// manifest.modelBoundsMin[0],
// manifest.modelBoundsMin[1],
// manifest.modelBoundsMin[2],
// manifest.modelBoundsMax[0],
// manifest.modelBoundsMax[1],
// manifest.modelBoundsMax[2]
// ];
// }
convert2xkt({
WebIFC,
configs,
source,
format: ext,
metaModelSource: (!externalMetadata) ? metaModelSource : null,
modelAABB,
output: path.join(outputDir, outputFileNameXKT),
includeTypes: options.include ? options.include.slice(",") : null,
excludeTypes: options.exclude ? options.exclude.slice(",") : null,
rotateX: options.rotatex,
reuseGeometries: (options.disablegeoreuse !== true),
minTileSize: options.minTileSize,
includeTextures: !options.disabletextures,
includeNormals: !options.disablenormals,
zip: !options.uncompressBuffers,
log
}).then(() => {
i++;
log(`[convert2xkt] Converted model${i}.xkt (${i} of ${numInputFiles})`);
xktManifest.xktFiles.push(outputFileNameXKT);
if (i === numInputFiles) {
fs.writeFileSync(options.output, JSON.stringify(xktManifest));
log(`[convert2xkt] Done.`);
process.exit(0);
} else {
convertNextFile();
}
}).catch((err) => {
console.error(`[convert2xkt] [ERROR]: ${err}`);
process.exit(1);
});
}
convertNextFile();
} else {
if (options.output) {
const outputDir = path.dirname(options.output);
if (outputDir !== "" && !fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, {recursive: true});
}
}
log(`[convert2xkt] Converting single input file ${options.source}...`);
convert2xkt({
WebIFC,
configs,
source: options.source,
format: options.format,
metaModelSource: options.metamodel,
output: options.output,
includeTypes: options.include ? options.include.slice(",") : null,
excludeTypes: options.exclude ? options.exclude.slice(",") : null,
rotateX: options.rotatex,
reuseGeometries: (options.disablegeoreuse !== true),
minTileSize: options.minTileSize,
includeTextures: !options.disabletextures,
includeNormals: !options.disablenormals,
zip: !options.uncompressBuffers,
log
}).then(() => {
log(`[convert2xkt] Done.`);
process.exit(0);
}).catch((err) => {
console.error(`[convert2xkt] [ERROR]: ${err}`);
process.exit(1);
});
}
}
function getFileNameWithoutExtension(filePath) {
const baseName = path.basename(filePath);
const fileNameWithoutExtension = path.parse(baseName).name;
return fileNameWithoutExtension;
}
main().catch(err => {
console.error('Error:', err);
process.exit(1);
});