ComsStatic.cs 26 KB

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