Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What happens in this code is that the selected object is shown according to the value entered in the textbox and the rest of the object is hidden.

Example :- 
The selected object is 90mm in total. Now if I enter 50 in the textbox, the 1mm to 50mm object is shown and the 51mm to 90mm object is hidden. Now what I want to do is that the object from 51mm to 90mm should be removed.


What I have tried:

C#
private void viewPort3d_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2 && e.ChangedButton == MouseButton.Left)
	{
		// Store the original material of the newly selected object
        var group2 = selectedModel.Content as Model3DGroup;
        if (group2 != null && group2.Children.Count > 0)
        {
            var model = group2.Children[0] as GeometryModel3D;
            if (model != null)
            {
                STL.UpdateMaterial = model.Material;

                var bounds = model.Bounds;
                double width = bounds.SizeX * 1000; // convert to millimeters
                double height = bounds.SizeY * 1000; // convert to millimeters
                double depth = bounds.SizeZ * 1000; // convert to millimeters

                double hideDepth = 0.0;
                var Z = depth / 1000.0;
							
                double.TryParse(depthTextBox.Text, out hideDepth);
                // Calculate the percentage of the object to be hidden
                double hidePercent = hideDepth / Z;

                // Iterate through all the mesh triangles and hide those above the hidePercent
                var meshGeometry = model.Geometry as MeshGeometry3D;
                if (meshGeometry != null)
                {
                    for (int i = 0; i < meshGeometry.TriangleIndices.Count; i += 3)
                    {
                       Point3D p1 = meshGeometry.Positions[meshGeometry.TriangleIndices[i]];
                       Point3D p2 = meshGeometry.Positions[meshGeometry.TriangleIndices[i + 1]];
                       Point3D p3 = meshGeometry.Positions[meshGeometry.TriangleIndices[i + 2]];

                       double minZ = Math.Min(Math.Min(p1.Z, p2.Z), p3.Z);
                       double maxZ = Math.Max(Math.Max(p1.Z, p2.Z), p3.Z);

                       if (maxZ >= bounds.SizeZ * hidePercent)
                       {
                          // Hide the triangle by setting its normal to (0,0,-1)
                          meshGeometry.Normals[meshGeometry.TriangleIndices[i]] = new Vector3D(0, 0, -1);
                          meshGeometry.Normals[meshGeometry.TriangleIndices[i + 1]] = new Vector3D(0, 0, -1);
                          meshGeometry.Normals[meshGeometry.TriangleIndices[i + 2]] = new Vector3D(0, 0, -1);
                       }
                    }
                    // Invalidate the visual to refresh the changes
                    model.Geometry = meshGeometry;
                    viewPort3d.InvalidateVisual();
                  }
			  }
		}
    }
}
Posted
Updated 17-Apr-23 19:22pm
v3

 
Share this answer
 
The selected object is 90mm in total. Now if I enter 50 in the textbox, 1mm to 50mm objects are shown and 51mm to 90mm objects are removed.


C#
if (meshGeometry != null)
   {
      var newMeshGeometry = new MeshGeometry3D();
      for (int i = 0; i < meshGeometry.TriangleIndices.Count; i += 3)
      {
         Point3D p1 = meshGeometry.Positions[meshGeometry.TriangleIndices[i]];
         Point3D p2 = meshGeometry.Positions[meshGeometry.TriangleIndices[i + 1]];
         Point3D p3 = meshGeometry.Positions[meshGeometry.TriangleIndices[i + 2]];

         double minZ = Math.Min(Math.Min(p1.Z, p2.Z), p3.Z);
         double maxZ = Math.Max(Math.Max(p1.Z, p2.Z), p3.Z);

         if (maxZ < bounds.SizeZ * hidePercent)
         {
             newMeshGeometry.Positions.Add(p1);
             newMeshGeometry.Positions.Add(p2);
             newMeshGeometry.Positions.Add(p3);

             newMeshGeometry.TriangleIndices.Add(newMeshGeometry.Positions.Count - 3);
             newMeshGeometry.TriangleIndices.Add(newMeshGeometry.Positions.Count - 2);
             newMeshGeometry.TriangleIndices.Add(newMeshGeometry.Positions.Count - 1);
         }
       }
       model.Geometry = newMeshGeometry;
       viewPort3d.InvalidateVisual();
   }
 
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