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;
                }
            }
        }
Beispiel #2
0
 public void Read_Sphere_ValidModel()
 {
     var r = new StLReader();
     var model = r.Read(@"Models\stl\sphere.stl");
     Assert.AreEqual(1, model.Children.Count);
     var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;
     Assert.AreEqual(228, m0.TriangleIndices.Count / 3);
 }
 public void Read_Bottle_ValidModel()
 {
     var r = new StLReader();
     var model = r.Read(@"Models\stl\bottle.stl");
     Assert.AreEqual("FLIRIS", r.Header);
     Assert.AreEqual(1, model.Children.Count);
     var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;
     Assert.AreEqual(1240, m0.TriangleIndices.Count / 3);
 }
 public void Read_BinaryBottle_ValidModel()
 {
     // ASCII format converted to BINARY by MeshLab
     var r = new StLReader();
     var model = r.Read(@"Models\stl\binary\bottle.stl");
     Assert.AreEqual("VCG", r.Header);
     Assert.AreEqual(1, model.Children.Count);
     var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;
     Assert.AreEqual(1240, m0.TriangleIndices.Count / 3);
 }
Beispiel #5
0
        public void Read_Cube_ValidModel()
        {
            // reader does not yet support quads
            var r = new StLReader();
            var model = r.Read(@"Models\stl\cube.stl");
            Assert.AreEqual(1, model.Children.Count);
            var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;

            // Expects 6 quad faces => 12 triangles
            Assert.AreEqual(12, m0.TriangleIndices.Count / 3);
        }
 public void Read_Bottle_DefaultMaterialChanged()
 {
     var r = new StLReader();
     r.DefaultMaterial = MaterialHelper.CreateMaterial(Colors.Aqua);
     var model = r.Read(@"Models\stl\bottle.stl");
     Assert.AreEqual(1, model.Children.Count);
     var gm0 = (GeometryModel3D)model.Children[0];
     var m0 = (MaterialGroup)gm0.Material;
     var dm = (DiffuseMaterial)m0.Children[0];
     Assert.AreEqual(Colors.Aqua, ((SolidColorBrush)dm.Brush).Color);
 }
Beispiel #7
0
        public void Read_BottleFromStream_ValidModel()
        {
            Model3DGroup model;
            using (var s = File.OpenRead(@"Models\stl\bottle.stl"))
            {
                var r = new StLReader();
                model = r.Read(s);
            }

            Assert.AreEqual(1, model.Children.Count);
            var m0 = (MeshGeometry3D)((GeometryModel3D)model.Children[0]).Geometry;
            Assert.AreEqual(1240, m0.TriangleIndices.Count / 3);
        }
Beispiel #8
0
        /// <summary>
        /// Loads a model from the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="dispatcher">The dispatcher used to create the model.</param>
        /// <param name="freeze">Freeze the model if set to <c>true</c>.</param>
        /// <returns>A model.</returns>
        /// <exception cref="System.InvalidOperationException">File format not supported.</exception>
        public Model3DGroup Load(string path, Dispatcher dispatcher = null, bool freeze = false)
        {
            if (path == null)
            {
                return(null);
            }

            if (dispatcher == null)
            {
                dispatcher = Dispatcher.CurrentDispatcher;
            }

            Model3DGroup model;
            var          ext = Path.GetExtension(path);

            if (ext != null)
            {
                ext = ext.ToLower();
            }

            switch (ext)
            {
            case ".3ds":
            {
                var r = new StudioReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.Read(path, ApplyNormalToFace);
                break;
            }

            case ".lwo":
            {
                var r = new LwoReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.Read(path);

                break;
            }

            case ".obj":
            {
                var r = new ObjReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.Read(path);
                break;
            }

            case ".objz":
            {
                var r = new ObjReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.ReadZ(path);
                break;
            }

            case ".stl":
            {
                var r = new StLReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.Read(path);
                break;
            }

            case ".off":
            {
                var r = new OffReader(dispatcher)
                {
                    DefaultMaterial = this.DefaultMaterial, Freeze = freeze
                };
                model = r.Read(path);
                break;
            }

            default:
                throw new InvalidOperationException("File format not supported.");
            }

            //if (!freeze)
            //{
            //    dispatcher.Invoke(new Action(() => model.SetName(Path.GetFileName(path))));
            //}

            return(model);
        }
        /// <summary>
        /// Loads a model from the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// A model.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">File format not supported.</exception>
        public Model3DGroup Load(string path)
        {
            if (path == null)
            {
                return(null);
            }

            Model3DGroup model;
            var          ext = Path.GetExtension(path);

            if (ext != null)
            {
                ext = ext.ToLower();
            }

            switch (ext)
            {
            case ".3ds":
            {
                var r = new StudioReader();
                model = r.Read(path);
                break;
            }

            case ".lwo":
            {
                var r = new LwoReader {
                    DefaultMaterial = this.DefaultMaterial
                };
                model = r.Read(path);

                break;
            }

            case ".obj":
            {
                var r = new ObjReader {
                    DefaultMaterial = this.DefaultMaterial
                };
                model = r.Read(path);
                break;
            }

            case ".objz":
            {
                var r = new ObjReader {
                    DefaultMaterial = this.DefaultMaterial
                };
                model = r.ReadZ(path);
                break;
            }

            case ".stl":
            {
                var r = new StLReader {
                    DefaultMaterial = this.DefaultMaterial
                };
                model = r.Read(path);
                break;
            }

            case ".off":
            {
                var r = new OffReader {
                    DefaultMaterial = this.DefaultMaterial
                };
                model = r.Read(path);
                break;
            }

            default:
                throw new InvalidOperationException("File format not supported.");
            }

            return(model);
        }
Beispiel #10
0
        public void LoadOsp()
        {
            //ModelGeometry已經有幾何模型存在內部 及 阻擋檔案不存在的情況
            if (this.IsLoaded || !System.IO.File.Exists(FilePath))
            {
                return;
            }
            //利用helixtoolkit.wpf裡面提供的StlReader讀檔案,後續要轉成wpf.sharpdx可用的格式
            StLReader reader = new HelixToolkit.Wpf.StLReader();

            this.ModelContainer = reader.Read(FilePath);

            var geometryModel = this.ModelContainer.Children[0] as System.Windows.Media.Media3D.GeometryModel3D;

            var mesh = geometryModel.Geometry as System.Windows.Media.Media3D.MeshGeometry3D;

            //MIP產生的OSP一定是六個點
            if (mesh.Positions.Count != 6)
            {
                return;
            }

            this.OspOriNormal = new Vector3D(mesh.Normals[0].X, mesh.Normals[0].Y, mesh.Normals[0].Z);
            this.OspOriNormal.Normalize();//將上述向量正規化
            this.OspPlanePoint = mesh.Positions[0];

            //設定模型幾何形狀
            HelixToolkit.Wpf.SharpDX.MeshGeometry3D modelGeometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D();

            modelGeometry.Normals   = new Vector3Collection();
            modelGeometry.Positions = new Vector3Collection();
            modelGeometry.Indices   = new IntCollection();
            //將從stlreader讀到的資料轉入
            foreach (Point3D position in mesh.Positions)
            {
                modelGeometry.Positions.Add(new Vector3(
                                                Convert.ToSingle(position.X)
                                                , Convert.ToSingle(position.Y)
                                                , Convert.ToSingle(position.Z)));
            }


            modelGeometry.Colors = new Color4Collection();
            for (int i = 0; i < mesh.Positions.Count; i++)
            {
                modelGeometry.Colors.Add(new Color4(0.1f, 0.1f, 0.8f, 0.2f));
            }

            foreach (Vector3D normal in mesh.Normals)
            {
                modelGeometry.Normals.Add(new Vector3(
                                              Convert.ToSingle(normal.X)
                                              , Convert.ToSingle(normal.Y)
                                              , Convert.ToSingle(normal.Z)));
            }



            foreach (Int32 triangleindice in mesh.TriangleIndices)
            {
                modelGeometry.Indices.Add(triangleindice);
            }

            this.Geometry = modelGeometry;

            SetOspMaterial();

            this.Transform = new MatrixTransform3D();

            this.IsLoaded = true;
        }
        /// <summary>
        /// Loads a model from the specified path.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// A model.
        /// </returns>
        public static Model3DGroup Load(string path)
        {
            if (path == null)
            {
                return null;
            }

            Model3DGroup model = null;
            string ext = Path.GetExtension(path).ToLower();
            switch (ext)
            {
                case ".3ds":
                    {
                        var r = new StudioReader();
                        model = r.Read(path);
                        break;
                    }

                case ".lwo":
                    {
                        var r = new LwoReader();
                        model = r.Read(path);

                        break;
                    }

                case ".obj":
                    {
                        var r = new ObjReader();
                        model = r.Read(path);
                        break;
                    }

                case ".objz":
                    {
                        var r = new ObjReader();
                        model = r.ReadZ(path);
                        break;
                    }

                case ".stl":
                    {
                        var r = new StLReader();
                        model = r.Read(path);
                        break;
                    }

                case ".off":
                    {
                        var r = new OffReader();
                        model = r.Read(path);
                        break;
                    }

                default:
                    throw new InvalidOperationException("File format not supported.");
            }

            return model;
        }
        /// <summary>
        /// Loads a model from the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="dispatcher">The dispatcher used to create the model.</param>
        /// <param name="freeze">Freeze the model if set to <c>true</c>.</param>
        /// <returns>A model.</returns>
        /// <exception cref="System.InvalidOperationException">File format not supported.</exception>
        public Model3DGroup Load(string path, Dispatcher dispatcher = null, bool freeze = false)
        {
            if (path == null)
            {
                return null;
            }

            if (dispatcher == null)
            {
                dispatcher = Dispatcher.CurrentDispatcher;
            }

            Model3DGroup model;
            var ext = Path.GetExtension(path);
            if (ext != null)
            {
                ext = ext.ToLower();
            }

            switch (ext)
            {
                case ".3ds":
                    {
                        var r = new StudioReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".lwo":
                    {
                        var r = new LwoReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.Read(path);

                        break;
                    }

                case ".obj":
                    {
                        var r = new ObjReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".objz":
                    {
                        var r = new ObjReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.ReadZ(path);
                        break;
                    }

                case ".stl":
                    {
                        var r = new StLReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".off":
                    {
                        var r = new OffReader(dispatcher) { DefaultMaterial = this.DefaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                default:
                    throw new InvalidOperationException("File format not supported.");
            }

            //if (!freeze)
            //{
            //    dispatcher.Invoke(new Action(() => model.SetName(Path.GetFileName(path))));
            //}

            return model;
        }
Beispiel #13
-1
        public static Model3DGroup Load(string path, Dispatcher dispatcher = null, bool freeze = false, bool ignoreErrors = false)
        {
            if (path == null)
            {
                return null;
            }

            var defaultMaterial = Materials.Blue;

            if (dispatcher == null)
            {
                dispatcher = Dispatcher.CurrentDispatcher;
            }

            Model3DGroup model;
            var ext = Path.GetExtension(path).ToLower();

            switch (ext)
            {
                case ".3ds":
                    {
                        var r = new StudioReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".lwo":
                    {
                        var r = new LwoReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".obj":
                    {
                        var r = new ObjReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze, IgnoreErrors = ignoreErrors };
                        model = r.Read(path);
                        break;
                    }

                case ".objx":
                    {
                        var r = new ObjReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze, IgnoreErrors = ignoreErrors };
                        model = r.ReadZ(path);
                        break;
                    }

                case ".stl":
                    {
                        var r = new StLReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                case ".off":
                    {
                        var r = new OffReader(dispatcher) { DefaultMaterial = defaultMaterial, Freeze = freeze };
                        model = r.Read(path);
                        break;
                    }

                default:
                    throw new InvalidOperationException("File format not supported.");
            }

            return model;
        }
Beispiel #14
-1
        /// <summary>
        ///     Read FileData
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public ContainerUIElement3D ReadFileData(string path)
        {
            var extension = Path.GetExtension(path);
            // var visModel = new ModelVisual3D();
            var container = new ContainerUIElement3D();

            switch (extension)
            {
                case ".obj":
                    var currentHelixObjReader = new ObjReader();
                    try
                    {
                        var myModel = currentHelixObjReader.Read(path);

                        foreach (var model in myModel.Children)
                        {
                            if (model is GeometryModel3D)
                            {
                                var geom = model as GeometryModel3D;

                                var element = new ModelUIElement3D {Model = geom};
                                element.MouseDown += (sender1, e1) => OnElementMouseDown(sender1, e1, this);
                                container.Children.Add(element);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                    break;
                case ".stl":
                    var currentHelixStlReader = new StLReader();
                    try
                    {
                        var myModel = currentHelixStlReader.Read(path);

                        foreach (var model in myModel.Children)
                        {
                            if (model is GeometryModel3D)
                            {
                                var geom = model as GeometryModel3D;

                                var element = new ModelUIElement3D {Model = geom};
                                element.MouseDown += (sender1, e1) => OnElementMouseDown(sender1, e1, this);
                                container.Children.Add(element);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                    break;
            }

            return container;
        }