MathFunc.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <div class="XFpdding" style="height: 350px;">
  3. <ul>
  4. <li
  5. class="item"
  6. v-for="(tab, index) in tabslist1"
  7. :key="index"
  8. :class="{ active: currentTab1 === index }"
  9. @click="selectTab1(index)"
  10. >
  11. <img :src="tab.imgSrc" style="width: 22px;"/>
  12. {{ tab.name }}
  13. </li>
  14. </ul>
  15. <div v-show="currentTab1 == '0'" class="eldesign classtable" style="margin-top: 10px">
  16. <el-table :data="inParams" border style="width: 100%; " :header-cell-class-name="headerCellClassName">
  17. <el-table-column type="index" width="70" label="编号"/>
  18. <el-table-column prop="code" label="编码">
  19. <!-- <template #default="{ row }">
  20. <el-input v-model="row.code" />
  21. </template> -->
  22. </el-table-column>
  23. <el-table-column prop="name" label="参数名称">
  24. <template #default="{ row }">
  25. <el-input v-model="row.name" />
  26. </template>
  27. </el-table-column>
  28. <el-table-column prop="value" label="参数值">
  29. <template #default="{ row }">
  30. <el-input v-model="row.value" type="number" />
  31. </template>
  32. </el-table-column>
  33. <el-table-column prop="flag" label="启用" width="100">
  34. <template v-slot="scope">
  35. <el-checkbox
  36. :false-label="0"
  37. :true-label="1"
  38. v-model="scope.row.flag"
  39. />
  40. </template>
  41. </el-table-column>
  42. </el-table>
  43. </div>
  44. <div v-if="currentTab1 == '1'" style="margin-top: 10px;height: 280px">
  45. <PythonEdit v-model:value="equation" language="python" />
  46. </div>
  47. <div v-show="currentTab1 == '2'" class="eldesign classtable" style="margin-top: 10px">
  48. <el-table :data="outParams" border style="width: 100%; height: 280px" :header-cell-class-name="headerCellClassName">
  49. <el-table-column type="index" width="70" label="编号" />
  50. <el-table-column prop="name" label="参数名称">
  51. <template #default="{ row }">
  52. <el-input v-model="row.name" @change="handleEdit(row)" />
  53. </template>
  54. </el-table-column>
  55. <el-table-column prop="value" label="参数值">
  56. <template #default="{ row }">
  57. <el-input v-model="row.value" type="number" />
  58. </template>
  59. </el-table-column>
  60. <el-table-column prop="flag" label="启用" width="100">
  61. <template v-slot="scope">
  62. <el-checkbox
  63. :false-label="0"
  64. :true-label="1"
  65. v-model="scope.row.flag"
  66. />
  67. </template>
  68. </el-table-column>
  69. </el-table>
  70. </div>
  71. <el-dialog v-model="xinjiandialog" align-center :modal="false" :close-on-click-modal="false"
  72. :append-to-body="true" draggable :fullscreen="false" :modal-append-to-body="false" modal-class="summary-dlg"
  73. width="400" class="dialog_class bgcolor colortext tianjia sel">
  74. <template #header="{ titleId, titleClass }">
  75. <div class="my-header ">
  76. <!-- <el-image :src="getImgPath('xuek0.png')" fit="contain"></el-image> -->
  77. <h4 :id="titleId" :class="titleClass">新建</h4>
  78. </div>
  79. </template>
  80. <div style="margin-top: 20px;padding: 20px;">
  81. <el-form>
  82. <el-form-item label="code:" :label-width="formLabelWidth1">
  83. <el-input v-model="code" maxlength="100"/>
  84. </el-form-item>
  85. <el-form-item label="参数名称:" :label-width="formLabelWidth1">
  86. <el-input v-model="name" maxlength="100"/>
  87. </el-form-item>
  88. </el-form>
  89. </div>
  90. <template #footer>
  91. <div class="dialog-footer">
  92. <el-button @click="xinjiandialog = false">取消</el-button>
  93. <el-button type="primary" @click="xinjiantablecolumn();xinjiandialog = false">
  94. 确定
  95. </el-button>
  96. </div>
  97. </template>
  98. </el-dialog>
  99. </div>
  100. </template>
  101. <script setup>
  102. import { ref, onMounted, reactive, provide, nextTick } from "vue"
  103. import { ElMessage, ElButton, ElDialog, ElSelect } from "element-plus"
  104. import { request, uploadFile } from "@/utils/request"
  105. import emitter from "@/utils/emitter"
  106. import PythonEdit from '@/components/PythonEditor/index.vue';
  107. import configParams from "@/assets/img/configParams.png";
  108. import MathFuncX from "@/assets/img/mathfuncX.png";
  109. import MathFuncFx from "@/assets/img/mathfuncFx.png";
  110. import MathFuncY from "@/assets/img/mathfuncY.png";
  111. let currentTab1 = ref(0)
  112. let pid = ref();
  113. let wid = ref();
  114. let mfcid = ref();
  115. let tabslist1 = ref([
  116. { id: "0", name: "自变量x", imgSrc: MathFuncX },
  117. { id: "1", name: "表达式y=F(x)", imgSrc: MathFuncFx },
  118. { id: "2", name: "因变量y", imgSrc: MathFuncY },
  119. ])
  120. let inParams = ref([
  121. // { code:'', name: "00", value: 0, flag: 1 },
  122. // { code:'', name: "00", value: 0, flag: 1 },
  123. // { code:'', name: "00", value: 0, flag: 1 },
  124. ])
  125. let equation = ref(`import os
  126. import numpy as np
  127. from surromdao.solver import BaseSolver
  128. class Branin(BaseSolver):
  129. def __init__(self, filename=os.path):
  130. super().__init__(filename)
  131. def compute(self, xdict):
  132. x = np.zeros(2)
  133. # x[1] = x[2]`);
  134. let outParams = ref([
  135. // { code:'', name: "00", value: 0, flag: 1 },
  136. // { code:'', name: "00", value: 0, flag: 1 },
  137. // { code:'', name: "00", value: 0, flag: 1 },
  138. // { code:'', name: "00", value: 0, flag: 1 },
  139. // { code:'', name: "00", value: 0, flag: 1 },
  140. // { code:'', name: "00", value: 0, flag: 1 },
  141. ])
  142. let xinjiandialog = ref(false)
  143. let formLabelWidth1 = ref(100)
  144. let code = ref('')
  145. let name = ref('')
  146. // 监听 outParams 的变化
  147. watch(
  148. outParams,
  149. (newParams) => {
  150. // 生成新的 equation 内容
  151. let newEquation = ``;
  152. // 遍历 outParams,添加 name = {name} 到 equation
  153. newParams.forEach(param => {
  154. newEquation += `${param.name} = \n`;
  155. });
  156. // 更新 equation 的值
  157. equation.value = newEquation;
  158. },
  159. { deep: true } // 深度监听,确保数组内部变化也能触发
  160. );
  161. const mathfuncnew = () => {
  162. // console.log('新建')
  163. xinjiandialog.value = true;
  164. }
  165. let xinjiantablecolumn = () => {
  166. // console.log('xinjiantablecolumn')
  167. if(code.value && name.value){
  168. if(currentTab1.value === 0){
  169. inParams.value.push({ code: code.value, name: name.value, value: 0, flag: 1 });
  170. }else if(currentTab1.value === 2){
  171. outParams.value.push({ code: code.value, name: name.value, value: 0, flag: 1 });
  172. }else {
  173. return;
  174. }
  175. code.value = '';
  176. name.value = '';
  177. }else{
  178. ElMessage.error('请输入code和参数名称')
  179. }
  180. }
  181. let emit = defineEmits(['selectTab']);
  182. const selectTab1 = (index) => {
  183. // console.log('dayin',index);
  184. currentTab1.value = index;
  185. // console.log('currentTab1',currentTab1.value);
  186. emit('selectTab', index);
  187. }
  188. const headerCellClassName = ({ column }) => {
  189. // console.log('列:',column.property)
  190. if (column.property === 'name') {
  191. console.log('yanse',column.property)
  192. return 'header-blue';
  193. } else if (column.property === 'value') {
  194. return 'header-green';
  195. } else if (column.property === 'flag') {
  196. return 'header-yellow';
  197. }
  198. return '';
  199. };
  200. const convertToStringArray = (result, Data) => {
  201. console.log('Data:', Data);
  202. // 安全检查 Data,确保它是一个数组
  203. if (!Array.isArray(Data)) {
  204. console.error('Data should be an array');
  205. return result; // 返回原 result 或者根据需要返回其他默认值
  206. }
  207. result = Data.map(row => {
  208. // 获取每一行的 `code`, `name`, `value` 和 `flag`
  209. const code = row.code || ' ';
  210. const name = row.name || ' ';
  211. const value = (row.value === null || row.value === undefined || row.value === '') ? ' ' : row.value;
  212. const flag = (row.flag === null || row.flag === undefined || row.flag === '') ? ' ' : row.flag;
  213. // 将字段连接为一个以逗号分隔的字符串
  214. return `${code},${name},${value},${flag}`;
  215. }).join(';'); // 每行之间用分号分隔
  216. return result;
  217. }
  218. const convertToStringArray1 = (result, Data) => {
  219. console.log('Data:', Data);
  220. // 安全检查 Data,确保它是一个数组
  221. if (!Array.isArray(Data)) {
  222. console.error('Data should be an array');
  223. return result; // 返回原 result 或者根据需要返回其他默认值
  224. }
  225. result = Data.map(row => {
  226. // 获取每一行的 `code`, `name`, `value` 和 `flag`
  227. const paramid = row.paramid || ' ';
  228. const code = row.code || ' ';
  229. const name = row.name || ' ';
  230. const value = (row.value === null || row.value === undefined || row.value === '') ? ' ' : row.value;
  231. const flag = (row.flag === null || row.flag === undefined || row.flag === '') ? ' ' : row.flag;
  232. // 将字段连接为一个以逗号分隔的字符串
  233. return `${paramid},${code},${name},${value},${flag}`;
  234. }).join(';'); // 每行之间用分号分隔
  235. return result;
  236. }
  237. const getmathfunc = (id,nowid) => {
  238. pid.value = id;
  239. if(nowid){
  240. wid.value = nowid;
  241. }
  242. // console.log('pid-1:',pid.value)
  243. const params = {
  244. transCode: "MDO0063",
  245. pid: pid.value,
  246. wid: wid.value
  247. };
  248. request(params).then((res) => {
  249. if(res == null){
  250. ElMessage.error('MathFunc初始化失败')
  251. return
  252. }
  253. if (res.hasOwnProperty("mfcid")) {
  254. getmathfuncAssign(res);
  255. emitter.emit("mfcidFromMathFunc", mfcid);
  256. }
  257. })
  258. .catch((err) => {
  259. console.log('MathFunc初始化失败err:', err)
  260. ElMessage.error('MathFunc初始化失败')
  261. })
  262. }
  263. const getmathfuncAssign = (data) => {
  264. // console.log('getxfoilAssign-data:', data);
  265. pid.value = data.pid;
  266. mfcid.value = data.mfcid;
  267. equation.value = data.equation;
  268. inParams.value = data.inParams;
  269. outParams.value = data.outParams;
  270. }
  271. const getmathfuncsave = (id,nowid) => {
  272. if(nowid){
  273. wid.value = nowid
  274. }
  275. pid.value = id;
  276. // console.log("pid:",pid.value);
  277. const params = {
  278. transCode: "MDO0064",
  279. pid: pid.value,
  280. wid: wid.value,
  281. equation: equation.value,
  282. inParams: convertToStringArray1([],inParams.value),
  283. outParams: convertToStringArray([],outParams.value)
  284. };
  285. request(params).then((res) => {
  286. ElMessage({
  287. message: '保存成功',
  288. type: 'success',
  289. })
  290. })
  291. .catch((err) => {
  292. ElMessage.error('保存失败')
  293. })
  294. }
  295. defineExpose({ getmathfunc, getmathfuncAssign, getmathfuncsave ,mathfuncnew})
  296. </script>