DataOperation.cs 8.7 KB

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