-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-ifc.js
417 lines (337 loc) · 18.9 KB
/
convert-ifc.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const fs = require("fs");
const path = require("path");
const execSync = require('child_process').execSync;
const si = require('systeminformation');
const axios = require('axios');
const {KDTree, math} = require('./lib/KDTree.js');
const configsData = fs.readFileSync("./convertconfig.json");
const configs = JSON.parse(configsData);
const inputFilesDir = "./inputFiles/ifc/";
const convertedModelsDir = "./convertedModels/ifc/";
const convertedProjectsDir = "./convertedModels/ifc/projects/";
const ifcSizes = {};
const xktSizes = {};
const conversionTimes = {};
const ifcAttributionPaths = {};
const errors = {};
const kdTree = new KDTree();
const bashScript = [];
(async () => {
try {
if (!fs.existsSync(convertedProjectsDir)) {
fs.mkdirSync(convertedProjectsDir);
}
const fetchPackageVersion = async (packageName) => {
try {
const response = await axios.get(`https://registry.npmjs.org/${packageName}`);
if (response.status === 200) {
return response.data['dist-tags'].latest;
} else {
console.error(`Error fetching version for ${packageName}: ${response.statusText}`);
return "Latest";
}
} catch (error) {
console.error(`Error fetching version for ${packageName}: ${error.message}`);
return "Latest";
}
};
async function getLatestReleaseVersion(owner, repo) {
try {
const response = await axios.get(`https://api.github.com/repos/${owner}/${repo}/releases/latest`);
if (response.status === 200) {
const latestVersion = response.data.tag_name;
return latestVersion;
} else {
console.error(`Error: Unable to fetch latest release. Status code: ${response.status}`);
return "Latest";
}
} catch (error) {
console.error('Error:', error.message);
return "Latest";
}
}
// const browser = await puppeteer.launch({ headless: false });
// const browserPage = await browser.newPage();
const xeokitVersion = await fetchPackageVersion('@xeokit/xeokit-sdk');
const xeokitConvertVersion = await fetchPackageVersion('@xeokit/xeokit-convert');
const cxConvertVersion = await getLatestReleaseVersion("Creoox", "creoox-ifc2gltfcxconverter");
const date = new Date();
const systemInfo = {
version: await si.version(),
time: await si.time(),
system: si.system(),
cpu: await si.cpu(),
mem: await si.mem(),
graphics: await si.graphics(),
os: await si.osInfo()
};
fs.writeFileSync("./_includes/ifc/ifc-ifc2gltfOptions.html",
`@@include('../../_includes/ifc/ifc-ifc2gltfOptionsCard.html', {
"ifc2gltfFileSize":"${configs.ifc2gltf.options.s}",
"ifc2gltfTileSize":"${configs.ifc2gltf.options.t}"
})`);
fs.writeFileSync("./_includes/ifc/ifc-cliInvocation.html",
`@@include('../../_includes/ifc/ifc-cliInvocationCard.html', {
"ifc2gltfFileSize":"${configs.ifc2gltf.options.s}",
"ifc2gltfTileSize":"${configs.ifc2gltf.options.t}"
})`);
fs.writeFileSync(`${convertedModelsDir}/systemInfo.json`, JSON.stringify(systemInfo, null, "\t"), {encoding: 'utf8'});
fs.writeFileSync("./_includes/ifc/ifc-systemInfo.html",
`@@include('../../_includes/ifc/ifc-systemInfoCard.html', {
"time":"${systemInfo.time.current}",
"date":"${date}",
"cpuManufacturer": "${systemInfo.cpu.manufacturer}",
"cpuBrand": "${systemInfo.cpu.brand}",
"osPlatform": "${systemInfo.os.platform}" ,
"osDistro": "${systemInfo.os.distro}",
"osRelease": "${systemInfo.os.release}",
"totalMemory":"${Math.round(systemInfo.mem.total / 1000 / 1000 / 1000)} GB",
"nodeVersion": "${process.version}",
"xeokitVersion": "${xeokitVersion}",
"xeokitConvertVersion": "${xeokitConvertVersion}",
"cxConverterVersion": "${configs.ifc2gltf.version}"
})`);
const inputBatchDirs = await fs.promises.readdir(inputFilesDir);
for (const inputBatchDir of inputBatchDirs) { // BIMData, IfcOpenShell etc
if (inputBatchDir.startsWith("_")) {
continue;
}
const inputBatchDirPath = path.join(inputFilesDir, inputBatchDir);
const isDir = fs.lstatSync(inputBatchDirPath).isDirectory();
if (isDir) {
console.log("Converting batch: " + inputBatchDirPath);
const outputBatchDirPath = path.join(convertedProjectsDir, inputBatchDir);
if (fs.existsSync(outputBatchDirPath)) {
fs.rmSync(outputBatchDirPath, {recursive: true, force: true});
}
fs.mkdirSync(outputBatchDirPath);
const inputFiles = await fs.promises.readdir(inputBatchDirPath);
// const attributionPath = `${__dirname}/${path.join(inputBatchDirPath, `attribution.json`)}`;
// const attributionData = JSON.parse(fs.readFileSync(attributionPath));
const convertedModelSetDir = `${outputBatchDirPath}/models/`;
if (fs.existsSync(convertedModelSetDir)) {
fs.rmSync(convertedModelSetDir, {recursive: true, force: true});
}
fs.mkdirSync(convertedModelSetDir);
for (const inputFile of inputFiles) { // foo.ifc, bar.ifc
const ext = inputFile.split('.').pop();
if (ext !== "ifc") {
continue;
}
console.log("Converting file: " + inputFile);
const inputFileName = path.parse(inputFile).name;
const ifcInputPath = `${__dirname}/${path.join(inputBatchDirPath, inputFile)}`;
const convertedModelDir = `${outputBatchDirPath}/models/${inputFileName}/`;
const modelOutPath = convertedModelDir;
const glbOutputPath = path.join(modelOutPath, `model.gltf`);
const jsonOutputPath = path.join(modelOutPath, `model.json`);
const logPath = path.join(modelOutPath, `log.txt`);
const jsonManifestPath = path.join(modelOutPath, `model.manifest.json`);
const xktManifestPath = path.join(modelOutPath, `model.xkt.manifest.json`);
if (fs.existsSync(convertedModelDir)) {
fs.rmSync(convertedModelDir, {recursive: true, force: true});
}
fs.mkdirSync(convertedModelDir);
if (fs.existsSync(modelOutPath)) {
fs.rmSync(modelOutPath, {recursive: true, force: true});
}
fs.mkdirSync(modelOutPath);
ifcSizes[ifcInputPath] = (getFileSize(ifcInputPath) / 1000000).toFixed(4);
const startTime = performance.now();
try {
const ifc2gltfCmd = `${configs.ifc2gltf.path}` +
` -i ${ifcInputPath}` +
` -o ${glbOutputPath}` +
` -m ${jsonOutputPath}` +
` -s ${configs.ifc2gltf.options.s}` +
` -t ${configs.ifc2gltf.options.t}` +
` -e 3 >> ${logPath}`;
console.log("ifc2gltfCmd = " + ifc2gltfCmd);
// Example of this commend:
// ifc2gltfcxconverter -i Clinic_A_20110906_optimized.ifc -o model.glb -m model.json -s 5 -t 200 -e 3 >> log.txt
fs.appendFileSync(logPath, ifc2gltfCmd + "\n");
bashScript.push(`echo "${ifc2gltfCmd}"`);
bashScript.push(ifc2gltfCmd);
const convert2xktCmd = `node --max-old-space-size=14000` +
` ${configs.convert2xkt.path}/convert2xkt.js -t ` +
` -c ${configs.convert2xkt.path}/convert2xkt.conf.json ` +
` -a ${jsonManifestPath} ` +
` -o ${xktManifestPath} ` +
` -l >> ${logPath}`;
console.log("convert2xktCmd = " + convert2xktCmd);
fs.appendFileSync(logPath, convert2xktCmd + "\n\n");
bashScript.push(`echo "${convert2xktCmd}"`);
bashScript.push(convert2xktCmd);
// xktSizes[ifcInputPath] = (getXKTSize(modelOutPath, `model.xkt.manifest.json`) / 1000000).toFixed(4);
console.log("xktSize = " + xktSizes[ifcInputPath])
// const glbManifest = JSON.parse(fs.readFileSync(jsonManifestPath));
// const batchId = inputBatchDir;
// const modelId = path.parse(inputFile).name;
//
// kdTree.addModel(glbManifest, batchId, modelId);
} catch (e) {
console.log(e)
xktSizes[ifcInputPath] = 0;
errors[ifcInputPath] = e;
continue;
}
const endTime = performance.now();
conversionTimes[ifcInputPath] = ((endTime - startTime) / 1000).toFixed(2);
// ifcAttributionPaths[ifcInputPath] = attributionData.link;
}
}
}
// fs.writeFileSync("./convertFiles.sh", script.join("\n\n"), {encoding: 'utf8'});
//
// console.log("Converting files...");
//
function getFileSize(filePath) {
try {
const stats = fs.statSync(filePath);
return stats.size; // Size in bytes
} catch (error) {
// console.error(`Error getting file size: ${error.message}`);
// return null;
return 0;
}
}
function getXKTSize(baseDir, manifestFilePath) {
try {
const manifest = fs.readFileSync(baseDir + "/" + manifestFilePath, 'utf8');
const data = JSON.parse(manifest);
let fileSize = 0;
for (let i = 0, len = data.xktFiles.length; i < len; i++) {
// fileSize += getFileSize(baseDir + "/" + data.xktFiles[i]);
}
return fileSize;
} catch (error) {
console.error(`Error loading JSON: ${error.message}`);
return 0;
// throw error;
}
}
console.log("Writing HTML test page..");
const conversionResultsHTML = [];
const convertedModelsIndex = {
batches: {}
};
conversionResultsHTML.push(`<section class="test-results-section">
<div class="container pt-6 pb-0">
<div class="row">
<div class="col-lg-12">
<table class="table table-sm table-hover table-bordered mb-0">
<tr style="background-color: rgba(0, 0, 0, 0.05);"><th>IFC File</th><th style="text-align:right;">IFC Size (Mb)</th><th style="text-align:right;">XKT Size (Mb)</th><th style="text-align:right;">Conversion Time (Secs)</th><th style="text-align:center;" colspan="5">Converted XKT</th></tr>
<tbody>`);
for (const inputBatchDir of inputBatchDirs) { // BIMData, IfcOpenShell etc
if (inputBatchDir.startsWith("_")) {
continue;
}
const inputBatchDirPath = path.join(inputFilesDir, inputBatchDir);
const isDir = fs.lstatSync(inputBatchDirPath).isDirectory();
if (isDir) {
const batchData = {
models: {}
}
const projectIndex = {
id: `${inputBatchDir}`,
name: `${inputBatchDir}`,
models: [],
viewerConfigs: {
backgroundColor: [
0.95,
0.95,
1.0
]
},
viewerContent: {
modelsLoaded: []
},
viewerState: {
tabOpen: "models",
expandObjectsTree: 3,
expandClassesTree: 1,
expandStoreysTree: 1,
saoEnabled: "true"
}
};
convertedModelsIndex.batches[inputBatchDir] = batchData;
const outputBatchDirPath = path.join(convertedProjectsDir, inputBatchDir);
conversionResultsHTML.push(`<tr style="height: 60px; vertical-align:bottom; background-color: rgba(0, 0, 0, 0.05);"><td style="font-size: larger; height: 60px; vertical-align:bottom;" ><b>${inputBatchDir}</td><td></td><td></td><td></td><td colspan="5"></td></tr>`);
const inputFiles = await fs.promises.readdir(inputBatchDirPath);
for (const inputFile of inputFiles) { // foo.ifc, bar.ifc
const ext = inputFile.split('.').pop();
if (ext !== "ifc") {
continue;
}
console.log("Converting file: " + inputFile);
const inputFileName = path.parse(inputFile).name;
const ifcInputPath = `${__dirname}/${path.join(inputBatchDirPath, inputFile)}`;
const modelConvertedModelsDirPath = `${outputBatchDirPath}/models/${inputFileName}/`;
const community1Path = `${modelConvertedModelsDirPath}/ifcCommunityPipeline1`;
const glbCommunity1Path = path.join(community1Path, `model.glb`);
const glbCommunity1PathAbs = `${__dirname}/${glbCommunity1Path}`;
const jsonCommunity1Path = path.join(community1Path, `model.json`);
const jsonCommunity1PathAbs = `${__dirname}/${jsonCommunity1Path}`;
const xktCommunity1Path = path.join(community1Path, `model.xkt`);
const xktCommunity1PathAbs = `${__dirname}/${xktCommunity1Path}`;
const logCommunity1Path = path.join(community1Path, `log.txt`);
const logCommunity1PathAbs = `${__dirname}/${logCommunity1Path}`;
const summaryCommunity1Path = path.join(community1Path, `summary.json`);
const summaryCommunity1PathAbs = `${__dirname}/${summaryCommunity1Path}`;
const modelOutPath = modelConvertedModelsDirPath;
const glbOutputPath = path.join(modelOutPath, `model.glb`);
const glbOutputPathAbs = `${__dirname}/${glbOutputPath}`;
const jsonOutputPath = path.join(modelOutPath, `model.json`);
const jsonOutputPathAbs = `${__dirname}/${jsonOutputPath}`;
const xktEnterprise1Path = path.join(modelOutPath, `model.xkt`);
const xktEnterprise1PathAbs = `${__dirname}/${xktEnterprise1Path}`;
const logEnterprise1Path = path.join(modelOutPath, `log.txt`);
const logPath = `${__dirname}/${logEnterprise1Path}`;
const jsonManifestPath = path.join(modelOutPath, `model.glb.manifest.json`);
const xktManifestPath = path.join(modelOutPath, `model.xkt.manifest.json`);
if (errors[ifcInputPath]) {
// TODO
console.log(errors[ifcInputPath])
} else {
conversionResultsHTML.push(`@@include('../../_includes/ifc/ifc-modelConversionResults.html', {
"batchId": "${inputBatchDir}",
"modelId": "${inputFileName}",
"sourceFileSize": "${ifcSizes[ifcInputPath]}",
"xktFileSize":"${xktSizes[ifcInputPath]}",
"conversionTime": "${conversionTimes[ifcInputPath]}",
"attributionPath": "",
"logPath": "${convertedModelsDir}/projects/${inputBatchDir}/models/${inputFileName}/log.txt",
"modelLinkPath": "${convertedModelsDir}/projects/${inputBatchDir}/models/${inputFileName}/model.xkt.manifest.json"
})`);
batchData.models[inputFileName] = {
pipelineId: "CxConverter1",
batchId: inputBatchDir,
modelId: inputFileName,
ifcFileSize: ifcSizes[ifcInputPath],
xktFileSize: xktSizes[ifcInputPath],
conversionTime: conversionTimes[ifcInputPath],
manifestPath: `./convertedModels/ifc/projects/${inputBatchDir}/models/${inputFileName}/model.xkt.manifest.json`,
logPath: `./convertedModels/ifc/projects/${inputBatchDir}/models/${inputFileName}/log.txt`
}
projectIndex.models.push({
id: `${inputFileName}`,
name: `${inputFileName}`,
manifest: "model.xkt.manifest.json"
});
}
}
fs.writeFileSync(`${outputBatchDirPath}/index.json`, JSON.stringify(projectIndex), {encoding: 'utf8'});
}
}
conversionResultsHTML.push(`</tbody></table></div></div></div></section>`);
fs.writeFileSync("./_includes/ifc/ifc-conversionResults.html", conversionResultsHTML.join("\n"), {encoding: 'utf8'});
fs.writeFileSync("./convertedModels/ifc/systemInfo.json", JSON.stringify(systemInfo), {encoding: 'utf8'});
fs.writeFileSync("./convertedModels/ifc/index.json", JSON.stringify(convertedModelsIndex), {encoding: 'utf8'});
fs.writeFileSync("./convertedModels/ifc/kdtree.json", JSON.stringify(kdTree.root), {encoding: 'utf8'});
fs.copyFileSync(`${configs.convert2xkt.path}/convert2xkt.conf.json`, `./convertedModels/ifc/convert2xkt.conf.json`);
console.log("HTML test page written.");
fs.writeFileSync("./convert-ifc.sh", bashScript.join("\n"), {encoding: 'utf8'});
} catch (e) {
console.error("[Error] ", e);
}
})();