Mesh.CreateFromBrep not capturing Z plane

Hello,

We are developing a plugin for Rhino 7 using RhinoCommon in C#. We’re trying to meshify BREP’s using the Mesh.CreateFromBrep function. When using Surface → plane and then extruding the surface to form a closed polysurface, Mesh.CreateFromBrep only captures the X-Y plane of vertices while keeping the Z plane as 0s. We printed out the mesh vertices to confirm this.

case ObjectType.Brep:
        var brep = rObj.Geometry as Brep;
        return Mesh.CreateFromBrep(brep, MeshingParameters.Minimal);

Here are the outputted vertices from a simple polysurface turned into a mesh by Mesh.CreateFromBrep:

SmallClosedPolysurface.3dm (38.4 KB)

8,-17,0

8,-4.25,0

8,8.5,0

8,21.25,0

8,34,0

13.5,-17,0

13.5,-4.25,0

13.5,8.5,0

13.5,21.25,0

13.5,34,0

19,-17,0

19,-4.25,0

19,8.5,0

19,21.25,0

19,34,0

24.5,-17,0

24.5,-4.25,0

24.5,8.5,0

24.5,21.25,0

24.5,34,0

30,-17,0

30,-4.25,0

30,8.5,0

30,21.25,0

30,34,0

Welcome to the forum! Could you please post a small code sample that reproduces the problem.

Here is our code for showing the vertices:

Mesh[] meshes = ObjectUtility.GetGeometryObjectMeshes(rObj);
AddGeometryObject(rObj, meshes);

// output meshes as a text file for debugging purposes
var mesh = meshes[0];
var meshVertices = mesh.Vertices.ToPoint3dArray();
// write to text file
var path = @"C:\mesh.txt";
using (StreamWriter sw = File.CreateText(path))
{
    foreach (var vertex in meshVertices)
    {
        sw.WriteLine(vertex);
    }
}

Where GetGeometryObjectMeshes is defined as follows:

 public static Mesh[] GetGeometryObjectMeshes(RhinoObject rObj)
 {
     switch (rObj.ObjectType)
     {
         case ObjectType.Mesh:
             return rObj.GetMeshes(MeshType.Any);
         case ObjectType.Extrusion:
             var extrusion = rObj as ExtrusionObject;
             extrusion.CreateMeshes(MeshType.Default, MeshingParameters.Default, true);
             return extrusion.GetMeshes(MeshType.Default);
         case ObjectType.Brep:
             var brep = rObj.Geometry as Brep;
             return Mesh.CreateFromBrep(brep, MeshingParameters.Minimal);
         default:
             return null;
     }
 }

And the rhino object is defined in the .3dm file here:
SmallClosedPolysurface.3dm (38.4 KB)

rObj is taken from the Rhino document like so:

 // Use filter to get only objects that can be parsed. Hidden object are on any layers that are not visible in rhino.
 var objectFilter = new ObjectEnumeratorSettings()
 {
     HiddenObjects = false,
     ObjectTypeFilter =
         ObjectType.Mesh
         & ObjectType.Brep
         & ObjectType.Extrusion
         & ObjectType.InstanceReference
         & ObjectType.Surface
 };
 // Add instances to CrDoc World's instance list from all objects in model.
 var rhinoObjects = doc.Objects.GetObjectList(objectFilter);

Where doc is the RhinoDoc document object.

it should have vertices above the 0 z-plane but it is instead flattened.

Here is a complete RhinoCommon plugin code to reproduce this behavior:

using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace MyRhinoPlugin1
{
    public class MyRhinoPlugin1Command : Command
    {
        public MyRhinoPlugin1Command()
        {
            // Rhino only creates one instance of each command class defined in a
            // plug-in, so it is safe to store a refence in a static property.
            Instance = this;
        }

        ///<summary>The only instance of this command.</summary>
        public static MyRhinoPlugin1Command Instance { get; private set; }

        ///<returns>The command name as it appears on the Rhino command line.</returns>
        public override string EnglishName => "testMesh";

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // Use filter to get only objects that can be parsed. Hidden object are on any layers that are not visible in rhino.
            var objectFilter = new ObjectEnumeratorSettings()
            {
                HiddenObjects = false,
                ObjectTypeFilter =
                    ObjectType.Mesh
                    & ObjectType.Brep
                    & ObjectType.Extrusion
                    & ObjectType.InstanceReference
                    & ObjectType.Surface
            };
            // Add instances to CrDoc World's instance list from all objects in model.
            var rhinoObjects = doc.Objects.GetObjectList(objectFilter);

            // Get the first object in the list
            var rObj = rhinoObjects.ToList()[0];

            Mesh[] meshes = GetGeometryObjectMeshes(rObj);

            // output meshes as a text file for debugging purposes
            var mesh = meshes[0];
            var meshVertices = mesh.Vertices.ToPoint3dArray();
            // write to text file
            var path = @"C:\Users\username\Documents\mesh.txt";
            using (StreamWriter sw = File.CreateText(path))
            {
                foreach (var vertex in meshVertices)
                {
                    sw.WriteLine(vertex);
                }
            }


            return Result.Success;
        }

        public static Mesh[] GetGeometryObjectMeshes(RhinoObject rObj)
        {
            switch (rObj.ObjectType)
            {
                case ObjectType.Mesh:
                    return rObj.GetMeshes(MeshType.Any);
                case ObjectType.Extrusion:
                    var extrusion = rObj as ExtrusionObject;
                    extrusion.CreateMeshes(MeshType.Default, MeshingParameters.Default, true);
                    return extrusion.GetMeshes(MeshType.Default);
                case ObjectType.Brep:
                    var brep = rObj.Geometry as Brep;
                    return Mesh.CreateFromBrep(brep, MeshingParameters.Minimal);
                default:
                    return null;
            }
        }
    }
}

And use this file
SmallClosedPolysurface.3dm (38.4 KB)

Hi @Wallace_Scott,

I have not look into your code yet. But if you’re just trying to get meshes from Rhino objects, then it is far easier to use RhinoObject.MeshObjects then to determine the geometry type and then cook up meshes based on that.

There is also RhinoObject.GetRenderMeshes if you want the meshes like what Rhino’s shaded display mode uses.

– Dale

Thank you @dale, using MeshObjects fixed the issue.