Example #1
0
        [HttpGet("Flatten/{doConvertCircles:bool}")]          // GET /api/Editor/Flatten/false
        public IActionResult Flatten(bool doConvertCircles)
        {
            var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel");

            if (drawModel != null)
            {
                drawModel.SortStartStopSegments();
                drawModel.GroupIntoSegments();

                foreach (var segment in drawModel.GroupedSegments)
                {
                    // if a segment contain a element that isn't
                    // polyline, convert first
                    var shapePoints = new List <PointF>();
                    foreach (var shape in segment)
                    {
                        if (shape.GetType() == typeof(DrawModel.DrawArc))
                        {
                            var a = shape as DrawModel.DrawArc;
                            // convert to polyline
                            var centerX     = a.Center.X;
                            var centerY     = a.Center.Y;
                            var radius      = a.Radius;
                            var startAngle  = a.StartAngle;
                            var endAngle    = a.EndAngle;
                            var isClockwise = a.IsClockwise;

                            var points = Transformation.RenderArc(centerX, centerY, radius, startAngle, endAngle, isClockwise);

                            Transformation.AddAvoidDuplicates(shapePoints, points);

                            // and remove arc
                            drawModel.Arcs.Remove(a);
                        }

                        if (shape.GetType() == typeof(DrawModel.DrawLine))
                        {
                            var l = shape as DrawModel.DrawLine;

                            var startPoint = l.StartPoint.PointF;
                            var endPoint   = l.EndPoint.PointF;

                            Transformation.AddAvoidDuplicates(shapePoints, startPoint, endPoint);

                            // and remove line
                            drawModel.Lines.Remove(l);
                        }
                    }

                    // add as separate polylines
                    var polyline = new DrawModel.DrawPolyline(shapePoints);
                    drawModel.Polylines.Add(polyline);
                }

                // converting circles
                if (doConvertCircles)
                {
                    for (int i = drawModel.Circles.Count - 1; i >= 0; i--)
                    {
                        var c = drawModel.Circles.ElementAt(i);

                        var cx = c.Center.X;
                        var cy = c.Center.Y;
                        var r  = c.Radius;

                        var points = Transformation.RenderCircle(cx, cy, r);

                        // add as separate polylines
                        var polyline = new DrawModel.DrawPolyline(points);
                        drawModel.Polylines.Add(polyline);

                        // and remove circle
                        drawModel.Circles.Remove(c);
                    }
                }

                // update model
                HttpContext.Session.SetObjectAsJson("DrawModel", drawModel);
            }
            else
            {
                _logger.LogError("Flatten unsuccessfull!");
                return(BadRequest());
            }

            return(Ok());
        }