private void UpdateMeshInfoBox(int itemIndex) { if (itemIndex < 0) { // nothing selected ResetMeshInfoLabels(); } else { MeshInfoBox.Text = String.Format("Mesh Info for {0}", _meshInfoList[itemIndex].MeshName); MeshInfo meshInfo = _meshInfoList[itemIndex]; lblQuadFaceCount.Text = String.Format("{0} quadrilateral faces", meshInfo.QuadFaceCount); lblTriangularFaceCount.Text = String.Format("{0} triangular faces", meshInfo.TriangularFaceCount); lblVertexCount.Text = String.Format("{0} vertices", meshInfo.VertexCount); lblNormalCount.Text = String.Format("{0} normals", meshInfo.NormalCount); lblTextureCoordCount.Text = String.Format("{0} uv coordinates", meshInfo.UVCoordCount); } }
// This event handler is where the (potentially) time-consuming work is done. static void LoadFile(object sender, DoWorkEventArgs e) { int currentMesh = 1; _bw.ReportProgress(currentMesh); string meshName = ""; string[] fileLines = null; int index = 0; // require at least "g mesh[\n]v x y z[\n]v a b c[\n]v u v w[\n]f 0 1 2" const int MinFileLines = 5; // 3-state FSM: 1) reading file; 2) scanning for line with "g [meshname]"; 3) loading mesh data int state = 0; while (state < 3) { _bw.ReportProgress(currentMesh); // the reason for using the state machine loop approach is // to provide a convenient place to put this check for cancellation if (_bw.CancellationPending) { e.Cancel = true; break; } else { switch (state) { case 0: // read file data fileLines = File.ReadAllLines(_objFilePath); if (fileLines.Length < MinFileLines) { // notify user about error condition MessageBox.Show("Error: file format not recognized"); Cancel(e); return; } ++state; break; case 1: // scan for 1st occurrence of "g" while (index < fileLines.Length) { string[] parts = fileLines[index++].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length > 0) { if (parts[0] == "g") { if (parts.Length > 1) { meshName = parts[1]; } else { meshName = "[unnamed]"; } _meshInfoList = new List <MeshInfo>(); ++state; break; } } } if (index == fileLines.Length) { // notify user of error condition if no "g" found in the file MessageBox.Show("Error: no named mesh (group) found in file"); Cancel(e); return; } break; case 2: // load mesh data MeshInfo meshInfo = new MeshInfo { MeshName = meshName }; // NOTE side effect of processing the current mesh: // if another mesh ("g") encountered, returns its name // else returns a special "flag" name meshName = meshInfo.LoadFileLines(fileLines, ref index); _meshInfoList.Add(meshInfo); if (MeshInfo.MeshEndFlag == meshName) { // all done loading ++state; } else { // there is another mesh to load, we'll get it on the // next pass through the while loop (remaining in this // same FSM state 2) ++currentMesh; } break; default: // "should never happen" // for a professional utility I would log an error message here break; } } } }