public bool ContainsContent()
        {
            bool result = false;

            try
            {
                for (int i = 0; i < Content.Length; i++)
                {
                    if (!Statements.IsNull(Content[i]))
                    {
                        if (Content[i].GetType().IsSubclassOf(typeof(FileCoordinateObject)))
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
        public Commands[] GetCommandsFromReference(string reference)
        {
            Commands[] result = { };

            try
            {
                for (int i = 0; i < CommandList.Count; i++)
                {
                    string[] refs = { };
                    CommandList.TryGetValue((Commands)i, out refs);

                    if (!Statements.IsNull(refs))
                    {
                        if (refs.Contains(reference))
                        {
                            Array.Resize <Commands>(ref result, result.Length + 1);
                            result[result.Length - 1] = (Commands)i;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
        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);
        }
        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);
        }
        public bool ContainsCoordinates()
        {
            bool result = false;

            try
            {
                for (int i = 0; i < Content.Length; i++)
                {
                    if (!Statements.IsNull(Content[i]) && (Content[i].GetType() == typeof(Line) || Content[i].GetType() == typeof(Arc)))
                    {
                        result = true;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }

            return(result);
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
        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);
        }