MeshGeometryHelper Cut Method Help: Normals Flipping and Cannot Cut Twice

Nathan Smela 7 years ago updated 7 years ago 0

Good Day Everyone,

I'm having a hard time with cutting a model in my program. I import a STL file into a Model3DGroup, add that model to a MeshBuilder (true for include normals), and attempt to cut the model along the 0 z-axis (pInf and nInf) This is to ensure it has a flat base to 3D print on.

First issue:

The flat cut is inverted when added. If i add a larger model, it inverts all the normals and appear to be inside out.

Second issue:

I attempt to cut the model at 10 mm (pSup and nSup), but the attempt keeps failing. It will fail even if I comment out the 0 z-axis cut.

private void CutBottom()        {
            //load the stl file to edit
            string file = @"E:\Test\test.stl";
            var mi = new ModelImporter();
            Model3DGroup model = mi.Load(file, System.Windows.Threading.Dispatcher.CurrentDispatcher);

            //convert model to Geometry3D
            MeshBuilder meshModel = new MeshBuilder(true, false);

            foreach (var m in model.Children)
                var mGeo = m as GeometryModel3D;
                var tri = (MeshGeometry3D)mGeo.Geometry;

                if (tri != null)

            MeshGeometry3D mesh = meshModel.ToMesh();

            //create a copy of the mesh 
            var g = mesh as MeshGeometry3D;
            MeshGeometry3D gOrig = g.Clone();

            //Cut the Mesh and then apply the cutplane
            var pInf = new Point3D(0, 0, 0);
            var nInf = new Vector3D(0, 0, 1);
            Plane3D cpInf = new Plane3D(pInf, nInf);

            var pSup = new Point3D(0, 0, 2);
            var nSup = new Vector3D(0, 0, 3);
            Plane3D cpSup = new Plane3D(pSup, nSup);

            //cp, p & n are the cut plane (Plane3D) and the position (Point3D) and the normal (Vector3D) of the plane
            g = MeshGeometryHelper.Cut(g, pInf, nInf);        
            g = MeshPlaneCut(g, gOrig, cpInf);
            //g = MeshPlaneCut(g, gOrig, cpSup);

            DiffuseMaterial graySide = new DiffuseMaterial(new SolidColorBrush(Colors.Gray));
            var meshGeo = new GeometryModel3D(g, graySide);
            Model3DGroup finalModel = new Model3DGroup();

            //remove gridlines

            bolusMesh.Content = finalModel;

            //export stl

        private static MeshGeometry3D MeshPlaneCut(MeshGeometry3D meshCut, MeshGeometry3D meshOrig, Plane3D plane)
            //Store the positions on the cut plane
            var segments = MeshGeometryHelper.GetContourSegments(meshOrig, plane.Position, plane.Normal).ToList();

            //assumes largest contour is the outer contour!
            IList<Point3D> vertexPoints = MeshGeometryHelper.CombineSegments(segments, 1e-6).ToList().OrderByDescending(x => x.Count).First();

            //meshCut the polygon opening and add to existing cut mesh
            var builder = new MeshBuilder(false, false);
            builder.Append(meshCut.Positions, meshCut.TriangleIndices);

            MeshGeometry3D mg3D = builder.ToMesh();

            return mg3D;