public PartProfile Read() { _partProfile = new PartProfile(); using (StreamReader = new StreamReader(FileName)) { var currentLine = 0; while (!StreamReader.EndOfStream) { var line = StreamReader.ReadLine(); currentLine++; var key = GetKey(line); var value = GetValue(line); if (key == "ITEMNAME") { _partProfile.ItemName = value; } if (key == "VERTQUANT") { if (!int.TryParse(value, out _linesLeftToParse)) { throw new Exception($"{FileName}:{currentLine}: invalid parameter VERTQUANT"); } if (ParseCircle()) { continue; } ParseArcOrLine(); } } } return(_partProfile); }
//TODO /*public static double CalcRightSide(TFlexTask task, bool isTflex = true) * { * if (isTflex) * { * * } * }*/ public static double CalcArea(PartProfile partProfile) { var contourAreas = partProfile.Contours.Select(CalcContourArea).ToList(); return(contourAreas.FirstOrDefault() - contourAreas.Where((d, i) => i > 0).Sum()); }
public void CalcSquareArea() { var outerContour = new FigureContour { Objects = new List <ContourObject> { new ContourLine { Begin = new Point { X = 0, Y = 0 }, End = new Point { X = 0, Y = 1000 } }, new ContourLine { Begin = new Point { X = 0, Y = 1000 }, End = new Point { X = 1000, Y = 1000 } }, new ContourLine { Begin = new Point { X = 1000, Y = 1000 }, End = new Point { X = 1000, Y = 0 } }, new ContourLine { Begin = new Point { X = 1000, Y = 0 }, End = new Point { X = 0, Y = 0 } } } }; var innerContour = new FigureContour { Objects = new List <ContourObject> { new ContourLine { Begin = new Point { X = 10, Y = 10 }, End = new Point { X = 10, Y = 110 } }, new ContourLine { Begin = new Point { X = 10, Y = 110 }, End = new Point { X = 110, Y = 110 } }, new ContourLine { Begin = new Point { X = 110, Y = 110 }, End = new Point { X = 110, Y = 10 } }, new ContourLine { Begin = new Point { X = 110, Y = 10 }, End = new Point { X = 10, Y = 10 } } } }; var partProfile = new PartProfile { Contours = new List <Contour> { outerContour, innerContour } }; Assert.AreEqual(1000000 - 10000, CalcHelper.CalcArea(partProfile)); }
/// <summary> /// Запись детали в отдельный файл /// </summary> private void WriteItemToFile(StreamWriter partFile, PartProfile part, bool isSheet = false) { foreach (var contour in part.Contours) { switch (contour) { case RectangularContour rect: partFile.Write(string.Join("\t", "VERTQUANT:", 4) + "\n"); AddVertex(partFile, new Point()); if (isSheet) { AddVertex(partFile, new Point { X = rect.Length }); AddVertex(partFile, new Point { X = rect.Length, Y = rect.Width }); AddVertex(partFile, new Point { Y = rect.Width }); } else { AddVertex(partFile, new Point { X = rect.Width }); AddVertex(partFile, new Point { X = rect.Width, Y = rect.Length }); AddVertex(partFile, new Point { Y = rect.Length }); } break; case CircleContour circle: partFile.Write(string.Join("\t", "VERTQUANT:", 2) + "\n"); var leftPoint = new Point { X = circle.Center.X - circle.Radius, Y = circle.Center.Y }; var rightPoint = new Point { X = circle.Center.X + circle.Radius, Y = circle.Center.Y }; AddVertex(partFile, leftPoint, 1); AddVertex(partFile, rightPoint, 1); break; case FigureContour figureContour: var numObj = figureContour.Objects.Select(obj => { if (obj is ContourArc) { return(2); } return(1); }).Sum(); if (figureContour.Objects.All(obj => obj is ContourLine)) { numObj++; } partFile.Write(string.Join("\t", "VERTQUANT:", numObj) + "\n"); var lastPoint = new Point(); foreach (var obj in figureContour.Objects) { switch (obj) { case ContourArc arc: var bulge = Math.Tan(arc.Angle / -4); AddVertex(partFile, arc.Begin, bulge); AddVertex(partFile, arc.End); lastPoint = arc.End; break; default: if (!obj.Begin.Equals(lastPoint)) { AddVertex(partFile, obj.Begin); } lastPoint = obj.End; AddVertex(partFile, lastPoint); break; } } break; } } }