Beispiel #1
0
        bool LoadModel(string PathToModel, Color DModelColor, bool LoadTexture, bool NewModel)
        {
            try
            {
                if (NewModel)
                {
                    LocalPath.CopyDirectoryToResources(System.IO.Path.GetDirectoryName(PathToModel));
                }

                Debug.WriteLine("Dir Name: " + System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(PathToModel)) + "   FileName: " + System.IO.Path.GetFileName(PathToModel));

                string Lpathtomod = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(PathToModel)) + "\\" + System.IO.Path.GetFileName(PathToModel);
                string pathtomod  = System.IO.Path.Combine(LocalPath.GetResourcesPath(), Lpathtomod);

                Debug.WriteLine("Lpath: " + Lpathtomod + " path: " + pathtomod);

                Model3DGroup mgroup = new Model3DGroup();

                string ext = System.IO.Path.GetExtension(PathToModel);
                if (ext == ".obj")
                {
                    ObjReader reader = new HelixToolkit.Wpf.ObjReader();
                    try
                    {
                        reader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(DModelColor));


                        if (LoadTexture)
                        {
                            mgroup = reader.Read(pathtomod);
                        }
                        else
                        {
                            reader.TexturePath = ".";
                            mgroup             = reader.Read(RichTextBoxWorker.StreamFromString(File.ReadAllText(pathtomod)));
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error Load obj First Try: Loading texture error?: " + LoadTexture);
                        if (LoadTexture)
                        {
                            reader = new HelixToolkit.Wpf.ObjReader();
                            reader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(DModelColor));
                            reader.TexturePath     = ".";
                            mgroup = reader.Read(RichTextBoxWorker.StreamFromString(File.ReadAllText(pathtomod)));
                        }
                        else
                        {
                            Debug.WriteLine("Error Load 3D model: " + ex);
                            MessageBox.Show(Strings.ResStrings.NotSaved, Strings.ResStrings.NotSavedTitle, MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                    }
                }
                else
                {
                    ModelImporter modelImporter = new ModelImporter();
                    modelImporter.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(DModelColor));
                    mgroup = modelImporter.Load(pathtomod);
                }

                ModelVisual3D model = new ModelVisual3D();
                model.Content = mgroup;

                DefaultLights defaultLights = new DefaultLights();

                cont.Children.Clear();
                cont.Children.Add(model);
                cont.Children.Add(defaultLights);

                Debug.WriteLine("Adding model tag: " + Lpathtomod);

                if (cont.Tag == null)
                {
                    List <object> list = new List <object>(2);
                    list.Add(null);
                    list.Add(null);
                    cont.Tag = list;
                    ((List <object>)cont.Tag)[0] = new ModelPath(Lpathtomod, LoadTexture, DModelColor);
                }
                else
                {
                    ((List <object>)cont.Tag)[0] = new ModelPath(Lpathtomod, LoadTexture, DModelColor);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error Load 3D model: " + ex);
                MessageBox.Show(Strings.ResStrings.ErrorLoadModel, Strings.ResStrings.Error, MessageBoxButton.OK, MessageBoxImage.Warning);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        private void LoadModel(HelixViewport3D cont, string path)
        {
            try
            {
                Model3DGroup mgroup = new Model3DGroup();

                string ext = System.IO.Path.GetExtension(path);
                if (ext == ".obj")
                {
                    ObjReader reader = new HelixToolkit.Wpf.ObjReader();
                    try
                    {
                        reader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(modelp.DefaultColor));


                        if (modelp.LoadTexture)
                        {
                            mgroup = reader.Read(path);
                        }
                        else
                        {
                            reader.TexturePath = ".";
                            mgroup             = reader.Read(RichTextBoxWorker.StreamFromString(File.ReadAllText(path)));
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error Load obj First Try: Loading texture error?: " + modelp.LoadTexture);
                        if (modelp.LoadTexture)
                        {
                            reader = new HelixToolkit.Wpf.ObjReader();
                            reader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(modelp.DefaultColor));
                            reader.TexturePath     = ".";
                            mgroup = reader.Read(RichTextBoxWorker.StreamFromString(File.ReadAllText(path)));
                        }
                        else
                        {
                            Debug.WriteLine("Error Load 3D model: " + ex);
                        }
                    }
                }
                else
                {
                    ModelImporter modelImporter = new ModelImporter();
                    modelImporter.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(modelp.DefaultColor));
                    mgroup = modelImporter.Load(path);
                }

                ModelVisual3D model = new ModelVisual3D();
                model.Content = mgroup;

                cont.Children.Clear();
                cont.Children.Add(model);

                cont.Tag = modelp;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("" + ex);
            }
        }