Question

i want to change all material color in the view port 3d using this code :

    DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Red));

        foreach (ModelVisual3D model3d in previewport.Children)
        {

            foreach (GeometryModel3D item in model3d.Content)
            {
                item.Material = mat;
            }
        }

but it get errors :

      Error  
     foreach statement cannot operate on variables of type 'System.Windows.Media.Media3D.Model3D' because 'System.Windows.Media.Media3D.Model3D' does not contain a public definition for 'GetEnumerator'

please help. thanks.

Was it helpful?

Solution

ModelVisual3D.Content is a single System.Windows.Media.Media3D object and that why it's complaining about foreach loop. Instead of the inner loop just cast Content as GeometryModel3D and change its Material like below:

DiffuseMaterial mat = new DiffuseMaterial(new SolidColorBrush(Colors.Red));

foreach (ModelVisual3D model3d in previewport.Children)
{
    var geometryModel = model3d.Content as GeometryModel3D;
    if (geometryModel != null) geometryModel.Material = mat;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top