fromSTL static method

List<Triangle>? fromSTL(
  1. String fileContent
)

Create a list of triangles from a String containing a valid STL file This function takes a string parameter fileContent and returns List of triangles, or null on error

Implementation

static List<Triangle>? fromSTL(String fileContent) {
  List<Triangle> triangles = [];

  List<String> lines = const LineSplitter().convert(fileContent);
  final int lineCount = lines.length;

  int currentLineNumber = 0;
  bool eof = false;

  StlToken token = _tokenType(lines[0]);

  // ASCII STL will start with solid tag
  if (token == StlToken.solid) {
    currentLineNumber++;

    while ((currentLineNumber < lineCount) && (!eof)) {
      token = _tokenType(lines[currentLineNumber]);

      if (token == StlToken.facet) {
        bool success = _readNextTriangle(lines, triangles, currentLineNumber);
        if (success) {
          currentLineNumber += _triangleLines;
        } else {
          eof = true;
        }
      } else if (token == StlToken.endSolid) {
        eof = true;
      } else {
        // Uh oh. In case of unexpected line, abort
        eof = true;
      }
    }
  }

  if (triangles.isEmpty) {
    return null;
  }

  return triangles;
}