Пример #1
0
        void emit_linear(GCodeLine line)
        {
            Debug.Assert(line.code == 1);

            double dx = 0, dy = 0;
            bool   brelx = GCodeUtil.TryFindParamNum(line.parameters, "XI", ref dx);
            bool   brely = GCodeUtil.TryFindParamNum(line.parameters, "YI", ref dy);

            LinearMoveData move = new LinearMoveData(new Vector2d(dx, dy));

            if (brelx || brely)
            {
                listener.LinearMoveToRelative2d(move);
                return;
            }

            double x = 0, y = 0;
            bool   absx = GCodeUtil.TryFindParamNum(line.parameters, "X", ref x);
            bool   absy = GCodeUtil.TryFindParamNum(line.parameters, "Y", ref y);

            if (absx && absy)
            {
                listener.LinearMoveToAbsolute2d(move);
                return;
            }

            // [RMS] can we have this??
            if (absx || absy)
            {
                System.Diagnostics.Debug.Assert(false);
            }
        }
Пример #2
0
        // any line we can't understand
        virtual protected GCodeLine make_string_line(string line, int nLineNum)
        {
            GCodeLine l = new GCodeLine(nLineNum, GCodeLine.LType.UnknownString);

            l.orig_string = line;
            return(l);
        }
Пример #3
0
 public LinearMoveData(Vector3d pos, double rateIn, Vector3d extrudeIn)
 {
     position = pos;
     rate     = rateIn;
     extrude  = extrudeIn;
     source   = null;
 }
Пример #4
0
        // :IF, :ENDIF, :ELSE
        virtual protected GCodeLine make_control_line(string line, string[] tokens, int nLineNum)
        {
            // figure out command type
            string command = tokens[0].Substring(1);

            GCodeLine.LType eType = GCodeLine.LType.UnknownControl;
            if (command.Equals("if", StringComparison.OrdinalIgnoreCase))
            {
                eType = GCodeLine.LType.If;
            }
            else if (command.Equals("else", StringComparison.OrdinalIgnoreCase))
            {
                eType = GCodeLine.LType.Else;
            }
            else if (command.Equals("endif", StringComparison.OrdinalIgnoreCase))
            {
                eType = GCodeLine.LType.EndIf;
            }

            GCodeLine l = new GCodeLine(nLineNum, eType);

            l.orig_string = line;

            if (tokens.Length > 1)
            {
                l.parameters = parse_parameters(tokens, 1);
            }

            return(l);
        }
Пример #5
0
 public LinearMoveData(Vector3d pos, double rateIn = GCodeUtil.UnspecifiedValue)
 {
     position = pos;
     rate     = rateIn;
     extrude  = GCodeUtil.UnspecifiedPosition;
     source   = null;
 }
Пример #6
0
        // G91
        private void set_relative_positioning(GCodeLine line)
        {
            UseRelativePosition = true;

            // [RMS] according to http://reprap.org/wiki/G-code#G91:_Set_to_Relative_Positioning,
            //   this should only happen on some firmware...
            UseRelativeExtruder = true;
        }
Пример #7
0
        // line starting with ;
        virtual protected GCodeLine make_comment(string line, int nLineNum)
        {
            GCodeLine l = new GCodeLine(nLineNum, GCodeLine.LType.Comment);

            l.orig_string = line;
            int iStart = line.IndexOf(';');

            l.comment = line.Substring(iStart);
            return(l);
        }
Пример #8
0
 protected virtual void close_current_line()
 {
     if (next_line != null)
     {
         if (next_params.Count > 0)
         {
             next_line.parameters = next_params.ToArray();
         }
         Target.AddLine(next_line);
         next_line = null;
     }
 }
Пример #9
0
        protected virtual void begin_new_line(GCodeLine.LType type)
        {
            if (next_line != null)
            {
                close_current_line();
            }

            next_line = new GCodeLine(next_line_number(), type);
            if (next_params == null || next_params.Count > 0)
            {
                next_params = new List <GCodeParam>();
            }
        }
Пример #10
0
        protected virtual GCodeLine MakeNCodeLine(string line, string nCodeToken, int lineNumber, LineType lineType)
        {
            var gcodeLine = new GCodeLine(lineNumber, lineType);

            gcodeLine.OriginalString = line;

            bool valid = int.TryParse(nCodeToken.Substring(1), out gcodeLine.N);

            // if we can't parse it as a valid N code, treat the entire line as an unknown string
            if (!valid)
            {
                return(make_string_line(line, lineNumber));
            }

            return(gcodeLine);
        }
Пример #11
0
 public virtual GCodeBuilder AddLine(GCodeLine line, bool bClone = true)
 {
     close_current_line();
     if (bClone)
     {
         GCodeLine clone = line.Clone();
         clone.lineNumber = next_line_number();
         Target.AddLine(clone);
     }
     else
     {
         line.lineNumber = next_line_number();
         Target.AddLine(line);
     }
     return(this);
 }
Пример #12
0
        // G### and M### code lines
        virtual protected GCodeLine make_GM_code_line(string line, string[] tokens, int nLineNum)
        {
            GCodeLine.LType eType = GCodeLine.LType.UnknownCode;
            if (tokens[0][0] == 'G')
            {
                eType = GCodeLine.LType.GCode;
            }
            else if (tokens[0][0] == 'M')
            {
                eType = GCodeLine.LType.MCode;
            }

            GCodeLine l = new GCodeLine(nLineNum, eType);

            l.orig_string = line;

            // [TODO] comments

            if (eType == GCodeLine.LType.UnknownCode)
            {
                l.N = int.Parse(tokens[0].Substring(1));
                if (tokens.Length > 1)
                {
                    l.parameters = parse_parameters(tokens, 1);
                }
            }
            else
            {
                var subcodePos = tokens[0].IndexOf('.');
                if (subcodePos == -1)
                {
                    l.code = int.Parse(tokens[0].Substring(1));
                }
                else
                {
                    l.code    = int.Parse(tokens[0].Substring(1, subcodePos - 1));
                    l.subcode = int.Parse(tokens[0].Substring(subcodePos + 1));
                }
                l.N = l.code;
                if (tokens.Length > 1)
                {
                    l.parameters = parse_parameters(tokens, 1);
                }
            }

            return(l);
        }
        // G92 - Position register: Set the specified axes positions to the given position
        // Sets the position of the state machine and the bot. NB: There are two methods of forming the G92 command:
        void set_position(GCodeLine line)
        {
            double x = 0, y = 0, z = 0;

            if (GCodeUtil.TryFindParamNum(line.parameters, "X", ref x))
            {
                CurPosition.x = x;
            }
            if (GCodeUtil.TryFindParamNum(line.parameters, "Y", ref y))
            {
                CurPosition.y = y;
            }
            if (GCodeUtil.TryFindParamNum(line.parameters, "Z", ref z))
            {
                CurPosition.z = z;
            }
        }
Пример #14
0
        public GCodeFile Parse(TextReader input)
        {
            GCodeFile file = new GCodeFile();

            int lines = 0;

            while (input.Peek() >= 0)
            {
                string line     = input.ReadLine();
                int    nLineNum = lines++;

                GCodeLine l = ParseLine(line, nLineNum);
                file.AppendLine(l);
            }

            return(file);
        }
Пример #15
0
        // G### and M### code lines
        virtual protected GCodeLine make_GM_code_line(string line, string[] tokens, int nLineNum)
        {
            LineType eType = LineType.UnknownCode;

            if (tokens[0][0] == 'G')
            {
                eType = LineType.GCode;
            }
            else if (tokens[0][0] == 'M')
            {
                eType = LineType.MCode;
            }

            GCodeLine l = new GCodeLine(nLineNum, eType);

            l.OriginalString = line;

            bool valid = int.TryParse(tokens[0].Substring(1), out l.N);

            // if we can't parse it as a valid G or M code, treat the entire line as an unknow string
            if (!valid)
            {
                return(make_string_line(line, nLineNum));
            }

            // [TODO] comments

            if (eType == LineType.UnknownCode)
            {
                if (tokens.Length > 1)
                {
                    l.Parameters = parse_parameters(tokens, 1);
                }
            }
            else
            {
                l.Code = int.Parse(tokens[0].Substring(1));
                if (tokens.Length > 1)
                {
                    l.Parameters = parse_parameters(tokens, 1);
                }
            }

            return(l);
        }
        public virtual GCodeLine Clone()
        {
            GCodeLine clone = new GCodeLine(this.lineNumber, this.type);

            clone.orig_string = this.orig_string;
            clone.N           = this.N;
            clone.code        = this.code;
            if (this.parameters != null)
            {
                clone.parameters = new GCodeParam[this.parameters.Length];
                for (int i = 0; i < this.parameters.Length; ++i)
                {
                    clone.parameters[i] = this.parameters[i];
                }
            }
            clone.comment = this.comment;
            return(clone);
        }
Пример #17
0
 protected virtual void ExtractDimensions(GCodeLine line, ref Vector2d dimensions)
 {
     if (line.comment != null && line.comment.Contains("tool"))
     {
         foreach (var word in line.comment.Split(' '))
         {
             int i = word.IndexOf('W');
             if (i >= 0)
             {
                 dimensions.x = double.Parse(word.Substring(i + 1));
             }
             i = word.IndexOf('H');
             if (i >= 0)
             {
                 dimensions.y = double.Parse(word.Substring(i + 1));
             }
         }
     }
 }
Пример #18
0
        protected static void ExtractPositionFeedrateAndExtrusion(GCodeLine line, ref Vector3d position, ref double feedrate, ref double extrusion)
        {
            if (line.parameters != null)
            {
                foreach (var param in line.parameters)
                {
                    switch (param.identifier)
                    {
                    case "X": position.x = param.doubleValue; break;

                    case "Y": position.y = param.doubleValue; break;

                    case "Z": position.z = param.doubleValue; break;

                    case "F": feedrate = param.doubleValue; break;

                    case "E": extrusion = param.doubleValue; break;
                    }
                }
            }
        }
Пример #19
0
        void emit_arc(GCodeLine line, bool clockwise)
        {
            double dx = 0, dy = 0;

            // either of these might be missing...
            bool brelx = GCodeUtil.TryFindParamNum(line.parameters, "XI", ref dx);
            bool brely = GCodeUtil.TryFindParamNum(line.parameters, "YI", ref dy);

            Debug.Assert(brelx == true && brely == true);

            double r  = 0;
            bool   br = GCodeUtil.TryFindParamNum(line.parameters, "R", ref r);

            Debug.Assert(br == true);

            // [RMS] seems like G5 always has negative radius and G4 positive ??
            //   (this will tell us)
            Debug.Assert((clockwise && r < 0) || (clockwise == false && r > 0));
            r = Math.Abs(r);

            listener.ArcToRelative2d(new Vector2d(dx, dy), r, clockwise);
        }
Пример #20
0
        // N### lines
        virtual protected GCodeLine make_N_code_line(string line, string[] tokens, int nLineNum)
        {
            GCodeLine.LType eType = GCodeLine.LType.UnknownCode;
            if (tokens[1][0] == 'G')
            {
                eType = GCodeLine.LType.GCode;
            }
            else if (tokens[1][0] == 'M')
            {
                eType = GCodeLine.LType.MCode;
            }

            GCodeLine l = new GCodeLine(nLineNum, eType);

            l.orig_string = line;

            l.N = int.Parse(tokens[0].Substring(1));

            // [TODO] comments

            if (eType == GCodeLine.LType.UnknownCode)
            {
                if (tokens.Length > 1)
                {
                    l.parameters = parse_parameters(tokens, 1);
                }
            }
            else
            {
                l.code = int.Parse(tokens[1].Substring(1));
                if (tokens.Length > 2)
                {
                    l.parameters = parse_parameters(tokens, 2);
                }
            }

            return(l);
        }
Пример #21
0
        // G92 - Position register: Set the specified axes positions to the given position
        // Sets the position of the state machine and the bot. NB: There are two methods of forming the G92 command:
        private void set_position(GCodeLine line)
        {
            double x = 0, y = 0, z = 0, a = 0;

            if (GCodeUtil.TryFindParamNum(line.Parameters, "X", ref x))
            {
                CurPosition.x = x;
            }
            if (GCodeUtil.TryFindParamNum(line.Parameters, "Y", ref y))
            {
                CurPosition.y = y;
            }
            if (GCodeUtil.TryFindParamNum(line.Parameters, "Z", ref z))
            {
                CurPosition.z = z;
            }
            if (GCodeUtil.TryFindParamNum(line.Parameters, "A", ref a))
            {
                ExtrusionA = a;
                listener.CustomCommand(
                    (int)CustomListenerCommands.ResetExtruder, GCodeUtil.Extrude(a));
                // reset our state
                in_travel = in_extrude = in_retract = false;
            }

            // E is "current" stepper (A for single extruder)
            double e = 0;

            if (GCodeUtil.TryFindParamNum(line.Parameters, "E", ref e))
            {
                ExtrusionA = e;
                listener.CustomCommand(
                    (int)CustomListenerCommands.ResetExtruder, GCodeUtil.Extrude(e));
                // reset our state
                in_travel = in_extrude = in_retract = false;
            }
        }
Пример #22
0
        virtual protected GCodeLine ParseLine(string line, int nLineNum)
        {
            if (line.Length == 0)
            {
                return(make_blank(nLineNum));
            }
            if (line[0] == ';')
            {
                return(make_comment(line, nLineNum));
            }

            // strip off trailing comment
            string comment = null;
            int    ci      = line.IndexOf(';');

            if (ci < 0)
            {
                int bo = line.IndexOf('(');
                int bc = line.IndexOf(')');
                if (bo >= 0 && bc > 0)
                {
                    ci = bo;
                }
            }
            if (ci >= 1)
            {
                comment = line.Substring(ci);
                line    = line.Substring(0, ci);
            }


            string[] tokens = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);

            // handle extra spaces at start...?
            if (tokens.Length == 0)
            {
                return(make_blank(nLineNum));
            }

            GCodeLine gcode = null;

            switch (tokens[0][0])
            {
            case ';':
                gcode = make_comment(line, nLineNum);
                break;

            case 'N':
                gcode = make_N_code_line(line, tokens, nLineNum);
                break;

            case 'G':
            case 'M':
                gcode = make_GM_code_line(line, tokens, nLineNum);
                break;

            case ':':
                gcode = make_control_line(line, tokens, nLineNum);
                break;

            default:
                gcode = make_string_line(line, nLineNum);
                break;
            }

            if (comment != null)
            {
                gcode.comment = comment;
            }

            return(gcode);
        }
Пример #23
0
 void emit_cw_arc(GCodeLine line)
 {
     emit_arc(line, true);
 }
 public abstract void WriteLine(GCodeLine line, StreamWriter outStream);
Пример #25
0
 void emit_ccw_arc(GCodeLine line)
 {
     emit_arc(line, false);
 }
        void emit_linear(GCodeLine line)
        {
            Debug.Assert(line.code == 0 || line.code == 1);

            double x         = GCodeUtil.UnspecifiedValue,
                   y         = GCodeUtil.UnspecifiedValue,
                   z         = GCodeUtil.UnspecifiedValue;
            bool     found_x = GCodeUtil.TryFindParamNum(line.parameters, "X", ref x);
            bool     found_y = GCodeUtil.TryFindParamNum(line.parameters, "Y", ref y);
            bool     found_z = GCodeUtil.TryFindParamNum(line.parameters, "Z", ref z);
            Vector3d newPos  = (UseRelativePosition) ? Vector3d.Zero : CurPosition;

            if (found_x)
            {
                newPos.x = x;
            }
            if (found_y)
            {
                newPos.y = y;
            }
            if (found_z)
            {
                newPos.z = z;
            }
            if (UseRelativePosition)
            {
                CurPosition += newPos;
            }
            else
            {
                CurPosition = newPos;
            }

            // F is feed rate (this changes?)
            double f     = 0;
            bool   haveF = GCodeUtil.TryFindParamNum(line.parameters, "F", ref f);

            LinearMoveData move = new LinearMoveData(
                newPos,
                (haveF) ? f : GCodeUtil.UnspecifiedValue);

            bool is_travel = (line.code == 0);

            if (is_travel)
            {
                if (in_travel == false)
                {
                    listener.BeginTravel();
                    in_travel = true;
                    in_cut    = false;
                }
            }
            else
            {
                if (in_cut == false)
                {
                    listener.BeginCut();
                    in_travel = false;
                    in_cut    = true;
                }
            }

            move.source = line;
            Debug.Assert(in_travel || in_cut);
            listener.LinearMoveToAbsolute3d(move);
        }
 // G91
 void set_relative_positioning(GCodeLine line)
 {
     UseRelativePosition = true;
 }
 // G90
 void set_absolute_positioning(GCodeLine line)
 {
     UseRelativePosition = false;
 }
Пример #29
0
        public override void WriteLine(GCodeLine line, StreamWriter outStream)
        {
            if (line.type == GCodeLine.LType.Comment)
            {
                if (CommentStyle == CommentStyles.Semicolon)
                {
                    if (line.comment[0] != ';')
                    {
                        outStream.Write(";");
                    }
                    outStream.WriteLine(line.comment);
                }
                else
                {
                    outStream.Write("(");
                    outStream.Write(line.comment);
                    outStream.WriteLine(")");
                }
                return;
            }
            else if (line.type == GCodeLine.LType.UnknownString)
            {
                outStream.WriteLine(line.orig_string);
                return;
            }
            else if (line.type == GCodeLine.LType.Blank)
            {
                return;
            }

            StringBuilder b = new StringBuilder();

            if (line.type == GCodeLine.LType.MCode)
            {
                b.Append('M');
            }
            else if (line.type == GCodeLine.LType.GCode)
            {
                b.Append('G');
            }
            else
            {
                throw new Exception("StandardGCodeWriter.WriteLine: unsupported line type");
            }

            b.Append(line.code);
            if (line.subcode.HasValue)
            {
                b.Append('.').Append(line.subcode.Value);
            }
            b.Append(' ');

            if (line.parameters != null)
            {
                foreach (GCodeParam p in line.parameters)
                {
                    if (p.type == GCodeParam.PType.Code)
                    {
                        //
                    }
                    else if (p.type == GCodeParam.PType.IntegerValue)
                    {
                        b.Append(p.identifier);
                        b.Append(p.intValue);
                        b.Append(' ');
                    }
                    else if (p.type == GCodeParam.PType.DoubleValue)
                    {
                        b.Append(p.identifier);
                        b.AppendFormat(float_format, p.doubleValue);
                        b.Append(' ');
                    }
                    else if (p.type == GCodeParam.PType.TextValue)
                    {
                        b.Append(p.identifier);
                        b.Append(p.textValue);
                        b.Append(' ');
                    }
                    else if (p.type == GCodeParam.PType.NoValue)
                    {
                        b.Append(p.identifier);
                        b.Append(' ');
                    }
                    else
                    {
                        throw new Exception("StandardGCodeWriter.WriteLine: unsupported parameter type");
                    }
                }
            }


            if (line.comment != null && line.comment.Length > 0)
            {
                if (CommentStyle == CommentStyles.Semicolon)
                {
                    if (line.comment[0] != '(' && line.comment[0] != ';')
                    {
                        b.Append(';');
                    }
                    b.Append(line.comment);
                }
                else
                {
                    b.Append("(");
                    b.Append(line.comment);
                    b.Append(")");
                }
            }

            outStream.WriteLine(b.ToString());
        }
Пример #30
0
 public virtual void AddLine(GCodeLine line)
 {
     File.AppendLine(line);
 }