Beispiel #1
0
        public void OpenFile()
        {
            OpenFileDialog file = new OpenFileDialog();

            file.Filter = "GCode files (*.nc;*ncc;*gcode)|*.nc;*ncc;*gcode|Text files (*.txt)|*.txt|All files (*.*)|*.*";

            if (file.ShowDialog() == true)
            {
                using (new UIUtils.WaitCursor())
                {
                    GCode.LoadFile(file.FileName);
                    grdGCode.DataContext = GCode.Data.DefaultView;
                    CurrLine             = 0;
                    PendingLine          = 0;
                    PgmEndLine           = GCode.Data.Rows.Count - 1;
                    scroll = UIUtils.GetScrollViewer(grdGCode);

                    SetStreamingState(GCode.Loaded ? StreamingState.Idle : StreamingState.NoFile);
                }
            }
        }
Beispiel #2
0
        private void grdGCode_Drop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            if (files.Count() == 1)
            {
                using (new UIUtils.WaitCursor())
                {
                    GCode.LoadFile(files[0]);

                    //      ppiControl.Speed = file.max_feed;

                    grdGCode.DataContext = GCode.Data.DefaultView;
                    CurrLine             = 0;
                    PendingLine          = 0;
                    PgmEndLine           = GCode.Data.Rows.Count - 1;
                    scroll = UIUtils.GetScrollViewer(grdGCode);

                    SetStreamingState(GCode.Loaded ? StreamingState.Idle : StreamingState.NoFile);
                }
            }
        }
Beispiel #3
0
        // M48 Start header
        // M71 metric
        // M72 Inch
        // G04Xn Dwell
        // INCH,LZ - LZ = LeadingZeros
        // M95 or % end header
        // G05 Drill Mode (modal)

        public bool LoadFile(CNC.Controls.GCode job, string filename)
        {
            bool ok = true, leadingZeros = false;

            JobParametersViewModel.Tool tool = new JobParametersViewModel.Tool {
                Id = 0, Diameter = 0d
            };

            this.job = job;

            settings.Profile = "Excellon";

            if (new JobParametersDialog(settings)
            {
                Owner = Application.Current.MainWindow
            }.ShowDialog() != true)
            {
                return(false);
            }

            FileInfo     file = new FileInfo(filename);
            StreamReader sr   = file.OpenText();

            string s = sr.ReadLine();

            while (s != null)
            {
                try
                {
                    s = s.Trim();

                    if (isHeader == null)
                    {
                        isHeader = s == "M48";
                    }

                    else if (isHeader == true)
                    {
                        switch (s)
                        {
                        case "METRIC":
                            isMetric     = true;
                            leadingZeros = false;
                            break;

                        case "METRIC,TZ":
                            isMetric     = true;
                            leadingZeros = true;
                            break;

                        case "INCH":
                            isMetric     = false;
                            leadingZeros = false;
                            break;

                        case "INCH,TZ":
                            isMetric     = false;
                            leadingZeros = true;
                            break;

                        case "%":
                        case "M95":
                            isHeader = false;
                            break;
                        }

                        if (s[0] == 'T')
                        {
                            int cpos = s.IndexOf('C');
                            tools.Add(new JobParametersViewModel.Tool {
                                Id = int.Parse(s.Substring(1, cpos - 1)), Diameter = dbl.Parse(s.Substring(cpos + 1))
                            });
                        }
                    }
                    else
                    {
                        switch (s[0])
                        {
                        case 'G':
                            switch (s)
                            {
                            case "G00":
                            case "G01":
                            case "G02":
                            case "G03":
                                isDrillMode = false;
                                break;

                            case "G05":
                                isDrillMode = true;
                                break;
                            }
                            break;

                        case 'X':
                            if (isDrillMode)
                            {
                                int    g85pos = s.IndexOf("G85");
                                string args   = g85pos >= 0 ? s.Substring(0, g85pos) : s;
                                int    ypos   = args.IndexOf('Y');
                                double factor = leadingZeros ? (ypos == 8 ? 1000d : 10000d) : 1.0;
                                var    cmd    = new ExcellonCommand();
                                cmd.Command = g85pos >= 0 ? "Slot" : "Drill";
                                cmd.tool    = tool.Id;
                                cmd.Start.X = dbl.Parse(args.Substring(1, ypos - 1)) / factor / (isMetric ? 1d : 25.4d);
                                cmd.Start.Y = dbl.Parse(args.Substring(ypos + 1)) / factor / (isMetric ? 1d : 25.4d);
                                if (g85pos >= 0)
                                {
                                    isDrillMode = false;
                                    args        = s.Substring(g85pos + 3);
                                    ypos        = args.IndexOf('Y');
                                    cmd.End.X   = dbl.Parse(args.Substring(1, ypos - 1)) / (isMetric ? 1d : 25.4d);
                                    cmd.End.Y   = dbl.Parse(args.Substring(ypos + 1)) / (isMetric ? 1d : 25.4d);
                                }
                                commands.Add(cmd);
                            }
                            break;

                        case 'T':
                            tool = tools.Where(x => x.Id == int.Parse(s.Substring(1))).FirstOrDefault();
                            break;
                        }
                    }
                }
                catch
                {
                }
                s = sr.ReadLine();
            }

            sr.Close();

            job.AddBlock(filename, CNC.Core.Action.New);
            job.AddBlock("(Translated by Excellon to GCode converter)");
            job.AddBlock("G90G17G21G50");

            if (settings.ScaleX != 1d || settings.ScaleY != 1d)
            {
                job.AddBlock(string.Format("G51X{0}Y{1}", settings.ScaleX.ToInvariantString(), settings.ScaleY.ToInvariantString()));
            }

            job.AddBlock("G0Z" + settings.ZRapids.ToInvariantString());
            job.AddBlock("X0Y0");

            var target = new Point3D(0d, 0d, settings.ZRapids);

            foreach (var t in tools)
            {
                job.AddBlock("M5");
                target.X = 0d;
                target.Y = 0d;
                target.Z = settings.ZHome;
                job.AddBlock("G0" + PosToParams(target));
                job.AddBlock(string.Format("M6 (MSG, {0} mm {1})", (t.Diameter < settings.ToolDiameter ? t.Diameter : settings.ToolDiameter).ToInvariantString(), t.Diameter < settings.ToolDiameter ? "drill" : "mill"));
                job.AddBlock("M3S" + settings.RPM.ToInvariantString());
                job.AddBlock("G4P1");
                target.Z = settings.ZRapids;
                job.AddBlock("G0" + PosToParams(target));
                job.AddBlock("F" + settings.PlungeRate.ToInvariantString());

                foreach (var cmd in commands)
                {
                    if (cmd.tool == t.Id)
                    {
                        switch (cmd.Command)
                        {
                        case "Drill":
                            if (t.Diameter > settings.ToolDiameter)
                            {
                                double r = (t.Diameter - settings.ToolDiameter) / 2d;
                                target.X = cmd.Start.X + r;
                                target.Y = cmd.Start.Y;
                                string p = PosToParams(target);
                                if (p.Length > 0)
                                {
                                    job.AddBlock("G0" + p);
                                }
                                target.Z = settings.ZMin;
                                job.AddBlock("G1" + PosToParams(target));
                                job.AddBlock(string.Format("G{0}X{1}I-{2}", "2", target.X.ToInvariantString(), r.ToInvariantString()));
                                target.Z = settings.ZRapids;
                                job.AddBlock("G0" + PosToParams(target));
                            }
                            else
                            {
                                OutputG81(new Point3D(cmd.Start.X, cmd.Start.Y, settings.ZMin));
                            }
                            break;

                        case "Slot":
                            OutputSlot(cmd, t.Diameter);
                            break;
                        }
                    }
                }
            }

            job.AddBlock("G0X0Y0Z" + settings.ZHome.ToInvariantString());
            job.AddBlock("M30", CNC.Core.Action.End);

            return(ok);
        }
Beispiel #4
0
        public bool LoadFile(CNC.Controls.GCode job, string filename)
        {
            bool ok = true;

            this.job = job;

            if (filename.EndsWith("Edge_Cuts.plt") || filename.EndsWith("Paste.plt"))
            {
                isCut = !filename.EndsWith("Paste.plt");
            }
            else
            {
                isCut = (GrblMode)GrblSettings.GetDouble(GrblSetting.Mode) != GrblMode.Laser;
            }

            settings.EnableToolSelection = true;
            settings.Profile             = "HPGL" + (isCut ? "" : "Laser");

            if (new JobParametersDialog(settings)
            {
                Owner = Application.Current.MainWindow
            }.ShowDialog() != true)
            {
                return(false);
            }

            FileInfo     file = new FileInfo(filename);
            StreamReader sr   = file.OpenText();

            using (new UIUtils.WaitCursor()) {
                string s = sr.ReadLine();

                while (s != null)
                {
                    foreach (string cmd in s.Split(';'))
                    {
                        try
                        {
                            switch (cmd.Substring(0, 2))
                            {
                            case "PM":
                                if ((inPolygon = dbl.Parse(cmd.Substring(2)) == 0d))
                                {
                                    pm0.Clear();
                                    HPGLCommand hpgl = new HPGLCommand();
                                    hpgl.Command = commands.Last().Command;
                                    hpgl.Pos.X   = commands.Last().Pos.X;
                                    hpgl.Pos.Y   = commands.Last().Pos.Y;
                                    pm0.Add(hpgl);
                                }
                                else if (pm0.Count > 0)
                                {
                                    Polygon polygon;
                                    toVectors(pm0);
                                    if ((polygon = Polygon.findPolygon(vectors, tolerance)) != null)
                                    {
                                        polygons.Add(polygon);
                                    }
                                }
                                break;

                            case "PT":
                                scaleFix = 0.025d;
                                break;

                            case "PA":
                            {
                                var args = cmd.Substring(2).Split(',');
                                if (args.Length > 1)
                                {
                                    HPGLCommand hpgl = new HPGLCommand();
                                    hpgl.Command = cmd.Substring(0, 2);
                                    hpgl.Pos.X   = dbl.Parse(args[0]) * scaleFix;
                                    hpgl.Pos.Y   = dbl.Parse(args[1]) * scaleFix;
                                    offset.X     = Math.Min(offset.X, hpgl.Pos.X);
                                    offset.Y     = Math.Min(offset.Y, hpgl.Pos.Y);
                                    if (inPolygon)
                                    {
                                        pm0.Add(hpgl);
                                    }
                                    else
                                    {
                                        commands.Add(hpgl);
                                    }
                                }
                            }
                            break;

                            case "AA":
                            {
                                var args = cmd.Substring(2).Split(',');
                                if (args.Length > 2)
                                {
                                    HPGLCommand hpgl = new HPGLCommand();
                                    hpgl.Command = cmd.Substring(0, 2);
                                    hpgl.Pos.X   = dbl.Parse(args[0]) * scaleFix;
                                    hpgl.Pos.Y   = dbl.Parse(args[1]) * scaleFix;
                                    hpgl.R       = dbl.Parse(args[2]) * scaleFix;
                                    offset.X     = Math.Min(offset.X, hpgl.Pos.X);
                                    offset.Y     = Math.Min(offset.Y, hpgl.Pos.Y);
                                    if (inPolygon)
                                    {
                                        pm0.Add(hpgl);
                                    }
                                    else
                                    {
                                        commands.Add(hpgl);
                                    }
                                }
                            }
                            break;

                            case "PD":
                            case "PU":
                            {
                                HPGLCommand hpgl = new HPGLCommand();
                                hpgl.Command = cmd.Substring(0, 2);
                                if (inPolygon)
                                {
                                    pm0.Add(hpgl);
                                }
                                else
                                {
                                    isDown = hpgl.Command == "PD";
                                    commands.Add(hpgl);
                                }
                            }
                            break;

                            default:
                            {
                                HPGLCommand hpgl = new HPGLCommand();
                                hpgl.Command = cmd.Substring(0, 2);
                                commands.Add(hpgl);
                            }
                            break;
                            }
                        }
                        catch
                        {
                        }
                    }

                    s = sr.ReadLine();
                }

                sr.Close();

                for (int i = 0; i < commands.Count; i++)
                {
                    var cmd = commands[i];

                    try {
                        switch (cmd.Command)
                        {
                        case "IN":
                            pos.X  = cmd.Pos.X = 0d;
                            pos.Y  = cmd.Pos.Y = 0d;
                            isDown = cmd.isDown = false;
                            break;

                        case "PU":
                            if (isDown)
                            {
                                cmd.Pos.X = pos.X;
                                cmd.Pos.Y = pos.Y;
                                isDown    = false;
                            }
                            break;

                        case "PD":
                            if (!isDown)
                            {
                                cmd.Pos.X = pos.X;
                                cmd.Pos.Y = pos.Y;
                                isDown    = true;
                            }
                            break;

                        case "PA":
                            cmd.Pos.X -= offset.X;
                            cmd.Pos.Y -= offset.Y;
                            if ((cmd.isDown = isDown))
                            {
                                var v = new Vector();
                                v.IsArc = false;
                                v.Start = pos;
                                v.End.X = cmd.Pos.X;
                                v.End.Y = cmd.Pos.Y;
                                vectors.Add(v);
                            }
                            break;

                        case "AA":
                        {
                            cmd.Pos.X = cmd.Pos.X - offset.X;
                            cmd.Pos.Y = cmd.Pos.Y - offset.X;
                            if ((cmd.isDown = isDown))
                            {
                                var v = new Vector();
                                v.IsArc = true;
                                v.Start = pos;
                                v.End.X = cmd.Pos.X + (cmd.Pos.X - pos.X);
                                v.End.Y = cmd.Pos.Y + (cmd.Pos.Y - pos.Y);
                                vectors.Add(v);
                            }
                        }
                        break;
                        }
                        pos.X = cmd.Pos.X;
                        pos.Y = cmd.Pos.Y;
                    }
                    catch
                    {
                    }
                }

                {
                    Polygon polygon;

                    while ((polygon = Polygon.findPolygon(vectors, tolerance)) != null)
                    {
                        polygons.Add(polygon);
                    }
                }

                job.AddBlock(filename, CNC.Core.Action.New);
                job.AddBlock("(Translated by HPGL to GCode converter)");
                job.AddBlock(string.Format("(Tool diameter: {0} mm)", settings.ToolDiameter.ToInvariantString()));
                job.AddBlock("G90G91.1G17G21G50");

                if (settings.ScaleX != 1d || settings.ScaleY != 1d)
                {
                    job.AddBlock(string.Format("G51X{0}Y{1}", settings.ScaleX.ToInvariantString(), settings.ScaleY.ToInvariantString()));
                }

                job.AddBlock("G0Z" + settings.ZRapids.ToInvariantString());
                job.AddBlock("X0Y0");

                if (isCut)
                {
                    job.AddBlock(string.Format("M3S{0}", settings.RPM.ToInvariantString()));
                    job.AddBlock("G4P2");
                }
                else
                {
                    job.AddBlock("M122P1");     // Enable laser
                    job.AddBlock("M123P800");   // PPI
                    job.AddBlock("M124P1500");  // Pulse width
                    job.AddBlock("M125Q1P2");   // Enable tube coolant
                    job.AddBlock("M4S90");
                    job.AddBlock("M7");
                    job.AddBlock("M8");
                }

                foreach (var polygon in polygons)
                {
                    if (settings.ToolDiameter != 0d)
                    {
                        polygon.Offset((isCut ? -settings.ToolDiameter : settings.ToolDiameter) / 2d);
                    }
                    cutPolygon(polygon);
                }

                job.AddBlock("G0X0Y0Z" + settings.ZHome.ToInvariantString());
                job.AddBlock("M30", CNC.Core.Action.End);
            }

            return(ok);
        }
Beispiel #5
0
 public void CloseFile()
 {
     GCode.CloseFile();
 }