Esempio n. 1
0
        public bool CompareLines(string line, string compare)
        {
            bool result = false;

            try
            {
                CommandRef cmd1 = DetectCommand(line);
                CommandRef cmd2 = DetectCommand(compare);

                if (cmd1.Command == cmd2.Command)
                {
                    if (cmd1.Reference == cmd2.Reference)
                    {
                        string cut1 = cmd1.Line.Substring(cmd1.Line.IndexOf(cmd1.Reference) + cmd1.Reference.Length).Trim();
                        string cut2 = cmd2.Line.Substring(cmd2.Line.IndexOf(cmd2.Reference) + cmd2.Reference.Length).Trim();

                        if (cut1 == cut2 || cmd1.Command == cmd2.Command)
                        {
                            result = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 2
0
        public bool LineContainsSecondCommand(CommandRef command)
        {
            bool result = false;

            try
            {
                CommandRef cmd = DetectCommand(command.Line.Substring(command.Line.IndexOf(command.Reference) + command.Reference.Length));

                if (cmd.Command == command.Command && cmd.Line == command.Line && cmd.Reference == command.Reference)
                {
                    result = false;
                }
                else if (Statements.IsNull(cmd))
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 3
0
        public CommandRef DetectCommand(string line)
        {
            CommandRef        result  = new CommandRef(Commands.NONE, "", "");
            List <CommandRef> results = new List <CommandRef>();

            try
            {
                for (int k = 0; k < CommandList.Count; k++)
                {
                    for (int v = 0; v < CommandList.ElementAt(k).Value.Length; v++)
                    {
                        if (line.Contains(CommandList.ElementAt(k).Value[v]))
                        {
                            results.Add(new CommandRef(CommandList.ElementAt(k).Key, CommandList.ElementAt(k).Value[v], line));
                        }
                    }
                }

                int highestLength = 0;

                for (int i = 0; i < results.Count; i++)
                {
                    if (results[i].Reference.Length > highestLength)
                    {
                        highestLength = results[i].Reference.Length;
                        result        = results[i];
                    }
                    else if (results[i].Reference.Length == highestLength)
                    {
                        if (results[i].Line.IndexOf(results[i].Reference) < result.Line.IndexOf(result.Reference))
                        {
                            result = results[i];
                        }
                    }
                }

                if (Statements.IsNull(result.Command))
                {
                    result = HasCoordinates(line);
                }

                if (result.Command == Commands.LINE || result.Command == Commands.ARC)
                {
                    ModularCommand = result.Reference;
                }
                if (result.Command == Commands.ARC)
                {
                    ModularArcCommand = result.Reference;
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 4
0
        private string[] MakeArguments(CommandRef command)
        {
            string[] result = { };

            try
            {
                Array.Resize <string>(ref result, result.Length + 1);
                result[result.Length - 1] = command.Reference;

                if (command.Line.Contains("X"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "X";
                }
                if (command.Line.Contains("Y"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "Y";
                }
                if (command.Line.Contains("I"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "I";
                }
                if (command.Line.Contains("J"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "J";
                }
                if (command.Line.Contains("F"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "F";
                }
                if (command.Line.Contains("R"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "R";
                }
                if (command.Line.Contains("Z"))
                {
                    Array.Resize <string>(ref result, result.Length + 1);
                    result[result.Length - 1] = "Z";
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 5
0
        public static bool IsNull(CommandRef obj)
        {
            bool result = false;

            try
            {
                if (IsNull(obj.Command) || IsNull(obj.Line) || IsNull(obj.Reference))
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 6
0
        public CommandRef DetectMain(string line)
        {
            CommandRef result = new CommandRef(Commands.NONE, "", "");

            try
            {
                string[] cmds;
                CommandList.TryGetValue(Commands.SUBPROG, out cmds);

                for (int i = 0; i < cmds.Length; i++)
                {
                    if (line.Contains(cmds[i]))
                    {
                        result = new CommandRef(Commands.SUBPROG, cmds[i], line);
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Esempio n. 7
0
        private FileObject HandleCommand(CommandRef command)
        {
            FileObject result = new FileObject();

            try
            {
                command.Line = command.Line.Substring(command.Line.IndexOf(command.Reference));
                command.Line = command.Line.Replace(" ", "");

                switch (command.Command)
                {
                case Commands.NONE:
                    break;

                case Commands.LINE:
                {
                    //PenDown = (command.Reference == "G0" || command.Reference == "G00") ? false : true;

                    string[] args   = MakeArguments(command);
                    double[] coords = Array.ConvertAll(command.Line.Split(args, StringSplitOptions.RemoveEmptyEntries), double.Parse);

                    for (int i = 0; i < coords.Length; i++)
                    {
                        coords[i] = coords[i] / Scaling;
                    }

                    Coordinate origen = UseOldCoordinates(coords, args)[0];

                    if (args.Contains("Z"))
                    {
                        Depth = coords[Array.IndexOf(args, "Z") - 1];
                    }

                    if (RelativeMode && !Statements.IsNull(origen))
                    {
                        origen += OldOrigen;
                    }

                    if (PenDown && !Statements.IsNull(origen))
                    {
                        result              = new Line(OldOrigen, origen, Color, Depth);
                        TotalDistanceDrawn += AddDistance((Line)result);
                    }
                    else
                    {
                        result = new FileObject();
                        TotalDistanceBridge += AddDistance(new Line(OldOrigen, origen, Color, Depth));
                    }

                    if (origen != null)
                    {
                        OldOrigen    = origen;
                        OldArcCentre = origen;
                    }

                    break;
                }

                case Commands.ARC:
                {
                    string[] args   = MakeArguments(command);
                    double[] coords = Array.ConvertAll(command.Line.Split(args, StringSplitOptions.RemoveEmptyEntries), double.Parse);

                    for (int i = 0; i < coords.Length; i++)
                    {
                        coords[i] = coords[i] / Scaling;
                    }

                    Coordinate[] coordinates = UseOldCoordinates(coords, args);
                    Coordinate   origen      = coordinates[0];
                    Coordinate   distance    = coordinates[1];

                    if (RelativeMode)
                    {
                        origen += OldOrigen;
                    }

                    if (Module_Main.RelativeArc)
                    {
                        if (!Statements.IsNull(distance) && !Statements.IsNull(OldArcCentre) && !Statements.IsNull(OldOrigen))
                        {
                            if (distance.X == OldArcCentre.X && distance.Y != OldArcCentre.Y)
                            {
                                distance.Y += OldOrigen.Y;
                            }
                            else if (distance.X != OldArcCentre.X && distance.Y == OldArcCentre.Y)
                            {
                                distance.X += OldOrigen.X;
                            }
                            else
                            {
                                distance += OldOrigen;
                            }
                        }
                    }

                    if (PenDown)
                    {
                        if (command.Reference != "G02" && command.Reference != "G2")
                        {
                            result = new Arc(OldOrigen, origen, distance, Color, false, Depth);
                        }
                        else
                        {
                            result = new Arc(OldOrigen, origen, distance, Color, true, Depth);
                        }

                        TotalDistanceDrawn += AddDistance((Arc)result);
                    }
                    else
                    {
                        result = new FileObject();
                    }

                    OldOrigen    = origen;
                    OldArcCentre = distance;
                    break;
                }

                case Commands.SUBPROG:
                {
                    if (CurrentProgram != null && CurrentProgram.MadeByProgram)
                    {
                        Programs.Add(CurrentProgram);
                    }

                    if (!Statements.IsNull(CurrentProgram))
                    {
                        if (!CurrentProgram.ContainsContent())
                        {
                            break;
                        }
                    }

                    if (Programs.Contains(CurrentProgram))
                    {
                        break;
                    }

                    if (command.Reference.Contains("DFS,PC"))
                    {
                        result         = new SubProgram("C" + SubCounter, new Coordinate(0, 0), 0, new SectionOptions(SubPrograms.BOX, true, true, true));
                        CurrentProgram = (SubProgram)result;
                        break;
                    }

                    int[]  indexs  = ObjHndlr.GetSubIndexs(Lines, Index);
                    int    size    = indexs[1] - indexs[0];
                    int    section = ObjHndlr.GetSection(command.Line);
                    string name    = section.ToString();

                    if (section <= 0)
                    {
                        name = "MAIN";
                    }

                    SectionOptions options = new SectionOptions();

                    if (section > ObjHndlr.LastSubIndex && MainFound)
                    {
                        result = new FileObject();
                        break;
                    }

                    if (!MainFound)
                    {
                        options = new SectionOptions(SubPrograms.BOX, false, false, false);
                    }
                    else if (ObjHndlr.IsLastSub(section) && section > 2)
                    {
                        options = new SectionOptions(SubPrograms.BOX, true, true, false);
                    }
                    else
                    {
                        options = new SectionOptions(SubPrograms.SECTION, false, false, false);
                    }

                    result = new SubProgram(name, new Coordinate(0, 0), size, Color, options);

                    if (!SubAlreadyExists((SubProgram)result))
                    {
                        CurrentProgram = (SubProgram)result;
                    }
                    else
                    {
                        result = new FileObject();
                    }

                    OldOrigen    = new Coordinate(0, 0);
                    OldArcCentre = new Coordinate(0, 0);
                    break;
                }

                case Commands.SUBEND:
                {
                    if (CurrentProgram.ContainsCoordinates() && CurrentProgram.Program == SubPrograms.BOX)
                    {
                        CurrentProgram.InnerSection   = true;
                        CurrentProgram.InitializeNull = true;
                    }

                    if (CurrentProgram != null)
                    {
                        Programs.Add(CurrentProgram);
                    }

                    if (command.Line != "CLOSEDBYPROGRAM")
                    {
                        MainNotClosed = false;
                    }

                    CurrentProgram = null;
                    break;
                }

                case Commands.SUBCALL:
                {
                    int section = int.Parse(command.Line.Substring(command.Line.IndexOf(command.Reference) + command.Reference.Length));

                    if (ObjHndlr.IsLastSub(section) || section > ObjHndlr.LastSubIndex)
                    {
                        result = new FileObject();
                    }
                    else
                    {
                        result = new SubCall(section, OldOrigen);
                    }

                    MainProgram.AddContent((SubCall)result);
                    SubCalled = true;
                    result    = null;
                    break;
                }

                case Commands.ABSOLUTE:
                {
                    RelativeMode = false;
                    break;
                }

                case Commands.RELATIVE:
                {
                    RelativeMode = true;
                    break;
                }

                case Commands.TOOL:
                {
                    int temp = 0;

                    if (int.TryParse(command.Line.Substring(command.Line.IndexOf(command.Reference) + command.Reference.Length), out temp))
                    {
                        Color = temp;
                    }
                    else
                    {
                        for (int i = 0; i < command.Line.Length - command.Reference.Length; i++)
                        {
                            string s = command.Line.Substring(command.Reference.Length, i);

                            if (!int.TryParse(s, out temp))
                            {
                                if (i > 0)
                                {
                                    if (int.TryParse(command.Line.Substring(command.Reference.Length, i - 1), out temp))
                                    {
                                        Color = int.Parse(command.Line.Substring(command.Reference.Length, i - 1));
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    break;
                }

                case Commands.ON:
                {
                    PenDown = true;
                    break;
                }

                case Commands.OFF:
                {
                    PenDown = false;
                    break;
                }

                default:
                    break;
                }

                if (result.GetType().IsSubclassOf(typeof(FileCoordinateObject)) && result.GetType() != typeof(SubProgram) && Statements.IsNull(CurrentProgram))
                {
                    NoSubClause();
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e, Index + " | " + Lines[Index]);
                result = new FileObject();
            }

            return(result);
        }
Esempio n. 8
0
        public FileObject CreateProgram()
        {
            FileObject result = new FileObject();

            try
            {
                while (Index < Lines.Length)
                {
                    CommandRef cmd = ObjHndlr.DetectCommand(Lines[Index]);

                    if (!MainFound)
                    {
                        if (cmd.Command == Commands.SUBPROG)
                        {
                            MainProgram   = (SubProgram)HandleCommand(cmd);
                            MainFound     = true;
                            MainNotClosed = true;
                            Index++;
                            continue;
                        }
                        else if (!Statements.IsNull(cmd.Command))
                        {
                            MainProgram   = new SubProgram("MAIN", new Coordinate(), 0, new SectionOptions(SubPrograms.BOX, true, true, false));
                            MainFound     = true;
                            MainNotClosed = true;
                            Index++;
                            continue;
                        }
                        else if (Statements.IsNull(cmd.Command) && ObjHndlr.LastSubIndex < 1)
                        {
                            CommandRef command = new CommandRef(Commands.SUBPROG, "DFS,P", "DFS,PMAIN");
                            MainProgram = (SubProgram)HandleCommand(command);
                            MainFound   = true;
                        }
                    }

                    if (Statements.IsNull(cmd))
                    {
                        Index++;
                        continue;
                    }
                    else if (MainFound)
                    {
                        FileObject obj = HandleCommand(cmd);

                        if ((obj.GetType() == typeof(Line) || obj.GetType() == typeof(Arc)) && MainNotClosed && SubCalled)
                        {
                            FileObject esub = HandleCommand(new CommandRef(Commands.SUBEND, "CLOSEDBYPROGRAM", "CLOSEDBYPROGRAM"));
                            FileObject nsub = HandleCommand(new CommandRef(Commands.SUBPROG, "DFS,PC", "DFS,PC" + SubCounter));
                            SubCounter++;
                            SubCalled = false;
                        }
                        if (obj.GetType().IsSubclassOf(typeof(FileCoordinateObject)) && !Statements.IsNull(CurrentProgram) && obj.GetType() != typeof(SubProgram))
                        {
                            CurrentProgram.AddContent((FileCoordinateObject)obj);
                        }
                    }

                    if (ObjHndlr.LineContainsSecondCommand(cmd))
                    {
                        Lines[Index] = cmd.Line.Substring(cmd.Line.IndexOf(cmd.Reference) + cmd.Reference.Length);

                        if (ObjHndlr.CompareLines(Lines[Index], cmd.Line))
                        {
                            Index++;
                        }

                        continue;
                    }

                    Index++;
                }

                if (Programs.Count <= 0)
                {
                    if (!Statements.IsNull(CurrentProgram))
                    {
                        Programs.Add(CurrentProgram);
                    }

                    CurrentProgram = null;
                }

                ErrorHandler.AddMessage("Total distance drawn: " + TotalDistanceDrawn + "\r\nTotal distance moved: " + TotalDistanceBridge);
                SortSubs();
                OneSubClause();
                result = MainProgram;
            }
            catch (Exception e)
            {
                if (!Statements.IsNull(Lines[Index]))
                {
                    ErrorHandler.AddMessage(e, Lines[Index]);
                }
                else
                {
                    ErrorHandler.AddMessage(e);
                }
            }

            return(result);
        }
Esempio n. 9
0
        private CommandRef HasCoordinates(string line)
        {
            CommandRef result = new CommandRef();

            try
            {
                if (ModularCommand == "" || ModularCommand == string.Empty)
                {
                    return(result);
                }

                Commands cmd = GetCommandFromReference(ModularCommand);

                if (line.Contains('X') || line.Contains('Y') || line.Contains('Z'))
                {
                    if (line.Contains('I') || line.Contains('J'))
                    {
                        if (!Statements.IsNull(ModularArcCommand))
                        {
                            line   = ModularArcCommand + line.Substring(line.IndexOf('X'));
                            result = new CommandRef(Commands.ARC, ModularArcCommand, line);
                        }
                        else
                        {
                            line   = "G03" + line.Substring(line.IndexOf('X'));
                            result = new CommandRef(Commands.ARC, "G03", line);
                        }
                    }
                    else
                    {
                        if (cmd == Commands.LINE)
                        {
                            if (line.Contains('X'))
                            {
                                line = ModularCommand + line.Substring(line.IndexOf('X'));
                            }
                            else if (line.Contains('Y'))
                            {
                                line = ModularCommand + line.Substring(line.IndexOf('Y'));
                            }
                            else if (line.Contains('Z'))
                            {
                                line = ModularCommand + line.Substring(line.IndexOf('Z'));
                            }

                            result = new CommandRef(Commands.LINE, ModularCommand, line);
                        }
                        else
                        {
                            if (line.Contains('X'))
                            {
                                line = "G0" + line.Substring(line.IndexOf('X'));
                            }
                            else if (line.Contains('Y'))
                            {
                                line = "G0" + line.Substring(line.IndexOf('Y'));
                            }

                            result = new CommandRef(Commands.LINE, "G0", line);
                        }
                    }
                }
                else
                {
                    result = new CommandRef();
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }