private static RoundnessPlot?ConvertToRoundness(Formplot source) { switch (source) { case CylindricityPlot cylindricity: return(ConvertToRoundness(cylindricity)); default: return(null); } }
private static void CopyDefaults(Formplot source, Formplot target) { target.OriginalFormplotType = source.FormplotType; target.ProjectionAxis = source.ProjectionAxis; target.Properties.Set(source.Properties); target.Tolerance = source.Tolerance; target.DefaultErrorScaling = source.DefaultErrorScaling; target.CreatorSoftware = source.CreatorSoftware; target.CreatorSoftwareVersion = source.CreatorSoftwareVersion; target.PreserveAspectRatio = source.PreserveAspectRatio; }
private static void WriteFileVersion <TPoint, TGeometry>(Formplot <TPoint, TGeometry> plot, ZipArchive zipOutput) where TPoint : Point <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { var versionInfoEntry = zipOutput.CreateEntry("fileversion.txt", CompressionLevel.NoCompression); versionInfoEntry.LastWriteTime = new DateTime(1980, 1, 1); using var versionInfoEntryStream = versionInfoEntry.Open(); using var textWriter = new StreamWriter(versionInfoEntryStream, Encoding.UTF8); textWriter.Write(FormplotHelper.GetFileFormatVersion(plot.FormplotType).ToString(2)); }
private static void WriteFormplotSpecificProperties <TPoint, TGeometry>(Formplot <TPoint, TGeometry> plot, XmlWriter metaDataWriter, Stream pointDataStream) where TPoint : Point <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { if (!plot.Tolerance.IsEmpty) { metaDataWriter.WriteStartElement("Tolerance"); plot.Tolerance.Serialize(metaDataWriter); metaDataWriter.WriteEndElement(); } if (plot.DefaultErrorScaling.HasValue) { metaDataWriter.WriteStartElement("ErrorScaling"); metaDataWriter.WriteValue(XmlConvert.ToString(plot.DefaultErrorScaling.Value)); metaDataWriter.WriteEndElement(); } if (plot.PreserveAspectRatio.HasValue) { metaDataWriter.WriteStartElement("PreserveAspectRatio"); metaDataWriter.WriteValue(XmlConvert.ToString(plot.PreserveAspectRatio.Value)); metaDataWriter.WriteEndElement(); } if (plot.FormplotType == FormplotTypes.Straightness && plot.ProjectionAxis != ProjectionAxis.None) { metaDataWriter.WriteStartElement("ProjectionAxis"); metaDataWriter.WriteValue(plot.ProjectionAxis.ToString()); metaDataWriter.WriteEndElement(); } if (plot.Actual.GeometryType != GeometryTypes.None) { metaDataWriter.WriteStartElement("Geometry"); metaDataWriter.WriteAttributeString("Type", plot.GeometryType.ToString()); metaDataWriter.WriteStartElement("Nominal"); plot.Nominal.Serialize(metaDataWriter); metaDataWriter.WriteEndElement(); metaDataWriter.WriteStartElement("Actual"); plot.Actual.Serialize(metaDataWriter); metaDataWriter.WriteEndElement(); metaDataWriter.WriteEndElement(); } metaDataWriter.WriteStartElement("Points"); plot.WritePoints(metaDataWriter, pointDataStream); metaDataWriter.WriteEndElement(); }
/// <summary> /// Writes the data of the plot into the specified <paramref name="stream"/>. /// </summary> /// <exception cref="ArgumentNullException"></exception> internal static void WriteTo <TPoint, TGeometry>(this Formplot <TPoint, TGeometry> plot, Stream stream) where TPoint : Point <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } using (var zipOutput = new ZipArchive(stream, ZipArchiveMode.Create, true, Encoding.UTF8)) { WriteFileVersion(plot, zipOutput); using (var metadataStream = new MemoryStream()) { using (var writer = XmlWriter.Create(metadataStream, XmlWriterSettings)) { Stream?pointDataStream; if (plot.FormplotType != FormplotTypes.None) { var plotPointsEntry = zipOutput.CreateEntry("plotpoints.dat", CompressionLevel.Fastest); plotPointsEntry.LastWriteTime = new DateTime(1980, 1, 1); pointDataStream = plotPointsEntry.Open(); } else { pointDataStream = null; } writer.WriteStartDocument(true); writer.WriteStartElement(plot.FormplotType == FormplotTypes.None ? FormplotReader.PropertiesRootTag : FormplotReader.FormplotRootTag); plot.Serialize(writer, pointDataStream); writer.WriteEndElement(); writer.WriteEndDocument(); pointDataStream?.Close(); } var metaDataEntry = zipOutput.CreateEntry("header.xml", CompressionLevel.Optimal); metaDataEntry.LastWriteTime = new DateTime(1980, 1, 1); using (var metaDataZipStream = metaDataEntry.Open()) metadataStream.WriteTo(metaDataZipStream); } } }
/// <summary> /// Writes the points into the specified <see cref="Stream" /> and writes their metadata with the specified <see cref="XmlWriter" />. /// </summary> /// <param name="plot">The plot.</param> /// <param name="writer">The xml writer.</param> /// <param name="pointDataStream">The point data stream.</param> private static void WritePoints <TPoint, TGeometry>(this Formplot <TPoint, TGeometry> plot, XmlWriter writer, Stream pointDataStream) where TPoint : Point <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (pointDataStream == null) { throw new ArgumentNullException(nameof(pointDataStream)); } writer.WriteStartElement("Count"); writer.WriteValue(XmlConvert.ToString(plot.Points.Count())); writer.WriteEndElement(); var propertyLists = new Dictionary <Property, RangeList>(); var statelists = new Dictionary <PointState, RangeList>(); var segmentlists = new Dictionary <Segment <TPoint, TGeometry>, RangeList>(); var tolerancelists = new Dictionary <Tolerance, RangeList>(); var lastPoint = default(Point <TPoint, TGeometry>); var index = 0; using (var pointDataWriter = new BinaryWriter(pointDataStream)) { foreach (var point in plot.Points) { CollectPointProperties(point, propertyLists, index, lastPoint); CollectStates(point, statelists, index, lastPoint); CollectSegments(point, segmentlists, index, lastPoint); CollectTolerances(point, tolerancelists, index, lastPoint); point.WriteToStream(pointDataWriter); lastPoint = point; index++; } } WritePropertyLists(writer, propertyLists); WriteStates(writer, statelists); WriteSegments(writer, segmentlists); WriteTolerances(writer, tolerancelists); }
/// <summary> /// Converts the specified source plot to the target type. /// </summary> /// <param name="source">Source plot</param> /// <param name="target">Target plot</param> /// <typeparam name="TPlot">Target plot type</typeparam> /// <returns><c>true</c> if the conversion was successful, otherwise <c>false</c></returns> public static bool TryConvert <TPlot>(Formplot source, out TPlot?target) where TPlot : Formplot { if (typeof(TPlot) == typeof(StraightnessPlot)) { target = ConvertToStraightness(source) as TPlot; return(target != null); } if (typeof(TPlot) == typeof(RoundnessPlot)) { target = ConvertToRoundness(source) as TPlot; return(target != null); } target = null; return(false); }
private static StraightnessPlot?ConvertToStraightness(Formplot source) { switch (source) { case CylindricityPlot cylindricity: return(ConvertToStraightness(cylindricity)); case CurveProfilePlot curveProfile: return(ConvertCurveToStraightness(curveProfile)); case FlushGapPlot flushGap: return(ConvertCurveToStraightness(flushGap)); case FilletPlot fillet: return(ConvertCurveToStraightness(fillet)); case FlatnessPlot flatness: return(ConvertToStraightness(flatness)); case RoundnessPlot roundness: return(ConvertCircleToStraightness(roundness)); case CircleInProfilePlot circleInProfile: return(ConvertCircleToStraightness(circleInProfile)); default: return(null); } }
/// <summary> /// Serializes the formplot. The header information is written to the <paramref name="metaDataWriter" />, whereas the points are stored as a /// blob in the <paramref name="pointDataStream" /></summary> /// <param name="plot">The plot.</param> /// <param name="metaDataWriter">An XML writer to store the header data, such as tolerances and segments.</param> /// <param name="pointDataStream">A stream to store the binary point data.</param> private static void Serialize <TPoint, TGeometry>(this Formplot <TPoint, TGeometry> plot, XmlWriter metaDataWriter, Stream?pointDataStream) where TPoint : Point <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { if (metaDataWriter == null) { throw new ArgumentNullException(nameof(metaDataWriter)); } if (plot.FormplotType != FormplotTypes.None) { metaDataWriter.WriteAttributeString("Type", plot.FormplotType.ToString()); } metaDataWriter.WriteStartElement("CreatorSoftware"); metaDataWriter.WriteValue(plot.CreatorSoftware); metaDataWriter.WriteEndElement(); metaDataWriter.WriteStartElement("CreatorSoftwareVersion"); metaDataWriter.WriteValue(plot.CreatorSoftwareVersion.ToString()); metaDataWriter.WriteEndElement(); if (plot.Properties.Count > 0) { foreach (var property in plot.Properties) { metaDataWriter.WriteStartElement("Property"); property.Serialize(metaDataWriter); metaDataWriter.WriteEndElement(); } } if (plot.FormplotType != FormplotTypes.None && pointDataStream != null) { WriteFormplotSpecificProperties(plot, metaDataWriter, pointDataStream); } }
private static StraightnessPlot ConvertCurveToStraightness <TPoint, TGeometry>(Formplot <TPoint, TGeometry> source) where TPoint : CurvePointBase <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { var result = new StraightnessPlot(); CopyDefaults(source, result); result.Actual.CoordinateSystem = source.Actual.CoordinateSystem; result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem; Vector?lastPoint = null; var currentLength = 0.0; foreach (var segment in source.Segments) { var resultSegment = new Segment <LinePoint, LineGeometry>(segment.Name, segment.SegmentType); result.Segments.Add(resultSegment); foreach (var point in segment.Points) { if (lastPoint == null) { lastPoint = point.Position; } else { var dx = point.Position.X - lastPoint.Value.X; var dy = point.Position.Y - lastPoint.Value.Y; var dz = point.Position.Z - lastPoint.Value.Z; lastPoint = point.Position; currentLength += Math.Sqrt(dx * dx + dy * dy + dz * dz); } var resultPoint = new LinePoint(currentLength, point.Deviation); CopyDefaults(point, resultPoint); resultSegment.Points.Add(resultPoint); } } return(result); }
private static StraightnessPlot ConvertCircleToStraightness <TPoint, TGeometry>(Formplot <TPoint, TGeometry> source) where TPoint : CirclePointBase <TPoint, TGeometry>, new() where TGeometry : Geometry, new() { var result = new StraightnessPlot(); CopyDefaults(source, result); result.Actual.CoordinateSystem = source.Actual.CoordinateSystem; result.Nominal.CoordinateSystem = source.Nominal.CoordinateSystem; double? lastAngle = null; const double radToDeg = 180 / Math.PI; foreach (var segment in source.Segments) { var resultSegment = new Segment <LinePoint, LineGeometry>(segment.Name, segment.SegmentType); result.Segments.Add(resultSegment); foreach (var point in segment.Points) { double position; if (!lastAngle.HasValue) { lastAngle = AdjustAngle(point.Angle * radToDeg); position = lastAngle.Value; } else { var angle = AdjustAngle(point.Angle * radToDeg); while (angle < lastAngle) { angle += 360; } lastAngle = angle; position = angle; } var resultPoint = new LinePoint(position, point.Deviation); CopyDefaults(point, resultPoint); resultSegment.Points.Add(resultPoint); } } return(result); }
/// <summary>Constructor.</summary> /// <param name="formplot">The formplot to which this collection belongs.</param> public SegmentCollection(Formplot <TPoint, TGeometry> formplot) { _Formplot = formplot; }