Esempio n. 1
0
        public void AddBlock(string block, Action action)
        {
            if (action == Action.New)
            {
                if (Loaded)
                {
                    gcode.Rows.Clear();
                }

                Reset();
                commands.Clear();
                gcode.BeginLoadData();

                filename = block;
            }
            else if (block != null && block.Trim().Length > 0)
            {
                try
                {
                    bool isComment;
                    block = block.Trim();
                    if (Parser.ParseBlock(ref block, false, out isComment))
                    {
                        gcode.Rows.Add(new object[] { LineNumber++, block, block.Length + 1, true, isComment, Parser.ProgramEnd, "", false });
                        while (commands.Count > 0)
                        {
                            block = commands.Dequeue();
                            gcode.Rows.Add(new object[] { LineNumber++, block, block.Length + 1, true, false, false, "", false });
                        }
                    }
                }
                catch //(Exception e)
                {
                    //
                }
            }

            if (action == Action.End)
            {
                gcode.EndLoadData();

#if DEBUG
                System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();
#endif

                // Calculate program limits (bounding box)

                GCodeEmulator emu = new GCodeEmulator(true);

                foreach (var cmd in emu.Execute(Tokens))
                {
                    if (cmd.Token is GCArc)
                    {
                        BoundingBox.AddBoundingBox((cmd.Token as GCArc).GetBoundingBox(emu.Plane, new double[] { cmd.Start.X, cmd.Start.Y, cmd.Start.Z }, emu.DistanceMode == DistanceMode.Incremental));
                    }
                    else if (cmd.Token is GCSpline)
                    {
                        BoundingBox.AddBoundingBox((cmd.Token as GCSpline).GetBoundingBox(emu.Plane, new double[] { cmd.Start.X, cmd.Start.Y, cmd.Start.Z }, emu.DistanceMode == DistanceMode.Incremental));
                    }
                    else
                    {
                        BoundingBox.AddPoint(cmd.End);
                    }
                }

                BoundingBox.Conclude();

#if DEBUG
                stopWatch.Stop();
#endif

                //GCodeParser.Save(@"d:\tokens.xml", Parser.Tokens);
                //GCodeParser.Save(@"d:\file.nc", GCodeParser.TokensToGCode(Parser.Tokens));

                FileChanged?.Invoke(filename);
            }
        }