[HttpGet("Split/{xSplit:float}/{splitDegrees:float}/{zClearance:float}/{index:int}")] // GET /api/Editor/Split/20/10/20/0 public IActionResult Split(float xSplit, float splitDegrees, float zClearance, int index) { // index means which side to get back if (xSplit != 0) { var splitPoint = new Point3D(xSplit, 0, 0); var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel"); if (drawModel != null) { var gCode = DrawModel.ToGCode(drawModel); SaveToFile("before_split.txt", gCode); var parsedInstructions = SimpleGCodeParser.ParseText(gCode); var gCodeArray = GCodeSplitter.Split(parsedInstructions, splitPoint, splitDegrees, zClearance); SaveToFile("after_split_1.txt", GCodeUtils.GetGCode(gCodeArray[0])); SaveToFile("after_split_2.txt", GCodeUtils.GetGCode(gCodeArray[1])); // clean up the mess with too many G0 commands var cleanedGCode = GCodeUtils.GetMinimizeGCode(gCodeArray[index]); SaveToFile("after_split_clean.txt", GCodeUtils.GetGCode(cleanedGCode)); var gCodeResult = Block.BuildGCodeOutput("Block_1", cleanedGCode, false); SaveToFile("after_split_build_output.txt", gCodeResult); // convert gcode to draw model var newDrawModel = DrawModel.FromGCode(gCodeResult, drawModel.FileName); HttpContext.Session.SetObjectAsJson("DrawModel", newDrawModel); } return(Ok()); } return(BadRequest()); }
[HttpGet("Rotate/{degrees:float}")] // GET /api/Editor/Rotate/20 public IActionResult Rotate(float degrees) { var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel"); if (drawModel != null) { var gCode = DrawModel.ToGCode(drawModel); SaveToFile("before_rotate.txt", gCode); var parsedInstructions = SimpleGCodeParser.ParseText(gCode); var center = new PointF(0, 0); if (degrees == 0) { degrees = 90; } var gcodeInstructions = GCodeUtils.GetRotatedGCode(parsedInstructions, center, degrees); SaveToFile("after_rotate.txt", GCodeUtils.GetGCode(gcodeInstructions)); // clean up the mess with too many G0 commands var cleanedGCode = GCodeUtils.GetMinimizeGCode(gcodeInstructions); SaveToFile("after_rotate_clean.txt", GCodeUtils.GetGCode(cleanedGCode)); var gCodeResult = Block.BuildGCodeOutput("Block_1", cleanedGCode, false); SaveToFile("after_rotate_build_output.txt", gCodeResult); // convert gcode to draw model var newDrawModel = DrawModel.FromGCode(gCodeResult, drawModel.FileName); HttpContext.Session.SetObjectAsJson("DrawModel", newDrawModel); return(Ok()); } return(BadRequest()); }
[HttpGet("Split/{xSplit:float}/{splitDegrees:float}/{zClearance:float}")] // GET /api/Editor/Split/100/0/10 public IActionResult Split(float xSplit, float splitDegrees, float zClearance) { // index means which side to get back if (xSplit != 0) { var splitPoint = new Point3D(xSplit, 0, 0); var drawModel = HttpContext.Session.GetObjectFromJson <DrawModel>("DrawModel"); if (drawModel != null) { var gCode = DrawModel.ToGCode(drawModel); // SaveToFile("before_split.txt", gCode); var parsedInstructions = SimpleGCodeParser.ParseText(gCode); var gCodeArray = GCodeSplitter.Split(parsedInstructions, splitPoint, splitDegrees, zClearance); // SaveToFile("after_split_1.txt", GCodeUtils.GetGCode(gCodeArray[0])); // SaveToFile("after_split_2.txt", GCodeUtils.GetGCode(gCodeArray[1])); // clean up the mess with too many G0 commands var cleanedGCode1 = GCodeUtils.GetMinimizeGCode(gCodeArray[0]); var cleanedGCode2 = GCodeUtils.GetMinimizeGCode(gCodeArray[1]); // SaveToFile("after_split_clean_1.txt", GCodeUtils.GetGCode(cleanedGCode1)); // SaveToFile("after_split_clean_2.txt", GCodeUtils.GetGCode(cleanedGCode2)); var gCodeResult1 = Block.BuildGCodeOutput("Block_1", cleanedGCode1, false); var gCodeResult2 = Block.BuildGCodeOutput("Block_1", cleanedGCode2, false); // SaveToFile("after_split_build_output_1.txt", gCodeResult1); // SaveToFile("after_split_build_output_2.txt", gCodeResult2); // convert gcode to draw model var fileName = Path.GetFileNameWithoutExtension(drawModel.FileName); var extension = Path.GetExtension(drawModel.FileName); var newDrawModel1 = DrawModel.FromGCode(gCodeResult1, fileName + "_split_1" + extension); var newDrawModel2 = DrawModel.FromGCode(gCodeResult2, fileName + "_split_2" + extension); // store with index HttpContext.Session.SetObjectAsJson("Split-0", newDrawModel1); HttpContext.Session.SetObjectAsJson("Split-1", newDrawModel2); } return(Ok()); } return(BadRequest()); }
[HttpPost("Upload")] // POST /api/Editor/Upload public async Task <IActionResult> Upload(List <IFormFile> files, [FromForm] string description, [FromForm] bool useContours) { DrawModel drawModel = null; foreach (var file in files) { // Read file fully and save to file system // var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\"); // bool basePathExists = System.IO.Directory.Exists(basePath); // if (!basePathExists) Directory.CreateDirectory(basePath); // var fileName = Path.GetFileNameWithoutExtension(file.FileName); // var filePath = Path.Combine(basePath, file.FileName); // var extension = Path.GetExtension(file.FileName); // if (!System.IO.File.Exists(filePath)) // { // using (var stream = new FileStream(filePath, FileMode.Create)) // { // await file.CopyToAsync(stream); // } // } // Read file fully into memory and act var hasParsedSuccessfully = false; using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); _logger.LogInformation("Successfully uploaded file!"); // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as dxf if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as DXF..."); var dxf = DxfDocument.Load(memoryStream); if (dxf != null) { _logger.LogInformation("DXF read successfully!"); // convert dxf to a model that can be serialized // since I am unable to get the default dxfnet model to be serialized drawModel = DrawModel.FromDxfDocument(dxf, file.FileName, useContours); _logger.LogInformation("Successfully parsed DXF to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as DXF!"); // ignore } } // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as SVG if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as SVG..."); var svg = SVGDocument.Load(memoryStream); if (svg != null) { _logger.LogInformation("SVG read successfully!"); drawModel = DrawModel.FromSVGDocument(svg, file.FileName, useContours); _logger.LogInformation("Successfully parsed SVG to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as SVG!"); // ignore } } // At this point, the Offset is at the end of the MemoryStream // Do this to seek to the beginning memoryStream.Seek(0, SeekOrigin.Begin); // try to parse as GCODE if (!hasParsedSuccessfully) { try { _logger.LogInformation("Trying to parse file as GCODE..."); StreamReader reader = new StreamReader(memoryStream); string gCode = reader.ReadToEnd(); if (!"".Equals(gCode)) { _logger.LogInformation("GCODE read successfully!"); drawModel = DrawModel.FromGCode(gCode, file.FileName); _logger.LogInformation("Successfully parsed GCODE to draw model!"); hasParsedSuccessfully = true; } } catch (System.Exception e) { _logger.LogError(e, "Failed parsing as GCODE!"); // ignore } } if (drawModel != null) { HttpContext.Session.SetObjectAsJson("DrawModel", drawModel); } } } if (HttpContext.Session.Keys.Contains("DrawModel")) { _logger.LogInformation("File successfully uploaded!"); } else { _logger.LogError("File upload failed!"); return(BadRequest()); } return(Ok()); }