_model.js 15 KB

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