1

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,

  • What's in that `parameters` array on each line? – krillgar Sep 16 '19 at 16:35
  • Hi @krillgar, the `parameters` contains the Values of the `orig_string` parameter. For instance the line in the image above 'G1 X108.925 Y97.547 E0.47890' the paramters are "X = 108925", "Y = 97547", "E = 0.47890" – Andreas Reitberger Sep 16 '19 at 16:38
  • The volume of plastic used? or the volume of the object being made? The former (volume of plastic) is probably more complicated then you imagine. The best case is every block is a G1. In that case can compute the length of each segment and with the flowrate ( E register I think) determine a volume for that move, and then add up all the moves. But maybe you have G2 and G3 circular motion records as well which you will have to deal with. Also different machines may have different gcode "flavors" even for the same part. As for the latter (volume of the object), it is even more complicated. – C R Johnson Sep 16 '19 at 17:14
  • @CRJohnson Unless it's a resin printer, in which case you would need to find the area of all points that get printed and multiply it by the height of the layer. Then add all the layers. – krillgar Sep 16 '19 at 18:09
  • Hi, I basically need the volume of the print object, not the extruded filament. – Andreas Reitberger Sep 16 '19 at 18:11
  • I guess I will try to find a method to get the "; filament used [cm3] = 5.3" from the comments for each slicer I know. I guess this is the easiest way ;) Thanks! – Andreas Reitberger Sep 17 '19 at 16:05

1 Answers1

1

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?

symaps
  • 62
  • 6
  • Hi! Thanks for your comment. Basically you are right. Using the stl instead of the gcode is the easier way, however I'm coding a tool to calculate print costs. Most users already have their gcodes and it's the best to use this. I already fixed it by calculating the volume of the extruded filament instead. – Andreas Reitberger Apr 30 '20 at 12:57