public static NFTask GetGeometry() { Msg("[Nesting Factory] Starting collect geometry..."); ICollection <Area> EO = Doc.GetAreas(); IEnumerator <Area> GeomEnum = EO.GetEnumerator(); GeomEnum.MoveNext(); NFTask task = new NFTask(); for (int area_num = 0; area_num < EO.Count; area_num++) { Area area = GeomEnum.Current; GeomEnum.MoveNext(); Rectangle BoundBox = area.BoundRect; double bound_x = BoundBox.Left; double bound_y = BoundBox.Top; NFItem item = new NFItem(area.ObjectId.ToString()); for (int num_contour = 0; num_contour < area.ContourCount; num_contour++) { Contour contour = area.GetContour(num_contour); NFContour cont = new NFContour(); for (int num_segment = 0; num_segment < contour.SegmentCount; num_segment++) { ContourSegment csegment = contour.GetSegment(num_segment); switch (csegment.GeometryType) { case ObjectGeometryType.Line: LineGeometry linegeom = csegment.Geometry as LineGeometry; cont.AddPoint(new NFPoint(linegeom.X1 - bound_x, bound_y - linegeom.Y1, 0)); cont.AddPoint(new NFPoint(linegeom.X2 - bound_x, bound_y - linegeom.Y2, 0)); break; /*case ObjectGeometryType.Polyline: * * PolylineGeometry polygeom = csegment.Geometry as PolylineGeometry; * CircleArcGeometry[] cArcs = polygeom.GetCircleArcApproximation(2); * * for (int i = 0; i < cArcs.GetLength(0); i++) * { * cArcToDoubles(cArcs[i], ref cont, BoundBox); * } * break;*/ case ObjectGeometryType.CircleArc: CircleArcGeometry cgeom = csegment.Geometry as CircleArcGeometry; cArcToDoubles(cgeom, ref cont, BoundBox, csegment.IsCounterclockwise); break; case ObjectGeometryType.Circle: CircleGeometry cirgeom = csegment.Geometry as CircleGeometry; cont.AddPoint(new NFPoint(cirgeom.CenterX + cirgeom.Radius - bound_x, bound_y - cirgeom.CenterY, 1)); cont.AddPoint(new NFPoint(cirgeom.CenterX - cirgeom.Radius - bound_x, bound_y - cirgeom.CenterY, 1)); break; default: PolylineGeometry polygeom = csegment.Geometry as PolylineGeometry; int v_count = polygeom.Count; for (int i = 0; i < v_count; i++) { if (v_count < 50 || i % (csegment.GeometryType == ObjectGeometryType.Ellipse ? 5 : 1) == 0 || i == v_count) { cont.AddPoint(new NFPoint(polygeom.GetX(i) - bound_x, bound_y - polygeom.GetY(i), 0)); } } break; } } item.AddContour(cont); } task.AddItem(item); } Msg("[Nesting Factory] Geometry collected"); return(task); }
/// <summary> /// Joins the contour segments. /// </summary> /// <param name="eps"> /// The tolerance for segment ends to connect (squared distance). /// </param> private void JoinContourSegments(double eps = 1e-10) { // This is a simple, slow, naïve method - should be improved: // http://stackoverflow.com/questions/1436091/joining-unordered-line-segments this.contours = new List <Contour>(); var contourPoints = new List <IDataPoint>(); int contourPointsCount = 0; ContourSegment firstSegment = null; int segmentCount = this.segments.Count; while (segmentCount > 0) { ContourSegment segment1 = null, segment2 = null; if (firstSegment != null) { bool reverse; // Find a segment that is connected to the head of the contour segment1 = this.FindConnectedSegment( (DataPoint)contourPoints[0], firstSegment.ContourLevel, eps, out reverse); if (segment1 != null) { contourPoints.Insert(0, reverse ? segment1.StartPoint : segment1.EndPoint); contourPointsCount++; this.segments.Remove(segment1); segmentCount--; } // Find a segment that is connected to the tail of the contour segment2 = this.FindConnectedSegment( (DataPoint)contourPoints[contourPointsCount - 1], firstSegment.ContourLevel, eps, out reverse); if (segment2 != null) { contourPoints.Add(reverse ? segment2.StartPoint : segment2.EndPoint); contourPointsCount++; this.segments.Remove(segment2); segmentCount--; } } if ((segment1 == null && segment2 == null) || segmentCount == 0) { if (contourPointsCount > 0 && firstSegment != null) { this.contours.Add(new Contour(contourPoints, firstSegment.ContourLevel)); contourPoints = new List <IDataPoint>(); contourPointsCount = 0; } if (segmentCount > 0) { firstSegment = this.segments.First(); contourPoints.Add(firstSegment.StartPoint); contourPoints.Add(firstSegment.EndPoint); contourPointsCount += 2; this.segments.Remove(firstSegment); segmentCount--; } } } }
private void listView1_Click(object sender, EventArgs e) { if (listView1.SelectedItems != null && listView1.SelectedItems.Count > 0) { string areaName = listView1.SelectedItems[0].Text; num = listView1.SelectedItems[0].Index; Item = task.GetItem(num); //Отрисовка превьюшки детали Document Doc = TFlex.Application.ActiveDocument; select = Doc.GetObjectByName(areaName) as Area; TFlex.Drawing.Rectangle bound = select.BoundRect; double Scale = 159 / Math.Max(bound.Width, bound.Height); Bitmap img = new Bitmap(160, 160); Graphics graph = Graphics.FromImage(img); Pen pen = new Pen(Brushes.White); graph.DrawRectangle(pen, new Rectangle(0, 0, 159, 159)); pen = new Pen(Brushes.Black); pen.Width = 1; for (int cc = 0; cc < select.ContourCount; cc++) { Contour cont = select.GetContour(cc); for (int sc = 0; sc < cont.SegmentCount; sc++) { ContourSegment segm = cont.GetSegment(sc); switch (segm.GeometryType) { case ObjectGeometryType.Line: LineGeometry line = segm.Geometry as LineGeometry; graph.DrawLine(pen, (float)((line.X1 - bound.Left) * Scale), (float)((bound.Top - line.Y1) * Scale), (float)((line.X2 - bound.Left) * Scale), (float)((bound.Top - line.Y2) * Scale)); break; case ObjectGeometryType.Circle: CircleGeometry circle = segm.Geometry as CircleGeometry; double radius = (circle.Radius * Scale); int xc = (int)((circle.CenterX - bound.Left) * Scale); int yc = (int)((bound.Top - circle.CenterY) * Scale); graph.DrawEllipse(pen, new Rectangle((int)(xc - radius), (int)(yc - radius), (int)radius * 2, (int)radius * 2)); break; case ObjectGeometryType.CircleArc: CircleArcGeometry cgeom = segm.Geometry as CircleArcGeometry; int xc1 = (int)((cgeom.CenterX - bound.Left) * Scale); int yc1 = (int)((bound.Top - cgeom.CenterY) * Scale); radius = (cgeom.Radius * Scale); double[] angles = NFGetGeom.getArcAngle(cgeom, segm.IsCounterclockwise); double ang = angles[0] * 180 / Math.PI; double ang1 = angles[1] * 180 / Math.PI - 90; graph.DrawArc(pen, (float)(xc1 - radius), (float)(yc1 - radius), (float)(radius * 2), (float)(radius * 2), (float)ang1, (float)ang); break; default: PolylineGeometry geom = segm.Geometry as PolylineGeometry; if (geom != null) { for (int i = 1; i < geom.Count; i++) { int x1 = (int)((geom.GetX(i) - bound.Left) * Scale); int y1 = (int)((bound.Top - geom.GetY(i)) * Scale); int x2 = (int)((geom.GetX(i - 1) - bound.Left) * Scale); int y2 = (int)((bound.Top - geom.GetY(i - 1)) * Scale); graph.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2); } } break; } } } pictureBox1.Image = img; //Задание параметров форме label10.Text = "Размер квадрата детали = " + (int)(159 / Scale); label9.Text = "Количество контуров: " + select.ContourCount; selectComboNum(ref comboBox2, Item.Rotation); selectComboNum(ref comboBox1, Item.Reflection); selectComboNum(ref comboBox1, Item.Reflection); textBox4.Text = Item.Count.ToString(); } }