Пример #1
0
        [HttpGet("CirclesToLayers/{doSave:bool}")]          // GET /api/Editor/CirclesToLayers/false
        public IActionResult CirclesToLayers(bool doSave)
        {
            // traverse through all circles and set the layer whenever the radius is the same
            var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel");

            if (drawModel != null)
            {
                // convert to a real dxf document
                var dxf = DrawModel.ToDxfDocument(drawModel);

                // create a bucket for each radius
                Dictionary <string, List <Circle> > radiusBuckets = dxf.Circles
                                                                    .GroupBy(o => o.Radius.ToString("F2", CultureInfo.InvariantCulture))
                                                                    .ToDictionary(g => g.Key, g => g.ToList());

                // move circle buckets to unique layers
                foreach (var pair in radiusBuckets)
                {
                    var radiusString = pair.Key;
                    var circles      = pair.Value;

                    double radius = 0;
                    if (Double.TryParse(radiusString, NumberStyles.Any, CultureInfo.InvariantCulture, out radius))
                    {
                        double dia       = radius * 2;
                        string diaString = dia.ToString("F2", CultureInfo.InvariantCulture);

                        foreach (var c in circles)
                        {
                            c.Layer = new netDxf.Tables.Layer("Diameter_" + diaString);
                        }
                    }
                }

                // build new filename
                string fileName    = drawModel.FileName;
                var    newFileName = Path.GetFileNameWithoutExtension(fileName);
                // var newFileExtension = Path.GetExtension(fileName);
                // always use the dxf extension since thats what we are saving
                var newFileExtension = ".dxf";
                var newFullFileName  = newFileName + "_layered" + newFileExtension;

                if (doSave)
                {
                    var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
                    bool basePathExists = System.IO.Directory.Exists(basePath);
                    if (!basePathExists)
                    {
                        Directory.CreateDirectory(basePath);
                    }

                    var filePath = Path.Combine(basePath, newFullFileName);
                    dxf.Save(filePath);
                }
                else
                {
                    // download converted file to user
                    var memoryStream = new MemoryStream();
                    dxf.Save(memoryStream);

                    // At this point, the Offset is at the end of the MemoryStream
                    // Either do this to seek to the beginning
                    memoryStream.Position = 0;
                    return(File(memoryStream, "APPLICATION/octet-stream", newFullFileName));
                }
            }
            else
            {
                _logger.LogError("Circles to Layers unsuccessfull!");
                return(BadRequest());
            }

            return(Ok());
        }