/// <summary> /// Parse the wrl file to make the data useable /// </summary> /// <param name="file">File to parse</param> /// <returns></returns> public static WrlData ParseWrl(string file) { string[] lines = GenericUtils.RemoveBlankSpaces(FileUtilsShared.ReadFile(file)); // Read the file and remove blankspace if (lines != null) { WrlData wrlData = new WrlData(); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains(("Transform"))) { int transformStart = i; int transformEnd = GetIndexEnd(lines, transformStart); if (transformEnd == -1) { return(null); } // Parsing error wrlData.TransformsList.Add(TransformData(lines, transformStart, transformEnd)); i = transformEnd; // We go to the end of transform } } return(wrlData); } return(null); }
/// <summary> /// Parse the obj file to make the data useable /// </summary> /// <param name="file">File to parse</param> /// <param name="parseMtl">Parse the mtl file directly</param> /// <returns></returns> public static ObjData ParseObj(string file, bool parseMtl = true) { string[] lines = GenericUtils.RemoveBlankSpaces(FileUtilsShared.ReadFile(file)); // Read the file and remove blankspaces if (lines != null) { UpdateParseLineGroup(lines); AllVerticesDataObj verticesData = new AllVerticesDataObj(); // To store the vertices data // Store all the other data to parse List <Tuple <string, string> > listKeysValues = GetListKeysValues(lines, verticesData); ObjData objData = new ObjData(file, verticesData.MtlLib); lines = null; // To let the garbage collector free the file from memory int i = 0; int length = listKeysValues.Count; while (i < length) { Tuple <string, string> keyValue = listKeysValues[i]; if (keyValue != null) { if (isObjectGroup(keyValue.Item1)) { int objectStart = i; int objectEnd = GetIndexEnd(listKeysValues, length, objectStart); if (objectEnd != -1) { objData.ObjectsList.Add(ObjectData(listKeysValues, verticesData, objectStart, objectEnd)); i = objectEnd; // Set to next object continue; } } } i++; // Unless error or final line } if (parseMtl) { objData.UpdateMtlData(); } return(objData); } return(null); }
/// <summary> /// Parse the given mtl file /// </summary> /// <param name="file">File to parse</param> /// <returns></returns> public static MtlData ParseMtl(string file) { string[] lines = GenericUtils.RemoveBlankSpaces(FileUtilsShared.ReadFile(file)); // Read the file and remove blankspaces if (lines != null) { // Store all the other data to parse List <Tuple <string, string> > listKeysValues = GetListKeysValues(lines); MtlData mtlData = new MtlData(file); lines = null; // To let the garbage collector free the file from memory int i = 0; int length = listKeysValues.Count; while (i < length) { Tuple <string, string> keyValue = listKeysValues[i]; if (keyValue != null) // Never null unless error or final line { if (MtlUtils.IsNewMtl(keyValue.Item1)) { int materialStart = i; int materialEnd = GetIndexEnd(listKeysValues, length, materialStart); if (materialEnd != -1) { mtlData.MaterialsList.Add(MaterialData(listKeysValues, materialStart, materialEnd)); i = materialEnd; // Set to next material continue; } } } i++; } return(mtlData); } return(null); }
/// <summary> /// Copy every texture used by the obj file /// </summary> /// <param name="mtlData">Data parsed from the mtl file</param> /// <param name="outputFolder">Where the textures will be copied</param> /// <param name="srcDir">Where the textures are located</param> /// <returns>True if successful</returns> public static bool CopyUsedTextures(MtlData mtlData, string outputFolder, string srcDir) { if (mtlData == null) { // No Mtl to read texture data return(false); } bool relative = false; if (srcDir == null) { // If a textureDirectory was not specified, the textures are found relative to the mtl file srcDir = System.IO.Path.GetDirectoryName(mtlData.FilePath); relative = true; } if (!System.IO.Directory.Exists(srcDir)) { // The source directory doesn't exist return(false); } if (!System.IO.Directory.Exists(outputFolder)) { if (!FileUtilsShared.TryCreateDirectory(outputFolder)) { // Impossible to create the destination directory return(false); } } HashSet <string> texturesList = MtlUtils.GetListTextures(mtlData); foreach (string texture in texturesList) { string srcFile; string destFile = System.IO.Path.Combine(outputFolder, System.IO.Path.GetFileName(texture)); if (relative) // The path to the texture is inside the material { if (System.IO.Path.IsPathRooted(texture)) // Absolute path to the picture { srcFile = texture; } else // Relative to the mtl { srcFile = System.IO.Path.Combine(srcDir, texture); } } else // We find the texture in a given folder { srcFile = System.IO.Path.Combine(srcDir, System.IO.Path.GetFileName(texture)); } if (!destFile.Equals(srcFile)) // Destination file is not the Source file { if (System.IO.File.Exists(srcFile)) { if (System.IO.File.Exists(destFile)) // The destination file already exists, we need to delete it { if (!FileUtilsShared.TryDeleteFile(destFile)) { continue; // Impossible to delete the file, we go to the next texture } } FileUtilsShared.TryCopyFile(srcFile, destFile); } } } return(true); }