Search code examples
c#volumeg-code

Calculate volume of a gcode file in C#


I'm trying to get or calcualte the volume of a gcode (3D printing) file in my C# project. To read the gcode I'm using this third-party library: https://github.com/gradientspace/gsGCode

Sadly the author haven't responded yet.

For now, this is how I read the file in:

public gs.GCodeFile Load(string path)
    {
        try
        {
            GenericGCodeParser parse = new GenericGCodeParser();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            return parse.Parse(sr);
        }
        catch(Exception)
        {
            return null;
        }
    }

This gets me a a list of all gcode lines.

enter image description here

<code>code</code>

Now I need to calculate the volume out of this information. This is the gcode file I used for the read: https://andreas-reitberger.de/wp-content/uploads/2015/12/4x_rod_0.1mm_PET_MK3.zip

P.S. I know that the volume is written in the comments of the gcode file, however this is not a must and not reliable. Thank you,


Solution

  • Is there any reason that you are using the gcode for this? If you are wanting the volume of the object and not the volume of plastic extruded then you are on a lost cause doing this from gcode as you would have to make a lot of inferences about the model from the gcode which would be prone to errors. Presumably the gcode was generated from an stl file? If this is the case then you would be better to calculate the volume base on the stl file. see How do I calculate the volume of an object stored in STL files?