using System; using System.IO; using System.Collections.Generic; using System.Xml.Serialization; namespace ConsoleApp1 { public class Program { [XmlRoot("ArrayOfMove")] public class ArrayOfMove { [XmlElement("Move")] public List Moves { get; set; } } public class Move { public int Line { get; set; } public int Block { get; set; } public int ToolNumber { get; set; } public string Type { get; set; } public string WorkPlane { get; set; } public string MaterialSide { get; set; } public double FeedRate { get; set; } public double SpindleSpeed { get; set; } public double OriginX { get; set; } public double OriginY { get; set; } public double OriginZ { get; set; } public double OriginRot1 { get; set; } public double OriginRot2 { get; set; } public double TargetX { get; set; } public double TargetY { get; set; } public double TargetZ { get; set; } public double TargetRot1 { get; set; } public double TargetRot2 { get; set; } public double Length { get; set; } public double ArcCenterX { get; set; } public double ArcCenterY { get; set; } public double ArcCenterZ { get; set; } public double ArcRadius { get; set; } } public static void Main(string[] args) { XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfMove)); using (FileStream fs = new FileStream(args[0], FileMode.Open)) { ArrayOfMove arrayOfMove = (ArrayOfMove)serializer.Deserialize(fs); string dxfFile = Path.GetDirectoryName(args[0]) + "\\" + Path.GetFileNameWithoutExtension(args[0]) + ".dxf"; using (FileStream FileStream = new FileStream(dxfFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (StreamWriter sw = new StreamWriter(FileStream)) { sw.WriteLine(" 0"); sw.WriteLine("SECTION"); sw.WriteLine(" 2"); sw.WriteLine("ENTITIES"); sw.WriteLine(" 0"); bool first = true; foreach (Move move in arrayOfMove.Moves) { if (first) { sw.WriteLine("SEQEND"); sw.WriteLine(" 0"); if (move.Type != "Rapid") { sw.WriteLine("POLYLINE"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 66"); sw.WriteLine("1"); sw.WriteLine(" 70"); sw.WriteLine("0"); sw.WriteLine("62"); sw.WriteLine("7"); sw.WriteLine("0"); sw.WriteLine("VERTEX"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 10"); sw.WriteLine("0.0"); sw.WriteLine(" 20"); sw.WriteLine("0.0"); sw.WriteLine(" 0"); } } if (move.Type == "Rapid") { if (!first) { sw.WriteLine(" 0"); } sw.WriteLine("POLYLINE"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 66"); sw.WriteLine("1"); sw.WriteLine(" 70"); sw.WriteLine("0"); sw.WriteLine("62"); sw.WriteLine("7"); sw.WriteLine("0"); sw.WriteLine("VERTEX"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 10"); sw.WriteLine(move.TargetX.ToString("0.######")); sw.WriteLine(" 20"); sw.WriteLine(move.TargetY.ToString("0.######")); } else if (move.Type == "Linear") { if (!first) { sw.WriteLine(" 0"); } sw.WriteLine("VERTEX"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 10"); sw.WriteLine(move.TargetX.ToString("0.######")); sw.WriteLine(" 20"); sw.WriteLine(move.TargetY.ToString("0.######")); } else if (move.Type == "CircularCW") { if (!first) { sw.WriteLine(" 42"); double bulge = -1 * GetBulge(move.OriginX, move.OriginY, move.TargetX, move.TargetY, move.ArcCenterX, move.ArcCenterY); sw.WriteLine(bulge.ToString("0.######")); sw.WriteLine(" 0"); } sw.WriteLine("VERTEX"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 10"); sw.WriteLine(move.TargetX.ToString("0.######")); sw.WriteLine(" 20"); sw.WriteLine(move.TargetY.ToString("0.######")); } else if (move.Type == "CircularCCW") { if (!first) { sw.WriteLine(" 42"); double bulge = GetBulge(move.OriginX, move.OriginY, move.TargetX, move.TargetY, move.ArcCenterX, move.ArcCenterY); sw.WriteLine(bulge.ToString("0.######")); sw.WriteLine(" 0"); } sw.WriteLine("VERTEX"); sw.WriteLine(" 8"); sw.WriteLine("0"); sw.WriteLine(" 10"); sw.WriteLine(move.TargetX.ToString("0.######")); sw.WriteLine(" 20"); sw.WriteLine(move.TargetY.ToString("0.######")); } first = false; } sw.WriteLine(" 0"); sw.WriteLine("SEQEND"); sw.WriteLine(" 0"); sw.WriteLine("ENDSEC"); sw.WriteLine(" 0"); sw.WriteLine("EOF"); } } } } static double GetBulge(double x0, double y0, double x1, double y1,double xc, double yc) { double r, a, i, u; r = Math.Sqrt(Math.Pow(x1 - xc, 2) + Math.Pow(y1 - yc, 2)); u = Math.Sqrt(Math.Pow(x1 - x0, 2) + Math.Pow(y1 - y0, 2)); a = Math.Sqrt(Math.Pow(r, 2) - Math.Pow(u, 2) / 4.0); i = r - a; return Math.Abs(2 * i / u); } } }