/// <summary> /// Creates a ToleranceCurve object from a histogram curve file. /// </summary> /// <param name="filePath">Name of the file containing the histogram curve.</param> /// <returns>A new instance of ToleranceCurve, based on the specified file.</returns> public static ToleranceCurve FromFile(string filePath) { XmlDocument xmlDoc = new XmlDocument(); using (Stream s = new FileInfo(filePath).OpenRead()) { try { xmlDoc.Load(s); } catch (XmlException) { throw new XmlException("Tolerance file is not a right xml format"); } } //Tolerance Node List is a means of containing multiple Tolerance curve profiles within a histogram XmlNodeList toleranceNodeList = xmlDoc.DocumentElement.SelectNodes("descendant::Tolerance"); if (toleranceNodeList.Count == 0) { throw new XmlException("There is no tolerance node in Tolerance file"); } ToleranceCurve toleranceCurve = new ToleranceCurve(); for (int i = 0; i < toleranceNodeList.Count; i++) { ToleranceLegacy toleranceLegacy = new ToleranceLegacy(); XmlAttribute dpiRatio = toleranceNodeList[i].Attributes["dpiRatio"]; if (dpiRatio == null) { throw new XmlException("It is a invalid Tolerance file.\nThere isn't dpiRadio attribute in tolerance node."); } toleranceLegacy.DpiRatio = Convert.ToDouble(dpiRatio.InnerText, NumberFormatInfo.InvariantInfo); XmlNodeList pointNodeList = toleranceNodeList[i].SelectNodes("Point"); if (pointNodeList.Count == 0) { toleranceCurve.SetGraph(0, 0); toleranceCurve.SetGraph(255, 0); return(toleranceCurve); } List <TolerancePoint> points = new List <TolerancePoint>(); for (int j = 0; j < pointNodeList.Count; j++) { XmlAttribute pointX = pointNodeList[j].Attributes["x"]; XmlAttribute pointY = pointNodeList[j].Attributes["y"]; if (pointX == null || pointY == null) { throw new XmlException("It is a invalid Tolerance file.\nThere isn't x or y attribute in Point node."); } if (toleranceLegacy.DpiRatio == defaultDpiRatio) { byte x = Convert.ToByte(pointX.InnerText, NumberFormatInfo.InvariantInfo); double y = Convert.ToDouble(pointY.InnerText, NumberFormatInfo.InvariantInfo); toleranceCurve.SetGraph(x, y); } else { TolerancePoint point = new TolerancePoint(); point.X = pointX.InnerText; point.Y = pointY.InnerText; points.Add(point); } } if (toleranceLegacy.DpiRatio != defaultDpiRatio) { toleranceLegacy.Points = points; toleranceCurve.AddTolerance(toleranceLegacy); } } return(toleranceCurve); }
/// <summary> /// Store a tolerance node from Tolerance.xml to a array /// </summary> /// <param name="legacy">The tolerance node to add</param> public void AddTolerance(ToleranceLegacy legacy) { tolerances.Add(legacy); }