Ejemplo n.º 1
0
        private void ReadHpglFile(string filename)
        {
            StreamReader reader = new StreamReader(filename);

            _hpgl = HPGL.Parse(reader.ReadToEnd());
            UpdateInfos();
            DrawPath();
        }
Ejemplo n.º 2
0
        private string CreateGcodeFromHpgl(HPGL hpgl, Profile profile)
        {
            StringBuilder GCode = new StringBuilder();
            //GCode.AppendFormat("; Generated by HPGL-to-GCODE\r\n; Profile: {0}\r\n", p.Profilename);

            float  endstopOffset    = profile.EndstopOffset;
            float  paperThickness   = profile.PaperThickness;
            float  paperPenetration = profile.PaperPenetraion / 100.0f;
            float  vinylThickness   = profile.VinylThickness;
            float  cutFeedrate      = profile.CutFeedrate;
            float  moveFeedrate     = profile.MoveFeedrate;
            float  retractHeight    = profile.RetractHeight;
            string startCode        = profile.StartCode;
            string endCode          = profile.EndCode;

            float retractZPos = endstopOffset + paperThickness + vinylThickness + retractHeight;
            float cutZPos     = endstopOffset + paperThickness - (paperThickness * paperPenetration);
            float travelFeed  = moveFeedrate * 60;
            float cuttingFeed = cutFeedrate * 60;

            if (startCode.Length > 0)
            {
                startCode = startCode.Replace("{height}", retractZPos.ToString());
                startCode = startCode.Replace("{feed}", travelFeed.ToString());
                GCode.AppendLine(startCode);
            }

            foreach (var command in _hpgl.Commands)
            {
                switch (command.Instruction)
                {
                case HPGL.Instruction.PU:
                    GCode.AppendLine($"G1 Z{Math.Round(retractZPos, 3)} F{travelFeed}");
                    break;

                case HPGL.Instruction.PD:
                    GCode.AppendLine($"G1 Z{Math.Round(cutZPos, 3)} F{cuttingFeed}");
                    break;
                }

                for (int i = 0; i < command.Coordinates.Count; i++)
                {
                    float x = (float)command.Coordinates[i].X * resolution;
                    float y = (float)command.Coordinates[i].Y * resolution;

                    GCode.AppendLine($"G1 X{Math.Round(x, 3)} Y{Math.Round(y, 3)}");
                }
            }

            if (endCode.Length > 0)
            {
                GCode.Append(endCode);
            }

            return(GCode.ToString());
        }
Ejemplo n.º 3
0
        private WinShapes.Path[] RenderHpglToPath(HPGL hpgl, float factor, float height)
        {
            WinShapes.Path[] pathCollection = new WinShapes.Path[hpgl.Commands.Capacity + 1];
            pathCollection[0] = DrawRect(sizeX, sizeY, factor, height);
            Point lastpoint = new Point(0, height);

            for (int i = 1; i < hpgl.Commands.Count; i++)
            {
                WinShapes.Path path     = new WinShapes.Path();
                StreamGeometry geometry = new StreamGeometry();
                HPGL.Command   cmd      = hpgl.Commands[i];

                if (cmd.Instruction == HPGL.Instruction.Undefined)
                {
                    continue;
                }
                else if (cmd.Instruction == HPGL.Instruction.PD)
                {
                    path.Stroke          = Brushes.Crimson;
                    path.StrokeThickness = 1.5;
                    path.Opacity         = 0.8;
                }
                else if (cmd.Instruction == HPGL.Instruction.PU)
                {
                    path.Stroke          = Brushes.Blue;
                    path.StrokeThickness = 1.5;
                    path.Opacity         = 0.4;
                }

                using (StreamGeometryContext context = geometry.Open())
                {
                    context.BeginFigure(lastpoint, false, false);

                    for (int j = 0; j < cmd.Coordinates.Count; j++)
                    {
                        Point nextPoint = new Point(cmd.Coordinates[j].X * factor, height - (cmd.Coordinates[j].Y * factor));
                        context.LineTo(nextPoint, true, false);
                    }
                    lastpoint = new Point(cmd.Coordinates[cmd.Coordinates.Count - 1].X * factor, height - (cmd.Coordinates[cmd.Coordinates.Count - 1].Y * factor));
                }
                geometry.Freeze();
                path.Data         = geometry;
                pathCollection[i] = path;
            }

            return(pathCollection);
        }
Ejemplo n.º 4
0
        public static HPGL Parse(string hpglString)
        {
            string[] commandSplits = hpglString.Split(';');
            HPGL     hpgl          = new HPGL();

            foreach (string commandSplit in commandSplits)
            {
                Command command = new Command();

                if (commandSplit.StartsWith("PD"))
                {
                    command.SetInstruction(Instruction.PD);
                }
                else if (commandSplit.StartsWith("PU"))
                {
                    command.SetInstruction(Instruction.PU);
                }
                else
                {
                    command.SetInstruction(Instruction.Undefined);
                }

                string[] coordinateSplits = commandSplit.Substring(2).Split(',');

                if (command.Instruction != Instruction.Undefined)
                {
                    for (int i = 0; i < coordinateSplits.Length; i += 2)
                    {
                        command.AddCoordinate(new Point(double.Parse(coordinateSplits[i]), double.Parse(coordinateSplits[i + 1])));
                    }
                }

                hpgl.AddCommand(command);
            }

            return(hpgl);
        }