ParticleData.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. using OTSCommon.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SQLite;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. namespace OTSIncAReportApp.DataOperation.DataAccess
  11. {
  12. public class ParticleData
  13. {
  14. private SqlHelper dbHelper;
  15. public ParticleData(string path)
  16. {
  17. dbHelper = new SqlHelper("data source='" + path + "\\FIELD_FILES\\Inclusion.db'");
  18. }
  19. /// <summary>
  20. /// 获取SegmentList
  21. /// </summary>
  22. /// <param name="model">Feature</param>
  23. /// <returns></returns>
  24. public List<Particle> GetParticleList(Particle model)
  25. {
  26. //存放查询数据的数据表
  27. SQLiteParameter[] Parameter = new SQLiteParameter[1]
  28. {
  29. new SQLiteParameter("@FieldId", model.FieldId)
  30. };
  31. string sql = "select * from IncAData where Fieldid=@FieldId";
  32. DataTable DT = dbHelper.ExecuteDataTable(sql, Parameter);
  33. var result = new List<Particle>();
  34. foreach (DataRow dr in DT.Rows)
  35. {
  36. Particle item = new Particle() { };
  37. result.Add(item);
  38. }
  39. return result;
  40. }
  41. /// <summary>
  42. /// 获取ParticleListBy
  43. /// </summary>
  44. /// <param name="model">Feature</param>
  45. /// <returns></returns>
  46. public List<Particle> GetParticleListByCon(string con, string max, string min, int display)
  47. {
  48. //string sqlp = "select a.*,b.XrayData from IncAData a left join XRayData b on a.FieldId =b.FieldId and a.XrayId= b.XrayIndex";
  49. //string where = " where 1=1 ";
  50. string sqlp = "select a.* from IncAData a ";
  51. string where = " where 1=1 ";
  52. if (display == 1)
  53. {
  54. where = where + " and a.XrayId >-1 ";
  55. }
  56. if (con != "")
  57. {
  58. where = where + " and a." + con + ">" + min + " and a." + con + "<" + max;
  59. }
  60. sqlp = sqlp + where;
  61. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  62. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  63. return listp;
  64. }
  65. /// <summary>
  66. /// 获取ParticleList
  67. /// </summary>
  68. /// <param name="model">Feature</param>
  69. /// <returns></returns>
  70. public DataTable GetParticleListAndEm()
  71. {
  72. string sqlp = @"select *,
  73. (select group_concat(name||'-'||Percentage,';')
  74. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element
  75. from INcAData where xrayid>-1";
  76. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  77. return DT;
  78. }
  79. /// <summary>
  80. /// 获取ParticleList
  81. /// </summary>
  82. /// <param name="model">Feature</param>
  83. /// <returns></returns>
  84. public DataTable GetMergedParticleInfo()
  85. {
  86. string sqlp = @"select *,
  87. (select group_concat(name||'-'||Percentage,';')
  88. from ElementChemistry where XRayId =MergedParticleInfo.XRayId and fieldid=MergedParticleInfo.fieldid ) as Element
  89. from MergedParticleInfo ";
  90. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  91. return DT;
  92. }
  93. /// <summary>
  94. /// 获取ParticleList
  95. /// </summary>
  96. /// <param name="model">Feature</param>
  97. /// <returns></returns>
  98. public DataTable GetParticleListByIncA(string con)
  99. {
  100. string sqlp = @"select TypeId,TypeName,TypeColor,count(1) as con,sum(Area) as Area,avg(" + con
  101. + ") as av,max(" + con
  102. + ") as max ";
  103. sqlp = sqlp + " from IncAData group by TypeId";
  104. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  105. return DT;
  106. }
  107. /// <summary>
  108. /// 获取ParticleList
  109. /// </summary>
  110. /// <param name="model">Feature</param>
  111. /// <returns></returns>
  112. public DataTable GetParticleListForParticlSize(string con, string fieldAndPartic)
  113. {
  114. string sqlp = @"select TypeId,TypeName,TypeColor,count(1) as con,avg(" + con
  115. + ") as av,max(" + con
  116. + ") as max ,GroupName";
  117. sqlp = sqlp + " from IncAData ";
  118. if (fieldAndPartic != "")
  119. {
  120. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  121. }
  122. sqlp = sqlp + " group by TypeId ";
  123. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  124. if (con == "area")
  125. {
  126. for (int i = 0; i < DT.Rows.Count; i++)
  127. {
  128. DT.Rows[i]["max"] = Math.Sqrt((double)DT.Rows[i]["max"] / Math.PI) * 2;
  129. }
  130. }
  131. return DT;
  132. }
  133. /// <summary>
  134. /// 获取element含量
  135. /// </summary>
  136. /// <returns></returns>
  137. public DataTable GetElementForArea(string fieldAndPartic)
  138. {
  139. string sqlp = @"select e.name,sum(e.percentage*p.area) as earea from ElementChemistry e
  140. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid";
  141. if (fieldAndPartic != "")
  142. {
  143. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  144. }
  145. sqlp = sqlp + " group by e.name order by sum(e.percentage*p.area) desc";
  146. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  147. return DT;
  148. }
  149. /// <summary>
  150. /// 获取element含量
  151. /// </summary>
  152. /// <returns></returns>
  153. public DataTable GetSmallElementForArea()
  154. {
  155. string sqlp = @"select e.name, sum(e.percentage*i.area) as earea from elementchemistry e inner join incadata i on e.xrayid = i.xrayid and e.fieldid = i.fieldid
  156. group by e.name order by sum(e.percentage*i.area) desc";
  157. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  158. return DT;
  159. }
  160. /// <summary>
  161. /// 获取所有Particle
  162. /// </summary>
  163. /// <returns></returns>
  164. public DataTable GetParticleAll(string fieldAndPartic)
  165. {
  166. string sqlp = @"select * from INcAData";
  167. if (fieldAndPartic != "")
  168. {
  169. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  170. }
  171. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  172. for (int i=0;i< DT.Rows.Count;i++)
  173. {
  174. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"]/Math.PI) * 2;
  175. }
  176. return DT;
  177. }
  178. /// <summary>
  179. /// 查找所有颗粒的颜色、面积、种类名称信息
  180. /// </summary>
  181. /// <returns></returns>
  182. public DataTable GetParticleTypeInformation()
  183. {
  184. string sqlp = @"select Area,TypeName,TypeColor from IncAData";
  185. DataTable dt = dbHelper.ExecuteDataTable(sqlp, null);
  186. return dt;
  187. }
  188. /// <summary>
  189. /// 查找IncaData表中所有种类
  190. /// </summary>
  191. /// <returns></returns>
  192. public DataTable GetParticleClassAll()
  193. {
  194. string sqlp = @"SELECT DISTINCT TypeName FROM IncaData";
  195. DataTable dt = dbHelper.ExecuteDataTable(sqlp, null);
  196. return dt;
  197. }
  198. /// <summary>
  199. /// 获取所有Particle
  200. /// </summary>
  201. /// <returns></returns>
  202. public DataTable GetParticleAllHaveXray(string fieldAndPartic)
  203. {
  204. string incaSql = @"select * from IncAData";
  205. if (fieldAndPartic != "")
  206. {
  207. incaSql = incaSql + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  208. }
  209. DataTable incaDT = dbHelper.ExecuteDataTable(incaSql, null);
  210. incaDT.Columns.Add("Element");
  211. DataTable dt_element = GetElementChemistry();
  212. DataTable elementchemistry = dt_element.Clone();
  213. for (int i = 0; i < dt_element.Rows.Count; i++)
  214. {
  215. if (dt_element.Rows[i]["Name"].ToString() != "Fe")
  216. {
  217. elementchemistry.Rows.Add(dt_element.Rows[i].ItemArray);
  218. }
  219. }
  220. for (int i = 0; i < incaDT.Rows.Count; i++)
  221. {
  222. string str = "XRayId = " + incaDT.Rows[i]["particleId"].ToString() + " and fieldid = " + incaDT.Rows[i]["fieldid"].ToString();
  223. DataRow[] drs = elementchemistry.Select(str);
  224. string ConcatenatedString = "";
  225. for (int j = 0; j < drs.Length; j++)
  226. {
  227. ConcatenatedString += drs[j]["name"] + "-" + ChangeDataToD(drs[j]["Percentage"].ToString()) + ';';
  228. }
  229. incaDT.Rows[i]["Element"] = ConcatenatedString;
  230. }
  231. return incaDT;
  232. }
  233. /// <summary>
  234. /// 获取所有Particle
  235. /// </summary>
  236. /// <returns></returns>
  237. public DataTable GetParticleHaveXray(string fieldAndPartic)
  238. {
  239. string sqlp = @"select *,
  240. (select group_concat(name||'-'||Percentage,';')
  241. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element from INcAData where xrayid>-1 ";
  242. if (fieldAndPartic != "")
  243. {
  244. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  245. }
  246. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  247. for (int i = 0; i < DT.Rows.Count; i++)
  248. {
  249. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"] / Math.PI) * 2;
  250. }
  251. return DT;
  252. }
  253. /// <summary>
  254. /// 获取所有Particle
  255. /// </summary>
  256. /// <returns></returns>
  257. public List<Particle> GetParticleAllList()
  258. {
  259. string sqlp = @"select * from INcAData";
  260. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  261. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  262. return listp;
  263. }
  264. /// <summary>
  265. /// 获取所有分类的面积
  266. /// </summary>
  267. /// <param name="fieldAndPartic">选择颗粒</param>
  268. /// <returns></returns>
  269. public DataTable GetAreaByAllIncA(string fieldAndPartic)
  270. {
  271. string sqlp = @"select TypeId,TypeName,sum(area) as ar,count(1) as con ,GroupName from INcAData";
  272. if (fieldAndPartic != "")
  273. {
  274. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  275. }
  276. sqlp = sqlp + " group by TypeId";
  277. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  278. return DT;
  279. }
  280. /// <summary>
  281. /// 获取所有大颗粒没有的小颗粒分类
  282. /// </summary>
  283. /// <param name="fieldAndPartic">选择颗粒</param>
  284. /// <returns></returns>
  285. public DataTable GetSmallParticleInfo()
  286. {
  287. string sqlp = @"select TypeId,TypeName,TypeColor,sum(area) as area,sum(ParticleQuant) as ParticleQuant from SmallParticleInfo where TypeId not in(select TypeId from INcAData) group by TypeId";
  288. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  289. return DT;
  290. }
  291. /// <summary>
  292. /// 获取不同分类的面积
  293. /// </summary>
  294. /// <param name="fieldAndPartic">选择颗粒</param>
  295. /// <returns></returns>
  296. public DataTable GetAreaByIncA(string TypeId, string fieldAndPartic)
  297. {
  298. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  299. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid where p.TypeId=" + TypeId + " ";
  300. if (fieldAndPartic != "")
  301. {
  302. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  303. }
  304. sqlp = sqlp + " group by e.name";
  305. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  306. return DT;
  307. }
  308. public DataTable GetAreaByIncA_All()
  309. {
  310. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  311. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid group by e.name ";
  312. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  313. return DT;
  314. }
  315. /// <summary>
  316. /// 获取全部的物质大类
  317. /// </summary>
  318. /// <returns></returns>
  319. public DataTable GetAllClass()
  320. {
  321. string sqlp = @"select GroupName from IncAData group by GroupName order by count(1) desc";
  322. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  323. return DT;
  324. }
  325. /// <summary>
  326. /// 获取所有元素
  327. /// </summary>
  328. /// <param name="model">Feature</param>
  329. /// <returns></returns>
  330. public DataTable GetAllElement()
  331. {
  332. string sqlp = @"select name from ElementChemistry group by name order by count(1) desc";
  333. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  334. return DT;
  335. }
  336. /// <summary>
  337. /// 获取常用夹杂物分类信息
  338. /// </summary>
  339. /// <returns></returns>
  340. public DataTable GetCommonlyUsedClassifyData()
  341. {
  342. string sqlp = @"select
  343. (select count(typeid) from incadata where typeid BETWEEN 10100 and 10199 and typeid BETWEEN 12200 and 12299 and typeid BETWEEN 11300 and 11299 ) as SPINEL ,
  344. (select count(typeid) from incadata where typeid BETWEEN 10000 and 10999 ) as OXIDE ,
  345. (select count(typeid) from incadata where typeid BETWEEN 11200 and 11299 and typeid BETWEEN 11400 and 11499 and typeid BETWEEN 11200 and 11599 ) as SULFIDE_OXIDE ,
  346. (select count(typeid) from incadata where typeid BETWEEN 12000 and 12999 ) as NITRIDE ,
  347. (select count(typeid) from incadata where typeid BETWEEN 11000 and 11999 ) as SULFIDE ";
  348. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  349. return DT;
  350. }
  351. public bool DeleteFromData(string fieldid, string particleid)
  352. {
  353. string sqlp = @"delete from IncAData where FieldId="+ fieldid + " and ParticleId="
  354. + particleid;
  355. if (dbHelper.ExecuteQuery_bool(sqlp))
  356. {
  357. return true;
  358. }else
  359. {
  360. return false;
  361. }
  362. }
  363. /// <summary>
  364. /// 获取颗粒信息
  365. /// </summary>
  366. /// <param name="model">Feature</param>
  367. /// <returns></returns>
  368. public Particle GetParticleByFidAndPid(string fieldid, string particleid)
  369. {
  370. string sqlp = @"select *,(select xraydata from xraydata where xrayindex=INcAData.xrayid and fieldid="
  371. + fieldid + ") as XRayData,(select group_concat(name || '_' || Percentage, ';')from ElementChemistry where XRayId = INcAData.XRayId and fieldid ="
  372. + fieldid + ") as Element from INcAData where fieldid="
  373. + fieldid + " and particleid= " + particleid;
  374. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  375. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  376. Particle particle = new Particle();
  377. if (listp.Count > 0)
  378. {
  379. particle = listp[0];
  380. List<Element> ElementList = new List<Element>();
  381. string element = DT.Rows[0]["Element"].ToString();
  382. for (int i = 0; i < element.Split(';').Count(); i++)
  383. {
  384. if (element.Split(';')[i] != "")
  385. {
  386. Element ele = new Element() { Name = element.Split(';')[i].Split('_')[0], Percentage = Convert.ToDouble(element.Split(';')[i].Split('_')[1]) };
  387. ElementList.Add(ele);
  388. }
  389. }
  390. particle.ElementList = ElementList;
  391. }
  392. return particle;
  393. }
  394. public Particle GetParticleXrayDataByFidAndPid(string fieldid, string xrayid)
  395. {
  396. string sqlp = @"select xraydata from xraydata where xrayindex="+xrayid+" and fieldid="
  397. + fieldid ;
  398. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  399. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  400. if (listp.Count > 0)
  401. {
  402. return listp[0];
  403. }
  404. else
  405. {
  406. return null;
  407. }
  408. }
  409. public enum PARTCLE_TYPE
  410. {
  411. SMALL = 0,//过小颗粒
  412. OVERSIZE = 1,//过大颗粒
  413. AVE_GRAY_NOT_INRANRE = 2,//亮度不在分析范围内的颗粒
  414. SEARCH_X_RAY = 3,
  415. LOW_COUNT = 4,//低计数率颗粒
  416. NO_INTEREST_ELEMENTS = 5,
  417. NO_ANALYSIS_X_RAY = 6,//不含分析元素
  418. NOT_IDENTIFIED_SIC = 7,//非夹杂物颗粒SiC
  419. NOT_IDENTIFIED_FEO = 8,//非夹杂物颗粒FeO
  420. NOT_IDENTIFIED = 9,//未识别颗粒
  421. IDENTIFIED = 10,//分析颗粒,当为可识别类型时,可以被进一步识别为用户类型(1000以上),系统预定义类型(10000以上),所以最终颗粒类型不会为8,但可能为7
  422. USER_DEFINED_MIN = 1000,
  423. SYS_DEFINED_MIN = 10000
  424. }
  425. #region 分页添加读取数据库函数
  426. /// <summary>
  427. /// 获取分页查询所需信息
  428. /// </summary>
  429. /// <param name=""></param>
  430. /// <param name=""></param>
  431. /// <returns></returns>
  432. public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction, string condition)
  433. {
  434. int p = (currentPage - 1) * pagesize;
  435. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX as 'SEMPosX',FieldPosY as 'SEMPosY',ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticleInfo.XRayId and fieldid = MergedParticleInfo.fieldid) as Element from MergedParticleInfo where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles,(select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, ','),'') from MergedParticleInfo)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " + condition + " order by " + OrderFunction + " limit " + pagesize.ToString() + " offset " + p.ToString();
  436. DataTable DT=new DataTable();
  437. DT = dbHelper.ExecuteQuery(sqliteString);
  438. return DT;
  439. }
  440. public DataTable GetIncaSurfaceData(DataTable dt,int sel,List<string> lst_str)
  441. {
  442. DataTable particlesAll = new DataTable();
  443. if (sel==0)
  444. {
  445. particlesAll = GetInfoForPartucleDevidePage2("");
  446. }
  447. else
  448. {
  449. particlesAll = dt.Copy();
  450. particlesAll.Columns.Add("Element");
  451. }
  452. DataTable dt_element = GetElementChemistry();
  453. DataTable elementchemistry = dt_element.Copy();
  454. List<int> list_int = new List<int>();
  455. for (int a=0;a<lst_str.Count;a++)
  456. {
  457. for (int i = 0; i < dt_element.Rows.Count; i++)
  458. {
  459. if (elementchemistry.Rows[i]["Name"].ToString() == lst_str[a].ToString())
  460. {
  461. list_int.Add(i);
  462. }
  463. }
  464. }
  465. for (int i=0;i< list_int.Count;i++)
  466. {
  467. elementchemistry.Rows[list_int[i]].Delete();
  468. }
  469. elementchemistry.AcceptChanges();
  470. for (int i = 0; i < particlesAll.Rows.Count; i++)
  471. {
  472. string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
  473. DataRow[] drs = elementchemistry.Select(str);
  474. string ConcatenatedString = "";
  475. for (int j = 0; j < drs.Length; j++)
  476. {
  477. ConcatenatedString += drs[j]["name"] + "-" + ChangeDataToD(drs[j]["Percentage"].ToString()) + ';';
  478. }
  479. particlesAll.Rows[i]["Element"] = ConcatenatedString;
  480. }
  481. //string sqlliteString = "select * from INcAData";
  482. //DataTable dt = dbHelper.ExecuteQuery(sqlliteString);
  483. return particlesAll;
  484. }
  485. /// <summary>
  486. /// 保留两位小数
  487. /// </summary>
  488. /// <param name="strData"></param>
  489. /// <returns></returns>
  490. private string ChangeDataToD(string strData)
  491. {
  492. Decimal dData = 0.00M;
  493. if (strData.Contains("E"))
  494. {
  495. dData = Convert.ToDecimal(Decimal.Parse(strData.ToString(), System.Globalization.NumberStyles.Float));
  496. }
  497. else
  498. {
  499. return Convert.ToDouble(strData).ToString("0.00");
  500. }
  501. return Convert.ToDouble(dData).ToString("0.00");
  502. }
  503. public DataTable GetInfoForPartucleDevidePage2(string condition)
  504. {
  505. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX " +
  506. "as 'SEMPosX',FieldPosY as 'SEMPosY',XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles," +
  507. " (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticleInfo.XRayId and fieldid = MergedParticleInfo.fieldid) " +
  508. "as Element from MergedParticleInfo where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId," +
  509. "SegmentNum,SEMPosX,SEMPosY,XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles,'' " +
  510. "as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, ','),'') from MergedParticleInfo)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " +
  511. condition;
  512. DataTable DT = new DataTable();
  513. DT = dbHelper.ExecuteQuery(sqliteString);
  514. return DT;
  515. }
  516. public DataTable GetInfoForPartucleDevidePage_analyticalParticle(string condition)
  517. {
  518. 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 typeid !=9 and typeid !=-1 " +
  519. condition;
  520. DataTable DT = new DataTable();
  521. DT = dbHelper.ExecuteQuery(sqliteString);
  522. return DT;
  523. }
  524. public DataTable GetInfoForPartucleDevidePage_otherParticle(string condition)
  525. {
  526. 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 (typeid =9 or typeid =-1) " +
  527. condition;
  528. DataTable DT = new DataTable();
  529. DT = dbHelper.ExecuteQuery(sqliteString);
  530. return DT;
  531. }
  532. public DataTable GetInfoForPartucleDevidePage_mergeParticles(string condition)
  533. {
  534. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX " +
  535. "as 'SEMPosX',FieldPosY as 'SEMPosY',XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles,''as Element from MergedParticleInfo where 1=1 " + condition;
  536. DataTable DT = new DataTable();
  537. DT = dbHelper.ExecuteQuery(sqliteString);
  538. return DT;
  539. }
  540. public DataTable GetElementChemistry()
  541. {
  542. string sqliteString = "select * from ElementChemistry";
  543. DataTable DT = new DataTable();
  544. DT = dbHelper.ExecuteQuery(sqliteString);
  545. return DT;
  546. }
  547. public Bitmap GetBitmapForBig(string sub, double xs, string path,int picHeight,int picWidth)
  548. {
  549. string vs = "," + sub.Replace(':', '-') + ",";
  550. DataTable dataTable = GetParticleAll(vs);
  551. if (dataTable.Rows.Count == 0)
  552. {
  553. return null;
  554. }
  555. //内接矩形
  556. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  557. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  558. double min_Y = max_Y;
  559. double min_X = max_X;
  560. //拼接field矩形
  561. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  562. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  563. double MIN_Y = MAX_Y;
  564. double MIN_X = MAX_X;
  565. foreach (DataRow item in dataTable.Rows)
  566. {
  567. //颗粒外接矩形
  568. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  569. if (lefttopX < min_X)
  570. {
  571. min_X = lefttopX;
  572. }
  573. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  574. {
  575. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  576. }
  577. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  578. if (max_Y < lrfttopY)
  579. {
  580. max_Y = lrfttopY;
  581. }
  582. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  583. {
  584. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  585. }
  586. //画布
  587. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  588. if (lefttopXH > MAX_X)
  589. {
  590. MAX_X = lefttopXH;
  591. }
  592. if (lefttopXH < MIN_X)
  593. {
  594. MIN_X = lefttopXH;
  595. }
  596. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  597. if (MAX_Y < lrfttopYH)
  598. {
  599. MAX_Y = lrfttopYH;
  600. }
  601. if (MIN_Y > lrfttopYH)
  602. {
  603. MIN_Y = lrfttopYH;
  604. }
  605. }
  606. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + picWidth;
  607. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + picHeight;
  608. //构造最终的图片白板
  609. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  610. Graphics graph = Graphics.FromImage(tableChartImage);
  611. //初始化这个大图
  612. graph.DrawImage(tableChartImage, 0, 0);
  613. int width = Convert.ToInt32(max_X - min_X);
  614. int height = Convert.ToInt32(max_Y - min_Y);
  615. int X = Convert.ToInt32(min_X - MIN_X);
  616. int Y = Convert.ToInt32(MAX_Y - max_Y);
  617. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  618. foreach (DataRow item in dataTable.Rows)
  619. {
  620. string filePath = path + "\\FIELD_FILES\\";
  621. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  622. //然后将取出的数据,转换成Bitmap对象
  623. Bitmap ls_bt = ReadImageFile(imagePath);
  624. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  625. int y = Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MIN_Y);
  626. graph.DrawImage(ls_bt, x, y);
  627. }
  628. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  629. return bmap;
  630. }
  631. /// <summary>
  632. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  633. /// </summary>
  634. /// <param name="path"></param>
  635. /// <returns></returns>
  636. public Bitmap ReadImageFile(string path)
  637. {
  638. if (!File.Exists(path))
  639. {
  640. return null;//文件不存在
  641. }
  642. FileStream fs = File.OpenRead(path); //OpenRead
  643. int filelength = 0;
  644. filelength = (int)fs.Length; //获得文件长度
  645. Byte[] image = new Byte[filelength]; //建立一个字节数组
  646. fs.Read(image, 0, filelength); //按字节流读取
  647. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  648. fs.Close();
  649. Bitmap bit = new Bitmap(result);
  650. return bit;
  651. }
  652. /// <summary>
  653. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  654. /// </summary>
  655. /// <param name="in_cotsparticleclr"></param>
  656. /// <returns></returns>
  657. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  658. {
  659. //为了能把整个颗粒显示完整
  660. offset_rect.X = offset_rect.X - 5;
  661. offset_rect.Y = offset_rect.Y - 5;
  662. offset_rect.Width = offset_rect.Width + 10;
  663. offset_rect.Height = offset_rect.Height + 10;
  664. //防止计算偏差后,有坐标溢出现象
  665. if (offset_rect.X < 0)
  666. offset_rect.X = 0;
  667. if (offset_rect.Y < 0)
  668. offset_rect.Y = 0;
  669. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  670. {
  671. offset_rect.Width = ls_bt.Width - offset_rect.X;
  672. }
  673. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  674. {
  675. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  676. }
  677. Bitmap new_ret_bp;
  678. //防止为0后面计算出错
  679. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  680. {
  681. //最后通过list_showsegment组建成新的图片,进行返回
  682. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  683. }
  684. else
  685. {
  686. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  687. }
  688. return new_ret_bp;
  689. }
  690. #endregion
  691. }
  692. public class UserLibraryData
  693. {
  694. private SqlHelper dbHelper;
  695. public UserLibraryData(string libraryName)
  696. {
  697. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  698. if (!libraryName.Contains(".db"))
  699. {
  700. libraryName += ".db";
  701. }
  702. string fullPath = System.IO.Directory.GetCurrentDirectory() + "\\Config\\SysData\\" + libraryName ;
  703. if (System.IO.File.Exists(fullPath))
  704. {
  705. dbHelper = new SqlHelper("data source='" + fullPath + "'");
  706. }
  707. else
  708. {
  709. dbHelper = null;
  710. log.Error("Failed to load user-defined library_"+ fullPath+"!");
  711. }
  712. }
  713. public DataTable GetSubAttributeFromDatabase()
  714. {
  715. string sqliteString = "select STDId,Hardness,Density,Electrical_conductivity from ClassifySTD";
  716. DataTable DT = new DataTable();
  717. DT = dbHelper.ExecuteQuery(sqliteString);
  718. return DT;
  719. }
  720. public SqlHelper GetSqlHelper() { return dbHelper; }
  721. }
  722. }