geomcli 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1,17 @@
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Ignore files for PNPM, NPM and YARN
11
+ pnpm-lock.yaml
12
+ package-lock.json
13
+ yarn.lock
14
+
15
+ # ignore recommended by eslint
16
+ vitest.config.ts
17
+ dist/
package/.eslintrc.cjs ADDED
@@ -0,0 +1,24 @@
1
+ module.exports = {
2
+ extends: [
3
+ 'eslint:recommended',
4
+ 'plugin:@typescript-eslint/recommended-type-checked',
5
+ //'plugin:@typescript-eslint/strict-type-checked',
6
+ 'plugin:@typescript-eslint/stylistic-type-checked',
7
+ 'prettier'
8
+ ],
9
+ plugins: ['@typescript-eslint'],
10
+ parser: '@typescript-eslint/parser',
11
+ parserOptions: {
12
+ //sourceType: 'module',
13
+ //ecmaVersion: 2020,
14
+ project: true,
15
+ tsconfigRootDir: __dirname
16
+ },
17
+ root: true
18
+ //env: {
19
+ // browser: true,
20
+ // es2021: true,
21
+ // node: true
22
+ //},
23
+ //ignorePatterns: ['*.cjs'],
24
+ };
@@ -0,0 +1,15 @@
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /dist
5
+ /.svelte-kit
6
+ /package
7
+ .env
8
+ .env.*
9
+ !.env.example
10
+ README.md
11
+
12
+ # Ignore files for PNPM, NPM and YARN
13
+ pnpm-lock.yaml
14
+ package-lock.json
15
+ yarn.lock
package/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "useTabs": true,
3
+ "singleQuote": true,
4
+ "trailingComma": "none",
5
+ "printWidth": 100,
6
+ "plugins": [],
7
+ "overrides": []
8
+ }
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ README of geomcli
2
+ =================
3
+
4
+
5
+ Presentation
6
+ ------------
7
+
8
+ *geomcli* is the companion *typescript* library of *geometrix* for creating *nodejs* script or *cli*.
9
+
10
+ Getting started
11
+ ---------------
12
+
13
+ ```bash
14
+ git clone https://github.com/charlyoleg2/parametrix
15
+ cd parametrix
16
+ npm install
17
+ npm ci
18
+ npm -w design-script run run
19
+ ```
20
+
21
+
22
+
23
+
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "geomcli",
3
+ "version": "0.5.5",
4
+ "description": "the nodejs companion library of geometrix",
5
+ "private": false,
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/charlyoleg2/parametrix.git"
9
+ },
10
+ "homepage": "https://github.com/charlyoleg2/parametrix#readme",
11
+ "keywords": [
12
+ "cad",
13
+ "programmatic",
14
+ "2D",
15
+ "circle",
16
+ "stroke",
17
+ "arc",
18
+ "gear"
19
+ ],
20
+ "author": "charlyoleg",
21
+ "tsup": {
22
+ "entry": [
23
+ "src/index.ts"
24
+ ],
25
+ "format": "esm",
26
+ "splitting": false,
27
+ "dts": true,
28
+ "sourcemap": true,
29
+ "clean": true
30
+ },
31
+ "scripts": {
32
+ "dev": "tsup --watch",
33
+ "build": "tsup",
34
+ "check": "tsc --noEmit",
35
+ "pretty": "prettier --check .",
36
+ "format": "prettier --write .",
37
+ "lint": "eslint .",
38
+ "test:unit": "vitest",
39
+ "test:unit:once": "vitest --run",
40
+ "ci": "run-s check build pretty lint test:unit:once",
41
+ "clean": "shx rm -fr build dist node_modules"
42
+ },
43
+ "dependencies": {
44
+ "geometrix": "^0.5.1",
45
+ "yargs": "^17.7.2"
46
+ },
47
+ "devDependencies": {
48
+ "@types/yargs": "^17.0.32",
49
+ "@typescript-eslint/eslint-plugin": "^6.16.0",
50
+ "@typescript-eslint/parser": "^6.16.0",
51
+ "eslint": "^8.56.0",
52
+ "eslint-config-prettier": "^9.1.0",
53
+ "npm-run-all": "^4.1.5",
54
+ "prettier": "^3.1.1",
55
+ "shx": "^0.3.4",
56
+ "tsup": "^8.0.1",
57
+ "typescript": "^5.3.3",
58
+ "vitest": "^1.1.0"
59
+ },
60
+ "exports": {
61
+ ".": {
62
+ "types": "./dist/index.d.ts",
63
+ "default": "./dist/index.js"
64
+ }
65
+ },
66
+ "types": "dist/index.d.ts",
67
+ "type": "module"
68
+ }
@@ -0,0 +1,550 @@
1
+ // geom_cli.ts
2
+
3
+ import type { tParamVal, tGeom, tSubDesign, tPageDef, tAllPageDef, tSubInst } from 'geometrix';
4
+ import { PType, EFormat, designParam, prefixLog, paramListToVal } from 'geometrix';
5
+ import { geom_write, writeParams, readParams } from './geom_write';
6
+ import yargs from 'yargs';
7
+ import { hideBin } from 'yargs/helpers';
8
+ import { version } from '../package.json';
9
+
10
+ function get_design_array(dList: tAllPageDef): string[] {
11
+ const rDesignArray = Object.keys(dList);
12
+ return rDesignArray;
13
+ }
14
+
15
+ function selectDesign(dList: tAllPageDef, selD: string): tPageDef {
16
+ if (!Object.keys(dList).includes(selD)) {
17
+ console.log(`err918: design ${selD} is not defined`);
18
+ process.exit(1);
19
+ }
20
+ return dList[selD];
21
+ }
22
+
23
+ function selectDesignN(dList: tAllPageDef, selD: string): string {
24
+ const theD = selectDesign(dList, selD);
25
+ const dName = theD.pDef.partName;
26
+ return dName;
27
+ }
28
+
29
+ function parseModif(modif: string[], printLog: boolean): tParamVal {
30
+ const pVal: tParamVal = {};
31
+ const arrayLen = modif.length;
32
+ if (arrayLen % 2 === 1) {
33
+ throw `err903: length ${arrayLen} of modif string array is odd!`;
34
+ }
35
+ for (let i = 0; i < arrayLen / 2; i++) {
36
+ const valStr = modif[2 * i + 1];
37
+ const val = parseFloat(valStr);
38
+ if (isNaN(val)) {
39
+ throw `err908: ${valStr} is not a number!`;
40
+ }
41
+ pVal[modif[2 * i]] = val;
42
+ }
43
+ const pValLen = Object.keys(pVal).length;
44
+ if (printLog && pValLen > 0) {
45
+ const rlog = `info308: ${pValLen} parameters of modifier`;
46
+ console.log(rlog);
47
+ }
48
+ return pVal;
49
+ }
50
+
51
+ function computeGeom(
52
+ dList: tAllPageDef,
53
+ selD: string,
54
+ paramPath: string,
55
+ modif: string[],
56
+ printLog: boolean
57
+ ): tGeom {
58
+ const theD = selectDesign(dList, selD);
59
+ let rlog = `Compute design ${selD} (${theD.pDef.partName}):\n`;
60
+ const dParam = designParam(theD.pDef);
61
+ try {
62
+ dParam.applyParamVal(readParams(paramPath, printLog));
63
+ dParam.applyParamVal(parseModif(modif, printLog));
64
+ } catch (emsg) {
65
+ console.log('err271: error while applying new parameters');
66
+ console.log(emsg);
67
+ process.exit(1);
68
+ }
69
+ const simtime = 0;
70
+ const dGeom = theD.pGeom(simtime, dParam.getParamVal());
71
+ //checkGeom(dGeom);
72
+ rlog += prefixLog(dGeom.logstr, dParam.partName);
73
+ if (dGeom.calcErr) {
74
+ rlog += `err907: Error while computing ${theD.pDef.partName}\n`;
75
+ console.log(rlog);
76
+ process.exit(1);
77
+ } else {
78
+ rlog += `${theD.pDef.partName} successfully computed\n`;
79
+ }
80
+ if (printLog) {
81
+ console.log(rlog);
82
+ }
83
+ return dGeom;
84
+ }
85
+
86
+ function get_figure_array(
87
+ dList: tAllPageDef,
88
+ selD: string,
89
+ paramPath: string,
90
+ modif: string[]
91
+ ): string[] {
92
+ const dGeom = computeGeom(dList, selD, paramPath, modif, false);
93
+ const rfigN = Object.keys(dGeom.fig);
94
+ return rfigN;
95
+ }
96
+
97
+ function get_subdesign_array(
98
+ dList: tAllPageDef,
99
+ selD: string,
100
+ paramPath: string,
101
+ modif: string[]
102
+ ): tSubDesign {
103
+ const dGeom = computeGeom(dList, selD, paramPath, modif, false);
104
+ const subd = dGeom.sub;
105
+ return subd;
106
+ }
107
+
108
+ function get_subd(
109
+ dList: tAllPageDef,
110
+ selD: string,
111
+ subdN: string,
112
+ paramPath: string,
113
+ modif: string[],
114
+ printLog: boolean
115
+ ): tSubInst {
116
+ const theD = selectDesign(dList, selD);
117
+ const dGeom = computeGeom(dList, selD, paramPath, modif, printLog);
118
+ if (!Object.keys(dGeom.sub).includes(subdN)) {
119
+ console.log(`err207: sub-design ${subdN} not defined in partName ${theD.pDef.partName}`);
120
+ process.exit(1);
121
+ }
122
+ const rSubd = dGeom.sub[subdN];
123
+ if (printLog) {
124
+ const rlog = `Subdesign ${subdN} (${rSubd.partName}) of ${selD} (${theD.pDef.partName}):\n`;
125
+ console.log(rlog);
126
+ }
127
+ return rSubd;
128
+ }
129
+
130
+ const c_fileFormat = [
131
+ 'json_param',
132
+ 'svg_all_figures',
133
+ 'dxf_all_figures',
134
+ 'pax_all',
135
+ 'scad_3d_openscad',
136
+ 'js_3d_openjscad',
137
+ 'zip_all'
138
+ ];
139
+
140
+ function get_outopt_array(
141
+ dList: tAllPageDef,
142
+ selD: string,
143
+ paramPath: string,
144
+ modif: string[]
145
+ ): string[] {
146
+ const rOutOpt: string[] = [];
147
+ const figN = get_figure_array(dList, selD, paramPath, modif);
148
+ const subdN = Object.keys(get_subdesign_array(dList, selD, paramPath, modif));
149
+ for (const figNi of figN) {
150
+ rOutOpt.push(`svg__${figNi}`);
151
+ }
152
+ for (const figNi of figN) {
153
+ rOutOpt.push(`dxf__${figNi}`);
154
+ }
155
+ for (const subdNi of subdN) {
156
+ rOutOpt.push(`json_sub_param_${subdNi}`);
157
+ }
158
+ for (const ffi of c_fileFormat) {
159
+ rOutOpt.push(`${ffi}`);
160
+ }
161
+ return rOutOpt;
162
+ }
163
+
164
+ enum EWrite {
165
+ eEGOPARAMS,
166
+ eSUBDPARAMS,
167
+ eOTHERS
168
+ }
169
+
170
+ interface tEFormat {
171
+ eWrite: EWrite;
172
+ eFormat: EFormat;
173
+ eFace: string;
174
+ eSubdesign: string;
175
+ }
176
+
177
+ function decompose_outopt(outopt: string): tEFormat {
178
+ let rWrite = EWrite.eOTHERS;
179
+ let rFormat = EFormat.ePAX;
180
+ let rFace = 'all';
181
+ let rSubD = '';
182
+ const reSvg = /^svg__/;
183
+ const reDxf = /^dxf__/;
184
+ const reSubP = /^json_sub_param_/;
185
+ if (outopt.match(reSvg)) {
186
+ rFace = outopt.replace(reSvg, '');
187
+ rFormat = EFormat.eSVG;
188
+ rWrite = EWrite.eOTHERS;
189
+ } else if (outopt.match(reDxf)) {
190
+ rFace = outopt.replace(reDxf, '');
191
+ rFormat = EFormat.eDXF;
192
+ rWrite = EWrite.eOTHERS;
193
+ } else if (outopt.match(reSubP)) {
194
+ rSubD = outopt.replace(reSubP, '');
195
+ rWrite = EWrite.eSUBDPARAMS;
196
+ } else {
197
+ switch (outopt) {
198
+ case 'json_param':
199
+ rWrite = EWrite.eEGOPARAMS;
200
+ break;
201
+ case 'svg_all_figures':
202
+ rFormat = EFormat.eSVGALL;
203
+ rWrite = EWrite.eOTHERS;
204
+ break;
205
+ case 'dxf_all_figures':
206
+ rFormat = EFormat.eDXFALL;
207
+ rWrite = EWrite.eOTHERS;
208
+ break;
209
+ case 'pax_all':
210
+ rFormat = EFormat.ePAX;
211
+ rWrite = EWrite.eOTHERS;
212
+ break;
213
+ case 'scad_3d_openscad':
214
+ rFormat = EFormat.eOPENSCAD;
215
+ rWrite = EWrite.eOTHERS;
216
+ break;
217
+ case 'js_3d_openjscad':
218
+ rFormat = EFormat.eJSCAD;
219
+ rWrite = EWrite.eOTHERS;
220
+ break;
221
+ case 'zip_all':
222
+ rFormat = EFormat.eZIP;
223
+ rWrite = EWrite.eOTHERS;
224
+ break;
225
+ default:
226
+ rFormat = EFormat.ePAX;
227
+ rWrite = EWrite.eOTHERS;
228
+ }
229
+ }
230
+ const eFormat: tEFormat = { eWrite: rWrite, eFormat: rFormat, eFace: rFace, eSubdesign: rSubD };
231
+ return eFormat;
232
+ }
233
+
234
+ function list_designs(dList: tAllPageDef, detail: boolean) {
235
+ let rlog = 'List of available designs:\n';
236
+ for (const [idx, dname] of get_design_array(dList).entries()) {
237
+ rlog += `${(idx + 1).toString().padStart(4, ' ')} : ${dname}\n`;
238
+ if (detail) {
239
+ rlog += ` ${dList[dname].pDef.partName}\n`;
240
+ rlog += ` ${dList[dname].pTitle}\n`;
241
+ rlog += ` ${dList[dname].pDescription}\n`;
242
+ }
243
+ }
244
+ console.log(rlog);
245
+ }
246
+
247
+ function list_parameters(dList: tAllPageDef, selD: string, paramPath: string, modif: string[]) {
248
+ const theD = selectDesign(dList, selD);
249
+ let rlog = `List of parameters of the design ${selD} (${theD.pDef.partName}):\n`;
250
+ const dParam = designParam(theD.pDef);
251
+ try {
252
+ dParam.applyParamVal(readParams(paramPath, true));
253
+ dParam.applyParamVal(parseModif(modif, true));
254
+ } catch (emsg) {
255
+ console.log('err272: error while applying new parameters');
256
+ console.log(emsg);
257
+ process.exit(1);
258
+ }
259
+ const paramVal = dParam.getParamVal();
260
+ const nameLength = 20;
261
+ const unitLength = 8;
262
+ const nameLabel = 'name'.padEnd(nameLength, ' ');
263
+ const unitLabel = 'unit'.padEnd(unitLength, ' ');
264
+ rlog += ` # : ${nameLabel} current ${unitLabel} init min max step\n`;
265
+ for (const [idx, pa] of theD.pDef.params.entries()) {
266
+ const idx2 = (idx + 1).toString().padStart(4, ' ');
267
+ const pname = pa.name.padEnd(nameLength, ' ');
268
+ const pcurr = paramVal[pa.name];
269
+ const pcurrP = pcurr.toString().padStart(6, ' ');
270
+ const punit = pa.unit.padEnd(unitLength, ' ');
271
+ const pinit = pa.init.toString().padStart(6, ' ');
272
+ switch (pa.pType) {
273
+ case PType.eCheckbox:
274
+ rlog += `${idx2} : ${pname} checkbox ${pcurr} ${pa.init}\n`;
275
+ break;
276
+ case PType.eDropdown:
277
+ rlog += `${idx2} : ${pname} ${pcurr} ${pa.init}`;
278
+ for (const [optI, optN] of pa.dropdown.entries()) {
279
+ rlog += ` ${optI}:${optN}`;
280
+ }
281
+ rlog += '\n';
282
+ break;
283
+ default:
284
+ rlog += `${idx2} : ${pname} ${pcurrP} ${punit} ${pinit} ${pa.min} ${pa.max} ${pa.step}\n`;
285
+ }
286
+ }
287
+ console.log(rlog);
288
+ }
289
+
290
+ function list_figures(dList: tAllPageDef, selD: string, paramPath: string, modif: string[]) {
291
+ const dPartName = selectDesignN(dList, selD);
292
+ const figN = get_figure_array(dList, selD, paramPath, modif);
293
+ let rlog = `List of figures of the design ${selD} (${dPartName}):\n`;
294
+ for (const [idx, figNi] of figN.entries()) {
295
+ const idx2 = (idx + 1).toString().padStart(4, ' ');
296
+ rlog += `${idx2} : ${figNi}\n`;
297
+ }
298
+ console.log(rlog);
299
+ }
300
+
301
+ function list_subdesigns(dList: tAllPageDef, selD: string, paramPath: string, modif: string[]) {
302
+ const dPartName = selectDesignN(dList, selD);
303
+ const subdA = get_subdesign_array(dList, selD, paramPath, modif);
304
+ const subdN = Object.keys(subdA);
305
+ let rlog = `List of sub-designs of the design ${selD} (${dPartName}):\n`;
306
+ for (const [idx, subdNi] of subdN.entries()) {
307
+ const idx2 = (idx + 1).toString().padStart(4, ' ');
308
+ const subd = subdA[subdNi];
309
+ const ori = `[ ${subd.orientation[0]}, ${subd.orientation[1]}, ${subd.orientation[2]}]`;
310
+ const pos = `[ ${subd.position[0]}, ${subd.position[1]}, ${subd.position[2]}]`;
311
+ const subdNp = subdNi.padEnd(15, ' ');
312
+ const subdPp = subd.partName.padEnd(15, ' ');
313
+ rlog += `${idx2} : ${subdNp} ${subdPp} orientation: ${ori} position: ${pos}\n`;
314
+ }
315
+ console.log(rlog);
316
+ }
317
+
318
+ function list_subd_parameters(
319
+ dList: tAllPageDef,
320
+ selD: string,
321
+ subdN: string,
322
+ paramPath: string,
323
+ modif: string[]
324
+ ) {
325
+ const subdParam = get_subd(dList, selD, subdN, paramPath, modif, true).dparam;
326
+ const nameLength = 20;
327
+ const nameLabel = 'name'.padEnd(nameLength, ' ');
328
+ let rlog = ` # : ${nameLabel} value init changed\n`;
329
+ for (const [idx, ipaN] of Object.keys(subdParam).entries()) {
330
+ const idx2 = (idx + 1).toString().padStart(4, ' ');
331
+ const paN = ipaN.padEnd(nameLength, ' ');
332
+ const pa = subdParam[ipaN];
333
+ const paVal = pa.val.toString().padStart(6, ' ');
334
+ const paInit = pa.init.toString().padStart(6, ' ');
335
+ rlog += `${idx2} : ${paN} ${paVal} ${paInit} ${pa.chg ? 'changed' : ''}\n`;
336
+ }
337
+ console.log(rlog);
338
+ }
339
+
340
+ function list_outopt(dList: tAllPageDef, selD: string, paramPath: string, modif: string[]) {
341
+ const dPartName = selectDesignN(dList, selD);
342
+ let rlog = `List of outputs of the design ${selD} (${dPartName}):\n`;
343
+ const outOpt = get_outopt_array(dList, selD, paramPath, modif);
344
+ for (const [idx, oneOpt] of outOpt.entries()) {
345
+ const idx2 = (idx + 1).toString().padStart(4, ' ');
346
+ rlog += `${idx2} : ${oneOpt}\n`;
347
+ }
348
+ console.log(rlog);
349
+ }
350
+
351
+ let cmd_write = false;
352
+ async function geom_cli(iArgs: string[], dList: tAllPageDef, outDir = 'output') {
353
+ const argv = yargs(hideBin(iArgs))
354
+ .scriptName('geom_cli')
355
+ .version(version)
356
+ .usage('Usage: $0 <global-options> command <command-argument>')
357
+ .example([
358
+ ['$0 list-designs', 'list the available designs'],
359
+ ['$0 list-designs-detailed', 'list the available designs with detailed information'],
360
+ ['$0 -d heliostat/rake compute-log', 'compute and print the log'],
361
+ ['$0 -d heliostat/swing list-outopt', 'list possible output-format-options'],
362
+ ['$0 -d heliostat/rod write zip_all', 'write a zip file']
363
+ ])
364
+ .option('design', {
365
+ alias: 'd',
366
+ type: 'string',
367
+ description: 'design to be used by the command',
368
+ default: ''
369
+ })
370
+ .option('param', {
371
+ alias: 'p',
372
+ type: 'string',
373
+ array: false,
374
+ description: 'path to the input parameter file',
375
+ default: ''
376
+ })
377
+ .option('modif', {
378
+ alias: 'm',
379
+ nargs: 2,
380
+ type: 'string',
381
+ description: 'modify parameter values <paramName> <paramValue>',
382
+ default: ''
383
+ })
384
+ .option('outDir', {
385
+ alias: 'o',
386
+ type: 'string',
387
+ description: 'the path of the directory where to write the output files',
388
+ default: outDir
389
+ })
390
+ .option('outFileName', {
391
+ type: 'string',
392
+ description: 'Rename the output filename',
393
+ default: ''
394
+ })
395
+ .command(['list-designs', 'list'], 'list the available designs', {}, () => {
396
+ list_designs(dList, false);
397
+ })
398
+ .command('list-designs-detailed', 'list the available designs with details', {}, () => {
399
+ list_designs(dList, true);
400
+ })
401
+ .command('list-parameters', 'list the parameters of the selected design', {}, (argv) => {
402
+ //console.log(argv)
403
+ list_parameters(
404
+ dList,
405
+ argv.design as string,
406
+ argv.param as string,
407
+ argv.modif as string[]
408
+ );
409
+ })
410
+ .command('list-figures', 'list the figures of the selected design', {}, (argv) => {
411
+ list_figures(
412
+ dList,
413
+ argv.design as string,
414
+ argv.param as string,
415
+ argv.modif as string[]
416
+ );
417
+ })
418
+ .command('list-subdesigns', 'list the subdesigns of the selected design', {}, (argv) => {
419
+ list_subdesigns(
420
+ dList,
421
+ argv.design as string,
422
+ argv.param as string,
423
+ argv.modif as string[]
424
+ );
425
+ })
426
+ .command(
427
+ 'list-subd-parameters <subdN>',
428
+ 'list the parameters of subdesigns',
429
+ {},
430
+ (argv) => {
431
+ list_subd_parameters(
432
+ dList,
433
+ argv.design as string,
434
+ argv.subdN as string,
435
+ argv.param as string,
436
+ argv.modif as string[]
437
+ );
438
+ }
439
+ )
440
+ .command('compute-log', 'Compute and print the log without writing file', {}, (argv) => {
441
+ computeGeom(
442
+ dList,
443
+ argv.design as string,
444
+ argv.param as string,
445
+ argv.modif as string[],
446
+ true
447
+ );
448
+ })
449
+ .command(
450
+ 'list-outopt',
451
+ 'list the possible output format options of the selected design',
452
+ {},
453
+ (argv) => {
454
+ list_outopt(
455
+ dList,
456
+ argv.design as string,
457
+ argv.param as string,
458
+ argv.modif as string[]
459
+ );
460
+ }
461
+ )
462
+ .command('write <outopt>', 'write the output format file', {}, () => {
463
+ cmd_write = true;
464
+ })
465
+ .demandCommand(1)
466
+ .help()
467
+ .strict()
468
+ .parseSync();
469
+ //console.log(argv.$0);
470
+ //console.log(argv.design);
471
+ //console.log(argv.param);
472
+ //console.log(argv.modif);
473
+ //console.log(argv.outDir);
474
+ //console.log(argv);
475
+ if (cmd_write) {
476
+ const iOutDir = argv.outDir;
477
+ if (iOutDir === '') {
478
+ console.log("err638: option 'outDir' is set to empty string. Nothing written!");
479
+ process.exit(1);
480
+ }
481
+ const selD = argv.design;
482
+ const outopt = argv.outopt as string;
483
+ const paramPath = argv.param;
484
+ const paramModif = argv.modif as unknown as string[];
485
+ const theD = selectDesign(dList, selD);
486
+ // check if outopt is valid
487
+ const outOpt = get_outopt_array(dList, selD, paramPath, paramModif);
488
+ if (!outOpt.includes(outopt)) {
489
+ console.log(`err639: outopt ${outopt} is not a valid option`);
490
+ process.exit(1);
491
+ }
492
+ // end of check of outopt
493
+ //let rlog = `Write ${outopt} of ${selD} (${theD.pDef.partName}):\n`;
494
+ let rlog = '';
495
+ const oOpt = decompose_outopt(outopt);
496
+ const dParam = designParam(theD.pDef);
497
+ try {
498
+ dParam.applyParamVal(readParams(paramPath, false));
499
+ dParam.applyParamVal(parseModif(paramModif, false));
500
+ } catch (emsg) {
501
+ console.log('err273: error while applying new parameters');
502
+ console.log(emsg);
503
+ process.exit(1);
504
+ }
505
+ computeGeom(dList, selD, paramPath, paramModif, true);
506
+ try {
507
+ if (oOpt.eWrite === EWrite.eEGOPARAMS) {
508
+ rlog += writeParams(
509
+ dParam.partName,
510
+ dParam.getParamVal(),
511
+ iOutDir,
512
+ argv.outFileName
513
+ );
514
+ } else if (oOpt.eWrite === EWrite.eSUBDPARAMS) {
515
+ const subD = get_subd(dList, selD, oOpt.eSubdesign, paramPath, paramModif, false);
516
+ rlog += writeParams(
517
+ subD.partName,
518
+ paramListToVal(subD.dparam),
519
+ iOutDir,
520
+ argv.outFileName
521
+ );
522
+ } else {
523
+ const simtime = 0;
524
+ rlog += await geom_write(
525
+ dParam.partName,
526
+ theD.pGeom,
527
+ simtime,
528
+ dParam.getParamVal(),
529
+ oOpt.eFormat, // output-format
530
+ //EFormat.eSVG,
531
+ //EFormat.eDXF,
532
+ //EFormat.ePAX,
533
+ //EFormat.eOPENSCAD,
534
+ //EFormat.eJSCAD,
535
+ //EFormat.eZIP,
536
+ oOpt.eFace, // selected-2d-face
537
+ iOutDir, // output-directory
538
+ argv.outFileName // output-filename
539
+ );
540
+ }
541
+ } catch (emsg) {
542
+ console.log('err279: error while writing file');
543
+ console.log(emsg);
544
+ process.exit(1);
545
+ }
546
+ console.log(rlog);
547
+ }
548
+ }
549
+
550
+ export { geom_cli };
@@ -0,0 +1,136 @@
1
+ // geom_write.ts
2
+
3
+ import type { tGeomFunc, tParamVal } from 'geometrix';
4
+ import {
5
+ EFormat,
6
+ fileBinContent,
7
+ fileTextContent,
8
+ fileSuffix,
9
+ fileBin,
10
+ createParamFile,
11
+ parseParamFile
12
+ } from 'geometrix';
13
+ import fs from 'fs';
14
+
15
+ function dateString(): string {
16
+ const re1 = /[-:]/g;
17
+ const re2 = /\..*$/;
18
+ const rDateStr = new Date().toISOString().replace(re1, '').replace(re2, '').replace('T', '_');
19
+ return rDateStr;
20
+ }
21
+
22
+ function createDir(iDir: string): string {
23
+ let rlog = '';
24
+ if (!fs.existsSync(iDir)) {
25
+ fs.mkdirSync(iDir, { recursive: true });
26
+ rlog += `info203: mkdir ${iDir}\n`;
27
+ }
28
+ return rlog;
29
+ }
30
+
31
+ async function write_binFile(fName: string, fContent: Blob): Promise<string> {
32
+ let rlog = '';
33
+ const buffer = await fContent.arrayBuffer();
34
+ const arrBufView = new DataView(buffer);
35
+ fs.writeFileSync(fName, arrBufView);
36
+ rlog += `info304: bin-file ${fName} has been written\n`;
37
+ return rlog;
38
+ }
39
+
40
+ function write_textFile(fName: string, fContent: string): string {
41
+ let rlog = '';
42
+ fs.writeFileSync(fName, fContent);
43
+ rlog += `info405: text-file ${fName} has been written\n`;
44
+ return rlog;
45
+ }
46
+
47
+ function checkDirFName(iDir: string, fName: string): string {
48
+ const reSlash = /\//;
49
+ if (reSlash.test(fName)) {
50
+ throw `err932: the filename ${fName} contains a slash '/'`;
51
+ }
52
+ if (iDir === '') {
53
+ throw `err074: geom_write output-directory is an empty string!`;
54
+ }
55
+ const fName2 = `${iDir}/${fName}`;
56
+ return fName2;
57
+ }
58
+
59
+ function writeParams(
60
+ iPartName: string,
61
+ idparams: tParamVal,
62
+ oDir: string,
63
+ oFileName: string
64
+ ): string {
65
+ const re1 = /[-:]/g;
66
+ const re2 = /\..*$/;
67
+ const datestr = new Date().toISOString().replace(re1, '').replace(re2, '').replace('T', '_');
68
+ let file_name = `px_${iPartName}_${datestr}.json`;
69
+ if (oFileName !== '') {
70
+ file_name = oFileName;
71
+ }
72
+ const paramNb = Object.keys(idparams).length;
73
+ const fName2 = checkDirFName(oDir, file_name);
74
+ let rlog = `Write ${paramNb} parameters in file ${fName2}\n`;
75
+ const file_content = createParamFile(datestr, idparams, 'Written by geom_cli');
76
+ rlog += createDir(oDir);
77
+ write_textFile(fName2, file_content);
78
+ return rlog;
79
+ }
80
+
81
+ function readParams(paramPath: string, printLog: boolean): tParamVal {
82
+ let rParamVal: tParamVal = {};
83
+ if (paramPath !== '') {
84
+ let rlog = `Read parameter file ${paramPath}\n`;
85
+ if (!fs.existsSync(paramPath)) {
86
+ throw `err533: file ${paramPath} doesn't exist!`;
87
+ }
88
+ const fContentStr = fs.readFileSync(paramPath, 'utf8');
89
+ const [obj] = parseParamFile(fContentStr);
90
+ //const [obj, tlog] = parseParamFile(fContentStr);
91
+ //rlog += tlog;
92
+ rlog += `file lastModif: ${obj.lastModif}\n`;
93
+ rlog += `file comment: ${obj.comment}\n`;
94
+ rlog += `info307: ${Object.keys(obj.pVal).length} parameters from file`;
95
+ rParamVal = obj.pVal;
96
+ if (printLog) {
97
+ console.log(rlog);
98
+ }
99
+ }
100
+ return rParamVal;
101
+ }
102
+
103
+ async function geom_write(
104
+ iPartName: string,
105
+ fgeom: tGeomFunc,
106
+ simTime: number,
107
+ iParam: tParamVal,
108
+ iFormat: EFormat,
109
+ iFace = '',
110
+ iDir = '.',
111
+ iFname = ''
112
+ ): Promise<string> {
113
+ let rlog = '';
114
+ const fSuffix = fileSuffix(iFormat);
115
+ const fBin = fileBin(iFormat);
116
+ let nFace = 'all';
117
+ if (iFace !== '') {
118
+ nFace = iFace;
119
+ }
120
+ let fName = iFname;
121
+ if (fName === '') {
122
+ fName = iPartName + '_' + nFace + '_' + dateString() + fSuffix;
123
+ }
124
+ const fName2 = checkDirFName(iDir, fName);
125
+ rlog += createDir(iDir);
126
+ if (fBin) {
127
+ const fContent = await fileBinContent(fgeom, simTime, iParam, iFormat);
128
+ rlog += await write_binFile(fName2, fContent);
129
+ } else {
130
+ const fContent = fileTextContent(fgeom, iParam, nFace, iFormat);
131
+ rlog += write_textFile(fName2, fContent);
132
+ }
133
+ return rlog;
134
+ }
135
+
136
+ export { geom_write, writeParams, readParams };
@@ -0,0 +1,16 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { geom_cli } from './index';
3
+ import type { tAllPageDef } from 'geometrix';
4
+
5
+ import { heliostatDef, swingDef } from 'designix';
6
+
7
+ const designList: tAllPageDef = {
8
+ 'heliostat/heliostat': heliostatDef,
9
+ 'heliostat/swing': swingDef
10
+ };
11
+
12
+ describe('geomcli index suit', () => {
13
+ it('geom_cli', async () => {
14
+ await expect(geom_cli(['node', 'dummy', 'list'], designList)).resolves.toBeUndefined();
15
+ });
16
+ });
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ // geomcli index.ts
2
+
3
+ import { geom_write } from './geom_write';
4
+ import { geom_cli } from './geom_cli';
5
+
6
+ export { geom_write, geom_cli };
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "esnext",
5
+ "outDir": "./build",
6
+ "esModuleInterop": true,
7
+ "resolveJsonModule": true,
8
+ "moduleResolution": "bundler",
9
+ "declaration": true,
10
+ "sourceMap": true,
11
+ "strict": true
12
+ },
13
+ "include": ["./src"]
14
+ }
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ include: ['src/**/*.{test,spec}.{js,ts}']
6
+ }
7
+ });