LoginSignup
0
1

More than 3 years have passed since last update.

C#でSTLのバイナリを読み込む

Last updated at Posted at 2020-02-15

某言語のライブラリじゃないやつ。

ソースコード

static Vector3f[][] LoadStlFile(Stream stream)
{
    byte[] buff = new byte[84];

    stream.Read(buff, 0, 84);
    string anyText = Encoding.ASCII.GetString(buff, 0, 80);
    int numOfPolygon = BitConverter.ToInt32(buff, 80);

    Vector3f[][] polygons = new Vector3f[numOfPolygon][];

    for (int i = 0; i < numOfPolygon; i++)
    {
        stream.Read(buff, 0, 50);

        polygons[i] = new Vector3f[3]
        {
            new Vector3f(
                BitConverter.ToSingle(buff, 4 * 3),
                BitConverter.ToSingle(buff, 4 * 4),
                BitConverter.ToSingle(buff, 4 * 5)),
            new Vector3f(
                BitConverter.ToSingle(buff, 4 * 6),
                BitConverter.ToSingle(buff, 4 * 7),
                BitConverter.ToSingle(buff, 4 * 8)),
            new Vector3f(
                BitConverter.ToSingle(buff, 4 * 9),
                BitConverter.ToSingle(buff, 4 * 10),
                BitConverter.ToSingle(buff, 4 * 11)),
        };
    }

    return (polygons);
}

static Vector3f[][] LoadStlFile(string path)
{
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        return (LoadStlFile(fs));
    }
}

任意の文字列法線ベクトルは読み捨ててます。
戻り値は頂点のジャグ配列ですが、環境に合わせて煮るなり焼くなり好きにしろ。
エラーチェックとか一切やってないので気をつけてね。

参考

STLファイルフォーマット

感想

なにゆえ1ポリ50バイト? 48ポリのほうがスッキリする気がする。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1