DataOperation.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Xml;
  12. using OTSCommon;
  13. using OTSCommon.DBOperate;
  14. namespace OTSInclusionsTraceability
  15. {
  16. class DataOperation
  17. {
  18. #region 数据库相关
  19. private SqLiteHelper dbHelper = null;
  20. public DataOperation(string path)
  21. {
  22. string[] vs = path.Split('\\');
  23. if (path.Split('\\').Contains("Inclusion.db"))
  24. {
  25. dbHelper = new SqLiteHelper("data source='" + path+"'");
  26. }
  27. else
  28. {
  29. dbHelper = new SqLiteHelper("data source='" + path + "\\FIELD_FILES\\Inclusion.db'");
  30. }
  31. }
  32. public DataTable GetParticlesByEquCircleDiameter(string condition1,string condition2)
  33. {
  34. 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" +
  35. " where xrayid > -1 " + /*"and "+ */condition1 /*+" and "*/+condition2 ;
  36. DataTable DT = new DataTable();
  37. DT = dbHelper.ExecuteQuery(sqliteString);
  38. return DT;
  39. }
  40. public DataTable GetElementChemistry()
  41. {
  42. string sqliteString = "select * from ElementChemistry";
  43. DataTable DT = new DataTable();
  44. DT = dbHelper.ExecuteQuery(sqliteString);
  45. return DT;
  46. }
  47. public Particle GetParticleXrayDataByFidAndPid(string fieldid, string xrayid)
  48. {
  49. string sqlp = @"select xraydata from xraydata where xrayindex=" + xrayid + " and fieldid=" + fieldid;
  50. DataTable DT = new DataTable();
  51. DT = dbHelper.ExecuteQuery(sqlp);
  52. List<Particle> listp = TableToList<Particle>(DT);
  53. if (listp.Count > 0)
  54. {
  55. return listp[0];
  56. }
  57. else
  58. {
  59. return null;
  60. }
  61. }
  62. /// <summary>
  63. /// /// tbale转list
  64. /// /// </summary>
  65. /// /// <typeparam name="T"></typeparam>
  66. /// /// <returns></returns>
  67. public List<T> TableToList<T>(DataTable table) where T : class, new()
  68. {
  69. var result = new List<T>();
  70. var propertys = typeof(T).GetProperties();
  71. foreach (DataRow dr in table.Rows)
  72. {
  73. var item = new T();
  74. result.Add(item);
  75. foreach (var pi in propertys)
  76. {
  77. if (!table.Columns.Contains(pi.Name))
  78. continue;
  79. var value = dr[pi.Name];
  80. if (value is DBNull || value == null)
  81. continue;
  82. if (value.GetType().ToString() == "System.Int64")
  83. {
  84. pi.SetValue(item, Convert.ToInt32(value));
  85. }
  86. else if (value.GetType().ToString() == "System.Decimal")
  87. {
  88. pi.SetValue(item, Convert.ToDouble(value));
  89. }
  90. else
  91. {
  92. pi.SetValue(item, value);
  93. }
  94. }
  95. }
  96. return result;
  97. }
  98. #endregion
  99. #region Xml 相关
  100. string XmlAddress = "\\Config\\SysData\\OTSInclusionsTraceabilityParam.rpf";
  101. string ELEXmlAddress = "\\Config\\SysData\\OTSReportMgrParam.rpf";
  102. XmlDocument doc = new XmlDocument();
  103. public bool WriteXmlDefaultData(string sEquivalentCircularDiameter, bool bContains, string sDisplaySource)
  104. {
  105. //要写入的文件是否存在
  106. if (File.Exists(Application.StartupPath + XmlAddress))
  107. {
  108. doc.Load(Application.StartupPath + XmlAddress);
  109. //获取根节点
  110. XmlElement xml = doc.DocumentElement;
  111. doc.DocumentElement.RemoveAll();
  112. }
  113. else
  114. {
  115. doc = new XmlDocument();
  116. XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
  117. doc.AppendChild(dec);
  118. XmlElement root = doc.CreateElement("XMLData");//加入根节点
  119. doc.AppendChild(root);
  120. }
  121. XmlElement user = doc.CreateElement("Member");
  122. user.SetAttribute("RegName", "EquivalentCircularDiameter");
  123. user.SetAttribute("strValue", sEquivalentCircularDiameter);
  124. doc.DocumentElement.AppendChild(user);
  125. user = doc.CreateElement("Member");
  126. user.SetAttribute("RegName", "Contains");
  127. user.SetAttribute("strValue", bContains.ToString());
  128. doc.DocumentElement.AppendChild(user);
  129. user = doc.CreateElement("Member");
  130. user.SetAttribute("RegName", "DisplaySource");
  131. user.SetAttribute("strValue", sDisplaySource);
  132. doc.DocumentElement.AppendChild(user);
  133. doc.Save(Application.StartupPath + XmlAddress);
  134. return true;
  135. }
  136. public bool ReadXmlDataDefault(string AttributeItem,ref string output,string _XmlAddress= "\\Config\\SysData\\OTSInclusionsTraceabilityParam.rpf")
  137. {
  138. if (!File.Exists(Application.StartupPath + _XmlAddress))
  139. {
  140. return false;
  141. }
  142. doc.Load(Application.StartupPath + _XmlAddress);
  143. //获取根节点
  144. XmlElement xn1 = doc.DocumentElement;
  145. XmlNodeList xn2 = xn1.ChildNodes;
  146. //遍历所有节点
  147. foreach (XmlNode item in xn2)
  148. {
  149. XmlElement xe = (XmlElement)item;
  150. //将遍历到的值追加到Dictionary中
  151. if (xe.GetAttribute("RegName") == AttributeItem)
  152. {
  153. output= xe.GetAttribute("strValue");
  154. break;
  155. }
  156. }
  157. return true;
  158. }
  159. #endregion
  160. /// <summary>
  161. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  162. /// </summary>
  163. /// <param name="path"></param>
  164. /// <returns></returns>
  165. public Bitmap ReadImageFile(string path)
  166. {
  167. if (!File.Exists(path))
  168. {
  169. return null;//文件不存在
  170. }
  171. FileStream fs = File.OpenRead(path); //OpenRead
  172. int filelength = 0;
  173. filelength = (int)fs.Length; //获得文件长度
  174. Byte[] image = new Byte[filelength]; //建立一个字节数组
  175. fs.Read(image, 0, filelength); //按字节流读取
  176. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  177. fs.Close();
  178. Bitmap bit = new Bitmap(result);
  179. return bit;
  180. }
  181. /// <summary>
  182. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  183. /// </summary>
  184. /// <param name="in_cotsparticleclr"></param>
  185. /// <returns></returns>
  186. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  187. {
  188. //为了能把整个颗粒显示完整
  189. offset_rect.X = offset_rect.X - 5;
  190. offset_rect.Y = offset_rect.Y - 5;
  191. offset_rect.Width = offset_rect.Width + 10;
  192. offset_rect.Height = offset_rect.Height + 10;
  193. //防止计算偏差后,有坐标溢出现象
  194. if (offset_rect.X < 0)
  195. offset_rect.X = 0;
  196. if (offset_rect.Y < 0)
  197. offset_rect.Y = 0;
  198. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  199. {
  200. offset_rect.Width = ls_bt.Width - offset_rect.X;
  201. }
  202. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  203. {
  204. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  205. }
  206. Bitmap new_ret_bp;
  207. //防止为0后面计算出错
  208. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  209. {
  210. //最后通过list_showsegment组建成新的图片,进行返回
  211. new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat);
  212. }
  213. else
  214. {
  215. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  216. }
  217. return new_ret_bp;
  218. }
  219. }
  220. }