Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code it happens that :- total object is 90mm, now if i enter 50 in textbox and object from 1mm to 50mm appears and object from 51mm to 90mm is removed.
  Now if I enter 60 in the textbox then the object from 1mm to 60mm should appear and the object from 61mm to 90mm should be removed but only the object from 1mm to 50mm is shown which should appear from 1mm to 60mm.

Now I want to show as many objects as I enter in the textbox


What I have tried:

C#
private void viewPort3d_MouseDown(object sender, MouseButtonEventArgs e)
{
   if (e.ClickCount == 2 && e.ChangedButton == MouseButton.Left)
   {
      isLeftButtonDoubleClicked = true;
      var hitResult = VisualTreeHelper.HitTest(viewPort3d, e.GetPosition(viewPort3d));
      if (hitResult != null && hitResult.VisualHit is ModelVisual3D visual3D)
      {
          // 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;
              {
                 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

                 var z = depth/1000.0;
                 // Get the value entered in the depthTextBox
                 double hideDepth = 0.0;
                 double.TryParse(depthTextBox.Text, out hideDepth);

                 // Calculate the percentage of the object that should be hidden
                 double hidePercent = Math.Min(hideDepth, z) / z;

                 // Update the mesh geometry to hide the appropriate part of the object
                 var meshGeometry = model.Geometry as MeshGeometry3D;
                 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();
                     }
				}
			}
		}
	}
}
Posted
Updated 23-Apr-23 6:55am
v5
Comments
Member 15627495 19-Apr-23 4:55am    
hello,

add a 'history' array, with non duplicates entries.
[no name] 19-Apr-23 14:59pm    
Sounds like you're actually altering but have fixed on some concept of removing; complicating the whole thing.

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