public Tuple <float, float> convertFromTo(ObjFigure objFigureData, string[] vertecData, float[] scaleData, float[] translateData) { StlWriter stlObjWriter = new StlWriter(); float area = 0; float volume = 0; List <int> elements = stringProcessing(vertecData); int triangleCount = elements.Count() - 2; for (int i = 0; i < triangleCount; i++) { StlFigure stlFigure = new StlFigure(); StlFigure newStlFigure = new StlFigure(); stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(0) - 1)); stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(i + 1) - 1)); stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(i + 2) - 1)); stlObjWriter.stlFormatWrite(stlFigure); area += stlFigure.calculateFacetArea(); volume += stlFigure.calculateFacetVolume(); } return(Tuple.Create(area, volume)); }
private void button1_Click(object sender, EventArgs e) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Choose File"; openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "obj files (*.obj)|*.obj|All files (*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; ObjFigure objFigure = new ObjFigure(); ObjToStlConverter convertFigure = new ObjToStlConverter(); List <string[]> facets = new List <string[]>(); List <string[]> options = new List <string[]>(); StlWriter stlObjWriter = new StlWriter(); VertexProp verteces; VertexProp vertecesNormals; VertexProp vertecesTextures; //checks scale values if (byXtext.Text != null && byXtext.Text != "") { scaleX = float.Parse(byXtext.Text); } if (byYtext.Text != null && byYtext.Text != "") { scaleY = float.Parse(byYtext.Text); } if (byZtext.Text != null && byZtext.Text != "") { scaleZ = float.Parse(byZtext.Text); } //checks translate values if (trnsByXtext.Text != null && trnsByXtext.Text != "") { translateX = float.Parse(trnsByXtext.Text); } if (trnsByYtext.Text != null && trnsByYtext.Text != "") { translateY = float.Parse(trnsByYtext.Text); } if (trnsByZtext.Text != null && trnsByZtext.Text != "") { translateZ = float.Parse(trnsByZtext.Text); } scaleByXYZ = new float[] { scaleX, scaleY, scaleZ }; trnsByXYZ = new float[] { translateX, translateY, translateZ }; if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath; float x, y, z; string line; filePath = openFileDialog.FileName; StreamReader file = new StreamReader(filePath); while ((line = file.ReadLine()) != null) { string[] propList = line.Split(' '); switch (propList[0]) { case "v": x = float.Parse(propList[1]); y = float.Parse(propList[2]); z = float.Parse(propList[3]); verteces = new VertexProp(x, y, z); scaleVertex(verteces, scaleByXYZ); translateVertex(verteces, trnsByXYZ); objFigure.Verteces.Add(verteces); break; case "vt": if (propList.Count() == 2) { x = float.Parse(propList[1]); vertecesTextures = new VertexProp(x); objFigure.VertecesTextures.Add(vertecesTextures); } else if (propList.Count() == 3) { x = float.Parse(propList[1]); y = float.Parse(propList[2]); vertecesTextures = new VertexProp(x, y); objFigure.VertecesTextures.Add(vertecesTextures); } else { x = float.Parse(propList[1]); y = float.Parse(propList[2]); z = float.Parse(propList[3]); vertecesTextures = new VertexProp(x, y, z); objFigure.VertecesTextures.Add(vertecesTextures); } break; case "vn": x = float.Parse(propList[1]); y = float.Parse(propList[2]); z = float.Parse(propList[3]); vertecesNormals = new VertexProp(x, y, z); objFigure.VertecesNormals.Add(vertecesNormals); break; case "f": facets.Add(propList); convertFigure.facetCalculation(propList); break; } } stlObjWriter.writeHeader(convertFigure.NumberOfFacets); foreach (string[] element in facets) { var tuple = convertFigure.convertFromTo(objFigure, element, scaleByXYZ, trnsByXYZ); totalArea += tuple.Item1; totalVolume += tuple.Item2; } MessageBox.Show("The work is done", "", MessageBoxButtons.OK); areaText.Text = totalArea.ToString(); volumeText.Text = totalVolume.ToString(); convertFrom.Text = ""; convertTo.Text = ""; convertTo.Enabled = false; button1.Enabled = false; } }