/// <summary> /// Perform somes user initializations. /// </summary> protected override void UserInitializations() { this.Background = System.Windows.Media.Brushes.LightBlue; this.BorderBrush = System.Windows.Media.Brushes.LightBlue; this.Tree = new ModelTree(); this.ContentPanel.Children.Add(this.Tree); }
public bool LoadFromFile(string modelFilePath) { if (!(File.Exists(modelFilePath))) { Debug.LogWarning("模型文件不存在!"); return(false); } FileStream fs = new FileStream(modelFilePath, FileMode.Open, FileAccess.Read); byte[] header = new byte[2]; fs.Read(header, 0, 2); fs.Close(); if ((header[0] == 80) && (header[1] == 75)) { //说明这是一个未解压缩的3dxml文件,需要将其解压缩。 try { try { Directory.Delete("cached\\", true); } catch (IOException) { } Directory.CreateDirectory("cached\\"); ZipFile.ExtractToDirectory(modelFilePath, "cached\\"); } catch (IOException) { } } else { Debug.Log("请不要手动解包3DXML!"); return(false); } string xmlname = ""; DirectoryInfo dir = new DirectoryInfo("cached\\"); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { } else { if (i.FullName.ToUpper().EndsWith(".3DXML")) { xmlname = i.FullName; } } } XElement xd = XElement.Load(xmlname); modelRoot = ConvertToInt(xd.Elements().Last().Attribute("root").Value); IEnumerable <XElement> xels = xd.Elements().Last().Elements(); foreach (XElement xel in xels) { string nametype = xel.Attribute(xsi + "type").Value; if (nametype.Equals("Reference3DType")) { //示例:<Reference3D xsi:type="Reference3DType" id="3" name="gf-3__21-90_pub_skel"/> //Reference3D r3 = new Reference3D(); ModelTree mt = new ModelTree(xel.Attribute("name").Value); TreeDict.Add(ConvertToInt(xel.Attribute("id").Value), mt); } else if (nametype.Equals("ReferenceRepType")) { //示例:<ReferenceRep xsi:type="ReferenceRepType" id="4" name="gf-3__21-90_pub_skel" format="TESSELLATED" version="1.2" associatedFile="urn:3DXML:gf-3__21-90_pub_skel.prt.3DRep"> string filepath = "cached\\" + xel.Attribute("associatedFile").Value.Split(':')[2]; RepDict.Add(ConvertToInt(xel.Attribute("id").Value), new RepPart(filepath, xel.Attribute("name").Value)); } else if (nametype.Equals("InstanceRepType")) { //<InstanceRep xsi:type="InstanceRepType" id="6" name="Part_InstanceRep"> //<IsAggregatedBy>3</IsAggregatedBy> //<IsInstanceOf>4</IsInstanceOf> //</InstanceRep> int isAggregatedBy = ConvertToInt(xel.Elements().First().Value); int isInstanceOf = ConvertToInt(xel.Elements().Last().Value); TreeDict[isAggregatedBy].isLeaf = true; TreeDict[isAggregatedBy].LeafPrt.Add(RepDict[isInstanceOf]); } else if (nametype.Equals("Instance3DType")) { //<Instance3D xsi:type="Instance3DType" id="17" name="GF-3__21-90_CABLE_PUB"> //<IsAggregatedBy>1</IsAggregatedBy> //<IsInstanceOf>2</IsInstanceOf> //<RelativeMatrix>1 0 0 0 1 0 0 0 1 0 0 0</RelativeMatrix> //</Instance3D> int isAggregatedBy = ConvertToInt(xel.Elements().First().Value); int IsInstanceOf = ConvertToInt(xel.Elements().ToArray()[1].Value); string RelativeMatrix = xel.Elements().Last().Value; TreeDict[isAggregatedBy].childList.Add(TreeDict[IsInstanceOf]); TreeDict[isAggregatedBy].childTransferMatrixList.Add(RelativeMatrix); } } loadFinished = true; return(true); }