ParticleData.cs 37 KB

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