ParticleData.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. using OTSCommon.DBOperate.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 where typeid !=-1 and typeid !=4 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 where typeid !=-1 and typeid !=4";
  118. if (fieldAndPartic != "")
  119. {
  120. sqlp = sqlp + " and '" + 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 from IncAData where typeid !=-1 and typeid !=4";
  141. if (fieldAndPartic != "")
  142. {
  143. sqlp = sqlp + " and '" + 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 where typeid !=-1 and typeid !=4";
  167. if (fieldAndPartic != "")
  168. {
  169. sqlp = sqlp + " and '" + 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. public DataTable GetParticleAllforparticlelist(string fieldAndPartic)
  179. {
  180. string sqlp = @"select * from INcAData";
  181. if (fieldAndPartic != "")
  182. {
  183. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  184. }
  185. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  186. for (int i = 0; i < DT.Rows.Count; i++)
  187. {
  188. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"] / Math.PI) * 2;
  189. }
  190. return DT;
  191. }
  192. /// <summary>
  193. /// 查找所有颗粒的颜色、面积、种类名称信息
  194. /// </summary>
  195. /// <returns></returns>
  196. public DataTable GetParticleTypeInformation()
  197. {
  198. string sqlp = @"select Area,TypeName,TypeColor from IncAData";
  199. DataTable dt = dbHelper.ExecuteDataTable(sqlp, null);
  200. return dt;
  201. }
  202. /// <summary>
  203. /// 查找IncaData表中所有种类
  204. /// </summary>
  205. /// <returns></returns>
  206. public DataTable GetParticleClassAll()
  207. {
  208. string sqlp = @"SELECT DISTINCT TypeName FROM IncaData";
  209. DataTable dt = dbHelper.ExecuteDataTable(sqlp, null);
  210. return dt;
  211. }
  212. /// <summary>
  213. /// 获取所有Particle
  214. /// </summary>
  215. /// <returns></returns>
  216. public DataTable GetParticleAllHaveXray(string fieldAndPartic)
  217. {
  218. string incaSql = @"select * from IncAData where typeid !=-1 and typeid !=4";
  219. if (fieldAndPartic != "")
  220. {
  221. incaSql = incaSql + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  222. }
  223. DataTable incaDT = dbHelper.ExecuteDataTable(incaSql, null);
  224. incaDT.Columns.Add("Element");
  225. DataTable dt_element = GetElementChemistry();
  226. DataTable elementchemistry = dt_element.Clone();
  227. for (int i = 0; i < dt_element.Rows.Count; i++)
  228. {
  229. if (dt_element.Rows[i]["Name"].ToString() != "Fe")
  230. {
  231. elementchemistry.Rows.Add(dt_element.Rows[i].ItemArray);
  232. }
  233. }
  234. for (int i = 0; i < incaDT.Rows.Count; i++)
  235. {
  236. string str = "XRayId = " + incaDT.Rows[i]["particleId"].ToString() + " and fieldid = " + incaDT.Rows[i]["fieldid"].ToString();
  237. DataRow[] drs = elementchemistry.Select(str);
  238. string ConcatenatedString = "";
  239. for (int j = 0; j < drs.Length; j++)
  240. {
  241. ConcatenatedString += drs[j]["name"] + "-" + ChangeDataToD(drs[j]["Percentage"].ToString()) + ';';
  242. }
  243. incaDT.Rows[i]["Element"] = ConcatenatedString;
  244. }
  245. return incaDT;
  246. }
  247. /// <summary>
  248. /// 获取所有Particle
  249. /// </summary>
  250. /// <returns></returns>
  251. public DataTable GetParticleHaveXray(string fieldAndPartic)
  252. {
  253. string sqlp = @"select *,
  254. (select group_concat(name||'-'||Percentage,';')
  255. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element from INcAData where xrayid>-1 ";
  256. if (fieldAndPartic != "")
  257. {
  258. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  259. }
  260. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  261. for (int i = 0; i < DT.Rows.Count; i++)
  262. {
  263. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"] / Math.PI) * 2;
  264. }
  265. return DT;
  266. }
  267. /// <summary>
  268. /// 获取所有Particle
  269. /// </summary>
  270. /// <returns></returns>
  271. public List<Particle> GetParticleAllList()
  272. {
  273. string sqlp = @"select * from INcAData";
  274. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  275. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  276. return listp;
  277. }
  278. /// <summary>
  279. /// 获取所有分类的面积
  280. /// </summary>
  281. /// <param name="fieldAndPartic">选择颗粒</param>
  282. /// <returns></returns>
  283. public DataTable GetAreaByAllIncA(string fieldAndPartic)
  284. {
  285. string sqlp = @"select TypeId,TypeName,sum(area) as ar,count(1) as con ,GroupName from INcAData where typeid !=-1 and typeid !=4";
  286. if (fieldAndPartic != "")
  287. {
  288. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  289. }
  290. sqlp = sqlp + " group by TypeId";
  291. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  292. return DT;
  293. }
  294. /// <summary>
  295. /// 获取所有大颗粒没有的小颗粒分类
  296. /// </summary>
  297. /// <param name="fieldAndPartic">选择颗粒</param>
  298. /// <returns></returns>
  299. public DataTable GetSmallParticleInfo()
  300. {
  301. 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";
  302. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  303. return DT;
  304. }
  305. /// <summary>
  306. /// 获取不同分类的面积
  307. /// </summary>
  308. /// <param name="fieldAndPartic">选择颗粒</param>
  309. /// <returns></returns>
  310. public DataTable GetAreaByIncA(string TypeId, string fieldAndPartic)
  311. {
  312. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  313. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid where p.TypeId=" + TypeId + " ";
  314. if (fieldAndPartic != "")
  315. {
  316. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  317. }
  318. sqlp = sqlp + " group by e.name";
  319. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  320. return DT;
  321. }
  322. public DataTable GetAreaByIncA_All()
  323. {
  324. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  325. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid group by e.name ";
  326. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  327. return DT;
  328. }
  329. /// <summary>
  330. /// 获取全部的物质大类
  331. /// </summary>
  332. /// <returns></returns>
  333. public DataTable GetAllClass()
  334. {
  335. string sqlp = @"select GroupName from IncAData group by GroupName order by count(1) desc";
  336. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  337. return DT;
  338. }
  339. /// <summary>
  340. /// 获取所有元素
  341. /// </summary>
  342. /// <param name="model">Feature</param>
  343. /// <returns></returns>
  344. public DataTable GetAllElement()
  345. {
  346. string sqlp = @"select name from ElementChemistry group by name order by count(1) desc";
  347. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  348. return DT;
  349. }
  350. /// <summary>
  351. /// 获取常用夹杂物分类信息
  352. /// </summary>
  353. /// <returns></returns>
  354. public DataTable GetCommonlyUsedClassifyData()
  355. {
  356. string sqlp = @"select
  357. (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 ,
  358. (select count(typeid) from incadata where typeid BETWEEN 10000 and 10999 ) as OXIDE ,
  359. (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 ,
  360. (select count(typeid) from incadata where typeid BETWEEN 12000 and 12999 ) as NITRIDE ,
  361. (select count(typeid) from incadata where typeid BETWEEN 11000 and 11999 ) as SULFIDE ";
  362. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  363. return DT;
  364. }
  365. public bool DeleteFromData(string fieldid, string particleid)
  366. {
  367. string sqlp = @"delete from IncAData where FieldId=" + fieldid + " and ParticleId="
  368. + particleid;
  369. if (dbHelper.ExecuteQuery_bool(sqlp))
  370. {
  371. return true;
  372. }
  373. else
  374. {
  375. return false;
  376. }
  377. }
  378. /// <summary>
  379. /// 获取颗粒信息
  380. /// </summary>
  381. /// <param name="model">Feature</param>
  382. /// <returns></returns>
  383. public Particle GetParticleByFidAndPid(string fieldid, string particleid)
  384. {
  385. string sqlp = @"select *,(select xraydata from xraydata where xrayindex=INcAData.xrayid and fieldid="
  386. + fieldid + ") as XRayData,(select group_concat(name || '_' || Percentage, ';')from ElementChemistry where XRayId = INcAData.XRayId and fieldid ="
  387. + fieldid + ") as Element from INcAData where fieldid="
  388. + fieldid + " and particleid= " + particleid;
  389. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  390. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  391. Particle particle = new Particle();
  392. if (listp.Count > 0)
  393. {
  394. particle = listp[0];
  395. List<Element> ElementList = new List<Element>();
  396. string element = DT.Rows[0]["Element"].ToString();
  397. for (int i = 0; i < element.Split(';').Count(); i++)
  398. {
  399. if (element.Split(';')[i] != "")
  400. {
  401. Element ele = new Element() { Name = element.Split(';')[i].Split('_')[0], Percentage = Convert.ToDouble(element.Split(';')[i].Split('_')[1]) };
  402. ElementList.Add(ele);
  403. }
  404. }
  405. particle.ElementList = ElementList;
  406. }
  407. return particle;
  408. }
  409. public Particle GetParticleXrayDataByFidAndPid(string fieldid, string xrayid)
  410. {
  411. string sqlp = @"select xraydata from xraydata where xrayindex=" + xrayid + " and fieldid="
  412. + fieldid;
  413. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  414. List<Particle> listp = dbHelper.TableToList<Particle>(DT);
  415. if (listp.Count > 0)
  416. {
  417. return listp[0];
  418. }
  419. else
  420. {
  421. return null;
  422. }
  423. }
  424. public enum PARTCLE_TYPE
  425. {
  426. SMALL = 0,//过小颗粒
  427. OVERSIZE = 1,//过大颗粒
  428. AVE_GRAY_NOT_INRANRE = 2,//亮度不在分析范围内的颗粒
  429. SEARCH_X_RAY = 3,
  430. LOW_COUNT = 4,//低计数率颗粒
  431. NO_INTEREST_ELEMENTS = 5,
  432. NO_ANALYSIS_X_RAY = 6,//不含分析元素
  433. NOT_IDENTIFIED_SIC = 7,//非夹杂物颗粒SiC
  434. NOT_IDENTIFIED_FEO = 8,//非夹杂物颗粒FeO
  435. NOT_IDENTIFIED = 9,//未识别颗粒
  436. IDENTIFIED = 10,//分析颗粒,当为可识别类型时,可以被进一步识别为用户类型(1000以上),系统预定义类型(10000以上),所以最终颗粒类型不会为8,但可能为7
  437. USER_DEFINED_MIN = 1000,
  438. SYS_DEFINED_MIN = 10000
  439. }
  440. #region 分页添加读取数据库函数
  441. /// <summary>
  442. /// 获取分页查询所需信息
  443. /// </summary>
  444. /// <param name=""></param>
  445. /// <param name=""></param>
  446. /// <returns></returns>
  447. public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction, string condition)
  448. {
  449. int p = (currentPage - 1) * pagesize;
  450. 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();
  451. DataTable DT = new DataTable();
  452. DT = dbHelper.ExecuteQuery(sqliteString);
  453. return DT;
  454. }
  455. public DataTable GetIncaSurfaceData( List<string> lst_str)
  456. {
  457. DataTable particlesAll = GetInfoForPartucleDevidePage2("");
  458. DataTable dt_element = GetElementChemistry();
  459. List<int> list_int = new List<int>();
  460. for (int a = 0; a < lst_str.Count; a++)
  461. {
  462. for (int i = 0; i < dt_element.Rows.Count; i++)
  463. {
  464. if (dt_element.Rows[i]["Name"].ToString() == lst_str[a].ToString())
  465. {
  466. list_int.Add(i);
  467. }
  468. }
  469. }
  470. for (int i = 0; i < list_int.Count; i++)
  471. {
  472. dt_element.Rows[list_int[i]].Delete();
  473. }
  474. dt_element.AcceptChanges();
  475. for (int i = 0; i < particlesAll.Rows.Count; i++)
  476. {
  477. string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
  478. DataRow[] drs = dt_element.Select(str);
  479. string ConcatenatedString = "";
  480. for (int j = 0; j < drs.Length; j++)
  481. {
  482. ConcatenatedString += drs[j]["name"] + "-" + ChangeDataToD(drs[j]["Percentage"].ToString()) + ';';
  483. }
  484. particlesAll.Rows[i]["Element"] = ConcatenatedString;
  485. }
  486. //string sqlliteString = "select * from INcAData";
  487. //DataTable dt = dbHelper.ExecuteQuery(sqlliteString);
  488. return particlesAll;
  489. }
  490. /// <summary>
  491. /// 保留两位小数
  492. /// </summary>
  493. /// <param name="strData"></param>
  494. /// <returns></returns>
  495. private string ChangeDataToD(string strData)
  496. {
  497. Decimal dData = 0.00M;
  498. if (strData.Contains("E"))
  499. {
  500. dData = Convert.ToDecimal(Decimal.Parse(strData.ToString(), System.Globalization.NumberStyles.Float));
  501. }
  502. else
  503. {
  504. return Convert.ToDouble(strData).ToString("0.00");
  505. }
  506. return Convert.ToDouble(dData).ToString("0.00");
  507. }
  508. public DataTable GetInfoForPartucleDevidePage2(string condition)
  509. {
  510. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX " +
  511. "as 'SEMPosX',FieldPosY as 'SEMPosY',XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles," +
  512. " (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticleInfo.XRayId and fieldid = MergedParticleInfo.fieldid) " +
  513. "as Element from MergedParticleInfo where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId," +
  514. "SegmentNum,SEMPosX,SEMPosY,XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles,'' " +
  515. "as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, ','),'') from MergedParticleInfo)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " +
  516. condition;
  517. DataTable DT = new DataTable();
  518. DT = dbHelper.ExecuteQuery(sqliteString);
  519. return DT;
  520. }
  521. public DataTable GetInfoForPartucleDevidePage_analyticalParticle(string condition)
  522. {
  523. 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 and typeid !=4" +
  524. condition;
  525. DataTable DT = new DataTable();
  526. DT = dbHelper.ExecuteQuery(sqliteString);
  527. return DT;
  528. }
  529. public DataTable GetInfoForPartucleDevidePage_otherParticle(string condition)
  530. {
  531. 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 typeid =9 or typeid =-1 or typeid =4 " +
  532. condition;
  533. DataTable DT = new DataTable();
  534. DT = dbHelper.ExecuteQuery(sqliteString);
  535. return DT;
  536. }
  537. public DataTable GetInfoForPartucleDevidePage_mergeParticles(string condition)
  538. {
  539. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX " +
  540. "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;
  541. DataTable DT1 = new DataTable();
  542. try
  543. {
  544. DT1 = dbHelper.ExecuteQuery(sqliteString);
  545. }
  546. catch (Exception ex)
  547. {
  548. }
  549. return DT1;
  550. }
  551. public DataTable GetInfoForPartucleDevidePage_allParticles(string condition)
  552. {
  553. 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 1=1 " +
  554. condition;
  555. DataTable DT = new DataTable();
  556. DT = dbHelper.ExecuteQuery(sqliteString);
  557. return DT;
  558. }
  559. public DataTable GetClassificationOfAllParticles()
  560. {
  561. string sqliteString1 = "select distinct TypeName from INcAData";
  562. DataTable DT = new DataTable();
  563. DT = dbHelper.ExecuteQuery(sqliteString1);
  564. return DT;
  565. }
  566. public DataTable GetXRayData()
  567. {
  568. string sqliteString = @"select * from xraydata";
  569. DataTable DT = new DataTable();
  570. DT = dbHelper.ExecuteQuery(sqliteString);
  571. return DT;
  572. }
  573. public DataTable GetElementChemistry()
  574. {
  575. string sqliteString = "select * from ElementChemistry";
  576. DataTable DT = new DataTable();
  577. DT = dbHelper.ExecuteQuery(sqliteString);
  578. return DT;
  579. }
  580. /// <summary>
  581. /// 得到图形形状
  582. /// </summary>
  583. /// <returns></returns>
  584. public DataTable GetSegment()
  585. {
  586. string sqliteString = "select * from Segment";
  587. DataTable DT = new DataTable();
  588. DT = dbHelper.ExecuteQuery(sqliteString);
  589. return DT;
  590. }
  591. public Bitmap GetBitmapForBig(string sub, double xs, string path, int picHeight, int picWidth)
  592. {
  593. string vs = "," + sub.Replace(':', '-') + ",";
  594. DataTable dataTable = GetParticleAll(vs);
  595. if (dataTable.Rows.Count == 0)
  596. {
  597. return null;
  598. }
  599. //内接矩形
  600. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  601. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  602. double min_Y = max_Y;
  603. double min_X = max_X;
  604. //拼接field矩形
  605. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  606. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  607. double MIN_Y = MAX_Y;
  608. double MIN_X = MAX_X;
  609. foreach (DataRow item in dataTable.Rows)
  610. {
  611. //颗粒外接矩形
  612. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  613. if (lefttopX < min_X)
  614. {
  615. min_X = lefttopX;
  616. }
  617. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  618. {
  619. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  620. }
  621. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  622. if (max_Y < lrfttopY)
  623. {
  624. max_Y = lrfttopY;
  625. }
  626. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  627. {
  628. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  629. }
  630. //画布
  631. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  632. if (lefttopXH > MAX_X)
  633. {
  634. MAX_X = lefttopXH;
  635. }
  636. if (lefttopXH < MIN_X)
  637. {
  638. MIN_X = lefttopXH;
  639. }
  640. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  641. if (MAX_Y < lrfttopYH)
  642. {
  643. MAX_Y = lrfttopYH;
  644. }
  645. if (MIN_Y > lrfttopYH)
  646. {
  647. MIN_Y = lrfttopYH;
  648. }
  649. }
  650. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + picWidth;
  651. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + picHeight;
  652. //构造最终的图片白板
  653. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  654. Graphics graph = Graphics.FromImage(tableChartImage);
  655. //初始化这个大图
  656. graph.DrawImage(tableChartImage, 0, 0);
  657. int width = Convert.ToInt32(max_X - min_X);
  658. int height = Convert.ToInt32(max_Y - min_Y);
  659. int X = Convert.ToInt32(min_X - MIN_X);
  660. int Y = Convert.ToInt32(MAX_Y - max_Y);
  661. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  662. foreach (DataRow item in dataTable.Rows)
  663. {
  664. string filePath = path + "\\FIELD_FILES\\";
  665. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  666. //然后将取出的数据,转换成Bitmap对象
  667. Bitmap ls_bt = ReadImageFile(imagePath);
  668. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  669. int y = Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MIN_Y);
  670. graph.DrawImage(ls_bt, x, y);
  671. }
  672. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  673. return bmap;
  674. }
  675. /// <summary>
  676. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  677. /// </summary>
  678. /// <param name="path"></param>
  679. /// <returns></returns>
  680. public Bitmap ReadImageFile(string path)
  681. {
  682. if (!File.Exists(path))
  683. {
  684. return null;//文件不存在
  685. }
  686. FileStream fs = File.OpenRead(path); //OpenRead
  687. int filelength = 0;
  688. filelength = (int)fs.Length; //获得文件长度
  689. Byte[] image = new Byte[filelength]; //建立一个字节数组
  690. fs.Read(image, 0, filelength); //按字节流读取
  691. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  692. fs.Close();
  693. Bitmap bit = new Bitmap(result);
  694. return bit;
  695. }
  696. /// <summary>
  697. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  698. /// </summary>
  699. /// <param name="in_cotsparticleclr"></param>
  700. /// <returns></returns>
  701. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  702. {
  703. //为了能把整个颗粒显示完整
  704. offset_rect.X = offset_rect.X - 10;
  705. offset_rect.Y = offset_rect.Y - 10;
  706. offset_rect.Width = offset_rect.Width + 20;
  707. offset_rect.Height = offset_rect.Height + 20;
  708. //防止计算偏差后,有坐标溢出现象
  709. if (offset_rect.X < 0)
  710. offset_rect.X = 0;
  711. if (offset_rect.Y < 0)
  712. offset_rect.Y = 0;
  713. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  714. {
  715. offset_rect.Width = ls_bt.Width - offset_rect.X;
  716. }
  717. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  718. {
  719. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  720. }
  721. Bitmap new_ret_bp;
  722. //防止为0后面计算出错
  723. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  724. {
  725. //最后通过list_showsegment组建成新的图片,进行返回
  726. new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat);
  727. }
  728. else
  729. {
  730. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  731. }
  732. return new_ret_bp;
  733. }
  734. #endregion
  735. }
  736. public class UserLibraryData
  737. {
  738. private SqlHelper dbHelper;
  739. public UserLibraryData(string libraryName,string resultpath)
  740. {
  741. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  742. if (!libraryName.Contains(".db"))
  743. {
  744. libraryName += ".db";
  745. }
  746. if(libraryName.ToLower()== "nostddb.db")
  747. {
  748. dbHelper = null;
  749. log.Error("Failed to load user-defined library" + "!");
  750. }
  751. string fullPath = resultpath + libraryName;
  752. string fullPath2 = System.IO.Directory.GetCurrentDirectory() + "\\Config\\SysData\\" + libraryName;
  753. if (System.IO.File.Exists(fullPath))
  754. {
  755. dbHelper = new SqlHelper("data source='" + fullPath + "'");
  756. log.Warn("Loading the user standard library:" + fullPath + "!");
  757. }
  758. else if(System.IO.File.Exists(fullPath2))
  759. {
  760. dbHelper = new SqlHelper("data source='" + fullPath2 + "'");
  761. log.Warn("Loading the user standard library:" + fullPath2 + "!");
  762. }
  763. else
  764. {
  765. dbHelper = null;
  766. log.Error("Failed to load user-defined library"+ "!");
  767. }
  768. }
  769. public DataTable GetSubAttributeFromDatabase()
  770. {
  771. string sqliteString = "select STDId,Hardness,Density,Electrical_conductivity from ClassifySTD";
  772. DataTable DT = new DataTable();
  773. DT = dbHelper.ExecuteQuery(sqliteString);
  774. return DT;
  775. }
  776. public SqlHelper GetSqlHelper() { return dbHelper; }
  777. }
  778. }