void addPoint(List<float> buffer, Vector3 pt)
{
buffer.Add((float)pt.X);
buffer.Add((float)pt.Y);
buffer.Add((float)pt.Z);
}
void ComputeMinMax(Vector3 a, Vector3 b, Vector3 c, Vector3 minPt, Vector3 maxPt)
{
minPt.X = Math.Min(Math.Min(a.X, b.X), c.X);
minPt.Y = Math.Min(Math.Min(a.Y, b.Y), c.Y);
minPt.Z = Math.Min(Math.Min(a.Z, b.Z), c.Z);
maxPt.X = Math.Max(Math.Max(a.X, b.X), c.X);
maxPt.Y = Math.Max(Math.Max(a.Y, b.Y), c.Y);
maxPt.Z = Math.Max(Math.Max(a.Z, b.Z), c.Z);
}
private void faceToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "TEXT (*.txt)|*.txt|All Files(*.*)|*.*";
if (DialogResult.OK != dlg.ShowDialog())
return;
String vertexFileName = dlg.FileName;
if (DialogResult.OK != dlg.ShowDialog())
return;
String faceFileName = dlg.FileName;
List<Vector3> points = new List<Vector3>();
StreamReader sr = new StreamReader(vertexFileName, Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
String[] items = line.Split('\t');
if (items.Length == 3)
{
Vector3 pt = new Vector3();
pt.X = float.Parse(items[0]);
pt.Y = float.Parse(items[1]);
pt.Z = float.Parse(items[2]);
points.Add(pt);
}
}
List<uint> faces = new List<uint>();
faces.Add(0);
faces.Add(1);
faces.Add(2);
Random num = new Random();
List<FaceStyle> faceStyles = new List<FaceStyle>();
FaceStyle fs1 = new FaceStyle();
fs1.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs1);
FaceStyle fs2 = new FaceStyle();
fs2.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs2);
FaceStyle fs3 = new FaceStyle();
fs3.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs3);
FaceStyle fs4 = new FaceStyle();
fs4.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs4);
FaceStyle fs5 = new FaceStyle();
fs5.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs5);
FaceStyle fs6 = new FaceStyle();
fs6.SetColor(num.Next(0, 255), num.Next(0, 255), num.Next(0, 255));
faceStyles.Add(fs6);
ElementId id = new ElementId();
sr = new StreamReader(faceFileName, Encoding.Default);
while ((line = sr.ReadLine()) != null)
{
String[] items = line.Split('\t');
if (items.Length == 3)
{
int a = int.Parse(items[0]) - 1;
int b = int.Parse(items[1]) - 1;
int c = int.Parse(items[2]) - 1;
List<float> positions = new List<float>();
addPoint(positions, points[a]);
addPoint(positions, points[b]);
addPoint(positions, points[c]);
Vector3 normal = (points[b] - points[a]).CrossProduct(points[c] - points[a]);
normal.Normalize();
List<float> normals = new List<float>();
addPoint(normals, normal);
addPoint(normals, normal);
addPoint(normals, normal);
AABox bbox = new AABox();
ComputeMinMax(points[a], points[b], points[c], bbox.MinPt, bbox.MaxPt);
var entity = GlobalInstance.TopoShapeConvert.CreateFaceEntity(positions.ToArray(), faces.ToArray(), normals.ToArray(), null, bbox);
EntitySceneNode node = new EntitySceneNode();
node.SetEntity(entity);
node.SetId(++id);
node.SetFaceStyle(faceStyles[num.Next(0, 5)]);
renderView.ShowSceneNode(node);
}
}
}