ComsStatic.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. using Sunny.UI;
  2. using SuperMap.Data;
  3. using SuperMap.Layout;
  4. using SuperMap.Mapping;
  5. using SuperMap.UI;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using WeifenLuo.WinFormsUI.Docking;
  15. using WWPipeLine.Commons;
  16. namespace WWPipeLine.MapBasic
  17. {
  18. public class ComsStatic
  19. {
  20. //public static ComsStatic Instance { get => new ComsStatic(); }
  21. private static ComsStatic m_Instance;
  22. public static ComsStatic Instance
  23. {
  24. get
  25. {
  26. if (m_Instance == null)
  27. m_Instance = new ComsStatic();
  28. return m_Instance;
  29. }
  30. }
  31. //private static Workspace m_Workspace;
  32. //public void SetWorkspace(Workspace ws) { m_Workspace = ws; }
  33. private static MapControl m_MapControl;
  34. public void SetMapControl(MapControl mp) { m_MapControl = mp; }
  35. private static DockPanel m_DockPanel;
  36. public void SetDockPanel(DockPanel dp) { m_DockPanel = dp; }
  37. private static IMessageInterface m_IMessageInterface;
  38. public void SetIMessageInterface(IMessageInterface mi) { m_IMessageInterface = mi; }
  39. #region Workspace MapControl DatasetVector
  40. /// <summary>
  41. /// 工作空间的地图索引值 配置在ini文件中WorkspaceMapIndex
  42. /// </summary>
  43. public static Int32 WorkspaceMapIndex
  44. {
  45. get
  46. {
  47. string index = IniHelper.Read(IniHelper.KEY_WorkspaceMapIndex_PATH);
  48. return StringToInt(index);
  49. }
  50. }
  51. /// <summary>
  52. /// 工作空间的数据源索引值 配置在ini文件中WorkspaceDatasourceIndex
  53. /// </summary>
  54. private static Int32 WorkspaceDatasourceIndex
  55. {
  56. get
  57. {
  58. string index = IniHelper.Read(IniHelper.KEY_WorkspaceDatasourceIndex_PATH);
  59. return StringToInt(index);
  60. }
  61. }
  62. /// <summary>
  63. /// 当前工作空间的数据源索引值
  64. /// <para>0:MapWorld_vec 1:MapWorld_cva 2:DaAnPostgres</para>
  65. /// </summary>
  66. public static Datasource Datasource { get => m_MapControl.Map.Workspace.Datasources[WorkspaceDatasourceIndex]; }
  67. public static Workspace Workspace { get => m_MapControl.Map.Workspace; }
  68. public static MapControl MapControl { get => m_MapControl; }
  69. public static IMessageInterface MessageInterface { get => m_IMessageInterface; }
  70. public static DockPanel DockPanel { get => m_DockPanel; }
  71. /// <summary>
  72. /// 配置表
  73. /// </summary>
  74. public static DatasetVector dvJSLK_PZ
  75. {
  76. get => Datasource.Datasets["JSLK_PZ"] as DatasetVector;
  77. }
  78. /// <summary>
  79. /// 供水节点表
  80. /// </summary>
  81. public static DatasetVector dvJSJDPT
  82. {
  83. get => Datasource.Datasets["JSJDPT"] as DatasetVector;
  84. }
  85. /// <summary>
  86. /// 供水管线表
  87. /// </summary>
  88. public static DatasetVector dvJSLK
  89. {
  90. get => Datasource.Datasets["JSLK"] as DatasetVector;
  91. }
  92. /// <summary>
  93. /// 网络数据集 分析用
  94. /// </summary>
  95. public static DatasetVector dvNetwork
  96. {
  97. get => Datasource.Datasets["CL_PostGreSQL_Network"] as DatasetVector;
  98. }
  99. public static Layers MapLayers { get => m_MapControl.Map.Layers; }
  100. public static DataTable getLayers()
  101. {
  102. DataTable dt = new DataTable { TableName = "LayersDataTable" };
  103. dt.Columns.Add("LayerName"); dt.Columns.Add("LayerCaption"); dt.Columns.Add("LayerDatasetName");
  104. DataRow dataRow;
  105. foreach (Layer r in MapLayers)
  106. {
  107. if (r.Caption.Contains("矢量")) continue;
  108. dataRow = dt.NewRow();
  109. dataRow[0] = r.Name;
  110. dataRow[1] = r.Caption;
  111. dataRow[2] = r.Dataset.Name;
  112. dt.Rows.Add(dataRow);
  113. }
  114. return dt;
  115. }
  116. ///// <summary>
  117. ///// 删除除2个底图外的图层
  118. ///// </summary>
  119. //public static void CreateLayers()
  120. //{
  121. // if (m_MapControl.Map.Layers.Count > 2)
  122. // m_MapControl.Map.Layers.RemoveRange(0, m_MapControl.Map.Layers.Count - 2);
  123. // LayerSettingVector layerSetting;
  124. // GeoStyle style;
  125. // Layer lyr = null;
  126. // foreach (LayerEx layerEx in layerExs)
  127. // {
  128. // Dataset ds = Datasource.Datasets[layerEx.Dataset];
  129. // if (ds is null) continue;
  130. // layerSetting = new LayerSettingVector();
  131. // style = new GeoStyle();
  132. // style.LineColor = layerEx.LineColor;
  133. // style.LineWidth = layerEx.LineWidth;
  134. // layerSetting.Style = style;
  135. // lyr = m_MapControl.Map.Layers.Add(ds, layerSetting, true);
  136. // lyr.Caption = layerEx.Caption;
  137. // }
  138. //}
  139. /// <summary>
  140. /// 设置所有图层为不可编辑状态
  141. /// </summary>
  142. public static void SetLayerIsEditableFalse()
  143. {
  144. foreach (Layer r in MapLayers)
  145. {
  146. r.IsEditable = false;
  147. }
  148. }
  149. public static void SetLayerIsEditableFalse(string layerName, bool isEditable)
  150. {
  151. foreach (Layer r in MapLayers)
  152. {
  153. if (r.Name == layerName || r.Caption == layerName || r.Dataset.Name == layerName)
  154. r.IsEditable = isEditable;
  155. else
  156. r.IsEditable = !isEditable;
  157. }
  158. }
  159. /// <summary>
  160. /// 设置图层是否可选择
  161. /// </summary>
  162. /// <param name="layerName"></param>
  163. /// <param name="isSelect"></param>
  164. public static void SetLayersIsSelectableFalse(string layerName, bool isSelect)
  165. {
  166. foreach (Layer r in MapLayers)
  167. {
  168. if (r.Name == layerName || r.Caption == layerName || r.Dataset.Name == layerName)
  169. r.IsSelectable = isSelect;
  170. else
  171. r.IsSelectable = !isSelect;
  172. }
  173. }
  174. /// <summary>
  175. /// 依据TrackingLayer的Name清空
  176. /// </summary>
  177. /// <param name="tag"></param>
  178. public static void TrackingLayerRemove(string tag)
  179. {
  180. int index = MapControl.Map.TrackingLayer.IndexOf(tag);
  181. while (index != -1)
  182. {
  183. MapControl.Map.TrackingLayer.Remove(index);
  184. index = MapControl.Map.TrackingLayer.IndexOf(tag);
  185. }
  186. }
  187. /// <summary>
  188. /// 关闭当前主程序中除了Tag=MainControls的其他所有窗体
  189. /// <para>默认4个主要窗体: 主窗体 工具栏 鹰眼 图层</para>
  190. /// </summary>
  191. public static void closeFormTagMainControls()
  192. {
  193. foreach (Form form in Application.OpenForms)
  194. {
  195. if (form.Tag?.ToString() != "MainControls")
  196. {
  197. form.Close(); break;
  198. }
  199. }
  200. if (Application.OpenForms.Count > 4)
  201. {
  202. closeFormTagMainControls();
  203. }
  204. }
  205. #endregion
  206. #region SuperMap.Data相关操作
  207. public static Recordset QueryRecordset(DatasetVector dv, string sqlWhere, string[] resultFields = null, string[] groupBy = null, string[] orderBy = null)
  208. {
  209. QueryParameter para = new QueryParameter();
  210. if (sqlWhere != null) para.AttributeFilter = sqlWhere;
  211. if (resultFields != null) para.ResultFields = Commons.StringEx.setToLower(resultFields);
  212. if (groupBy != null) para.GroupBy = Commons.StringEx.setToLower(groupBy);
  213. if (orderBy != null) para.OrderBy = Commons.StringEx.setToLower(orderBy);
  214. para.CursorType = CursorType.Static;
  215. return dv.Query(para);
  216. }
  217. public static Recordset QueryRecordsetDynamic(DatasetVector dv, string sqlWhere, string[] resultFields = null, string[] groupBy = null, string[] orderBy = null)
  218. {
  219. QueryParameter para = new QueryParameter();
  220. if (sqlWhere != null) para.AttributeFilter = sqlWhere;
  221. if (resultFields != null) para.ResultFields = Commons.StringEx.setToLower(resultFields);
  222. if (groupBy != null) para.GroupBy = Commons.StringEx.setToLower(groupBy);
  223. if (orderBy != null) para.OrderBy = Commons.StringEx.setToLower(orderBy);
  224. para.CursorType = CursorType.Dynamic;
  225. return dv.Query(para);
  226. }
  227. public static DataTable QueryDataTable(DatasetVector dv, string sqlWhere, string[] resultFields = null, string[] groupBy = null, string[] orderBy = null)
  228. {
  229. QueryParameter para = new QueryParameter();
  230. if (sqlWhere != null) para.AttributeFilter = sqlWhere;
  231. if (resultFields != null) para.ResultFields = Commons.StringEx.setToLower(resultFields);
  232. if (groupBy != null) para.GroupBy = Commons.StringEx.setToLower(groupBy);
  233. if (orderBy != null) para.OrderBy = Commons.StringEx.setToLower(orderBy);
  234. para.CursorType = CursorType.Static;
  235. Recordset rd = dv.Query(para);
  236. return RecordsetToDataTable(rd, dv.Name);
  237. }
  238. public static DataTable RecordsetToDataTable(Recordset recordset, string tableName = null)
  239. {
  240. if (string.IsNullOrEmpty(tableName)) tableName = recordset.Dataset.Name;
  241. DataTable dt = new DataTable { TableName = tableName };
  242. FieldInfos fieldInfos = recordset.GetFieldInfos();
  243. Int32 count = fieldInfos.Count;
  244. DataColumn dataColumn;
  245. for (Int32 i = 0; i < count; i++)
  246. {
  247. dataColumn = new DataColumn { ColumnName = fieldInfos[i].Name, Caption = fieldInfos[i].Caption };
  248. dt.Columns.Add(dataColumn);
  249. }
  250. recordset.MoveFirst();
  251. Int32 length = recordset.RecordCount;
  252. DataRow dr;
  253. for (int i = 0; i < length; i++)
  254. {
  255. dr = dt.NewRow();
  256. var actIndex = 0;
  257. for (Int32 j = 0; j < count; j++)
  258. {
  259. var objValue = recordset.GetFieldValue(j);
  260. string value = objValue?.ToString();
  261. dr[actIndex++] = value;
  262. }
  263. dt.Rows.Add(dr);
  264. recordset.MoveNext();
  265. }
  266. return dt;
  267. }
  268. public static List<DataTable> TongJiFieldGroupCount(DatasetVector dv, string[] strField)
  269. {
  270. Recordset rd = null;
  271. List<DataTable> dts = new List<DataTable>();
  272. DataTable dt;
  273. DataRow dr;
  274. string fieldValue = string.Empty;
  275. foreach (string field in strField)
  276. {
  277. string[] ck = new string[] { field };
  278. rd = ComsStatic.QueryRecordset(dv, null, ck, ck, ck);
  279. dt = new DataTable(); dt.Columns.Add("类别"); dt.Columns.Add("数量");
  280. if (!HasField(dv, field)) continue;
  281. dt.TableName = rd.GetFieldInfos()[field].Caption;
  282. if (rd.RecordCount == 0)//该字段所有值均为null 不能GroupBy
  283. {
  284. dr = dt.NewRow();
  285. dr["类别"] = "未知"; dr["数量"] = dv.RecordCount;
  286. dt.Rows.Add(dr);
  287. }
  288. else
  289. {
  290. rd.MoveFirst();
  291. while (!rd.IsEOF)
  292. {
  293. dr = dt.NewRow();
  294. fieldValue = rd.GetFieldValue(field)?.ToString();
  295. if (string.IsNullOrEmpty(fieldValue))
  296. {
  297. dr["类别"] = "未知";
  298. dr["数量"] = dv.Query(string.Format("{0} is null", field), CursorType.Static).RecordCount;
  299. }
  300. else
  301. {
  302. dr["类别"] = fieldValue;
  303. dr["数量"] = dv.Query(string.Format("{0}='{1}'", field, fieldValue), CursorType.Static).RecordCount;
  304. }
  305. dt.Rows.Add(dr);
  306. rd.MoveNext();
  307. }
  308. }
  309. dr = dt.NewRow();
  310. dr["类别"] = dt.TableName + "总量"; dr["数量"] = dv.RecordCount;
  311. dt.Rows.Add(dr);
  312. dts.Add(dt);
  313. }
  314. ComsStatic.RecordsetDispose(rd);
  315. return dts;
  316. }
  317. public static string getSqlCheckListBox(CheckedListBox clb, string field)
  318. {
  319. StringBuilder sb = new StringBuilder();
  320. if (clb.CheckedItems.Count > 0)
  321. {
  322. sb.Append(" AND (1=2 ");
  323. foreach (string str in clb.CheckedItems)
  324. {
  325. if (str == "未知")
  326. sb.AppendFormat(" OR {0} is null ", field);
  327. else
  328. sb.AppendFormat(" OR {0}='{1}'", field, str);
  329. }
  330. sb.Append(" ) ");
  331. }
  332. return sb.ToString();
  333. }
  334. /// <summary>
  335. /// 获取DatasetVector的FieldInfos数组 不包含系统字段
  336. /// </summary>
  337. /// <param name="dv"></param>
  338. /// <returns></returns>
  339. public static string[] GetFieldInfos(DatasetVector dv)
  340. {
  341. List<string> listStr = new List<string>();
  342. foreach (FieldInfo f in dv.FieldInfos)
  343. {
  344. if (f.IsSystemField) continue;
  345. listStr.Add(f.Name);
  346. }
  347. return listStr.ToArray();
  348. }
  349. public static bool HasField(DatasetVector m_DatasetVector, string _field)
  350. {
  351. bool hasField = false;
  352. FieldInfos fieldInfos = m_DatasetVector.FieldInfos;
  353. for (int i = 0; i < fieldInfos.Count; i++)
  354. {
  355. if (fieldInfos[i].Name.ToLower() == _field)
  356. {
  357. hasField = true; break;
  358. }
  359. }
  360. return hasField;
  361. }
  362. public static bool HasField(Recordset m_Recordset, string _field)
  363. {
  364. bool hasField = false;
  365. FieldInfos fieldInfos = m_Recordset.GetFieldInfos();
  366. for (int i = 0; i < fieldInfos.Count; i++)
  367. {
  368. if (fieldInfos[i].Name.ToLower() == _field)
  369. {
  370. hasField = true; break;
  371. }
  372. }
  373. return hasField;
  374. }
  375. public static void RecordsetDispose(Recordset rd)
  376. {
  377. if (!(rd == null)) { rd.Close(); rd.Dispose(); }
  378. }
  379. public static void EditHistoryUndo(EditHistory eh)
  380. {
  381. while (eh.CanUndo) { eh.Undo(); }
  382. eh.Clear(); eh.Dispose();
  383. }
  384. public static DataTable GetJSLKbyJSJDPT(DataTable dt)
  385. {
  386. #region 构造 管线的起始管点和终点管点 信息
  387. DataColumn dataColumn;
  388. dataColumn = new DataColumn { ColumnName = "qdsmid", Caption = "起点SmID" }; dt.Columns.Add(dataColumn);
  389. dataColumn = new DataColumn { ColumnName = "qdsmx", Caption = "起点X坐标" }; dt.Columns.Add(dataColumn);
  390. dataColumn = new DataColumn { ColumnName = "qdsmy", Caption = "起点Y坐标" }; dt.Columns.Add(dataColumn);
  391. dataColumn = new DataColumn { ColumnName = "qddmgc", Caption = "起点地面高程" }; dt.Columns.Add(dataColumn);
  392. dataColumn = new DataColumn { ColumnName = "qdgdms", Caption = "起点埋深" }; dt.Columns.Add(dataColumn);
  393. dataColumn = new DataColumn { ColumnName = "qdgc", Caption = "起点高程" }; dt.Columns.Add(dataColumn);
  394. dataColumn = new DataColumn { ColumnName = "zdsmid", Caption = "终点SmID" }; dt.Columns.Add(dataColumn);
  395. dataColumn = new DataColumn { ColumnName = "zdsmx", Caption = "终点X坐标" }; dt.Columns.Add(dataColumn);
  396. dataColumn = new DataColumn { ColumnName = "zdsmy", Caption = "终点Y坐标" }; dt.Columns.Add(dataColumn);
  397. dataColumn = new DataColumn { ColumnName = "zddmgc", Caption = "终点地面高程" }; dt.Columns.Add(dataColumn);
  398. dataColumn = new DataColumn { ColumnName = "zdgdms", Caption = "终点埋深" }; dt.Columns.Add(dataColumn);
  399. dataColumn = new DataColumn { ColumnName = "zdgc", Caption = "终点高程" }; dt.Columns.Add(dataColumn);
  400. #endregion
  401. DataTable dtJSJDPT;
  402. foreach (DataRow dr in dt.Rows)
  403. {
  404. dtJSJDPT = QueryDataTable(dvJSJDPT, string.Format(" bsm='{0}' ", dr["qsdh"]));
  405. if (dtJSJDPT.Rows.Count == 1)
  406. {
  407. dr["qdsmid"] = dtJSJDPT.Rows[0]["smid"]; dr["qdsmx"] = dtJSJDPT.Rows[0]["x"]; dr["qdsmy"] = dtJSJDPT.Rows[0]["y"];
  408. dr["qddmgc"] = dtJSJDPT.Rows[0]["dmgc"]; dr["qdgdms"] = dtJSJDPT.Rows[0]["ms"];
  409. //dr["qdgc"] = Math.Round(double.Parse(dtJSJDPT.Rows[0]["dmgc"]?.ToString()) - double.Parse(dtJSJDPT.Rows[0]["ms"]?.ToString()), 2);
  410. dr["qdgc"] = StringToDouble(dtJSJDPT.Rows[0]["dmgc"], 2) - StringToDouble(dtJSJDPT.Rows[0]["ms"], 2);
  411. }
  412. dtJSJDPT = QueryDataTable(dvJSJDPT, string.Format(" bsm='{0}' ", dr["zddh"]));
  413. if (dtJSJDPT.Rows.Count == 1)
  414. {
  415. dr["zdsmid"] = dtJSJDPT.Rows[0]["smid"]; dr["zdsmx"] = dtJSJDPT.Rows[0]["x"]; dr["zdsmy"] = dtJSJDPT.Rows[0]["y"];
  416. dr["zddmgc"] = dtJSJDPT.Rows[0]["dmgc"]; dr["zdgdms"] = dtJSJDPT.Rows[0]["ms"];
  417. //dr["zdgc"] = Math.Round(double.Parse(dtJSJDPT.Rows[0]["dmgc"]?.ToString()) - double.Parse(dtJSJDPT.Rows[0]["ms"]?.ToString()), 2);
  418. dr["zdgc"] = StringToDouble(dtJSJDPT.Rows[0]["dmgc"], 2) - StringToDouble(dtJSJDPT.Rows[0]["ms"], 2);
  419. }
  420. }
  421. return dt;
  422. }
  423. public static Dictionary<string, object> getGuanXianPeiZhi(string pzlx)
  424. {
  425. DatasetVector dv = ComsStatic.dvJSLK_PZ;
  426. Dictionary<string, object> dic = new Dictionary<string, object>();
  427. Recordset _rd = dv.Query(string.Format(" gxpzlx='{0}' ", pzlx), CursorType.Static);
  428. if (_rd.RecordCount == 0) return dic;
  429. _rd.MoveFirst();
  430. while (!_rd.IsEOF)
  431. {
  432. string gxcz = _rd.GetFieldValue("gxcz").ToString();
  433. dic.Add(gxcz + "_gxyjnx", _rd.GetFieldValue("gxyjnx"));
  434. dic.Add(gxcz + "_gxbznx", _rd.GetFieldValue("gxbznx"));
  435. dic.Add(gxcz + "_gxms", _rd.GetFieldValue("gxms"));
  436. _rd.MoveNext();
  437. }
  438. return dic;
  439. }
  440. #region 在当前的DatasetVector中,获取指定列名的最大值和最小值
  441. /// <summary>
  442. /// 在当前的DatasetVector中,获取指定列名的最大值
  443. /// </summary>
  444. /// <param name="fieldName"></param>
  445. /// <returns></returns>
  446. public static double GetMaxValue(DatasetVector dv, string fieldName)
  447. {
  448. var val = MinMaxValue(dv, fieldName, 1);
  449. if (val == null) return double.NaN;
  450. else return double.Parse(val.ToString());
  451. }
  452. /// <summary>
  453. /// 在当前的DatasetVector中,获取指定列名的最小值
  454. /// </summary>
  455. /// <param name="fieldName"></param>
  456. /// <returns></returns>
  457. public static double GetMinValue(DatasetVector dv, string fieldName)
  458. {
  459. var val = MinMaxValue(dv, fieldName, -1);
  460. if (val == null) return double.NaN;
  461. else return double.Parse(val.ToString());
  462. }
  463. /// <summary>
  464. /// 在当前的DatasetVector中,获取指定列名的最大值和最小值
  465. /// </summary>
  466. /// <param name="fieldName"></param>
  467. /// <returns></returns>
  468. public static double[] GetMinMaxValue(DatasetVector dv, string fieldName)
  469. {
  470. var val = MinMaxValue(dv, fieldName, 0);
  471. if (val == null) return new double[] { };
  472. else return (double[])val;
  473. }
  474. /// <summary>
  475. /// 在当前的DatasetVector中,获取指定列名的最大值、最小值
  476. /// </summary>
  477. /// <param name="fieldName">列名</param>
  478. /// <param name="min">min = 0,获取最小值和最大值;-1时获取最小值,1时获取最大值</param>
  479. /// <returns></returns>
  480. private static object MinMaxValue(DatasetVector dv, string fieldName, int minMax = 0)
  481. {
  482. object result = null;
  483. if (minMax == 1)// 最大值
  484. {
  485. result = dv.Statistic(fieldName, StatisticMode.Max);
  486. }
  487. else if (minMax == -1)//最小值
  488. {
  489. result = dv.Statistic(fieldName, StatisticMode.Min);
  490. }
  491. else //最大值和最小值
  492. {
  493. result = new double[] { dv.Statistic(fieldName, StatisticMode.Max), dv.Statistic(fieldName, StatisticMode.Min) };
  494. }
  495. return result;
  496. }
  497. #endregion
  498. #endregion
  499. #region ShowUIMessageTip界面提示信息
  500. public static void ShowUIMessageTipOKorError(bool _result, string msg = "")
  501. {
  502. if (_result)
  503. {
  504. ShowOKLog(msg + " 操作成功!");
  505. }
  506. else
  507. {
  508. ShowErrorLog(msg + " 操作失败!");
  509. }
  510. }
  511. public static void ShowErrorLog(string msg)
  512. {
  513. Commons.LogHelper.Error(msg);
  514. Sunny.UI.UIMessageTip.ShowError(msg, 2000, false, new Point(800, 400), true);
  515. }
  516. public static void ShowOKLog(string msg)
  517. {
  518. Commons.LogHelper.Info(msg);
  519. Sunny.UI.UIMessageTip.ShowOk(msg, 2000, false, new Point(800, 400), true);
  520. }
  521. #endregion
  522. #region String操作
  523. public static double StringToDouble(object str, int dleng = -1)
  524. {
  525. if (str is null) return 0;
  526. double result = 0.0D;
  527. double.TryParse(str.ToString(), out result);
  528. if (dleng != -1)
  529. {
  530. result = Math.Round(result, dleng);
  531. }
  532. return result;
  533. }
  534. /// <summary>
  535. /// Object转Int
  536. /// </summary>
  537. /// <param name="str"></param>
  538. /// <returns></returns>
  539. public static int StringToInt(object str)
  540. {
  541. if (str is null) return 0;
  542. int result = 0;
  543. int.TryParse(str.ToString(), out result);
  544. return result;
  545. }
  546. public static string GetStrAddChar(object str, char ch)
  547. {
  548. return ch + str.ToString() + ch;
  549. }
  550. #endregion
  551. #region SummyUI的控件设置和数据绑定
  552. public static UIStyle uiStyleCom = UIStyle.LightBlue;
  553. public static void setDataGridView(UIDataGridView dgv, DataTable dt = null)
  554. {
  555. dgv.Dock = DockStyle.Fill;
  556. dgv.ShowEditingIcon = false;
  557. dgv.ShowCellToolTips = false;
  558. dgv.AllowUserToAddRows = false;
  559. dgv.AllowUserToResizeRows = false;
  560. dgv.AllowUserToDeleteRows = false;
  561. dgv.AllowUserToOrderColumns = false;
  562. dgv.ReadOnly = true;
  563. dgv.MultiSelect = false;
  564. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  565. dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
  566. dgv.Style = ComsStatic.uiStyleCom;
  567. dgv.RowPostPaint += Dgv_RowPostPaint;
  568. if (dt != null)
  569. {
  570. dgv.DataSource = dt;
  571. for (int i = 0; i < dgv.Columns.Count; i++)
  572. {
  573. dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  574. dgv.Columns[i].HeaderText = dt.Columns[i].Caption;
  575. string hideColums = ",enabled,shape_length,objectid,tcdw,cqbh,gcbh,zcbh,ancillaryrole,rotateangle,tfh,".ToUpper();
  576. bool isVisible = hideColums.Contains("," + dgv.Columns[i].Name.ToUpper() + ",") ? true : false;
  577. if ((dgv.Columns[i].Name.ToUpper().StartsWith("SM") && dgv.Columns[i].Name.ToUpper() != "SMID") || isVisible)//不显示SM开头的列 排除SMID显示
  578. {
  579. dgv.Columns[i].Visible = false;
  580. }
  581. }
  582. }
  583. }
  584. private static void Dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  585. {
  586. var dgv = sender as DataGridView;
  587. Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
  588. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rect, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  589. }
  590. public static void BindDataTableOnCheckedListBox(DatasetVector _dv, CheckedListBox _cbg, string _ziduan)
  591. {
  592. Recordset rd = QueryRecordset(_dv, null, new string[] { _ziduan }, new string[] { _ziduan }, new string[] { _ziduan });
  593. rd.MoveFirst();
  594. string fieldValue = string.Empty;
  595. _cbg.Items.Clear();
  596. while (!rd.IsEOF)
  597. {
  598. fieldValue = rd.GetFieldValue(_ziduan)?.ToString();
  599. if (string.IsNullOrEmpty(fieldValue))
  600. _cbg.Items.Add("未知");
  601. else
  602. _cbg.Items.Add(fieldValue);
  603. rd.MoveNext();
  604. }
  605. }
  606. public static void BindDataTableOnComboBox(DatasetVector _dv, UIComboBox _cbg, string _ziduan)
  607. {
  608. Recordset rd = QueryRecordset(_dv, null, new string[] { _ziduan }, new string[] { _ziduan }, new string[] { _ziduan });
  609. rd.MoveFirst();
  610. _cbg.Items.Clear();
  611. string fieldValue = string.Empty;
  612. while (!rd.IsEOF)
  613. {
  614. fieldValue = rd.GetFieldValue(_ziduan)?.ToString();
  615. if (string.IsNullOrEmpty(fieldValue))
  616. _cbg.Items.Add("未知");
  617. else
  618. _cbg.Items.Add(fieldValue);
  619. rd.MoveNext();
  620. }
  621. }
  622. public static void AddUICheckBox(Control parentControl, Dictionary<string, string> keyValuePairs)
  623. {
  624. UICheckBox uiCheckBox;
  625. foreach (string key in keyValuePairs.Keys)
  626. {
  627. uiCheckBox = new UICheckBox() { Name = key, Text = keyValuePairs[key], Location = new Point(20, 20 + parentControl.Controls.Count * 28), Style = Sunny.UI.UIStyle.Gray };
  628. parentControl.Controls.Add(uiCheckBox);
  629. }
  630. }
  631. public static DataTable getPaperSize()
  632. {
  633. DataTable dt = new DataTable { TableName = "PaperSizeTable" };
  634. dt.Columns.Add("sizekey"); dt.Columns.Add("sizedesc");
  635. dt.Rows.Add("A4", "A4 210x297mm");
  636. dt.Rows.Add("A3", "A3 297x420mm");
  637. dt.Rows.Add("ISOA2", "标准纸面大小(A2)");
  638. dt.Rows.Add("ISOA1", "标准纸面大小(A1)");
  639. dt.Rows.Add("ISOA0", "标准纸面大小(A0)");
  640. return dt;
  641. }
  642. public static PaperSize getPaperSize(string size)
  643. {
  644. PaperSize ps = PaperSize.A4;
  645. switch (size)
  646. {
  647. case "A4": ps = PaperSize.A4; break;
  648. case "A3": ps = PaperSize.A3; break;
  649. case "ISOA2": ps = PaperSize.ISOA2; break;
  650. case "ISOA1": ps = PaperSize.ISOA1; break;
  651. case "ISOA0": ps = PaperSize.ISOA0; break;
  652. default: break;
  653. }
  654. return ps;
  655. }
  656. /// <summary>
  657. /// 设置CheckedListBox的选择状态
  658. /// </summary>
  659. /// <param name="clb"></param>
  660. /// <param name="doState">1全选 2反选 3全不选</param>
  661. public static void CheckListBoxDo(CheckedListBox clb, int doState)
  662. {
  663. switch (doState)
  664. {
  665. case 1://全选
  666. for (int i = 0; i < clb.Items.Count; i++)
  667. {
  668. clb.SetItemChecked(i, true);
  669. }
  670. break;
  671. case 2://反选
  672. for (int i = 0; i < clb.Items.Count; i++)
  673. {
  674. bool ck = clb.GetItemChecked(i);
  675. clb.SetItemChecked(i, !ck);
  676. }
  677. break;
  678. case 3://清空
  679. for (int i = 0; i < clb.Items.Count; i++)
  680. {
  681. clb.SetItemChecked(i, false);
  682. }
  683. break;
  684. default:
  685. break;
  686. }
  687. }
  688. #endregion
  689. #region 其他计算操作
  690. /// <summary>
  691. /// 通过起点的坐标和高程、终点的坐标和高程,计算线上交点的高程
  692. /// </summary>
  693. /// <param name="gx_start">起点坐标</param>
  694. /// <param name="gx_start_gc">起点高程</param>
  695. /// <param name="gx_end">终点坐标</param>
  696. /// <param name="gx_end_gc">终点高程</param>
  697. /// <param name="JiaoDian">线上的交点坐标</param>
  698. /// <returns></returns>
  699. public static double getPointHeight(Point2D gx_start, double gx_start_gc, Point2D gx_end, double gx_end_gc, Point2D JiaoDian)
  700. {
  701. if (!Geometrist.IsPointOnLine(JiaoDian, gx_start, gx_end, false))
  702. return 0.0;
  703. double JiaoDian_gc = 0.0D;
  704. if (gx_start_gc == gx_end_gc)
  705. {
  706. JiaoDian_gc = gx_start_gc;
  707. }
  708. else
  709. {
  710. if (gx_start_gc < gx_end_gc)//确保起点高程 大于 终点高程
  711. {
  712. double temp = gx_start_gc;
  713. gx_start_gc = gx_end_gc;
  714. gx_end_gc = temp;
  715. Point2D temPoint = gx_start;
  716. gx_start = gx_end;
  717. gx_end = temPoint;
  718. }
  719. // 参考链接 https://zhuanlan.zhihu.com/p/43660758 下面的公式可以用,是正式的,这里备份一下
  720. //JiaoDian_gc = gx_end_gc + (gx_start_gc - gx_end_gc) * (Math.Sqrt(Math.Pow((JiaoDian.X - gx_end.X), 2.0) + Math.Pow((JiaoDian.Y - gx_end.Y), 2.0))) / (Math.Sqrt(Math.Pow((gx_start.X - gx_end.X), 2.0) + Math.Pow((gx_start.Y - gx_end.Y), 2.0)));
  721. JiaoDian_gc = gx_end_gc + (gx_start_gc - gx_end_gc) * (Math.Sqrt(Math.Pow((JiaoDian.X - gx_end.X), 2.0) + Math.Pow((JiaoDian.Y - gx_end.Y), 2.0))) / (Math.Sqrt(Math.Pow((gx_start.X - gx_end.X), 2.0) + Math.Pow((gx_start.Y - gx_end.Y), 2.0)));
  722. if (JiaoDian_gc == double.PositiveInfinity || JiaoDian_gc == double.NegativeInfinity)
  723. {
  724. JiaoDian_gc = gx_end_gc;
  725. }
  726. }
  727. return Math.Round(JiaoDian_gc, 2);
  728. }
  729. /// <summary>
  730. /// 返回Double数组的最大值或者最小值
  731. /// </summary>
  732. /// <param name="dblist"></param>
  733. /// <param name="isMax">True表示最大值,false表示最小值</param>
  734. /// <returns></returns>
  735. public static double getMaxMin(double[] dblist, bool isMax)
  736. {
  737. double finalValue = dblist[0];
  738. if (isMax)//最大值
  739. {
  740. for (int i = 0; i < dblist.Length; i++)
  741. {
  742. if (finalValue < dblist[i])
  743. finalValue = dblist[i];
  744. }
  745. }
  746. else
  747. {
  748. for (int i = 0; i < dblist.Length; i++)
  749. {
  750. if (finalValue > dblist[i])
  751. finalValue = dblist[i];
  752. }
  753. }
  754. return finalValue;
  755. }
  756. #endregion
  757. #region 管网标注的配置
  758. /// <summary>
  759. /// GeoStyle LineColor = Color.Brown, LineWidth = 0.5
  760. /// </summary>
  761. public static GeoStyle geoStyle_Brown_05mm = new GeoStyle() { LineColor = Color.Brown, LineWidth = 0.5 };
  762. /// <summary>
  763. /// TextStyle ForeColor = Color.Brown, IsSizeFixed = true, FontHeight = 6, Alignment = TextAlignment.BottomLeft
  764. /// </summary>
  765. public static TextStyle textStyle_Brown_6mm_BottomLeft = new TextStyle { ForeColor = Color.Brown, IsSizeFixed = true, FontHeight = 6, Alignment = TextAlignment.BottomLeft };
  766. /// <summary>
  767. /// 红色线条 宽度1mm 背景透明度10
  768. /// </summary>
  769. public static GeoStyle geoStyle_Red_1mm_OpaqueRate = new GeoStyle { LineColor = Color.Red, LineWidth = 1, FillOpaqueRate = 10 };
  770. #endregion
  771. }
  772. public class LayerEx
  773. {
  774. public static LayerEx[] layerExs = new LayerEx[]{
  775. new LayerEx{ Index = 9, Caption = "供水管线", Dataset = "JSLK", LineWidth = 1, LineColor = Color.FromArgb(84, 141, 212) },
  776. new LayerEx{ Index = 8, Caption = "供水水表", Dataset = "JSSBPT", LineWidth = 2.4, LineColor = Color.FromArgb(206, 137, 102) },
  777. new LayerEx{ Index = 7, Caption = "供水阀门", Dataset = "JSFMPT", LineWidth = 2.4, LineColor = Color.FromArgb(0, 160, 233) },
  778. new LayerEx{ Index = 6, Caption = "供水节点", Dataset = "JSJDPT", LineWidth = 2.4, LineColor = Color.FromArgb(127, 194, 105) }
  779. };
  780. public LayerEx() { }
  781. private string caption;
  782. private string name;
  783. private string dataset;
  784. private int opaqueRate = 0;
  785. private double maxVisibleScale = 0;
  786. private double minVisibleScale = 0;
  787. private bool isEditable = true;
  788. private bool isVisible = true;
  789. private int index;
  790. private Color lineColor = Color.FromArgb(0, 0, 0);
  791. private double lineWidth;
  792. public string Caption { get => caption; set => caption = value; }
  793. public string Name { get => name; set => name = value; }
  794. public string Dataset { get => dataset; set => dataset = value; }
  795. public int OpaqueRate { get => opaqueRate; set => opaqueRate = value; }
  796. public double MaxVisibleScale { get => maxVisibleScale; set => maxVisibleScale = value; }
  797. public double MinVisibleScale { get => minVisibleScale; set => minVisibleScale = value; }
  798. public bool IsEditable { get => isEditable; set => isEditable = value; }
  799. public bool IsVisible { get => isVisible; set => isVisible = value; }
  800. public int Index { get => index; set => index = value; }
  801. public double LineWidth { get => lineWidth; set => lineWidth = value; }
  802. public Color LineColor { get => lineColor; set => lineColor = value; }
  803. }
  804. }