var fs = require('fs'); var path = require('path'); const net = require('net'); var exec = require('child_process').exec; var ios = require('socket.io-client'); exports.acax_app = require('@acax/application'); const os = require('os'); const { spawn } = require('child_process'); const { fork } = require('child_process'); const { Readable } = require('stream'); const io = require('socket.io')(); const { execSync } = require('child_process'); var judge = 0; var setStartEnd = 0; var hash = []; var con = 0; var isBeam = false; const str01 = []; var nums = 0; var order = 0; var poi = 0; var mat = 0; var midu = 0; var pathStep = 0; var Bpp1 = 0; var Bpp2 = 0; var modelCode = 0; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Initialize codes. AutoLoadPlugins(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //You can load the plugins in global field, if the next line codes are uncommented. AutoLoadGlobalPlugins(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Sample codes. //写前处理文件 exports.writeFile=function(arr) { const filePath = unit.dir + 'data/preFiles.txt'; fs.writeFile(filePath, arr.join("\n"), (err) => { if (err) { console.error("写入失败:", err); return; } console.log("写入成功!"); }); }; //检查路径 exports.checkPath = function(folderPath, cb){ fs.readdir(folderPath, (err, items) => { if (err) { return console.error('无法读取文件夹:', err.message); } const directories = items.filter((item) => { const fullPath = path.join(folderPath, item); return fs.statSync(fullPath).isDirectory(); }); cb(directories); }); }; exports.setBpp1 = function(input){ Bpp1 = input; }; exports.getBpp1 = function(){ return Bpp1; }; exports.setBpp2 = function(input){ Bpp2 = input; }; exports.getBpp2 = function(){ return Bpp2; }; exports.getParallels = function(cb, cpuss){ let num_of_cpu_cores = os.cpus().length; let para_of_cpu_cores = os.availableParallelism(); let free_memory = os.freemem() / 1024 / 1024 / 1024; //GB let total_memory = os.totalmem() / 1024 / 1024 / 1024; //GB let cmd = 'wmic'; let opt = ['cpu', 'get', 'numberofcores']; let cp = require('child_process').spawn(cmd, opt); cp.stdout.on('data', data=>{ if(data.toString() !== '') { var res = data.toString().replace(/[ \r\t]/g, ''); if(cb){ //cb(res.split('\n')[1]); cb(`number of current computer cores: ${res.split('\n')[1]} memory status of current computer: ${free_memory.toFixed(1)}GB / ${total_memory.toFixed(1)}GB 请先保存文件后选择进程数并进行求解,模态分析模块点击模态分析进行求解!`); } } }); //cb(`number of current computer cores: ${para_of_cpu_cores} memory status of current computer: ${free_memory.toFixed(1)}GB / ${total_memory.toFixed(1)}GB`); //console.log(`number of current computer cores : ${num_of_cpu_cores}`); //console.log(`memory status of current computer: ${free_memory.toFixed(1)}GB / ${total_memory.toFixed(1)}GB`); }; var g_init = false; var g_cp_www_js = false; exports.startLocalhost = function(cb){ if(!g_init) { g_init = true; var spawn = require('child_process').spawn; var www_js = path.join(__dirname, '/../localhost/bin/www'); var cp = spawn('node', [www_js]); g_cp_www_js = cp; cp.stdout.on('data', data=>{ //console.log(data.toString()); }); } var client = ios("ws://localhost:4000"); client.on('connect', ()=>{ if(cb){cb();} client.disconnect(); }); }; exports.updateLineChart = function(dataLineChart){ var client = ios("ws://localhost:4000"); client.on('connect', ()=>{ client.emit('updateLineChart', dataLineChart); }); }; exports.closeLocalhost = function(){ if(g_cp_www_js) { process.kill(g_cp_www_js.pid); } }; exports.readFiles = function(path, res){ fs.readFile(path, 'utf8', (err, data) => { if (err) { console.error('Error reading file:', err); return; } // 正则表达式提取数值 const regex = /Eigenvalue \d+: ([\d.e+-]+)/g; let match; var values = []; while ((match = regex.exec(data)) !== null) { values.push(parseFloat(match[1])); } //return values; res = values; console.log('values Array:', values); }); }; exports.getNodePath = function(){ return require('path').join(unit.dir, '../../../node_portable').replace(/\\/g, '/') + '/node'; }; exports.getModelCode = function(){ return modelCode; }; exports.setModelCode = function(input){ modelCode = input; }; exports.getPicturePath = function(filename){ return path.join(unit.dir, 'picture', filename); }; exports.setStepPath = function(input){ pathStep = input; }; exports.getStepPath = function(){ return pathStep; }; exports.setMidu = function(input){ midu = input; }; exports.getMidu = function(){ return midu; }; exports.setMat = function(input){ mat = input; }; exports.getMat = function(){ return mat; }; exports.setPoi = function(input){ poi = input; }; exports.getPoi = function(){ return poi; }; exports.setOrder = function(input){ order = input; }; exports.getOrder = function(){ return order; }; exports.setNums = function(input){ nums = input; }; exports.getNums = function(){ return nums; }; exports.setStr = function(input){ str01.push(input); }; exports.getStr = function(input){ return str01; }; exports.clearStr = function(){ //str01 = []; }; exports.setBeam = function(input){ isBeam = input; }; exports.getBeam = function(input){ return isBeam; }; exports.setCon = function(input){ con = input; }; exports.getCon = function(){ return con; }; exports.setHash = function(input){ hash = input; }; exports.getHash = function(){ return hash; }; exports.Test = function() { console.log("Hi, I'm a model test funciton"); }; exports.getDefaultDataDir = ()=>{ return path.join(unit.dir, 'data'); }; exports.getExtDir = filename=>{ return path.extname(filename).toLowerCase(); }; exports.getBrepInfoContext = ()=>{ var brep_file = model.getDefaultDataDir() + '_temp.brep'; return fs.readFileSync(brep_file).toString(); }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Put you codes here exports.execs = function(path, cb) { exec(path, (error, stdout, stderr)=>{ if(error) { console.error(`exec error: ${error}`); if(cb){cb(false);} return; } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`); if(cb){cb(true);} }); /*try { // 执行同步的命令 const stdout = execSync(path).toString(); cb(stdout); return { stdout: stdout, stderr: '' }; } catch (error) { // 捕获同步执行中的错误 cb(error.stderr.toString()); return { stdout: '', stderr: error.stderr.toString()}; }*/ }; exports.setJudge = function(idx){ judge = idx; }; exports.getJudge = function(){ return judge; }; exports.setStartEnd = function(idx){ setStartEnd = idx; }; exports.getStartEnd = function(){ return setStartEnd; }; exports.deleteFile = function(){ fs.unlink(filePath, (err) => { if (err) { console.error('Error deleting file:', err); return; } console.log('File deleted successfully'); }); }; exports.deleteFile = function deleteFolderRecursive(folderPath) { if (fs.existsSync(folderPath)) { fs.readdirSync(folderPath).forEach((file, index) => { const curPath = path.join(folderPath, file); if (fs.lstatSync(curPath).isDirectory()) { // 判断是否为文件夹 deleteFilesInFolder(curPath); // 递归删除文件夹 } else { fs.unlinkSync(curPath); // 删除文件 console.log(`Deleted file: ${curPath}`); } }); } }; exports.call = function(fname, path, cb) { // 执行 check_model.exe 程序 const cp = spawn(path, [fname]); // 监听子进程的标准输出流,将输出结果传递给回调函数 cp.stdout.on('data', data => { cb(data.toString()); }); // 在 check_model.exe 程序运行后,在其标准输入中输入文件路径并执行 //cp.stdin.write(fname + '\n'); }; exports.write = function(fname, path) { //写文件 var ws = fs.createWriteStream(path); ws.on('open', () => { console.log('打开流'); }); ws.write(fname); ws.on('close', () => { console.log('关闭流'); }); ws.end(); }; exports.getCurrentDirectory = () => { var currentWorkingDirectory = process.cwd(); var twoLevelsUpDirectory = path.join(currentWorkingDirectory, '..', '..', '..', 'unit', 'hypermesh', '/'); return twoLevelsUpDirectory; }; exports.getPID = function() { return process.pid; }; exports.startSubProcess = function(cb, on_msg, attribute, ids, spring, selectFace, Eigenvalue, cmd, cwds, ios) { let cp = spawn(cmd, ['hide'], { cwd: cwds }); cp.stdout.on("data", data => { //console.log(data.toString()); }); io.on('connection', client => { client.on('ready', () => { if (cb) { cb(cp.pid); } }); client.on('message', msg => { if (on_msg) { on_msg(msg); } }); client.on('attribute', msg => { if (attribute) { attribute(msg); } }); client.on('id', msg => { if (ids) { ids(msg); } }); client.on('spring', msg => { if (spring) { spring(msg); } }); client.on('selectFace', msg => { if (selectFace) { selectFace(msg); } }); client.on('Eigenvalue', msg => { if(Eigenvalue){ Eigenvalue(msg); } }); }); io.listen(ios); return io; }; exports.spawn = function(path, cmd, cb) { const child = spawn(path, [cmd]); // 监听子进程的 stdout 和 stderr 输出 child.stdout.on('data', (data) => { //console.log(`stdout: ${data}`); cb(data.toString()); }); child.stderr.on('data', (data) => { //console.error(`stderr: ${data}`); //cb(data.toString()); if(cb){cb(false);} }); // 监听子进程的退出事件 child.on('close', (code) => { //console.log(`子进程退出,退出码 ${code}`); }); }; var g_wait_form = false; exports.wait = () => { var cmd = (unit.dir + '_entry.js'); g_wait_form = fork(cmd, ['wait']); //g_wait_form.send('wait_cueline', 'test'); }; exports.cueline = (info) => { if (g_wait_form && g_wait_form.send) { //g_wait_form.send('wait_cueline', '' + info); //exports.ide_info(info); g_wait_form.send({ type: 'wait_cueline', info: info }); } }; exports.stop = () => { var timeID = setInterval(() => { if (g_wait_form) { clearInterval(timeID); g_wait_form.send({ type: "close", form: 'wait' }); g_wait_form = false; } }, 100); }; exports.updateGeometry = (geometry) => { if (g_wait_form) { g_wait_form.send({ type: "setGeometry", geometry: geometry, form: 'wait' }); } }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // AutoLoadPlugins Function Implement Start. function AutoLoadPlugins() { var plugin_dir = (__dirname + '/../addon/'); if(!fs.existsSync(plugin_dir)) { return; } var files = fs.readdirSync(plugin_dir); files.forEach(function(filename){ var filedir = path.join(plugin_dir, filename); var stats = fs.statSync(filedir); if(!stats.isDirectory()) { if(filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') { require(filedir); } if(filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') { require(filedir); } } }); } function AutoLoadGlobalPlugins() { var plugin_dir = (process.env.COMX_SDK + 'addon/'); if (!fs.existsSync(plugin_dir)) { return; } var files = fs.readdirSync(plugin_dir); files.forEach(function(filename) { var filedir = path.join(plugin_dir, filename); var stats = fs.statSync(filedir); if (!stats.isDirectory()) { if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') { require(filedir); } if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') { require(filedir); } } }); } //AutoLoadPlugins Function Implement End. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // ide_info Function Implement Start. exports.ide_info = (msg) => { if (process.send) { process.send({ type: 'debug', info: msg }); } }; //ide_info Function Implement End. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////