public void getallgeometry() { Geometry3Dxml geometryFromInstance3dlist = getFromInstance3dlist(); Geometry3Dxml geometryFromReferenceRepList = getFromReferenceRepList(); this.allgeometry.MeshGeometry3DXmls = geometryFromReferenceRepList.MeshGeometry3DXmls.Concat(geometryFromInstance3dlist.MeshGeometry3DXmls).ToList <MeshGeometry3DXml>(); }
//读取ReferenceRepList(相当于装配体)中所有零部件的面片信息,将他们存放在Geometry3Dxml中的MeshGeometry3DXmls里 public Geometry3Dxml getFromReferenceRepList() { Geometry3Dxml result = new Geometry3Dxml(); foreach (ReferenceRepType eachReferenceRep in referencereplist) { foreach (MeshGeometry3DXml triangleMesh in eachReferenceRep.MeshGeometry.MeshGeometry3DXmls) { if (triangleMesh.Points.Count / 3 >= 3)//判断MeshGeometry3DXml中的点是否有三个,是否可以至少组成一个三角形,一堆三角形就可组成一个复杂形状的面 { result.MeshGeometry3DXmls.Add(triangleMesh); } } } return(result); }
public Geometry3Dxml getFromInstance3dlist() { Geometry3Dxml result = new Geometry3Dxml(); foreach (Instance3DType eachinstance3d in instance3dlist) { foreach (MeshGeometry3DXml triangleMesh in eachinstance3d.MeshGeometry.MeshGeometry3DXmls) { if (triangleMesh.Points.Count / 3 >= 3)//判断MeshGeometry3DXml中的点是否有三个,是否可以至少组成一个三角形,一堆三角形就可组成一个复杂形状的面 { result.MeshGeometry3DXmls.Add(triangleMesh); } } } return(result); }
public Geometry3Dxml Read3Drep(string fileName) { try { Geometry3Dxml geometry3Dxml = new Geometry3Dxml(); XmlReaderSettings xmlReaderSettings = new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true }; XmlReader xmlReader = XmlReader.Create(fileName, xmlReaderSettings); while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.Equals("Rep") && xmlReader.GetAttribute("xsi:type").Equals("PolygonalRepType")) //错误 { MeshGeometry3DXml meshGeometry3DXml = new MeshGeometry3DXml(); xmlReader.ReadStartElement(); while (xmlReader.NodeType != XmlNodeType.EndElement || !xmlReader.Name.Equals("Rep")) { if (xmlReader.Name.Equals("PolygonalLOD")) { } else if (xmlReader.Name.Equals("Faces")) { xmlReader.ReadStartElement(); while (xmlReader.NodeType != XmlNodeType.EndElement || !xmlReader.Name.Equals("Faces")) { if (xmlReader.Name.Equals("Face")) { string faceTrianglesString = xmlReader.GetAttribute("triangles"); string faceStripsString = xmlReader.GetAttribute("strips"); string faceFansString = xmlReader.GetAttribute("fans"); if (faceTrianglesString != null) { string[] array = new Regex("[\\s]+").Replace(faceTrianglesString, " ").Trim().Split(' '); for (int i = 0; i < array.Length; i++) { //if (i % 3 == 0) // faces.Add(3); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i])); } } if (faceStripsString != null) { string[] stripsArray = faceStripsString.Split(','); foreach (string stripString in stripsArray) { string[] array = new Regex("[\\s]+").Replace(stripString, " ").Trim().Split(' '); //faces.Add(3); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[0])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[1])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[2])); for (int i = 3; i < array.Length; i++) { //faces.Add(3); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 2])); if (i % 2 == 0) //保证面的法向正确 { meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 1])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 0])); } else { meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 0])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 1])); } } } if (faceFansString != null) { //处理 trianglsFans string[] fansArray = faceFansString.Split(','); foreach (string fanString in fansArray) { string[] array = new Regex("[\\s]+").Replace(fanString, " ").Trim().Split(' '); //faces.Add(3); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[0])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[1])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[2])); for (int i = 3; i < array.Length; i++) { //faces.Add(3); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[0])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i - 1])); meshGeometry3DXml.TriangleIndices.Add(int.Parse(array[i])); } } } } } xmlReader.Skip(); } } else if (xmlReader.Name.Equals("VertexBuffer")) { xmlReader.ReadStartElement(); while (xmlReader.NodeType != XmlNodeType.EndElement || !xmlReader.Name.Equals("VertexBuffer")) { if (xmlReader.Name.Equals("Positions")) { string vertexString = xmlReader.ReadInnerXml(); string[] array = vertexString.Replace(",", " ").Trim().Split(' '); for (int i = 0; i < array.Length; i++) { meshGeometry3DXml.Points.Add(float.Parse(array[i])); } } xmlReader.Skip(); } } xmlReader.Skip(); } geometry3Dxml.MeshGeometry3DXmls.Add(meshGeometry3DXml); } //xmlReader.Skip(); } xmlReader.Close(); return(geometry3Dxml); } catch (Exception ex) { throw (new Exception("Parase Model3DxmlRep Error!" + ex)); } }