_model.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. var fs = require('fs');
  2. var path = require('path');
  3. const net = require('net');
  4. var exec = require('child_process').exec;
  5. var ios = require('socket.io-client');
  6. exports.acax_app = require('@acax/application');
  7. const os = require('os');
  8. const {
  9. spawn
  10. } = require('child_process');
  11. const {
  12. fork
  13. } = require('child_process');
  14. const {
  15. Readable
  16. } = require('stream');
  17. const io = require('socket.io')();
  18. const { execSync } = require('child_process');
  19. var judge = 0;
  20. var setStartEnd = 0;
  21. var hash = [];
  22. var con = 0;
  23. var isBeam = false;
  24. const str01 = [];
  25. var nums = 0;
  26. var order = 0;
  27. var poi = 0;
  28. var mat = 0;
  29. var midu = 0;
  30. var pathStep = 0;
  31. var Bpp1 = 0;
  32. var Bpp2 = 0;
  33. var modelCode = 0;
  34. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. //Initialize codes.
  36. AutoLoadPlugins();
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. //You can load the plugins in global field, if the next line codes are uncommented.
  39. AutoLoadGlobalPlugins();
  40. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. //Sample codes.
  42. //写前处理文件
  43. exports.writeFile=function(arr) {
  44. const filePath = unit.dir + 'data/preFiles.txt';
  45. fs.writeFile(filePath, arr.join("\n"), (err) => {
  46. if (err) {
  47. console.error("写入失败:", err);
  48. return;
  49. }
  50. console.log("写入成功!");
  51. });
  52. };
  53. //检查路径
  54. exports.checkPath = function(folderPath, cb){
  55. fs.readdir(folderPath, (err, items) => {
  56. if (err) {
  57. return console.error('无法读取文件夹:', err.message);
  58. }
  59. const directories = items.filter((item) => {
  60. const fullPath = path.join(folderPath, item);
  61. return fs.statSync(fullPath).isDirectory();
  62. });
  63. cb(directories);
  64. });
  65. };
  66. exports.setBpp1 = function(input){
  67. Bpp1 = input;
  68. };
  69. exports.getBpp1 = function(){
  70. return Bpp1;
  71. };
  72. exports.setBpp2 = function(input){
  73. Bpp2 = input;
  74. };
  75. exports.getBpp2 = function(){
  76. return Bpp2;
  77. };
  78. exports.getParallels = function(cb, cpuss){
  79. let num_of_cpu_cores = os.cpus().length;
  80. let para_of_cpu_cores = os.availableParallelism();
  81. let free_memory = os.freemem() / 1024 / 1024 / 1024; //GB
  82. let total_memory = os.totalmem() / 1024 / 1024 / 1024; //GB
  83. let cmd = 'wmic';
  84. let opt = ['cpu', 'get', 'numberofcores'];
  85. let cp = require('child_process').spawn(cmd, opt);
  86. cp.stdout.on('data', data=>{
  87. if(data.toString() !== '') {
  88. var res = data.toString().replace(/[ \r\t]/g, '');
  89. if(cb){
  90. //cb(res.split('\n')[1]);
  91. 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 请先保存文件后选择进程数并进行求解,模态分析模块点击模态分析进行求解!`);
  92. }
  93. }
  94. });
  95. //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`);
  96. //console.log(`number of current computer cores : ${num_of_cpu_cores}`);
  97. //console.log(`memory status of current computer: ${free_memory.toFixed(1)}GB / ${total_memory.toFixed(1)}GB`);
  98. };
  99. var g_init = false;
  100. var g_cp_www_js = false;
  101. exports.startLocalhost = function(cb){
  102. if(!g_init) {
  103. g_init = true;
  104. var spawn = require('child_process').spawn;
  105. var www_js = path.join(__dirname, '/../localhost/bin/www');
  106. var cp = spawn('node', [www_js]);
  107. g_cp_www_js = cp;
  108. cp.stdout.on('data', data=>{
  109. //console.log(data.toString());
  110. });
  111. }
  112. var client = ios("ws://localhost:4000");
  113. client.on('connect', ()=>{
  114. if(cb){cb();}
  115. client.disconnect();
  116. });
  117. };
  118. exports.updateLineChart = function(dataLineChart){
  119. var client = ios("ws://localhost:4000");
  120. client.on('connect', ()=>{
  121. client.emit('updateLineChart', dataLineChart);
  122. });
  123. };
  124. exports.closeLocalhost = function(){
  125. if(g_cp_www_js) {
  126. process.kill(g_cp_www_js.pid);
  127. }
  128. };
  129. exports.readFiles = function(path, res){
  130. fs.readFile(path, 'utf8', (err, data) => {
  131. if (err) {
  132. console.error('Error reading file:', err);
  133. return;
  134. }
  135. // 正则表达式提取数值
  136. const regex = /Eigenvalue \d+: ([\d.e+-]+)/g;
  137. let match;
  138. var values = [];
  139. while ((match = regex.exec(data)) !== null) {
  140. values.push(parseFloat(match[1]));
  141. }
  142. //return values;
  143. res = values;
  144. console.log('values Array:', values);
  145. });
  146. };
  147. exports.getNodePath = function(){
  148. return require('path').join(unit.dir, '../../../node_portable').replace(/\\/g, '/') + '/node';
  149. };
  150. exports.getModelCode = function(){
  151. return modelCode;
  152. };
  153. exports.setModelCode = function(input){
  154. modelCode = input;
  155. };
  156. exports.getPicturePath = function(filename){
  157. return path.join(unit.dir, 'picture', filename);
  158. };
  159. exports.setStepPath = function(input){
  160. pathStep = input;
  161. };
  162. exports.getStepPath = function(){
  163. return pathStep;
  164. };
  165. exports.setMidu = function(input){
  166. midu = input;
  167. };
  168. exports.getMidu = function(){
  169. return midu;
  170. };
  171. exports.setMat = function(input){
  172. mat = input;
  173. };
  174. exports.getMat = function(){
  175. return mat;
  176. };
  177. exports.setPoi = function(input){
  178. poi = input;
  179. };
  180. exports.getPoi = function(){
  181. return poi;
  182. };
  183. exports.setOrder = function(input){
  184. order = input;
  185. };
  186. exports.getOrder = function(){
  187. return order;
  188. };
  189. exports.setNums = function(input){
  190. nums = input;
  191. };
  192. exports.getNums = function(){
  193. return nums;
  194. };
  195. exports.setStr = function(input){
  196. str01.push(input);
  197. };
  198. exports.getStr = function(input){
  199. return str01;
  200. };
  201. exports.clearStr = function(){
  202. //str01 = [];
  203. };
  204. exports.setBeam = function(input){
  205. isBeam = input;
  206. };
  207. exports.getBeam = function(input){
  208. return isBeam;
  209. };
  210. exports.setCon = function(input){
  211. con = input;
  212. };
  213. exports.getCon = function(){
  214. return con;
  215. };
  216. exports.setHash = function(input){
  217. hash = input;
  218. };
  219. exports.getHash = function(){
  220. return hash;
  221. };
  222. exports.Test = function() {
  223. console.log("Hi, I'm a model test funciton");
  224. };
  225. exports.getDefaultDataDir = ()=>{
  226. return path.join(unit.dir, 'data');
  227. };
  228. exports.getExtDir = filename=>{
  229. return path.extname(filename).toLowerCase();
  230. };
  231. exports.getBrepInfoContext = ()=>{
  232. var brep_file = model.getDefaultDataDir() + '_temp.brep';
  233. return fs.readFileSync(brep_file).toString();
  234. };
  235. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  236. //Put you codes here
  237. exports.execs = function(path, cb) {
  238. exec(path, (error, stdout, stderr)=>{
  239. if(error) {
  240. console.error(`exec error: ${error}`);
  241. if(cb){cb(false);}
  242. return;
  243. }
  244. console.log(`stdout: ${stdout}`);
  245. console.log(`stderr: ${stderr}`);
  246. if(cb){cb(true);}
  247. });
  248. /*try {
  249. // 执行同步的命令
  250. const stdout = execSync(path).toString();
  251. cb(stdout);
  252. return { stdout: stdout, stderr: '' };
  253. } catch (error) {
  254. // 捕获同步执行中的错误
  255. cb(error.stderr.toString());
  256. return { stdout: '', stderr: error.stderr.toString()};
  257. }*/
  258. };
  259. exports.setJudge = function(idx){
  260. judge = idx;
  261. };
  262. exports.getJudge = function(){
  263. return judge;
  264. };
  265. exports.setStartEnd = function(idx){
  266. setStartEnd = idx;
  267. };
  268. exports.getStartEnd = function(){
  269. return setStartEnd;
  270. };
  271. exports.deleteFile = function(){
  272. fs.unlink(filePath, (err) => {
  273. if (err) {
  274. console.error('Error deleting file:', err);
  275. return;
  276. }
  277. console.log('File deleted successfully');
  278. });
  279. };
  280. exports.deleteFile = function deleteFolderRecursive(folderPath) {
  281. if (fs.existsSync(folderPath)) {
  282. fs.readdirSync(folderPath).forEach((file, index) => {
  283. const curPath = path.join(folderPath, file);
  284. if (fs.lstatSync(curPath).isDirectory()) { // 判断是否为文件夹
  285. deleteFilesInFolder(curPath); // 递归删除文件夹
  286. } else {
  287. fs.unlinkSync(curPath); // 删除文件
  288. console.log(`Deleted file: ${curPath}`);
  289. }
  290. });
  291. }
  292. };
  293. exports.call = function(fname, path, cb) {
  294. // 执行 check_model.exe 程序
  295. const cp = spawn(path, [fname]);
  296. // 监听子进程的标准输出流,将输出结果传递给回调函数
  297. cp.stdout.on('data', data => {
  298. cb(data.toString());
  299. });
  300. // 在 check_model.exe 程序运行后,在其标准输入中输入文件路径并执行
  301. //cp.stdin.write(fname + '\n');
  302. };
  303. exports.write = function(fname, path) { //写文件
  304. var ws = fs.createWriteStream(path);
  305. ws.on('open', () => {
  306. console.log('打开流');
  307. });
  308. ws.write(fname);
  309. ws.on('close', () => {
  310. console.log('关闭流');
  311. });
  312. ws.end();
  313. };
  314. exports.getCurrentDirectory = () => {
  315. var currentWorkingDirectory = process.cwd();
  316. var twoLevelsUpDirectory = path.join(currentWorkingDirectory, '..', '..', '..', 'unit', 'hypermesh', '/');
  317. return twoLevelsUpDirectory;
  318. };
  319. exports.getPID = function() {
  320. return process.pid;
  321. };
  322. exports.startSubProcess = function(cb, on_msg, attribute, ids, spring, selectFace, Eigenvalue, cmd, cwds, ios) {
  323. let cp = spawn(cmd, ['hide'], {
  324. cwd: cwds
  325. });
  326. cp.stdout.on("data", data => {
  327. //console.log(data.toString());
  328. });
  329. io.on('connection', client => {
  330. client.on('ready', () => {
  331. if (cb) {
  332. cb(cp.pid);
  333. }
  334. });
  335. client.on('message', msg => {
  336. if (on_msg) {
  337. on_msg(msg);
  338. }
  339. });
  340. client.on('attribute', msg => {
  341. if (attribute) {
  342. attribute(msg);
  343. }
  344. });
  345. client.on('id', msg => {
  346. if (ids) {
  347. ids(msg);
  348. }
  349. });
  350. client.on('spring', msg => {
  351. if (spring) {
  352. spring(msg);
  353. }
  354. });
  355. client.on('selectFace', msg => {
  356. if (selectFace) {
  357. selectFace(msg);
  358. }
  359. });
  360. client.on('Eigenvalue', msg => {
  361. if(Eigenvalue){
  362. Eigenvalue(msg);
  363. }
  364. });
  365. });
  366. io.listen(ios);
  367. return io;
  368. };
  369. exports.spawn = function(path, cmd, cb) {
  370. const child = spawn(path, [cmd]);
  371. // 监听子进程的 stdout 和 stderr 输出
  372. child.stdout.on('data', (data) => {
  373. //console.log(`stdout: ${data}`);
  374. cb(data.toString());
  375. });
  376. child.stderr.on('data', (data) => {
  377. //console.error(`stderr: ${data}`);
  378. //cb(data.toString());
  379. if(cb){cb(false);}
  380. });
  381. // 监听子进程的退出事件
  382. child.on('close', (code) => {
  383. //console.log(`子进程退出,退出码 ${code}`);
  384. });
  385. };
  386. var g_wait_form = false;
  387. exports.wait = () => {
  388. var cmd = (unit.dir + '_entry.js');
  389. g_wait_form = fork(cmd, ['wait']);
  390. //g_wait_form.send('wait_cueline', 'test');
  391. };
  392. exports.cueline = (info) => {
  393. if (g_wait_form && g_wait_form.send) {
  394. //g_wait_form.send('wait_cueline', '' + info);
  395. //exports.ide_info(info);
  396. g_wait_form.send({
  397. type: 'wait_cueline',
  398. info: info
  399. });
  400. }
  401. };
  402. exports.stop = () => {
  403. var timeID = setInterval(() => {
  404. if (g_wait_form) {
  405. clearInterval(timeID);
  406. g_wait_form.send({
  407. type: "close",
  408. form: 'wait'
  409. });
  410. g_wait_form = false;
  411. }
  412. }, 100);
  413. };
  414. exports.updateGeometry = (geometry) => {
  415. if (g_wait_form) {
  416. g_wait_form.send({
  417. type: "setGeometry",
  418. geometry: geometry,
  419. form: 'wait'
  420. });
  421. }
  422. };
  423. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  424. // AutoLoadPlugins Function Implement Start.
  425. function AutoLoadPlugins() {
  426. var plugin_dir = (__dirname + '/../addon/');
  427. if(!fs.existsSync(plugin_dir)) {
  428. return;
  429. }
  430. var files = fs.readdirSync(plugin_dir);
  431. files.forEach(function(filename){
  432. var filedir = path.join(plugin_dir, filename);
  433. var stats = fs.statSync(filedir);
  434. if(!stats.isDirectory()) {
  435. if(filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  436. require(filedir);
  437. }
  438. if(filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  439. require(filedir);
  440. }
  441. }
  442. });
  443. }
  444. function AutoLoadGlobalPlugins() {
  445. var plugin_dir = (process.env.COMX_SDK + 'addon/');
  446. if (!fs.existsSync(plugin_dir)) {
  447. return;
  448. }
  449. var files = fs.readdirSync(plugin_dir);
  450. files.forEach(function(filename) {
  451. var filedir = path.join(plugin_dir, filename);
  452. var stats = fs.statSync(filedir);
  453. if (!stats.isDirectory()) {
  454. if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  455. require(filedir);
  456. }
  457. if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  458. require(filedir);
  459. }
  460. }
  461. });
  462. }
  463. //AutoLoadPlugins Function Implement End.
  464. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  465. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  466. // ide_info Function Implement Start.
  467. exports.ide_info = (msg) => {
  468. if (process.send) {
  469. process.send({
  470. type: 'debug',
  471. info: msg
  472. });
  473. }
  474. };
  475. //ide_info Function Implement End.
  476. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////