using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using OTSCommon; using OTSCommon.DBOperate; namespace OTSInclusionsTraceability { class DataOperation { #region 数据库相关 private SqLiteHelper dbHelper = null; public DataOperation(string path) { string[] vs = path.Split('\\'); if (path.Split('\\').Contains("Inclusion.db")) { dbHelper = new SqLiteHelper("data source='" + path+"'"); } else { dbHelper = new SqLiteHelper("data source='" + path + "\\FIELD_FILES\\Inclusion.db'"); } } public DataTable GetParticlesByEquCircleDiameter(string condition1,string condition2) { string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,SEMPosX,SEMPosY,XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as Element from INcAData" + " where xrayid > -1 " + /*"and "+ */condition1 /*+" and "*/+condition2 ; DataTable DT = new DataTable(); DT = dbHelper.ExecuteQuery(sqliteString); return DT; } public DataTable GetElementChemistry() { string sqliteString = "select * from ElementChemistry"; DataTable DT = new DataTable(); DT = dbHelper.ExecuteQuery(sqliteString); return DT; } public Particle GetParticleXrayDataByFidAndPid(string fieldid, string xrayid) { string sqlp = @"select xraydata from xraydata where xrayindex=" + xrayid + " and fieldid=" + fieldid; DataTable DT = new DataTable(); DT = dbHelper.ExecuteQuery(sqlp); List listp = TableToList(DT); if (listp.Count > 0) { return listp[0]; } else { return null; } } /// /// /// tbale转list /// /// /// /// /// /// public List TableToList(DataTable table) where T : class, new() { var result = new List(); var propertys = typeof(T).GetProperties(); foreach (DataRow dr in table.Rows) { var item = new T(); result.Add(item); foreach (var pi in propertys) { if (!table.Columns.Contains(pi.Name)) continue; var value = dr[pi.Name]; if (value is DBNull || value == null) continue; if (value.GetType().ToString() == "System.Int64") { pi.SetValue(item, Convert.ToInt32(value)); } else if (value.GetType().ToString() == "System.Decimal") { pi.SetValue(item, Convert.ToDouble(value)); } else { pi.SetValue(item, value); } } } return result; } #endregion #region Xml 相关 string XmlAddress = "\\Config\\SysData\\OTSInclusionsTraceabilityParam.rpf"; string ELEXmlAddress = "\\Config\\SysData\\OTSReportMgrParam.rpf"; XmlDocument doc = new XmlDocument(); public bool WriteXmlDefaultData(string sEquivalentCircularDiameter, bool bContains, string sDisplaySource) { //要写入的文件是否存在 if (File.Exists(Application.StartupPath + XmlAddress)) { doc.Load(Application.StartupPath + XmlAddress); //获取根节点 XmlElement xml = doc.DocumentElement; doc.DocumentElement.RemoveAll(); } else { doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "yes"); doc.AppendChild(dec); XmlElement root = doc.CreateElement("XMLData");//加入根节点 doc.AppendChild(root); } XmlElement user = doc.CreateElement("Member"); user.SetAttribute("RegName", "EquivalentCircularDiameter"); user.SetAttribute("strValue", sEquivalentCircularDiameter); doc.DocumentElement.AppendChild(user); user = doc.CreateElement("Member"); user.SetAttribute("RegName", "Contains"); user.SetAttribute("strValue", bContains.ToString()); doc.DocumentElement.AppendChild(user); user = doc.CreateElement("Member"); user.SetAttribute("RegName", "DisplaySource"); user.SetAttribute("strValue", sDisplaySource); doc.DocumentElement.AppendChild(user); doc.Save(Application.StartupPath + XmlAddress); return true; } public bool ReadXmlDataDefault(string AttributeItem,ref string output,string _XmlAddress= "\\Config\\SysData\\OTSInclusionsTraceabilityParam.rpf") { if (!File.Exists(Application.StartupPath + _XmlAddress)) { return false; } doc.Load(Application.StartupPath + _XmlAddress); //获取根节点 XmlElement xn1 = doc.DocumentElement; XmlNodeList xn2 = xn1.ChildNodes; //遍历所有节点 foreach (XmlNode item in xn2) { XmlElement xe = (XmlElement)item; //将遍历到的值追加到Dictionary中 if (xe.GetAttribute("RegName") == AttributeItem) { output= xe.GetAttribute("strValue"); break; } } return true; } #endregion /// /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// /// /// public Bitmap ReadImageFile(string path) { if (!File.Exists(path)) { return null;//文件不存在 } FileStream fs = File.OpenRead(path); //OpenRead int filelength = 0; filelength = (int)fs.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fs.Read(image, 0, filelength); //按字节流读取 System.Drawing.Image result = System.Drawing.Image.FromStream(fs); fs.Close(); Bitmap bit = new Bitmap(result); return bit; } /// /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒 /// /// /// public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect) { //为了能把整个颗粒显示完整 offset_rect.X = offset_rect.X - 5; offset_rect.Y = offset_rect.Y - 5; offset_rect.Width = offset_rect.Width + 10; offset_rect.Height = offset_rect.Height + 10; //防止计算偏差后,有坐标溢出现象 if (offset_rect.X < 0) offset_rect.X = 0; if (offset_rect.Y < 0) offset_rect.Y = 0; if (offset_rect.X + offset_rect.Width > ls_bt.Width) { offset_rect.Width = ls_bt.Width - offset_rect.X; } if (offset_rect.Y + offset_rect.Height > ls_bt.Height) { offset_rect.Height = ls_bt.Height - offset_rect.Y; } Bitmap new_ret_bp; //防止为0后面计算出错 if (offset_rect.Width > 0 && offset_rect.Height > 0) { //最后通过list_showsegment组建成新的图片,进行返回 new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat); } else { new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height); } return new_ret_bp; } } }