HelixToolkit.Wpf.SharpDX.ObjReader.Read C# (CSharp) Method

Read() public method

Reads the model from the specified stream.
public Read ( Stream s, HelixToolkit.Wpf.SharpDX.ModelInfo info = default(ModelInfo) ) : System.Collections.Generic.List
s Stream /// The stream. ///
info HelixToolkit.Wpf.SharpDX.ModelInfo
return System.Collections.Generic.List
        public Object3DGroup Read(Stream s, ModelInfo info = default(ModelInfo))
        {
            using (this.Reader = new StreamReader(s))
            {
                this.currentLineNo = 0;
                while (!this.Reader.EndOfStream)
                {
                    this.currentLineNo++;
                    var line = this.Reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    line = line.Trim();
                    while (line.EndsWith("\\"))
                    {
                        var nextLine = this.Reader.ReadLine();
                        while (nextLine.Length == 0)
                        {
                            nextLine = this.Reader.ReadLine();
                        }

                        line = line.TrimEnd('\\') + nextLine;
                    }

                    if (line.StartsWith("#") || line.Length == 0)
                    {
                        continue;
                    }

                    string keyword, values;
                    SplitLine(line, out keyword, out values);

                    try
                    {

                        switch (keyword.ToLower())
                        {
                            // Vertex data
                            case "v": // geometric vertices
                                this.AddVertex(values);
                                break;
                            case "vt": // texture vertices
                                this.AddTexCoord(values);
                                break;
                            case "vn": // vertex normals
                                this.AddNormal(values);
                                break;
                            case "vp": // parameter space vertices
                            case "cstype": // rational or non-rational forms of curve or surface type: basis matrix, Bezier, B-spline, Cardinal, Taylor
                            case "degree": // degree
                            case "bmat": // basis matrix
                            case "step": // step size
                                // not supported
                                break;

                            // Elements
                            case "f": // face
                                this.AddFace(values);
                                break;
                            case "p": // point
                            case "l": // line
                            case "curv": // curve
                            case "curv2": // 2D curve
                            case "surf": // surface
                                // not supported
                                break;

                            // Free-form curve/surface body statements
                            case "parm": // parameter name
                            case "trim": // outer trimming loop (trim)
                            case "hole": // inner trimming loop (hole)
                            case "scrv": // special curve (scrv)
                            case "sp":  // special point (sp)
                            case "end": // end statement (end)
                                // not supported
                                break;

                            // Connectivity between free-form surfaces
                            case "con": // connect
                                // not supported
                                break;

                            // Grouping
                            case "g": // group name
                                this.AddGroup(values);
                                break;
                            case "s": // smoothing group
                                this.SetSmoothingGroup(values);
                                break;
                            case "mg": // merging group
                                break;
                            case "o": // object name
                                // not supported
                                break;

                            // Display/render attributes
                            case "mtllib": // material library
                                this.LoadMaterialLib(values);
                                break;
                            case "usemtl": // material name
                                this.EnsureNewMesh();

                                this.SetMaterial(values);
                                break;
                            case "usemap": // texture map name
                                this.EnsureNewMesh();

                                break;
                            case "bevel": // bevel interpolation
                            case "c_interp": // color interpolation
                            case "d_interp": // dissolve interpolation
                            case "lod": // level of detail
                            case "shadow_obj": // shadow casting
                            case "trace_obj": // ray tracing
                            case "ctech": // curve approximation technique
                            case "stech": // surface approximation technique
                                // not supported
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(string.Format("Error loading object: {0}", ex.Message), "Error", MessageBoxButton.OKCancel);
                    }
                }
            }

            return this.BuildModel();
        }

Same methods

ObjReader::Read ( string path, HelixToolkit.Wpf.SharpDX.ModelInfo info = default(ModelInfo) ) : System.Collections.Generic.List

Usage Example

        public override void Calculate()
        {
            if (InputPorts[0].Data == null) return;

            if (InputPorts[0].Data is string)
            {
                var extension = Path.GetExtension(InputPorts[0].Data as string);

                var flag = false;

                switch (extension)
                {
                    case ".obj":
                        var currentHelixObjReader = new ObjReader();
                        try
                        {
                            var objModel = currentHelixObjReader.Read((string) InputPorts[0].Data);
                            var modelGroup = new GroupModel3D();
                            var modelGeometry = new Element3DCollection();
                            modelGeometry.AddRange(
                                objModel.Select(
                                    x =>
                                        new MeshGeometryModel3D
                                        {
                                            Geometry = x.Geometry as MeshGeometry3D,
                                            Material = x.Material
                                        }));

                            modelGroup.Children = modelGeometry;

                            Dispatcher.BeginInvoke((Action) delegate { _control.ViewPort3D.Items.Add(modelGroup); });
                        }
                        catch (Exception)
                        {
                            // ignore
                        }
                        break;
                    case ".stl":
                        var currentHelixStlReader = new StLReader();
                        try
                        {
                            var myModel = currentHelixStlReader.Read((string) InputPorts[0].Data);
                            // _control.ViewPort3D.Items.Add(myModel);
                        }
                        catch (Exception)
                        {
                            // ignore
                        }
                        break;
                }
            }
        }
All Usage Examples Of HelixToolkit.Wpf.SharpDX.ObjReader::Read