Search code examples
c#wpfhelix-3d-toolkit

Same Model3D does not seem equal each other?


I am loading STL files from a local file using HelixToolKit ModelImporter function. After loading Model3D I am store it in a dictionary as a key with value of file name. My purpose of doing that I want to compare models for prevent loading same models. After loading same model from same path it looks the dictionary for that models as a key. But it doesn't found like that model. Where am I going wrong, please help?

    private Model3D currModel = null;
    private string modelName = null;
    // A record of the 3D models we build.
    private Dictionary<Model3D, string> Models= new Dictionary<Model3D, string>();
    private Model3D device = null;     

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
        if (open.ShowDialog() == true)
        {
            try
            {
                string path = open.FileName;
                modelName = open.SafeFileName;

                currModel = display3D(path);

                if (!Models.ContainsKey(currModel))
                    Models.Add(currModel, modelName);
                ModelVisual3D device3D = new ModelVisual3D();
                device3D.Content = currModel;
                mainViewport.Children.Add(device3D);
                mainViewport.ZoomExtents();//modeli ekrana sığdırmak için
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error\n" + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
    private Model3D display3D(string path)
    {
        //Import 3D model file
        ModelImporter import = new ModelImporter();
        //Load the 3D model file
        device = import.Load(path);        
        return device;
    }

Solution

  • Update #1: Problem is solved. In my project ModelImporter does not load 3D Model; it loads 3DModelGroup. So if you add the Models.Children it works.

    if (!Models.ContainsKey(currModel.Children))
            Models.Add(currModel.Children, modelName);