using System; using System.Collections.Generic; using System.Linq; namespace HStation.RevitDev.RevitDataExport { public class GroupPoint : ICloneable { private int id = 0; public int ID { get { return id; } set { id = value; } } public double Q { get; set; } public double H { get; set; } public double E { get; set; } public double P { get; set; } public double NPSH { get { return _npsh; } set { _npsh = value; } } private double _npsh = -1; public GroupPoint() { } public GroupPoint(GroupPoint rhs) { this.ID = rhs.ID; this.Q = rhs.Q; this.H = rhs.H; this.E = rhs.E; this.P = rhs.P; this.NPSH = rhs.NPSH; } public GroupPoint(string dsString) { ResetParas(dsString); } public bool ResetParas(string dsString) { if (string.IsNullOrEmpty(dsString)) return false; if (dsString.Contains("|")) {//可能是GroupPointList的字符 string[] strParas = dsString.Split('|'); int iCount = strParas.Count(); if (iCount < 2) return false; return ResetParas(strParas[1]);//取默认第一个成员即可 } var paras = dsString.Split(','); if (paras.Count() < 5) return false; ID = Convert.ToInt32(paras[0]); Q = Convert.ToDouble(paras[1]); H = Convert.ToDouble(paras[2]); E = Convert.ToDouble(paras[3]); P = Convert.ToDouble(paras[4]); NPSH = Convert.ToDouble(paras[5]); return true; } public static GroupPoint ToParameter(string dsString) { if (string.IsNullOrEmpty(dsString)) return null; if (dsString.Contains("|")) {//可能是GroupPointList的字符 string[] strParas = dsString.Split('|'); int iCount = strParas.Count(); if (iCount < 2) return null; return GroupPoint.ToParameter(strParas[1]);//取默认第一个成员即可 } var paras = dsString.Split(','); if (paras.Count() < 5) return null; GroupPoint pt = new GroupPoint(); pt.ID = Convert.ToInt32(paras[0]); pt.Q = Convert.ToDouble(paras[1]); pt.H = Convert.ToDouble(paras[2]); pt.E = Convert.ToDouble(paras[3]); pt.P = Convert.ToDouble(paras[4]); pt.NPSH = Convert.ToDouble(paras[5]); return pt; } public GroupPoint(int id, double q, double h, double e, double p) { ID = id; Q = q; H = h; E = e; P = p; } public GroupPoint(int id, double q, double h, double e, double p, double npsh) { ID = id; Q = q; H = h; E = e; P = p; NPSH = npsh; } public override string ToString() { return string.Format("Q:{0:0.0},H:{1:0.0},E:{2:0.0},P:{3:0.0}", Q, H, E, P); } public string ToDsString() { return string.Format("{0},{1},{2},{3},{4},{5}", ID, Q, H, E, P, NPSH); } public GroupPoint RoundAll(int decimals) { this.Q = Math.Round(this.Q, decimals); this.H = Math.Round(this.H, decimals); this.E = Math.Round(this.E, decimals); this.P = Math.Round(this.P, decimals); this.NPSH = Math.Round(this.NPSH, decimals); return this; } public GroupPoint RoundAll_Small(int decimals, double small = 2) { if (this.Q < small) this.Q = Math.Round(this.Q, 3); else this.Q = Math.Round(this.Q, decimals); if (this.H < small) this.H = Math.Round(this.H, 3); else this.H = Math.Round(this.H, decimals); if (this.E < small) this.E = Math.Round(this.E, 3); else this.E = Math.Round(this.E, decimals); if (this.P < small) this.P = Math.Round(this.P, 3); else this.P = Math.Round(this.P, decimals); this.NPSH = Math.Round(this.NPSH, decimals); return this; } #region Clone public GroupPoint Clone() //对外提供一个创建自身的浅表副本的能力 { return (GroupPoint)this.MemberwiseClone(); } object ICloneable.Clone() { return this.Clone(); } #endregion public enum eSortType { ID, Q, H, E, P } //排序方法 public class Comparer : IComparer { private eSortType sortType; public Comparer(eSortType type = eSortType.Q) { sortType = type; } #region IComparer 成员 int IComparer.Compare(GroupPoint obj1, GroupPoint obj2) { if (sortType == eSortType.ID) { if (obj1.ID == obj2.ID) { return 0; } else if (obj1.ID > obj2.ID) { return 1; } else { return -1; } } else if (sortType == eSortType.Q) { if (Math.Abs(obj1.Q - obj2.Q) < 0.0001) { return 0; } else if (obj1.Q > obj2.Q) { return 1; } else { return -1; } } else if (sortType == eSortType.H) { if (Math.Abs(obj1.H - obj2.H) < 0.0001) { return 0; } else if (obj1.H > obj2.H) { return 1; } else { return -1; } } else if (sortType == eSortType.E) { if (Math.Abs(obj1.E - obj2.E) < 0.0001) { return 0; } else if (obj1.E > obj2.E) { return 1; } else { return -1; } } else { if (Math.Abs(obj1.P - obj2.P) < 0.0001) { return 0; } else if (obj1.P > obj2.P) { return 1; } else { return -1; } } } #endregion } //比较相同:主要用与LIST的Contains方法 public class EqualComparer : IEqualityComparer { private double ignoreDis = 0.001; public EqualComparer() { ignoreDis = 0.001; } public EqualComparer(double dis) { ignoreDis = dis; } public bool Equals(GroupPoint lhs, GroupPoint rhs) { if (Math.Abs(lhs.Q - rhs.Q) < ignoreDis && Math.Abs(lhs.H - rhs.H) < ignoreDis) return true; return false; } public int GetHashCode(GroupPoint obj) { return 0; // return obj.H.GetHashCode() + obj.Q.GetHashCode(); } } } }