Monday, September 8, 2014

C# code snippet to write a Stanford Polygon binary file

The Stanford Polygon file format (PLY) is a standard three-dimensional file format typically used in 3-D software such as Blender, 3D Studio Max and many others. I looked for some C# source code to write 3-D data into a file in binary PLY format but could only find C++ examples. So I came up with my own code snippet which just writes out simple 3-D vertices without additional properties, as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
BinaryWriter writer = new BinaryWriter(new FileStream("myBinaryFile.ply", FileMode.Create), Encoding.ASCII);
//Write the headers for 3 vertices
writer.Write(str2byteArray("format binary_little_endian 1.0\n"));
writer.Write(str2byteArray("element vertex 3\n"));
writer.Write(str2byteArray("property float x\n"));
writer.Write(str2byteArray("property float y\n"));
writer.Write(str2byteArray("property float z\n"));

//Write the 1st vertex

//... write the remaining 2 vertices here

//Close the binary writer
private byte[] float2byteArray(float value)
return BitConverter.GetBytes(value);
private byte[] str2byteArray(string theString)
return System.Text.Encoding.ASCII.GetBytes(theString);
This example is meant only for little endian machines.

1 comment:

pil sung Kim said...


found this code.

thank you.

thank you so mush.

Related Posts with Thumbnails