コード例 #1
0
        private string[] SplitLine(string cmdLine)
        {
            string cmd = TextHndlr.CutCommand(cmdLine);

            string[] result = { };
            string[] temp   = { };

            temp   = cmdLine.Split(new char[] { ';', ',' });
            result = (string[])temp.Clone();

            for (int i = 0; i < result.Length; i++)
            {
                for (int n = 0; n < result[i].Length; n++)
                {
                    if (TextHndlr.IsLetter(result[i][n].ToString()))
                    {
                        temp[i] = result[i].Substring(n + 1);
                    }
                    else if (TextHndlr.IsNumber(result[i][n].ToString()))
                    {
                        continue;
                    }
                    else
                    {
                        if (result[i][n] != '.' && result[i][n] != '-')
                        {
                            temp[i] = result[i].Substring(n + 1);
                        }
                    }
                }

                if (temp[i] == "")
                {
                    Array.Clear(temp, i, 1);
                }
            }

            result = (string[])temp.Clone();
            temp   = new string[] { };

            for (int i = 0; i < result.Length; i++)
            {
                if (result[i] != null)
                {
                    Array.Resize(ref temp, temp.Count() + 1);
                    temp[temp.Count() - 1] = result[i];
                }
            }

            result = temp;

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Sets color of the pen
        /// </summary>
        /// <param name="cmdLine">Current command line</param>
        /// <returns>True or false whether tool is changed or not</returns>
        public virtual bool Tool(string cmdLine)
        {
            if (cmdLine[cmdLine.Length - 1] == ';')
            {
                cmdLine = cmdLine.Substring(0, cmdLine.Length - 1).Trim();
            }

            if (cmdLine[0] == 'M')
            {
                cmdLine = TextHndlr.NextCommand(cmdLine, "M");
            }

            int color;

            // Skip command after tool select
            if (cmdLine.Contains('F'))
            {
                color = int.Parse(cmdLine.Substring(1, cmdLine.IndexOf('F') - 1).Trim());
            }
            else if (cmdLine.Contains('H'))
            {
                color = int.Parse(cmdLine.Substring(1, cmdLine.IndexOf('M') - 1).Trim());
            }
            else if (cmdLine.Contains('T'))
            {
                if (cmdLine.Substring(1).Contains('M'))
                {
                    color = int.Parse(cmdLine.Substring(1, cmdLine.IndexOf('M') - 1));
                }
                else
                {
                    color = int.Parse(cmdLine.Substring(1).Trim());
                }
            }
            else if (cmdLine.Contains("SP"))
            {
                color = int.Parse(cmdLine.Substring(2).Trim());
            }
            else if (TextHndlr.IsNumber(cmdLine.Substring(1)))
            {
                color = int.Parse(cmdLine.Substring(1).Trim());
            }
            else
            {
                color = Color;
            }

            // Set color
            Color = color;

            return(true);
        }
コード例 #3
0
        public bool Circle(string cmdLine)
        {
            string fCmd = TextHndlr.CutCommand(cmdLine);

            string[] coords = SplitLine(cmdLine);

            if (PenDown)
            {
                Output.OutputCircle(LastX, LastY, Radius, Color);
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Call a new sub
        /// </summary>
        /// <param name="cmdLine">Current command line</param>
        /// <returns>True of false whether a new sub is called or not</returns>
        public virtual bool CallSubmain(string cmdLine)
        {
            if (TextHndlr.IsNumber(cmdLine[1].ToString()))
            {
                NewSubMain = int.Parse(cmdLine.Substring(1), CultureInfo.InvariantCulture);
                // Call the sub
                Output.OutputInstance(NewSubMain, LastX, LastY);

                if (!SubColors.ContainsKey(NewSubMain.ToString()))
                {
                    SubColors.Add(NewSubMain.ToString(), Color);
                }
            }

            return(true);
        }
コード例 #5
0
ファイル: Conversion.cs プロジェクト: Toez/CNC-HPGL-Converter
        private bool ConvertLine(string Line)
        {
            bool result = false;

            /// Delete comments from current line
            Line = TextHndlr.CleanComments(Line);

            do
            {
                // Get command from current line
                Command = TextHndlr.CutCommand(Line);

                // Check if command is avaible, invoke command's method with current line
                if (FunctionDic.ContainsKey(Command))
                {
                    result = FunctionDic[Command].Invoke(Line);
                }
                else if (Command != "" && (Command[0] == 'X' || Command[0] == 'Y') && Functions.ModularCommand != null)
                {
                    // Get reoccurring command in line
                    Command = Functions.ModularCommand;
                    // Create new line for reoccuring command
                    Line = Command + Line;
                    // Invoke new commandline
                    result = FunctionDic[Command].Invoke(Line);
                }

                // Save command as old command
                LastCommand = Command;
                // Look for next command in line
                Line = TextHndlr.NextCommand(Line, Command);

                // Skip line if the pen already moved
                if (LastCommand == "G01" || LastCommand == "G1" || LastCommand == "G02" || LastCommand == "G2" || LastCommand == "G03" || LastCommand == "G3" || LastCommand == "G0" || LastCommand == "G00" || LastCommand == "DFS")
                {
                    Line = "";
                }
            } while (Line != "");

            return(result);
        }
コード例 #6
0
        public virtual bool Rotary(string cmdLine)
        {
            string fCmd = cmdLine.Substring(0, cmdLine.IndexOf('X')).Trim();

            cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);

            string[] cmds = cmdLine.Split('X', 'A', 'R');
            X = double.Parse(cmds[1], CultureInfo.InvariantCulture);
            Y = double.Parse(cmds[2], CultureInfo.InvariantCulture);
            I = double.Parse(cmds[3], CultureInfo.InvariantCulture);

            NewX = (RelativeCoord) ? X + LastX : X;
            NewY = (RelativeCoord) ? Y * I + LastY : Y * I;

            if (PenDown)
            {
                Output.OutputLine(LastX, LastY, NewX, NewY, Color);
            }

            LastX = NewX;
            LastY = NewY;

            return(true);
        }
コード例 #7
0
        /// <summary>
        /// Begins writing a new sub
        /// </summary>
        /// <param name="cmdLine">Current command line</param>
        /// <returns>True of false whether a new sub began</returns>
        public virtual bool StartSub(string cmdLine)
        {
            string result = "";

            // Sub keywords
            string[] subRefs = { "P", "O", "DFS", "$PROGIN", "%" };
            // String used to detect start of sub
            string thisSubRef = "";
            bool   subFound   = false;

            // Look for the sub reference in line
            for (int i = 0; i < cmdLine.Length && !subFound; i++)
            {
                for (int n = 0; n < subRefs.Length && !subFound; n++)
                {
                    if (cmdLine[i] == subRefs[n][0])
                    {
                        if (cmdLine.Substring(i, subRefs[n].Length) == subRefs[n])
                        {
                            subFound   = true;
                            thisSubRef = subRefs[n];
                        }
                    }
                }
            }

            // Get index of current sub
            switch (thisSubRef)
            {
            case "P":
                result = cmdLine.Substring(cmdLine.IndexOf(thisSubRef) + thisSubRef.Length);
                break;

            case "O":
                result = cmdLine.Substring(cmdLine.IndexOf(thisSubRef) + thisSubRef.Length);
                break;

            case "DFS":
                result = cmdLine.Substring(cmdLine.IndexOf('P') + 1, (cmdLine.IndexOf(',') + cmdLine.Substring(cmdLine.IndexOf(',') + 1).IndexOf(',')) - cmdLine.IndexOf('P'));
                break;

            case "$PROGIN":
                result = cmdLine.Substring(cmdLine.IndexOf(thisSubRef) + thisSubRef.Length);
                break;

            case "%":
                if (cmdLine.Length > 1 && TextHndlr.IsNumber(cmdLine.Substring(cmdLine.IndexOf("%")) + 1))
                {
                    result = cmdLine.Substring(cmdLine.IndexOf(thisSubRef) + thisSubRef.Length);
                }
                else
                {
                    return(true);
                }
                break;

            default:
                result = "";
                break;
            }

            if (TextHndlr.IsNumber(result))
            {
                CurrentSub = result;
            }

            if (TextHndlr.IsNumber(result) && (!SubFirst && !IsLastSub) && !SubOpen)
            {
                // Change current sub
                CurrentSub = result;

                // Start new section
                Output.OutputSectionStart(CurrentSub, CurrentSub);

                // The sub is now open
                SubOpen = true;

                LastX = 0;
                LastY = 0;
            }
            else if (TextHndlr.IsNumber(result) && (SubFirst || IsLastSub) && !SubOpen)
            {
                CurrentSub = result;
                BoxOpen    = true;

                Output.OutputBoxEnd();
                Output.OutputBoxStart(CurrentSub);
                Output.OutputSectionStart(CurrentSub, CurrentSub);

                LastX = 0;
                LastY = 0;
            }

            return(true);
        }
コード例 #8
0
        /// <summary>
        /// Creates an arc in the output file
        /// </summary>
        /// <param name="cmdLine">Current command line</param>
        /// <returns>True of false whether the arc is drawn</returns>
        public virtual bool Arc(string cmdLine)
        {
            ReplaceAxis(ref cmdLine);

            if (((cmdLine.Contains("G0") || cmdLine.Contains("G00")) && !cmdLine.Contains("G01")) && SubColors.ContainsKey(CurrentSub))
            {
                Color = SubColors[CurrentSub];
            }

            // Detect move command
            //if ((cmdLine.Contains("G2") || cmdLine.Contains("G02")) && !cmdLine.Contains("G03"))
            //    PenDown = false;
            //else
            //    PenDown = true;

            // Get first command
            string fCmd = TextHndlr.CutCommand(cmdLine);

            cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);

            // If X is still not found, use modular command
            if (!cmdLine.Contains('X') && !cmdLine.Contains('Y'))
            {
                ModularCommand = fCmd;

                if (cmdLine.Contains('Z'))
                {
                    PenDown = cmdLine[cmdLine.IndexOf('Z') + 1] == '-';
                }

                return(true);
            }

            // Split the string
            string[] cmds;
            if (!cmdLine.Contains("F"))
            {
                cmds = cmdLine.Split('X', 'Y', 'I', 'J');
            }
            else
            {
                cmds = cmdLine.Split('X', 'Y', 'I', 'J', 'F');
            }

            X = double.Parse(cmds[1], CultureInfo.InvariantCulture);
            Y = double.Parse(cmds[2], CultureInfo.InvariantCulture);
            I = double.Parse(cmds[3], CultureInfo.InvariantCulture);
            J = double.Parse(cmds[4], CultureInfo.InvariantCulture);

            // Add old coordinates if coordinates are relative
            NewX = (RelativeCoord) ? X + LastX : X;
            NewY = (RelativeCoord) ? Y + LastY : Y;

            // Add old centre coordinates if whole program is relative
            if (Module_Main.RelativeArc)
            {
                I = I + LastX;
                J = J + LastY;
            }
            // Add old centre coordinates if centre coordinates are relative
            else
            {
                I = (RelativeCoord) ? I + LastX : I;
                J = (RelativeCoord) ? J + LastY : J;
            }

            // Skip if radius is too small
            if (Radius == 0)
            {
                return(false);
            }

            // Draw circle if coordinates are from the same origen
            if (LastX == NewX && LastY == NewY)
            {
                // Draw circle
                if (PenDown)
                {
                    Output.OutputCircle(I, J, Radius, Color);
                }
            }
            else
            {
                // otherwise it is an arc
                if (fCmd == "G02" || fCmd == "G2")
                {
                    Radius = -Radius;
                }
                //  Draw arc
                if (PenDown)
                {
                    Output.OutputArc(LastX, LastY, NewX, NewY, I, J, Radius, Color);
                }
            };

            // Set new coordinates to old coordinates
            LastX = NewX;
            LastY = NewY;

            ModularCommand = fCmd;

            return(true);
        }
コード例 #9
0
        /// <summary>
        /// Creates a line and writes it in output file
        /// </summary>
        /// <param name="cmdLine">Current command line</param>
        /// <returns>Returns true or false whether line is drawn or not</returns>
        public virtual bool Line(string cmdLine)
        {
            ReplaceAxis(ref cmdLine);

            if (((cmdLine.Contains("G0") || cmdLine.Contains("G00")) && !cmdLine.Contains("G01")) && SubColors.ContainsKey(CurrentSub))
            {
                Color = SubColors[CurrentSub];
            }

            // If there is no axis, skip command
            if (!cmdLine.Contains('X') && !cmdLine.Contains('Y'))
            {
                ModularCommand = TextHndlr.CutCommand(cmdLine);

                if (cmdLine.Contains('Z'))
                {
                    PenDown = cmdLine[cmdLine.IndexOf('Z') + 1] == '-';
                }

                return(true);
            }

            // Detect move command
            if ((cmdLine.Contains("G0") || cmdLine.Contains("G00")) && !cmdLine.Contains("G01"))
            {
                PenDown = false;
            }
            else if (!cmdLine.Contains("G0") && !cmdLine.Contains("G00") && !cmdLine.Contains("G01") && !cmdLine.Contains("G1") && cmdLine.Contains("X"))
            {
                PenDown = false;
            }
            else
            {
                PenDown = true;
            }

            // Get first command
            string fCmd = TextHndlr.CutCommand(cmdLine);

            // Get begin of coordinates
            if (fCmd != "X")
            {
                cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);
            }
            // If X is still not found, use modular command
            if (fCmd != "X")
            {
                ModularCommand = fCmd;
            }

            if (cmdLine.Contains("G76"))
            {
                cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);
            }

            // Split the string for X and Y coords
            if (cmdLine.Contains('X') && cmdLine.Contains('Y'))
            {
                string[] cmds = cmdLine.Split('X', 'Y');
                X = double.Parse(cmds[1], CultureInfo.InvariantCulture);
                Y = double.Parse(cmds[2], CultureInfo.InvariantCulture);
            }
            // Check if only X has to be changes
            else if (cmdLine.Contains('X'))
            {
                // If the line contains an F, cut number from X to F
                if (cmdLine.Contains('F'))
                {
                    X = double.Parse(cmdLine.Substring(cmdLine.IndexOf('X') + 1, cmdLine.IndexOf('F') - cmdLine.IndexOf('X') - 1).Trim());
                }
                else
                {
                    X = double.Parse(cmdLine.Substring(cmdLine.IndexOf('X') + 1).Trim());
                }
            }
            // Check if only Y has to be changes
            else if (cmdLine.Contains('Y'))
            {
                // If the line contains an F, cut number from Y to F
                if (cmdLine.Contains('F'))
                {
                    Y = double.Parse(cmdLine.Substring(cmdLine.IndexOf('Y') + 1, cmdLine.IndexOf('F') - cmdLine.IndexOf('Y') - 1).Trim());
                }
                else
                {
                    Y = double.Parse(cmdLine.Substring(cmdLine.IndexOf('Y') + 1).Trim());
                }
            }

            // If line contains Z, use Z to move pen up and down
            if (cmdLine.Contains('Z'))
            {
                PenDown = cmdLine[cmdLine.IndexOf('Z') + 1] == '-';
            }

            // Add old coordinates if coordinates are relative
            NewX = (RelativeCoord) ? X + LastX : X;
            NewY = (RelativeCoord) ? Y + LastY : Y;

            // Draw line
            if (PenDown)
            {
                Output.OutputLine(LastX, LastY, NewX, NewY, Color);
            }

            // Set new coordinates to old coordinates
            LastX = NewX;
            LastY = NewY;

            if (PenDown)
            {
                ModularCommand = "G1";
            }
            else
            {
                ModularCommand = "G0";
            }

            return(true);
        }
コード例 #10
0
        public override bool Arc(string cmdLine)
        {
            // Get the first command it finds
            string fCmd = TextHndlr.CutCommand(cmdLine);

            //cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);

            if (fCmd == "AA")
            {
                Absolute(cmdLine);
            }
            else if (fCmd == "AR")
            {
                Relative(cmdLine);
            }

            // Get coordinates and angle from line
            string[] coords = SplitLine(cmdLine);
            // Define I coord
            I = Math.Round(double.Parse(coords[0]) / Factor, 3);
            // Define J coord
            J = Math.Round(double.Parse(coords[1]) / Factor, 3);
            // Define angle
            Angle = Math.Round(double.Parse(coords[2]), 3);

            // 360 Angle is a circle
            if (Angle == 360)
            {
                // Draw circle
                Output.OutputCircle(I, J, Radius, Color);
            }
            else
            {
                if (DiX == 0)
                {
                    // Calcute end of angle
                    EndAngle = Angle * Module_Main.PI / 180 + (Module_Main.PI / 2) * Math.Sign(DiY);
                }
                else
                {
                    if (DiY != 0)
                    {
                        if (DiX < 0)
                        {
                            // Calculate start of angle
                            StartAngle = (DiY < 0) ? Math.Atan(DiY / DiX) + Module_Main.PI : Module_Main.PI - Math.Atan(DiY / (-DiX));
                        }
                        else
                        {
                            // Calculate start of angle
                            StartAngle = Math.Atan(DiY / DiX);
                        }

                        // Calculate end of angle
                        EndAngle = Angle * Module_Main.PI / 180 + StartAngle;
                    }
                    else
                    {
                        // Calculate end of engle
                        EndAngle = Angle * Module_Main.PI / 180 + ((DiX > 0) ? 0 : Module_Main.PI);
                    }
                }

                // Calculate new X and Y coordinates
                NewX = Radius * Math.Cos(EndAngle) + I;
                NewY = Radius * Math.Sin(EndAngle) + J;

                // Clockwise or counterclockwise
                if (Angle < 0)
                {
                    Radius = -Radius;
                }

                if (PenDown)
                {
                    Output.OutputArc(LastX, LastY, NewX, NewY, I, J, Radius, Color);
                }

                LastX = NewX;
                LastY = NewY;
            }

            return(true);
        }
コード例 #11
0
        public override bool Line(string cmdLine)
        {
            try
            {
                // Get the first command it finds
                string fCmd = TextHndlr.CutCommand(cmdLine);
                //cmdLine = TextHndlr.NextCommand(cmdLine, fCmd);
                // Create a string array of coordinates
                string[] coords = { };

                //PenDown = (fCmd == "PD" && fCmd != "PU") ? true : false;

                if (cmdLine.Contains("AA"))
                {
                    Arc(cmdLine);
                }

                // Look if the command is absolute
                if (fCmd == "PA")
                {
                    Absolute(cmdLine);
                }
                // Look if the command is relative
                else if (fCmd == "PR")
                {
                    Relative(cmdLine);
                }

                // Look if the commandline contains any coords
                if (cmdLine.Contains(','))
                {
                    // Fill string array with coordinates
                    coords = SplitLine(cmdLine);
                }
                else
                {
                    return(false);
                }

                // Define X coord
                X = double.Parse(coords[0]);
                // Define Y coord
                Y = double.Parse(coords[1]);
                // Scale new by factor
                NewX = X / Factor;
                NewY = Y / Factor;

                // Write if current penstate is down
                if (PenDown)
                {
                    Output.OutputLine(LastX, LastY, NewX, NewY, Color);
                }

                // Change old coords to new coords
                LastX = NewX;
                LastY = NewY;

                if (coords.Length > 2)
                {
                    string temp = fCmd;

                    for (int i = 2; i < coords.Length; i++)
                    {
                        temp += coords[i] + ",";
                    }

                    Line(temp);
                }
            }
            catch (Exception e)
            {
            }

            return(true);
        }