poly-mech/cad/drawers/tools/sw/cli/cli.js
2025-07-04 18:21:52 +02:00

100 lines
3.2 KiB
JavaScript

#!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { spawn } = require('child_process');
const path = require('path');
// --- Command-Line Interface Definition ---
const argv = yargs(hideBin(process.argv))
.command(
'$0 [output]',
'Generate a parametric sheet metal box in SolidWorks',
(yargs) => {
return yargs
.positional('output', {
describe: 'Absolute path to the output .SLDPRT file. Defaults to a file in your Documents folder.',
default: path.join(require('os').homedir(), 'Documents', 'sw-generated-box.SLDPRT')
});
}
)
.option('width', {
alias: 'w',
type: 'number',
description: 'Total width of the box',
default: 500
})
.option('length', {
alias: 'l',
type: 'number',
description: 'Total length of the box',
default: 500
})
.option('height', {
alias: 'h',
type: 'number',
description: 'Height of the box walls',
default: 80
})
.option('thickness', {
alias: 't',
type: 'number',
description: 'Sheet metal thickness',
default: 3
})
.option('radius', {
alias: 'r',
type: 'number',
description: 'Bend radius for sheet metal',
default: 1
})
.help()
.argv;
// --- Constructing the Arguments for the C# Application ---
// Path to the C# executable. We assume it's in './sw/box/bin/Debug/net48/SolidWorksBoxGenerator.exe'
// You may need to adjust the '.net48' folder depending on your target framework version.
const generatorExePath = path.resolve(__dirname, '..', 'box', 'bin', 'Debug', 'net48', 'SolidWorksBoxGenerator.exe');
// The C# app expects arguments in the format "--key value"
const generatorArgs = [
'--output', argv.output,
'--width', argv.width,
'--length', argv.length,
'--height', argv.height,
'--thickness', argv.thickness,
'--radius', argv.radius
];
// --- Executing the C# Application ---
console.log('Executing SolidWorks Box Generator...');
console.log(`Command: "${generatorExePath}" ${generatorArgs.join(' ')}`);
try {
const generatorProcess = spawn(generatorExePath, generatorArgs);
generatorProcess.stdout.on('data', (data) => {
// Trim whitespace from generator output for cleaner logging
process.stdout.write(`[Generator]: ${data.toString().trim()}\n`);
});
generatorProcess.stderr.on('data', (data) => {
console.error(`[Generator ERROR]: ${data}`);
});
generatorProcess.on('close', (code) => {
console.log(`\nGenerator process exited with code ${code}`);
if (code !== 0) {
console.error("Box generation failed. Ensure SolidWorks is running.");
} else {
console.log(`Box generation complete. File saved to: ${argv.output}`);
}
});
} catch (error) {
console.error(`Failed to start the generator process. Make sure the C# project has been built.`);
console.error(`Expected executable at: ${generatorExePath}`);
console.error(error);
}