Grasshopper geometry to STL as String


I am looking for a way to generate a string with the content of a STL file of some grasshopper geometry.
Currently I am baking the geometry, then use the rhino _Export command followed by reading the file into grasshopper again.

Is there a way to generate the STL string without creating an actual file?

I am using Rhino 6 and 7.

Thank you!

What is a STL string?

Pancake can export mesh as ASCII STL.

1 Like

With “STL string” I mean that I don’t want to save the STL to a file but to a string.
My current approach is saving to a file and then loading it into the desired string.
I want to send the send the STL over Rhino.Compute so I don’t need the actual file.

I thought that there might be something in RhinoCommon that allows me to avoid the creation of the file.

I have created a simple stl export for another app. Actually it is simple. All you do is accessing all elements of your mesh(es) and then you write it down in a very specific way. You can reverse it by exporting a simple mesh with Rhino in Ascii format and open it up in an text editor. If you want to exchange data from one app to another, I would just simply use “Shared Memory” (or any other interprocess communication method) and simple create an own mapping for a mesh. You can serialize objects directly in binary format and deserialize it on the other end. If both apps know the Rhino Mesh object, then its even simpler, because you can directly serialize it, just telling your deserializer what type of object this byte array is. Best is to read more about binary serialisation.

1 Like

Here is my C# code for writing binary STL files.
Unfortunately it doesn’t work right now.
I am not really experienced with C#, so can someone please point out the problem with this component?

STL specification:

   //Adapted from
    var m = mesh.DuplicateMesh();
    string stlString;
    using (var stream = new System.IO.MemoryStream())
    {var writer = new System.IO.BinaryWriter(stream);
      var header = new byte[80];
      writer.Write((uint) mesh.Faces.Count);

      for (var i = 0; i < m.Faces.Count; i++)
        var f = m.Faces[i];

        var n = m.FaceNormals[i];

        var a = m.Vertices[f.A];
        var b = m.Vertices[f.B];
        var c = m.Vertices[f.C];

        writer.Write((float) n.X);
        writer.Write((float) n.Y);
        writer.Write((float) n.Z);

        writer.Write((float) a.X);
        writer.Write((float) a.Y);
        writer.Write((float) a.Z);

        writer.Write((float) b.X);
        writer.Write((float) b.Y);
        writer.Write((float) b.Z);

        writer.Write((float) c.X);
        writer.Write((float) c.Y);
        writer.Write((float) c.Z);

        writer.Write((ushort) 0);

      stream.Seek(0, System.IO.SeekOrigin.Begin);

      var sr = new System.IO.StreamReader(stream);

      stlString = sr.ReadToEnd();

      //var folder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
      //var path = System.IO.Path.Combine(folder, "SampleCsWriteStl.stl");
      //System.IO.File.WriteAllBytes(path, stream.ToArray());

    A = stlString;```