Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can select the model but the color of the selected model does not change. I want that the color of the selected object should be red and all other objects should be yellow.

What I have tried:

this is my code for load object:-

private void loadbtn_Click(object sender, RoutedEventArgs e)
   Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
   dlg.DefaultExt = ".stl";
   dlg.Filter = "STL files (*.stl)|*.stl";
   Nullable<bool> result = dlg.ShowDialog();
   if (result == true)
       // Load the STL file using the Helix Toolkit's STL reader
       var reader = new StLReader();
       _modelGroup = reader.Read(dlg.FileName);
       _modelGroup.Transform = _translate;
       // Create a ModelVisual3D object and add the _modelGroup to it
       _modelVisual = new ModelVisual3D();
       _modelVisual.Content = _modelGroup;
       // Add the model to the viewport's 3D model collection

this my code for select object:-

private void viewPort3d_MouseDown(object sender, MouseButtonEventArgs e)
   var hitResult = VisualTreeHelper.HitTest(viewPort3d, e.GetPosition(viewPort3d));
   if (hitResult != null && hitResult.VisualHit is ModelVisual3D visual3D)
       // Set the selected object to the one that was clicked on
       selectedModel = visual3D;
       STL.RotateSTLModel = selectedModel;

       var material = new DiffuseMaterial(new SolidColorBrush(Colors.Red));
       //Set the material of the selected model
       selectedModel.Material = material;

private void viewPort3d_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
   if (selectedModel != null && e.LeftButton == MouseButtonState.Pressed)
      // Convert the mouse position to a 3D point in the viewport's coordinate system
      var position = e.GetPosition(viewPort3d);
      var point3D = viewPort3d.FindNearestPoint(position);

      // Move the selected model to the new position
      var transform = selectedModel.Transform as TranslateTransform3D;

      if (transform == null)
          transform = new TranslateTransform3D();
          selectedModel.Transform = transform;

      if (point3D.HasValue)
          transform.OffsetX = point3D.Value.X;
          transform.OffsetY = point3D.Value.Y;
Updated 22-Feb-23 22:53pm
Kenneth Haugland 22-Feb-23 5:48am    
Perhaps you should call InvalidateVisual or something similar? And have you debugged the code?
Member 15746979 22-Feb-23 23:13pm    
this is my image:- "". There is an object of yellow color in this image, I want to change its color
Gerry Schmitz 22-Feb-23 11:14am    
"WPF" uses Foreground and Fill and Background and Stroke brushes and colors; you've got something called "DiffuseMaterial". No one except "helixview" (or you) knows what you're talking about.
Member 15746979 22-Feb-23 23:12pm    
this is my image:- "". There is an object of yellow color in this image, I want to change its color

1 solution

you can set the SelectedMaterial property of the StLModelVisual3D to a new Material. Your code will look something like this -

// Get the selected StLModelVisual3D
StLModelVisual3D selectedModel = myHelixViewport3D.Children.OfType<StLModelVisual3D>().FirstOrDefault(m => m.IsSelected);

// Create a new material with the desired color
DiffuseMaterial selectedMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Red));

// Set the material of the selected model
selectedModel.SelectedMaterial = selectedMaterial;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900