_model.js 16 KB

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