Search code examples
c#wpfhelix-3d-toolkit

How to remove 3dModel from Viewport [HelixToolKit WPF]


I am trying to create a question game with 3d models involved. I want my app to change the 3d model if the question is right. To do that I added a function called setupQ

Here is the code for it:

        // To Setup Next 3dModel
        public void SetupQ(string ModelS)
        {
            // SET 3d Model
            ModelVisual3D device3D = new ModelVisual3D();
            device3D.Content = Display3d(ModelS);
            // Add to view port
            viewPort3d.Children.Add(device3D);
        }

Display3D Function:

 private Model3D Display3d(string model)
        {
            Model3D device = null;
            try
            {
                //Adding a gesture here
                viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);

                //Import 3D model file
                ModelImporter import = new ModelImporter();

                //Load the 3D model file
                device = import.Load(model);
            }
            catch (Exception e)
            {
                // Handle exception in case can not file 3D model
                MessageBox.Show("Exception Error : " + e.StackTrace);
            }
            return device;
        }
  • ModelS is the path to the 3d model. [The path does work].

How can I remove the 3d model that I just added and then add a new 3d model instead?

  • Is there a replace or clear function for the HelixToolKit?

Solution

  • Well I just figured the answer myself, it was quite simple.

    • To Clear the viewport you can:
    viewPort3d.Children.Clear();
    
    • This will remove all lights, so you might as well want to add those back.
    viewPort3d.Children.Add(new DefaultLights());