Mesh Topology Error C#

I have created a MeshBox Using its 8 Vertices. That I have in specific order.
But When I try to access the VertexTopology again in another component. it shows a different order of Points than the one I set it with.

Here is the Code

  {

    Plane mPlane = new Plane(basePlane, Plane.WorldXY.XAxis, Plane.WorldXY.YAxis);
    
    ICModule myModule = new ICModule(mPlane, width, depth, height);
    Mesh iModule = myModule.CreateBaseModule();

    A = iModule;
   // B = mPlane2;

  }

  // <Custom additional code> 

  public class ICModule
  {
    public Plane BasePlane = Plane.WorldXY;
    public double Width = 1.0;
    public double Depth = 1.0;
    public double Height = 1.0;

    //Create default constructor method/function
    public ICModule(double width, double depth, double height)
    {
      Width = width;
      Depth = depth;
      Height = height;
    }

    //Create method/function overload to be used in different situation
    //We can create more functions to make modules using the assigned variables with different inputs
    public ICModule(Plane basePlane, double width, double depth, double height)
    {
      BasePlane = basePlane;
      Width = width;
      Depth = depth;
      Height = height;
    }   

    public Mesh CreateBaseModule()
    {
      //Generate the Mesh Vertices
      List<Point3d> meshVertices = new List<Point3d>();
      {
        meshVertices.Add(BasePlane.Origin);                                                                                         
        meshVertices.Add(BasePlane.Origin + BasePlane.XAxis * Width);                                                               
        meshVertices.Add(BasePlane.Origin + BasePlane.XAxis * Width + BasePlane.YAxis * Depth);                                     
        meshVertices.Add(BasePlane.Origin + BasePlane.YAxis * Depth);                                                               
        meshVertices.Add(BasePlane.Origin + BasePlane.ZAxis * Height);                                                              
        meshVertices.Add(BasePlane.Origin + BasePlane.XAxis * Width + BasePlane.ZAxis * Height);                                    
        meshVertices.Add(BasePlane.Origin + BasePlane.XAxis * Width + BasePlane.YAxis * Depth + BasePlane.ZAxis * Height);          
        meshVertices.Add(BasePlane.Origin + BasePlane.YAxis * Depth + BasePlane.ZAxis * Height);                                    
      }
      Mesh outModule = Mesh.CreateFromBox(meshVertices, 1, 1, 1);


      return outModule;
    }
  }

  // </Custom additional code> 
}

Here in the image you can see the vertex topology on right(Expectation) on left( actual)

Any Idea how to fix it.

Attached is the Script.MeshTopologyExercise.gh (5.9 KB)
MeshTopologyExercise.3dm (36.7 KB)

Hi @su.lwpac,

If you want to control the order of mesh vertices, then you’ll want to create the mesh yourself. Mesh.CreateFromBoxis somewhat of a black box, so it won’t provide you the control you want.

Here is an alternative method:

public Mesh CreateBaseModule()
{
  var plane = BasePlane;
  var rect0 = new Rectangle3d(plane, Width, Depth);

  plane.Origin = plane.Origin + (plane.ZAxis * Height);
  var rect1 = new Rectangle3d(plane, Width, Depth);

  var mesh = new Mesh();

  mesh.Vertices.Add(rect0.Corner(0));
  mesh.Vertices.Add(rect0.Corner(1));
  mesh.Vertices.Add(rect0.Corner(2));
  mesh.Vertices.Add(rect0.Corner(3));
  mesh.Vertices.Add(rect1.Corner(0));
  mesh.Vertices.Add(rect1.Corner(1));
  mesh.Vertices.Add(rect1.Corner(2));
  mesh.Vertices.Add(rect1.Corner(3));

  mesh.Faces.AddFace(0, 1, 5, 4);
  mesh.Faces.AddFace(1, 2, 6, 5);
  mesh.Faces.AddFace(2, 3, 7, 6);
  mesh.Faces.AddFace(3, 0, 4, 7);
  mesh.Faces.AddFace(0, 3, 2, 1);
  mesh.Faces.AddFace(4, 5, 6, 7);

  mesh.Normals.ComputeNormals();

  return mesh;
}

– Dale

1 Like

Hi Dale,
Yes I was looking for the way to control the order.
That works awesome.
Thanks!