利用记事本打开文件,可以看到STL格式是用三角网组成一个平面与平面的法线构成。
具体格式如下:
facet normal -1.000000e+000 0.000000e+000 0.000000e+000
outer loop
vertex 0.000000e+000 -2.604723e+000 1.477212e+001
vertex 0.000000e+000 -3.673941e-015 1.500000e+001
vertex 0.000000e+000 2.604723e+000 1.477212e+001
endloop
endfacet
一共7句语句的循环而已。利用C#的代码如下:
*********************************
Object0= Create_face_object(1);
F_0= Create_Polyhedral_Face (3,1,0,/* points, triangles and contours */
-10000.000000,11000.000000,-1000.000000,1000.000000, /* limits in u and v */
1,FALSE,FALSE,0); /* direction, closed in u and v, wire_frame mode */
Add_Point_in_Face(F_0,-0.0002429949,0.0007674078,0.001637324, /* point */
0.00000,0.00000, /* u,v */
-0.0220894,-0.9984601,-0.05088779 /* normal */);
Add_Point_in_Face(F_0,-0.0003329492,0.0007692427,0.001645924, /* point */
0.00000,0.00000, /* u,v */
-0.0220894,-0.9984601,-0.05088779 /* normal */);
Add_Point_in_Face(F_0,-0.0003337466,0.0007701771,0.001627938, /* point */
0.00000,0.00000, /* u,v */
-0.0220894,-0.9984601,-0.05088779 /* normal */);
Add_Triangle_In_Face(F_0,0,1,2/* point indices */);
Face_Dvisions_in_u(F_0,0);
Face_Dvisions_in_v(F_0,0);
Add_face_in_object(Object0,0,F_0);
*********************************************************************
public double[, ,] stldata = null;
public int FaceCount = 0;
public string Srs = null;
public int PointCount = 1;
public bool GetSTLData(string FileName)
{
FileStream fs = null;
StreamReader sr = null;
try
{
if (FileName == null || !File.Exists(FileName))
{
return false;
}
//一个面为三个点,
//前三个为 X,Y,Z, 法线的x,y,z U,V
double[, ,] Buff = new double[1000000, 3, 9];
FaceCount = 0;
PointCount = 0;
string StrLine = null;
string[] SplitChar;
fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs); //使用StreamReader类来读取文件
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while ((StrLine = sr.ReadLine()) != null)
{
StrLine = StrLine.Trim();
if (StrLine.IndexOf("facet normal ") >= 0)
{
SplitChar = StrLine.Split(' ');
if (SplitChar.Length != 5)
{
return false;
}
for (int t = 0; t < 3; t++)
{
Buff[FaceCount, t, 3] = double.Parse(SplitChar[2]);
Buff[FaceCount, t, 4] = double.Parse(SplitChar[3]);
Buff[FaceCount, t, 5] = double.Parse(SplitChar[4]);
}
FaceCount++;
}
if (StrLine.IndexOf("vertex ") >= 0)
{
SplitChar = StrLine.Split(' ');
if (SplitChar.Length != 4)
{
return false;
}
if (PointCount >= 3)
{
PointCount = 0;
}
Buff[FaceCount - 1, PointCount, 0] = double.Parse(SplitChar[1]);
Buff[FaceCount - 1, PointCount, 1] = double.Parse(SplitChar[2]);
Buff[FaceCount - 1, PointCount, 2] = double.Parse(SplitChar[3]);
PointCount++;
}
}
stldata = new double[FaceCount, 3, 9];
for (int i = 0; i < FaceCount; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 9; k++)
{
stldata[i, j, k] = Buff[i, j, k];
}
}
}
return true;
}
catch
{
return false;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (sr != null)
{
sr.Close();
}
}
}
public bool ActFile()
{
return ActFile(@"C:\Program Files\Staubli\" + Srs + @"\Robots\ground.act");
}
private string ToStr(double data)
{
if (Math.Abs(data) < 0.00001)
{
return "0.00000";
}
if (data == (double)((int)data))
{
return data.ToString() + ".00000";
}
return data.ToString();
}
public bool ActFile(string FileName)
{
FileStream fs = null;
try
{
if (FileName == null)
{
return false;
}
fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
sw.Write(@"O0 = Create_Parallelepiped(""para14"",0.200000,0.200000,0.005000);
Graphic(O0,A_TYPE_PO,MODE,SHADING,COLOR,WHITE,TRANSPARENCY,SEMI_OPAQUE,MATERIAL,NORMAL);
Buff = Create_Parallelepiped(""para"",0.200000,0.200000,0.005000);
Graphic(Buff,A_TYPE_PO,MODE,SHADING,COLOR,WHITE,TRANSPARENCY,SEMI_OPAQUE,MATERIAL,NORMAL);
Buff = Assemble(""obj___"",Buff,O0,
Create_Transf( 1.000000,0.000000,0.000000,0.410000,
0.000000,1.000000,0.000000,0.000000,
0.000000,0.000000,1.000000,0.000000));");
sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
int FaceIndex = 0;
string ObjectName = "Object0";
string FaceName = "F_0";
for (int k = 0; k < FaceCount; k++)
{
sw.WriteLine(ObjectName + "= Create_face_object(1);");
sw.WriteLine();
sw.WriteLine(FaceName + "= Create_Polyhedral_Face (3,1,0,/* points, triangles and contours */");
sw.WriteLine(" -10000.000000,11000.000000,-1000.000000,1000.000000, /* limits in u and v */");
sw.WriteLine(" 1,FALSE,FALSE,0); /* direction, closed in u and v, wire_frame mode */");
sw.WriteLine();
sw.WriteLine("Add_Point_in_Face(" + FaceName + "," + ToStr(stldata[k, 0, 0] / 1000) + "," + ToStr(stldata[k, 0, 1] / 1000) + "," + ToStr(stldata[k, 0, 2] / 1000) + ", /* point */");
sw.WriteLine(" " + ToStr(stldata[k, 0, 6]) + "," + ToStr(stldata[k, 0, 7]) + "," + " /* u,v */");
sw.WriteLine(" " + ToStr(stldata[k, 0, 3]) + "," + ToStr(stldata[k, 0, 4]) + "," + ToStr(stldata[k, 0, 5]) + " /* normal */);");
sw.WriteLine("Add_Point_in_Face(" + FaceName + "," + ToStr(stldata[k,1, 0]/1000) + "," + ToStr(stldata[k, 1, 1]/1000) + "," + ToStr(stldata[k, 1, 2]/1000) + ", /* point */");
sw.WriteLine(" " + ToStr(stldata[k, 1, 6]) + "," + ToStr(stldata[k, 1, 7]) + "," + " /* u,v */");
sw.WriteLine(" " + ToStr(stldata[k, 1, 3]) + "," + ToStr(stldata[k, 1, 4]) + "," + ToStr(stldata[k, 1, 5]) + " /* normal */);");
sw.WriteLine("Add_Point_in_Face(" + FaceName + "," + ToStr(stldata[k, 2, 0]/1000) + "," + ToStr(stldata[k, 2, 1]/1000) + "," + ToStr(stldata[k, 2, 2]/1000) + ", /* point */");
sw.WriteLine(" " + ToStr(stldata[k, 2, 6]) + "," + ToStr(stldata[k, 2, 7]) + "," + " /* u,v */");
sw.WriteLine(" " + ToStr(stldata[k, 2, 3]) + "," + ToStr(stldata[k, 2, 4]) + "," + ToStr(stldata[k, 0, 5]) + " /* normal */);");
sw.WriteLine();
sw.WriteLine("Add_Triangle_In_Face(F_0,0,1,2/* point indices */);");
sw.WriteLine();
sw.WriteLine("Face_Dvisions_in_u(" + FaceName + ",0);");
sw.WriteLine("Face_Dvisions_in_v(" + FaceName + ",0);");
sw.WriteLine("Add_face_in_object(" + ObjectName + ",0," + FaceName + ");");
sw.WriteLine();
sw.WriteLine(@"End_face_object(" + ObjectName + @",""SR6_stand_6_"");");
sw.WriteLine("Graphic(" + ObjectName + ",A_TYPE_PO,MODE,SHADING,COLOR,17,TRANSPARENCY,OPAQUE,MATERIAL,NORMAL,BACK_FACE);");
sw.WriteLine();
sw.Write(@"
Buff= Assemble(""obj0"",Buff," + ObjectName + @",
Create_Transf( 1.000000,0.000000,0.000000,0.00000,
0.000000,1.000000,0.000000,-0.03150000,
0.000000,0.000000,1.000000,0.000000));
"
);
sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
}
sw.Write(@"
Buff= Make_Obstacle(""ground"",Buff);
Base_Default_Dds(Buff,0.050000);
Place_In_Cell(Buff,
Create_Transf( 1.000000,0.000000,0.000000,0.00000,
0.000000,1.000000,0.000000,-0.000000,
0.000000,0.000000,1.000000,-0.000000));"")
"
);
sw.Close();
return true;
}
}
catch
{
return false;
}
finally
{
if (fs != null)
{
fs.Close();
}
}
}