public void ParsesNCodeWithGCode()
        {
            var parser = new GenericGCodeParser();
            var line   = parser.ParseLine("N100 G1", 0);

            Assert.AreEqual(LineType.GCode, line.Type);
        }
Esempio n. 2
0
        protected virtual GCodeFile LoadGCodeFileFromDisk(string gcodeFilePath)
        {
            var parser = new GenericGCodeParser();

            using var fileReader = File.OpenText(gcodeFilePath);
            return(parser.Parse(fileReader));
        }
        public void ParsesUnknownString(string s)
        {
            var parser = new GenericGCodeParser();
            var line   = parser.ParseLine(s, 0);

            Assert.AreEqual(LineType.UnknownString, line.Type);
        }
        public void ParsesNCodeOnly()
        {
            var parser = new GenericGCodeParser();
            var line   = parser.ParseLine("N100", 0);

            Assert.AreEqual(LineType.Blank, line.Type);
        }
Esempio n. 5
0
        static void LoadGeneratedGCodeFile(string sPath)
        {
            // read gcode file
            GenericGCodeParser parser = new GenericGCodeParser();
            GCodeFile          gcode;

            using (FileStream fs = new FileStream(sPath, FileMode.Open, FileAccess.Read)) {
                using (TextReader reader = new StreamReader(fs)) {
                    gcode = parser.Parse(reader);
                }
            }

            // write back out gcode we loaded
            //StandardGCodeWriter writer = new StandardGCodeWriter();
            //using ( StreamWriter w = new StreamWriter("../../../sample_output/writeback.gcode") ) {
            //	writer.WriteFile(gcode, w);
            //}

            GCodeToToolpaths    converter   = new GCodeToToolpaths();
            MakerbotInterpreter interpreter = new MakerbotInterpreter();

            interpreter.AddListener(converter);

            InterpretArgs interpArgs = new InterpretArgs();

            interpreter.Interpret(gcode, interpArgs);

            View.SetPaths(converter.PathSet);
            if (LastSettings != null)
            {
                View.PathDiameterMM = (float)LastSettings.Machine.NozzleDiamMM;
            }
        }
Esempio n. 6
0
        static DMesh3 GenerateTubeMeshesForGCode(string sPath, double pathWidth = 0.4)
        {
            GenericGCodeParser parser = new GenericGCodeParser();
            GCodeFile          gcode;

            using (FileStream fs = new FileStream(sPath, FileMode.Open, FileAccess.Read)) {
                using (TextReader reader = new StreamReader(fs)) {
                    gcode = parser.Parse(reader);
                }
            }
            GCodeToLayerTubeMeshes make_tubes = new GCodeToLayerTubeMeshes()
            {
                TubeProfile = Polygon2d.MakeCircle(pathWidth / 2, 12),
                InterpretZChangeAsLayerChange = false
            };

            make_tubes.WantTubeTypes.Add(ToolpathTypes.Travel);
            ThreeAxisCNCInterpreter interpreter = new ThreeAxisCNCInterpreter();

            interpreter.AddListener(make_tubes);
            interpreter.Interpret(gcode, new InterpretArgs());
            DMesh3 tubeMesh2 = make_tubes.GetCombinedMesh(1);

            return(tubeMesh2);
        }
Esempio n. 7
0
    public GCodeToolpath(string file, CartesianTarget referenceTarget, Vector3d alignment)
    {
        using var reader = File.OpenText(file);

        var parser = new GenericGCodeParser();
        var code   = parser.Parse(reader);

        Toolpath = new FiveAxisToRobots(referenceTarget, alignment, code);
        _targets = Toolpath.Targets;
    }
Esempio n. 8
0
        static DMesh3 GenerateTubeMeshesForGCode(string sPath)
        {
            GenericGCodeParser parser = new GenericGCodeParser();
            GCodeFile          gcode;

            using (FileStream fs = new FileStream(sPath, FileMode.Open, FileAccess.Read)) {
                using (TextReader reader = new StreamReader(fs)) {
                    gcode = parser.Parse(reader);
                }
            }
            GCodeToLayerTubeMeshes make_tubes = new GCodeToLayerTubeMeshes()
            {
                TubeProfile = Polygon2d.MakeCircle(0.2f, 12)
            };
            MakerbotInterpreter interpreter = new MakerbotInterpreter();

            interpreter.AddListener(make_tubes);
            interpreter.Interpret(gcode, new InterpretArgs());
            DMesh3 tubeMesh2 = make_tubes.GetCombinedMesh(1);

            return(tubeMesh2);
        }
Esempio n. 9
0
        static void LoadGCodeFile(string sPath)
        {
            GenericGCodeParser parser = new GenericGCodeParser();
            GCodeFile          gcode;

            using (FileStream fs = new FileStream(sPath, FileMode.Open, FileAccess.Read)) {
                using (TextReader reader = new StreamReader(fs)) {
                    gcode = parser.Parse(reader);
                }
            }

            GCodeToToolpaths    converter   = new GCodeToToolpaths();
            MakerbotInterpreter interpreter = new MakerbotInterpreter();

            interpreter.AddListener(converter);
            InterpretArgs interpArgs = new InterpretArgs();

            interpreter.Interpret(gcode, interpArgs);

            ToolpathSet Paths = converter.PathSet;

            View.SetPaths(Paths);
        }
Esempio n. 10
0
        static void PrintFile(PrinterControl simCtl)
        {
            //Console.Clear();
            Console.WriteLine("\nDefault file will be used unless alternate file is given");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
            OpenFileDialog filePath = new OpenFileDialog();
            string         path     = "";

            if (filePath.ShowDialog() == DialogResult.OK)
            {
                path = filePath.FileName;
            }
            if (path == "")
            {
                path = "..\\..\\..\\SampleSTLs\\F-35_Corrected.gcode";
            }
            StreamReader file = new System.IO.StreamReader(path);

            Stopwatch swTimer = new Stopwatch();

            swTimer.Start();

            // Parse the GCode file
            var parser       = new GenericGCodeParser();
            var instructions = parser.Parse(file);

            /*
             * Commands to note:
             * G28: Home all axes
             * G28 X0: Home x axis
             *
             *
             */

            double currentX     = 0;
            double currentY     = 0;
            double currentZ     = 0;
            bool   isLaserOn    = false;
            bool   oldIsLaserOn = false;

            int instructionCount = 0;

            foreach (var line in instructions.AllLines())
            {
                if (line.parameters != null)
                {
                    //Console.WriteLine(line.code);
                    if (line.code == 1)
                    {
                        bool containsX = false;
                        bool containsY = false;
                        bool containsZ = false;
                        bool containsE = false;
                        foreach (var parameter in line.parameters)
                        {
                            if (parameter.identifier.ToUpper() == "X")
                            {
                                currentX  = parameter.doubleValue;
                                containsX = true;
                            }

                            if (parameter.identifier.ToUpper() == "Y")
                            {
                                currentY  = parameter.doubleValue;
                                containsY = true;
                            }

                            if (parameter.identifier.ToUpper() == "Z")
                            {
                                currentZ  = parameter.doubleValue;
                                containsZ = true;
                            }

                            if (parameter.identifier.ToUpper() == "E")
                            {
                                isLaserOn = parameter.doubleValue > 0;
                                containsE = true;
                            }
                        }

                        // Command number: 0x00
                        // Format: X (4 bytes), Y (4 bytes)
                        if (containsX && containsY)
                        {
                            instructionCount++;
                            //continue;
                            var bytesToSend = new byte[12];
                            bytesToSend[0] = 0x00; // Command number
                            bytesToSend[1] = 0x08; // Data length
                            bytesToSend[2] = 0x00; // Blank (for checksum)
                            bytesToSend[3] = 0x00; // Blank (for checksum)

                            // Convert x position and y position to a byte array
                            var xBytes = BitConverter.GetBytes((float)currentX);
                            var yBytes = BitConverter.GetBytes((float)currentY);

                            // Insert x position
                            for (int i = 0; i < xBytes.Length; i++)
                            {
                                bytesToSend[i + 4] = xBytes[i];
                            }

                            // Insert y position
                            for (int i = 0; i < yBytes.Length; i++)
                            {
                                bytesToSend[i + 8] = yBytes[i];
                            }

                            // Send data
                            if (!HostToFirmware(bytesToSend, simCtl))   // NOTE: the first byte here is 8!!!!!!!!!!!!!=============
                            {
                                //Console.Write("retry");
                                Console.WriteLine("Print failed - command failed to send.");
                                return;
                            }
                        }

                        // Command number: 0x01
                        // Format: Z (4 bytes)
                        else if (containsZ)
                        {
                            instructionCount++;
                            //continue;
                            var bytesToSend = new byte[8];
                            bytesToSend[0] = 0x01; // Command number
                            bytesToSend[1] = 0x04; // Data length   // changed from 04
                            bytesToSend[2] = 0x00; // Blank (for checksum)
                            bytesToSend[3] = 0x00; // Blank (for checksum)

                            // Convert z position to a byte array
                            var zBytes = BitConverter.GetBytes((float)currentZ);

                            // Insert z position
                            for (int i = 0; i < zBytes.Length; i++)
                            {
                                bytesToSend[i + 4] = zBytes[i];
                            }

                            // Send data
                            if (!HostToFirmware(bytesToSend, simCtl))
                            {
                                Console.WriteLine("Print failed - command failed to send.");
                                return;
                            }
                        }
                        else if (containsX || containsY)
                        {
                            throw new InvalidDataException("Invalid GCode command - command contains one X/Y command without the other.");
                        }

                        // Separately, if it contains an E
                        // Command number: 0x02
                        // Format: isLaserOn (1 byte)
                        if (containsE && (isLaserOn != oldIsLaserOn))
                        {
                            oldIsLaserOn = isLaserOn;
                            instructionCount++;
                            //continue;
                            var bytesToSend = new byte[8];
                            bytesToSend[0] = 0x02; // Command number
                            bytesToSend[1] = 0x01; // Data length
                            bytesToSend[2] = 0x00; // Blank (for checksum)
                            bytesToSend[3] = 0x00; // Blank (for checksum)

                            // Convert z position to a byte array
                            var isLaserOnBytes = BitConverter.GetBytes(isLaserOn);

                            // Insert isLaserOn
                            for (int i = 0; i < isLaserOnBytes.Length; i++)
                            {
                                bytesToSend[i + 4] = isLaserOnBytes[i];
                            }

                            // Send data
                            if (!HostToFirmware(bytesToSend, simCtl))
                            {
                                Console.WriteLine("Print failed - command failed to send.");
                                return;
                            }
                        }
                    }
                }
            }

            Console.WriteLine("Host: There were " + instructionCount.ToString() + " instructions sent by the host.");

            swTimer.Stop();
            long elapsedMS = swTimer.ElapsedMilliseconds;

            Console.WriteLine("Total Print Time: {0}", elapsedMS / 1000.0);
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }