squancy / stl-parser

Convert between ASCII and binary STL files, calculate volume, surface area and estimated price for 3D printing.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

STL parser

A command line utility for converting between ASCII and binary STL files, getting the volume, number of vertices & surface area and calculating a rough price estimate of a 3D printed model.

Algorithm for price calculation

Due to the structure of the STL file, it is not possible to calculate the exact price of a 3D printed model since it only contains a mesh made up of triangles and a normal vector for each of those triangles. Popular slicers like Cura or Slic3r make a good job of calculating the price of a 3D model by generating the associated gcode, acquiring information such as the feed path or print time.

However, calculating the price merely from an STL file is a lot simpler and faster than slicing (and less accurate at the same time).

Pseudocode for price calculation:

function calculate_price:
  area = surface area of mesh // in cm^2
  volume = volume of mesh // in cm^3
  outer_shell_volume = area * wall_width 
  inner_infill_volume = volume - outer_shell_volume
  weight = outer_shell_volume * filament_density + inner_infill_volume * filament_density * infill // in gramm
  return weight * filament_price_per_gramm

The above function calculates the weight of the outer shell (the outermost part of the model that is printed with 100% infill and has a width of wall_width, given as input) and the weight of the inner part that has a certain percentage of infill, also given as input.

The volume is calculated as the sum of the signed volumes of the tetrahedrons from a given point in space, thus it returns a correct value for STL files with arbitrary complexity. There may be certain edge cases when a few triangles overlap or the STL does determine a closed form in space. In this case an incorrect result may be returned since there is no error checking on triangles.

Also, note that the volume calculation is dependent on the order of the vertices listed in the file (right-hand rule) and no error checking is done when it's not the case. When an STL is given as a parameter such that the order of the vertices does not follow the specification an incorrect volume (and perhaps price) may be returned without any warning.

Usage

Usage: stlp <filename> [-c=asc|bin | -i | -p <infill> <fprice> <wall-width> <material>]
  -c=asc|bin: convert to ASCII or binary STL, output file is 'output.stl' created in the current directory
  -i: info about STL file (closed volume, number of vertices, surface area, number of triangles)
  -h: prints usage
  -p <infill> <fprice> <wall-width> <material>: calculates the price of an FDM 3D-printed model
    <infill>: amount of infill used by the printer in percentage (default is 20)
    <fprice>: price of 1 gramm of filament used to print the model in dollars (default is 0.08)
    <wall-width>: width of the outer, solid wall in millimeters (default is 1.2)
    <material>: type of materail used for printing, exactly one of:
      ABS, PLA, CFRP, Plexiglass, Alumide, Aluminum, Brass, Bronze, Copper, Gold_14K, Gold_18K,
      Polyamide_MJF, Polyamide_SLS, Rubber, Silver, Steel, Titanium, Resin
  If only <filename> is given as argument then it's the same as executing stlp <filename> -i

Note: surface area is returned in cm2 and volume is returned in cm3.

Compiling

git clone https://github.com/squancy/stl-parser
cd stl-parser/src
make

It should create an executable called stlp in the directory.

Note: as pointed out in this issue you may run into an error when compiling in WSL since gcc will not include the math libraries. In this case add the -lm flag and it will resolve the issue.

Compiling unit tests

In case you want to check if the unit tests are passing on your machine run:

cd test
make

It should create four executables:

  • mainUnitTest -> test/mainUnitTest.c
  • stlInfoTest -> test/stlInfoTest.c
  • unitTests -> test/unitTests.c
  • validatorTests -> test/stlValidatorTests.c

About

Convert between ASCII and binary STL files, calculate volume, surface area and estimated price for 3D printing.


Languages

Language:C 96.8%Language:Makefile 3.2%