磁矩表磁计算器
请老师们抽空帮看看,有哪些代码走了弯路,有哪些不良的习惯,请帮我纠正一下。谢谢!有几个地方现在处理不了:
1)输入限制,输入框只允许输入带小数点的数字
import fonts.fontAwesome;
import win.ui;
/*DSG{{*/
mainForm = win.form(text="磁矩表磁计算器";right=599;bottom=553;border="dialog frame";max=false)
mainForm.add(
bkplusAS={cls="bkplus";text="Anson";left=101;top=8;right=580;bottom=28;align="right";bgcolor=12639424;color=15793151;forecolor=15780518;linearGradient=1;z=2};
bkplusD={cls="bkplus";left=20;top=303;right=580;bottom=543;bgcolor=12639424;db=1;dl=1;dr=1;z=3};
bkplusF={cls="bkplus";left=20;top=30;right=580;bottom=270;bgcolor=15780518;dl=1;dr=1;dt=1;z=4};
bkplusLK={cls="bkplus";text="说明";left=101;top=281;right=580;bottom=301;align="right";bgcolor=15780518;color=15793151;forecolor=12639424;linearGradient=0;z=1};
buttonD={cls="button";text="点击计算";left=247;top=471;right=461;bottom=532;border=1;color=14120960;db=1;dr=1;font=LOGFONT(h=-14);note="磁化方向必须填在高度方向";z=8};
buttonF={cls="button";text="点击计算";left=247;top=192;right=461;bottom=254;border=1;color=14120960;dr=1;dt=1;font=LOGFONT(h=-14);note="磁化方向必须填在高度方向";z=6};
checkboxBC1={cls="checkbox";text="启用";left=484;top=221;right=552;bottom=243;bgcolor=15780518;checked=1;z=40};
checkboxBC2={cls="checkbox";text="启用";left=484;top=499;right=552;bottom=521;bgcolor=12639424;checked=1;z=42};
editBr={cls="edit";left=116;top=191;right=234;bottom=213;dl=1;dt=1;edge=1;tabstop=1;z=29};
editBr2={cls="edit";left=115;top=452;right=233;bottom=474;dl=1;dt=1;edge=1;tabstop=1;z=33};
editCS1={cls="edit";text="3";left=483;top=81;right=536;bottom=103;edge=1;readonly=1;z=17};
editCS2={cls="edit";text="3";left=483;top=355;right=536;bottom=377;edge=1;readonly=1;z=21};
editD={cls="edit";left=115;top=339;right=229;bottom=361;db=1;dl=1;edge=1;group=1;tabstop=1;z=31};
editH={cls="edit";left=116;top=150;right=234;bottom=172;dl=1;dt=1;edge=1;tabstop=1;z=28};
editL={cls="edit";left=116;top=68;right=234;bottom=90;dl=1;dt=1;edge=1;tabstop=1;z=26};
editMM1={cls="edit";text="0";left=481;top=146;right=552;bottom=166;edge=1;readonly=1;z=19};
editMM2={cls="edit";text="0";left=483;top=418;right=554;bottom=440;edge=1;readonly=1;z=23};
editS1={cls="edit";text="0.01";left=116;top=232;right=234;bottom=254;dl=1;dt=1;edge=1;tabstop=1;z=30};
editS2={cls="edit";text="0.01";left=115;top=509;right=233;bottom=531;dl=1;dt=1;edge=1;tabstop=1;z=34};
editT={cls="edit";left=115;top=396;right=229;bottom=418;db=1;dl=1;edge=1;group=1;tabstop=1;z=32};
editW={cls="edit";left=116;top=109;right=234;bottom=131;dl=1;dt=1;edge=1;tabstop=1;z=27};
groupbox3={cls="groupbox";text="磁损(%)";left=471;top=332;right=566;bottom=388;bgcolor=12639424;edge=1;z=20};
groupbox7={cls="groupbox";text="亮片磁矩";left=471;top=394;right=566;bottom=450;bgcolor=12639424;edge=1;z=22};
groupboxBC1={cls="groupbox";text="表磁补偿";left=471;top=189;right=566;bottom=253;bgcolor=15780518;edge=1;z=39};
groupboxBC2={cls="groupbox";text="表磁补偿";left=471;top=468;right=566;bottom=532;bgcolor=12639424;edge=1;z=41};
groupboxGs1={cls="groupbox";text="亮片磁矩";left=471;top=120;right=566;bottom=176;bgcolor=15780518;edge=1;z=18};
groupboxMM1={cls="groupbox";text="磁损(%)";left=471;top=58;right=566;bottom=114;bgcolor=15780518;edge=1;z=16};
listviewD={cls="listview";left=247;top=340;right=461;bottom=452;edge=1;fullRow=1;msel=false;z=37};
listviewF={cls="listview";left=247;top=68;right=461;bottom=176;edge=1;fullRow=1;msel=false;z=35};
plus3={cls="plus";text=" 方块产品";left=7;top=16;right=117;bottom=50;align="right";bgcolor=12639424;clipBk=false;dl=1;dt=1;edge=1;font=LOGFONT(h=-18;name='微软雅黑');forecolor=15780518;iconStyle={align="left";font=LOGFONT(h=-18;name='FontAwesome');padding={left=12}};iconText='\uF04D';linearGradient=1;z=9};
plus4={cls="plus";text=" 圆柱产品";left=8;top=288;right=118;bottom=322;align="right";bgcolor=15780518;clipBk=false;dl=1;dt=1;edge=1;font=LOGFONT(h=-18;name='微软雅黑');forecolor=12639424;iconStyle={align="left";font=LOGFONT(h=-18;name='FontAwesome');padding={left=12}};iconText='\uF111';linearGradient=1;z=10};
spin1={cls="spin";left=536;top=81;right=556;bottom=103;z=36};
spin2={cls="spin";left=536;top=355;right=556;bottom=377;z=38};
staticBr1={cls="static";text="Br(kGs):";left=48;top=190;right=116;bottom=212;center=1;dl=1;dt=1;font=LOGFONT(name='微软雅黑');transparent=1;z=13};
staticBr2={cls="static";text="Br(kGs):";left=41;top=452;right=109;bottom=474;center=1;dl=1;dt=1;transparent=1;z=15};
staticD={cls="static";text="直径(mm):";left=41;top=339;right=109;bottom=361;center=1;db=1;dl=1;group=1;transparent=1;z=7};
staticH={cls="static";text="高(mm):";left=48;top=149;right=102;bottom=171;center=1;dl=1;dt=1;font=LOGFONT(name='微软雅黑';weight=700);transparent=1;z=12};
staticL={cls="static";text="长(mm):";left=48;top=68;right=102;bottom=90;center=1;dl=1;dt=1;font=LOGFONT(name='微软雅黑');transparent=1;z=5};
staticS1={cls="static";text="距离(mm):";left=48;top=230;right=116;bottom=252;center=1;dl=1;dt=1;font=LOGFONT(name='微软雅黑');transparent=1;z=25};
staticS2={cls="static";text="距离(mm):";left=41;top=509;right=109;bottom=531;center=1;dl=1;dt=1;transparent=1;z=24};
staticT={cls="static";text="高度(mm):";left=41;top=396;right=109;bottom=418;center=1;db=1;dl=1;font=LOGFONT(weight=700);group=1;transparent=1;z=14};
staticW={cls="static";text="宽(mm):";left=48;top=109;right=102;bottom=131;center=1;dl=1;dt=1;font=LOGFONT(name='微软雅黑');transparent=1;z=11}
)
/*}}*/
_Urec = 1.05; //回复磁导率
_Pai = math.pi; //圆周率
mainForm.bkplusAS.text = "Anson _v1.12_24_0613";
mainForm.bkplusLK.text = "基于黑片状态计算,需要考虑公差、倒角、酸洗、镀层屏蔽等因素。";
/*listview控件*/
//属性设置“选择整行”
//插入列
mainForm.listviewF.insertColumn("项目",80);
mainForm.listviewF.insertColumn("数据",60);
mainForm.listviewF.insertColumn("单位",60);
mainForm.listviewF.fillParent() //让最后一列自动填满控件
mainForm.listviewD.insertColumn("项目",80);
mainForm.listviewD.insertColumn("数据",60);
mainForm.listviewD.insertColumn("单位",60);
mainForm.listviewD.fillParent() //让最后一列自动填满控件
//表磁补偿选择框1
if(mainForm.checkboxBC1.checked){
mainForm.editS1.text = "0.5";
mainForm.editS1.readonly =false;
}else {
mainForm.editS1.text = "0.01";
mainForm.editS1.readonly =true;
}
//表磁补偿选择框2
if(mainForm.checkboxBC2.checked){
mainForm.editS2.text = "0.5";
mainForm.editS2.readonly =false;
}else {
mainForm.editS2.text = "0.01";
mainForm.editS2.readonly =true;
}
var mm1 = 0; //黑片磁矩变量,不考虑磁损
//方块产品计算
mainForm.buttonF.oncommand = function(id,event){
mainForm.listviewF.clear()
mainForm.editL.text = string.trim(mainForm.editL.text);
if mainForm.editL.text == ""{
mainForm.msgboxErr("方块长度数值不能为空");
mainForm.editL.setFocus();
return ;
}
mainForm.editW.text = string.trim(mainForm.editW.text);
if mainForm.editW.text == ""{
mainForm.msgboxErr("方块宽度数值不能为空");
mainForm.editW.setFocus();
return ;
}
mainForm.editH.text = string.trim(mainForm.editH.text);
if mainForm.editH.text == ""{
mainForm.msgboxErr("方块高度数值不能为空");
mainForm.editH.setFocus();
return ;
}
mainForm.editBr.text = string.trim(mainForm.editBr.text);
if mainForm.editBr.text == ""{
mainForm.msgboxErr("剩磁Br数值不能为空");
mainForm.editBr.setFocus();
return ;
}
mainForm.editS1.text = string.trim(mainForm.editS1.text);
if mainForm.editS1.text == ""{
mainForm.msgboxErr("距离数值不能为空");
mainForm.editS1.setFocus();
return ;
}
var a = math.abs(tonumber( mainForm.editL.text)); //方块长度,取绝对值
var b = math.abs(tonumber( mainForm.editW.text)); //方块宽度,取绝对值
var L = math.abs(tonumber( mainForm.editH.text)); //方块高度,取绝对值,大写是为了区分
var V = a*b*L; //方块体积
var Pc = math.sqrt(_Pai*(a*b+(a+b)*L))*L/(a*b);
Pc = math.round(Pc,2); //保留2位小数
mainForm.listviewF.addItem({
text={"磁导系数Pc",Pc,"(--)"}
})
var Br = math.abs(tonumber(mainForm.editBr.text)); //方块剩磁
var Bdi = Br*(Pc+1)/(_Urec+Pc); //本质磁通密度
mm1 = V*Bdi/100;
mm1 = math.round(mm1,2); //保留2位小数
mainForm.listviewF.addItem({
text={"磁矩",mm1,"(uVS·cm)"}
})
//磁损后的磁矩
var mm =math.round(mm1*(100-tonumber(mainForm.editCS1.text))/100,2);
mainForm.editMM1.text=mm;
var X1 = math.abs(tonumber( mainForm.editS1.text)); //探头的气隙,距离磁铁表面的距离
var G1 = math.atan(a*b/(2*X1*math.sqrt(a**2+b**2+4*X1**2)));
var G2 = math.atan(a*b/(2*(X1+L)*math.sqrt(a**2+b**2+4*(X1+L)**2)));
var Gs = 0;
if(mainForm.checkboxBC1.checked){
Gs = Bdi*1000/_Pai*(G1-G2);
}else {
Gs = Br*1000/_Pai*(G1-G2);
}
Gs = math.round(Gs,1); //保留1位小数
mainForm.listviewF.addItem({
text={"中心表磁",Gs,"(Gs)"}
})
}
var mm2 = 0 ;
//圆柱产品计算
mainForm.buttonD.oncommand = function(id,event){
mainForm.listviewD.clear()
mainForm.editD.text = string.trim(mainForm.editD.text);
if mainForm.editD.text == ""{
mainForm.msgboxErr("圆柱直径数值不能为空");
mainForm.editD.setFocus();
return ;
}
mainForm.editT.text = string.trim(mainForm.editT.text);
if mainForm.editT.text == ""{
mainForm.msgboxErr("圆柱高度数值不能为空");
mainForm.editT.setFocus();
return ;
}
mainForm.editBr2.text = string.trim(mainForm.editBr2.text)
if mainForm.editBr2.text == ""{
mainForm.msgboxErr("剩磁Br数值不能为空");
mainForm.editBr2.setFocus();
return ;
}
mainForm.editS2.text = string.trim(mainForm.editS2.text);
if mainForm.editS2.text == ""{
mainForm.msgboxErr("距离数值不能为空");
mainForm.editS2.setFocus();
return ;
}
var d = math.abs(tonumber( mainForm.editD.text)); //圆柱直径,取绝对值
var L = math.abs(tonumber( mainForm.editT.text)); //圆柱高度,取绝对值
var R = d/2;
var V = _Pai*R**2*L; //圆柱体积,取绝对值,2个*号连在一起是幂运算,这里2是平方,3是立方
var Pc = math.sqrt(R*(R+L))*L/(R**2);
Pc = math.round(Pc,2); //保留2位小数
mainForm.listviewD.addItem({
text={"磁导系数Pc",Pc,"(--)"}
})
var Br = math.abs(tonumber(mainForm.editBr2.text)); //方块剩磁
var Bdi = Br*(Pc+1)/(_Urec+Pc); //本质磁通密度
mm2 = V*Bdi/100;
mm2 = math.round(mm2,2); //保留2位小数
mainForm.listviewD.addItem({
text={"磁矩",mm2,"(uVS·cm)"}
})
var mm =math.round(mm2*(100-tonumber(mainForm.editCS2.text))/100,2);
mainForm.editMM2.text=mm;
var X2 = math.abs(tonumber( mainForm.editS2.text)); //探头的气隙,距离磁铁表面的距离
var G1 = (X2+L)/math.sqrt(4*(X2+L)**2+d**2);
var G2 = X2/math.sqrt(4*X2**2+d**2);
var Gs = 0;
if(mainForm.checkboxBC2.checked){
Gs = Bdi*1000*(G1-G2);
}else {
Gs = Br*1000*(G1-G2);
}
Gs = math.round(Gs,1); //保留1位小数
mainForm.listviewD.addItem({
text={"中心表磁",Gs,"(Gs)"}
})
}
//滚动选框1
mainForm.spin1.setRange(0,30)//磁损最多也就30%了吧
mainForm.spin1.pos = mainForm.editCS1.text;
mainForm.spin1.oncommand = function(id,event,pos){
if( event == 8/*_SB_ENDSCROLL*/ ){
mainForm.editCS1.text = mainForm.spin1.pos;
}
}
mainForm.editCS1.onChange = function(){
mainForm.spin1.pos = mainForm.editCS1.text;
var mm = math.round(mm1*(100-tonumber(owner.text))/100,2);
mainForm.editMM1.text=mm;
}
//滚动选框2
mainForm.spin2.setRange(0,30)//磁损最多也就30%了吧
mainForm.spin2.pos = mainForm.editCS2.text;
mainForm.spin2.oncommand = function(id,event,pos){
if( event == 8/*_SB_ENDSCROLL*/ ){
mainForm.editCS2.text = mainForm.spin2.pos;
}
}
mainForm.editCS2.onChange = function(){
var mm = math.round(mm2*(100-tonumber(owner.text))/100,2);
mainForm.editMM2.text=mm;
}
mainForm.checkboxBC1.oncommand = function(id,event){
if(owner.checked){
mainForm.editS1.text = "0.5";
mainForm.editS1.readonly =false;
}else {
mainForm.editS1.text = "0.01";
mainForm.editS1.readonly =true;
}
}
mainForm.checkboxBC2.oncommand = function(id,event){
if(owner.checked){
mainForm.editS2.text = "0.5";
mainForm.editS2.readonly =false;
}else {
mainForm.editS2.text = "0.01";
mainForm.editS2.readonly =true;
}
}
mainForm.show();
return win.loopMessage();
页:
[1]