_model.js 16 KB

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