示例#1
0
        /// <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);
        }
示例#2
0
        /// <summary>
        /// Try to parse the wrl file to make the data useable. Return null if error during the parsing
        /// </summary>
        /// <param name="file">File to parse</param>
        /// <returns>WrlData if successful, null otherwise</returns>
        public static WrlData TryParseWrl(string file)
        {
            WrlData wrlData = null;

            try { wrlData = ParseWrl(file); } catch { }
            return(wrlData);
        }
示例#3
0
        /// <summary>
        /// Reverse the vertex order of the model
        /// </summary>
        /// <param name="wrlData">Data parsed from the wrl file</param>
        public static void ReverseVertexOrder(WrlData wrlData)
        {
            foreach (Transform transform in wrlData.TransformsList)
            {
                foreach (Shape shape in transform.ShapesList)
                {
                    if (shape.Geometry != null)
                    {
                        CoordIndexesWrl coordIndexes    = shape.Geometry.CoordIndexes;
                        CoordIndexesWrl texCoordIndexes = shape.Geometry.TexCoordIndexes;

                        if (coordIndexes != null)
                        {
                            foreach (CoordIndexWrl indexWrl in coordIndexes.IndexesList)
                            {
                                indexWrl.Indexes.Reverse();
                            }
                        }
                        if (texCoordIndexes != null)
                        {
                            foreach (CoordIndexWrl indexWrl in texCoordIndexes.IndexesList)
                            {
                                indexWrl.Indexes.Reverse();
                            }
                        }
                    }
                }
            }
        }
示例#4
0
        private static ToolResult ConvertWrlToObj(string wrlFile, string objOutput, ObjOptions objOptions)
        {
            bool    success = false;
            string  message = "";
            WrlData wrlData = WrlParser.TryParseWrl(wrlFile);

            if (wrlData != null)
            {
                if (objOptions.HasFlag(ObjOptions.ReverseVertex))
                {
                    WrlModifier.ReverseVertexOrder(wrlData);
                }
                success = WrlExporter.WriteObj(wrlData, objOutput);
                message = MessageBoxConstants.GetMessageExecutionCreation(success, objOutput);
            }
            else
            {
                message = MessageBoxConstants.GetMessageExecutionErrorParse(wrlFile);
            }
            return(new ToolResult(message, success));
        }
示例#5
0
        /// <summary>
        /// Create the obj and mtl files with the given data
        /// </summary>
        /// <param name="wrlData">Data parsed from the wrl file</param>
        /// <param name="objFilename">Output file</param>
        /// <returns></returns>
        public static bool WriteObj(WrlData wrlData, string objFilename)
        {
            // objFilename has the .obj extension
            string directory   = System.IO.Path.GetDirectoryName(objFilename);
            string noExtension = System.IO.Path.GetFileNameWithoutExtension(objFilename);
            string mtlRelative = $"{noExtension}.mtl";
            string mtlFilename = System.IO.Path.Combine(directory, mtlRelative);

            List <string> mtlLines = new List <string>(); // To store the lines to append to the mtl file

            try
            {
                using (StreamWriter obj = new StreamWriter(objFilename))
                {
                    // mtllib, o, v, vt, g, usemtl, s, f

                    obj.WriteLine(GenericUtils.GetCreditsFile());
                    obj.WriteLine(ObjUtils.GetNewMtlLib(mtlRelative));

                    int i_coordIndex    = 0;
                    int i_texCoordIndex = 0;

                    int index = 0;

                    foreach (Transform transform in wrlData.TransformsList)
                    {
                        foreach (Shape shape in transform.ShapesList)
                        {
                            string groupName    = "";
                            string urlTexture   = null;
                            string diffuseColor = null;

                            if (shape.Appearance != null)
                            {
                                if (shape.Appearance.Material != null)
                                {
                                    if (shape.Appearance.Material.DiffuseColor != null)
                                    {
                                        diffuseColor = shape.Appearance.Material.DiffuseColor.ToString();
                                    }
                                }

                                if (shape.Appearance.Texture != null)
                                {
                                    urlTexture = shape.Appearance.Texture.Url;
                                    if (urlTexture != null)
                                    {
                                        groupName = ObjUtils.GetGroupName(index, urlTexture);
                                    }
                                    else
                                    {
                                        groupName = ObjUtils.GetGroupName(index);
                                    }
                                }
                                else
                                {
                                    groupName = ObjUtils.GetGroupName(index);
                                }
                            }

                            mtlLines.Add(MtlUtils.GetMtlData(groupName, urlTexture, diffuseColor));

                            obj.WriteLine(ObjUtils.GetNewObject(groupName)); // o


                            int sizeCoord = 0; int sizeTexCoord = 0;

                            if (shape.Geometry != null)
                            {
                                if (shape.Geometry.Coord != null)
                                {
                                    sizeCoord = shape.Geometry.Coord.PointsList.Count;
                                    foreach (Point point in shape.Geometry.Coord.PointsList)
                                    {
                                        obj.WriteLine(ObjUtils.GetNewCoord(point.ToString())); // v
                                    }
                                }

                                if (shape.Geometry.TexCoord != null)
                                {
                                    sizeTexCoord = shape.Geometry.TexCoord.UVList.Count;
                                    foreach (UVCoordinates uv in shape.Geometry.TexCoord.UVList)
                                    {
                                        obj.WriteLine(ObjUtils.GetNewTexCoord(uv.ToString())); // vt
                                    }
                                }
                            }

                            obj.WriteLine(ObjUtils.GetNewGroup(groupName));  // g
                            obj.WriteLine(ObjUtils.GetNewUseMtl(groupName)); // usemtl
                            obj.WriteLine(ObjUtils.GetNewSmoothGroup());     // s

                            if (shape.Geometry != null)
                            {
                                CoordIndexesWrl coordIndexes    = shape.Geometry.CoordIndexes;
                                CoordIndexesWrl texCoordIndexes = shape.Geometry.TexCoordIndexes;

                                if (coordIndexes != null)
                                {
                                    List <CoordIndexWrl> coordIndexesList = coordIndexes.IndexesList;
                                    if (texCoordIndexes != null)
                                    {
                                        List <CoordIndexWrl> texCoordIndexesList = texCoordIndexes.IndexesList;
                                        for (int i = 0; i < coordIndexesList.Count; i++)
                                        {
                                            string strIndexesF = GetIndexesF(coordIndexesList[i], texCoordIndexesList[i], i_coordIndex, i_texCoordIndex);
                                            obj.WriteLine(ObjUtils.GetNewF(strIndexesF)); // f
                                        }
                                    }
                                    else
                                    {
                                        foreach (CoordIndexWrl coordIndex in coordIndexesList)
                                        {
                                            string strIndexesF = GetIndexesF(coordIndex, i_coordIndex);
                                            obj.WriteLine(ObjUtils.GetNewF(strIndexesF)); // f
                                        }
                                    }
                                }
                            }
                            i_coordIndex    += sizeCoord;
                            i_texCoordIndex += sizeTexCoord;
                            index++;
                        }
                    }
                }
            }
            catch
            {
                return(false);
            }
            return(MtlExporter.WriteMtl(mtlLines, mtlFilename)); // Create the mtl file
        }