コード例 #1
0
ファイル: SubProgram.cs プロジェクト: Toez/CNC-HPGL-Converter
        public void AddContent(FileCoordinateObject item)
        {
            try
            {
                if (ContentIndex < Content.Length)
                {
                    Content[ContentIndex] = item;
                }
                else
                {
                    FileObject[] temp = new FileObject[ContentIndex + 1];
                    Array.Copy(Content, temp, Content.Length);
                    Content = temp;
                    Content[ContentIndex] = item;
                }

                ContentIndex++;
            }
            catch (Exception e)
            {
                ErrorHandler.AddMessage(e);
            }
        }
コード例 #2
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);
        }
コード例 #3
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);
        }