/// <summary> /// 扫描背景 /// </summary> private void btnBackground_Click(object sender, RoutedEventArgs e) { try { if (Common.CommonMethod.QuestionMsgBox("请确认背景测量标样放置到测量台上") == false) { return; } if (!Directory.Exists(SettingData.settingData.runing_para.savePath)) { throw new Exception("找不到文件保存路径:" + SettingData.settingData.runing_para.savePath + ", 请在系统设置里面重新指定"); } string backFile = Path.Combine(SettingData.settingData.runing_para.savePath, "Background", DateTime.Now.ToString("yyyy-MM-dd")); if (!Directory.Exists(backFile)) { Directory.CreateDirectory(backFile); } backFile = Path.Combine(backFile, "Background" + DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss") + ".spc"); //创建扫描任务并显示扫描等待窗口,开始扫描 curScanTaskInfo = new ScanTaskInfo(SettingData.settingData.runing_para.scanPara, true, backFile); MainWindow mainWnd = App.Current.MainWindow as MainWindow; ProcessWaitDialog waitDlg = new ProcessWaitDialog(ScanSampleTask, "取消背景扫描"); waitDlg.Owner = App.Current.MainWindow; if (waitDlg.ShowDialog() == false) { mainWnd.BackgroundScaned = false; Common.CommonMethod.ErrorMsgBox(curScanTaskInfo.ErrorString); } else { mainWnd.BackgroundScaned = true; } } catch (Exception ex) { Common.CommonMethod.ErrorMsgBox(ex.Message); } }
private string ScanSpectrum(bool isBackground) { if (isBackground && !(bool)VspecInstrument.IsIntegratingSphere) { if (Common.CommonMethod.QuestionMsgBox("请确认背景测量标样放置到测量台上") == false) { return(null); } } Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.Filter = "仪器光谱(*.SPC)|*.SPC"; dlg.Title = "保存光谱文件"; if (dlg.ShowDialog() == true) { //创建扫描任务并显示扫描等待窗口,开始扫描 curScanTaskInfo = new ScanTaskInfo(curScanPara, isBackground, dlg.FileName); ProcessWaitDialog waitDlg = new ProcessWaitDialog(ScanSampleTask, "取消扫描"); waitDlg.Owner = App.Current.MainWindow; // bool successed = (bool) waitDlg.ShowDialog(); if (isBackground) { MainWindow mainWnd = App.Current.MainWindow as MainWindow; mainWnd.BackgroundScaned = curScanTaskInfo.scanSuccessed; } if (!curScanTaskInfo.scanSuccessed) { Common.CommonMethod.ErrorMsgBox(curScanTaskInfo.ErrorString); return(null); } else { if (isBackground) { backFile = dlg.FileName; } if (File.Exists(backFile) && !isBackground) { Ai.Hong.CommonLibrary.SpecFileFormatDouble sampleData1 = new Ai.Hong.CommonLibrary.SpecFileFormatDouble(); Ai.Hong.CommonLibrary.SpecFileFormatDouble sampleData2 = new Ai.Hong.CommonLibrary.SpecFileFormatDouble(); if (sampleData1.ReadFile(backFile) == false) { MessageBox.Show("读取光谱错误:" + backFile + " " + sampleData1.ErrorString); return(null); } if (sampleData2.ReadFile(dlg.FileName) == false) { MessageBox.Show("读取光谱错误:" + dlg.FileName + " " + sampleData2.ErrorString); return(null); } if (sampleData1.YDatas.Length != sampleData2.YDatas.Length) { MessageBox.Show("样品和背景不一致,请重新扫描背景!"); return(null); } //计算样品1和样品2比值,得到100%线 for (int index = 0; index < sampleData1.Parameter.dataCount; index++) { if (sampleData2.YDatas[index] == 0) { sampleData2.YDatas[index] = 1; } else { sampleData2.YDatas[index] = Math.Abs(sampleData2.YDatas[index] / sampleData1.YDatas[index]); } } float[] trData = new float[sampleData2.YDatas.Length]; for (int index = 0; index < trData.Length; index++) { trData[index] = (float)Math.Log10(1 / sampleData2.YDatas[index]); } Ai.Hong.CommonLibrary.SPCFile.SaveFile(dlg.FileName, trData, sampleData2.Parameter); } return(dlg.FileName); } } else { return(null); } }
private void btnDetect_Click(object sender, RoutedEventArgs e) { try { if (btnDetect.Text == "新增样品") { SetDrugInfoCtrlEnable(true); btnDetect.Text = "开始检定"; /* * DrugInfo newdrug = new DrugInfo(); * newdrug.licenseCode = "国药准字H20030863"; * newdrug.identOperator = drugInfo.identOperator; * newdrug.scanType = drugInfo.scanType; * newdrug.licenseType = drugInfo.licenseType; * newdrug.identThresold = drugInfo.identThresold; * newdrug.validMonth = drugInfo.validMonth; * newdrug.sampleNumber = drugInfo.sampleNumber; * newdrug.batchNumber = drugInfo.batchNumber; * newdrug.productTime = drugInfo.productTime; * * if (curAnalyteModel != null) * newdrug.identThresold = curAnalyteModel.thresold; */ DrugInfo newdrug = null; if (drugInfo != null) { newdrug = drugInfo.Clone(); } else { newdrug = new DrugInfo(); newdrug.licenseCode = "国药准字"; newdrug.productTime = new DateTime(2008, 1, 1); newdrug.identUnit = SettingData.settingData.runing_para.unitName; newdrug.validMonth = 48; newdrug.identThresold = 0.97; if (SettingData.settingData.dictionary.scanTypes.Count > 0) { newdrug.scanType = SettingData.settingData.dictionary.scanTypes[0]; } if (SettingData.settingData.dictionary.operators.Count > 0) { newdrug.scanType = SettingData.settingData.dictionary.operators[0]; } if (SettingData.settingData.dictionary.licenseTypes.Count > 0) { newdrug.scanType = SettingData.settingData.dictionary.licenseTypes[0]; } if (SettingData.settingData.dictionary.forms.Count > 0) { newdrug.scanType = SettingData.settingData.dictionary.forms[0]; } } drugInfo = newdrug; gridDrugInfo.DataContext = drugInfo; } else { //检查信息是否有填充 for (int i = 0; i < CtrlToFields.Length; i++) { if (CtrlToFields[i].displayName != "备注" && (CtrlToFields[i].ctrlElement is TextBox || CtrlToFields[i].ctrlElement is ComboBox)) { bool isempty = false; if (CtrlToFields[i].ctrlElement is TextBox) { isempty = CommonMethod.IsEmpty((CtrlToFields[i].ctrlElement as TextBox).Text); } else if (CtrlToFields[i].ctrlElement is ComboBox) { isempty = CommonMethod.IsEmpty((CtrlToFields[i].ctrlElement as ComboBox).Text); } if (isempty) { CtrlToFields[i].ctrlElement.Focus(); throw new Exception("请输入:" + CtrlToFields[i].displayName); } } } if (drugInfo.productTime > DateTime.Now || (DateTime.Now - drugInfo.productTime).Days > 360 * 20) { txtProductTime.Focus(); throw new Exception("请输入正确的生产日期"); } if (drugInfo.validMonth <= 0) { txtValidMonth.Focus(); throw new Exception("请输入正确的有效期"); } if (drugInfo.identThresold <= 0) { txtIdentThresold.Focus(); throw new Exception("请输入正确的阈值"); } if (checkNotAnalyze.IsChecked != true && curAnalyteModel == null) { throw new Exception("请选择分析模型"); } string sampleFile = null; //使用模拟方式,从文件中调入光谱 if (SettingData.settingData.runing_para.isSimulator) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "光谱文件|*.spc"; dlg.Multiselect = false; if (dlg.ShowDialog() == true) { sampleFile = dlg.FileName; } } else { if (!System.IO.Directory.Exists(SettingData.settingData.runing_para.savePath)) { throw new Exception("找不到文件保存路径:" + SettingData.settingData.runing_para.savePath + ", 请在系统设置里面重新指定"); } /* * //样品编号_通用名_商品名_剂型_规格_厂商_批号_生产日期_有效期_测量方式_仪器编号_检测单位_检测时间 * sampleFile = MaxString(drugInfo.sampleNumber, 10) + "_" + drugInfo.chemicalName + "_" + drugInfo.commercialName + "_" + * drugInfo.form + "_" + drugInfo.specification + "_" + MaxString(drugInfo.productUnit, 10) + "_" + * MaxString(drugInfo.batchNumber, 10) + "_" + drugInfo.productTime.ToString("yyyyMMdd") + "_" + drugInfo.validMonth + * "_" + drugInfo.scanType + "_" + drugInfo.identInstrumentID + "_" + MaxString(drugInfo.identUnit, 10) + * "_" + drugInfo.identTime.ToString("yyyyMMddHHmmss") + ".SPC"; */ sampleFile = MaxString(drugInfo.sampleNumber, 10) + "_" + drugInfo.chemicalName + "_" + MaxString(drugInfo.productUnit, 10) + "_" + drugInfo.licenseCode + "_" + drugInfo.identTime.ToString("yyyyMMddHHmmss"); sampleFile = Common.CommonMethod.GetValidFilename(sampleFile, ""); sampleFile = sampleFile.Replace('.', '-') + ".SPC"; string pathstr = GetFileSavePath(); if (pathstr == null) { throw new Exception("不能创建目录:" + pathstr); } sampleFile = System.IO.Path.Combine(pathstr, sampleFile); //创建扫描任务并显示扫描等待窗口,开始扫描 curScanTaskInfo = new ScanTaskInfo(SettingData.settingData.runing_para.scanPara, false, sampleFile); ProcessWaitDialog waitDlg = new ProcessWaitDialog(ScanSampleTask, "取消样品扫描"); waitDlg.Owner = App.Current.MainWindow; if (waitDlg.ShowDialog() == false) { throw new Exception(curScanTaskInfo.ErrorString); } } if (checkNotAnalyze.IsChecked != true) { //用相关系数法分析光谱 drugInfo.identValue = DrugAnalyte.CorCoeffAnalyte(curAnalyteModel, sampleFile); //保留小数点后5位 drugInfo.identValue = Math.Round(drugInfo.identValue, 5); if (drugInfo.identValue < 0) { throw new Exception(DrugAnalyte.ErrorString); } //填写检测信息 drugInfo.filename = sampleFile; drugInfo.identTime = DateTime.Now; drugInfo.identUnit = SettingData.settingData.runing_para.unitName; drugInfo.identInstrumentID = SettingData.settingData.runing_para.serialNo; //listSampleFiles.InitDisplayData(drugInfo); drugInfo.isChecked = true; // 显示光谱图像 drugInfo.identResult = drugInfo.identValue < curAnalyteModel.thresold ? EnumIdentResult.FAULT : EnumIdentResult.OK; drugInfo.identModel = curAnalyteModel.licenseCode; drugInfo.identMethod = EnumIdentifyMethod.Correlation; //保存检测结果 Common.CommonMethod.WriteDrugInfo(drugInfo); } else { //填写检测信息 drugInfo.filename = sampleFile; drugInfo.identTime = DateTime.Now; drugInfo.identUnit = SettingData.settingData.runing_para.unitName; drugInfo.identInstrumentID = SettingData.settingData.runing_para.serialNo; //listSampleFiles.InitDisplayData(drugInfo); drugInfo.isChecked = true; // 显示光谱图像 drugInfo.identValue = 0; drugInfo.identResult = EnumIdentResult.UNKNOWN; drugInfo.identModel = null; drugInfo.identMethod = EnumIdentifyMethod.Correlation; } //加入到结果列表 //读取光谱数据 if (drugInfo.fileData == null) { drugInfo.fileData = new Ai.Hong.CommonLibrary.SpecFileFormatDouble(); drugInfo.fileData.ReadFile(drugInfo.filename); drugInfo.color = Ai.Hong.CommonLibrary.spectrumDisplayInfo.GetDisplayColor(resultDatas.Count); } resultDatas.Add(drugInfo); listSampleFiles.SelectItem(drugInfo); //准备输入新的药品信息 SetDrugInfoCtrlEnable(false); btnDetect.Text = "新增样品"; } } catch (Exception ex) { CommonMethod.ErrorMsgBox(ex.Message); } }