C# script about mesh report not working with solid mesh

Mesh Report Debug V0.gh (1.1 MB)
Hi, I am trying to learn C# scripting, and I’ve created a simple script that generates a mesh report. However, I’m encountering an issue—it doesn’t work with meshes that have no naked edges. I’ve spent hours trying to debug it, but I haven’t had good results.
Could you please help me identify the problem?
Thank you!

The error message shows:

  1. Object reference not set to an instance of an object. (line: 96)

string validText,closedText, manifoldText;
Boolean isOriented,hasBoundary;

if (M.IsValid)
{
  validText = "valid";
}
else
{
  validText = "not valid";
}


if (M.IsClosed)
{
  closedText = "closed";
}
else
{
  closedText = "not closed";
}


if (M.IsManifold(true, out isOriented, out hasBoundary))
{
  manifoldText = "manifold";
}
else
{
  manifoldText = "not manifold";
}



int nbDisjointPieces = M.DisjointMeshCount;

int nNakedEdges = 0;
Polyline [] plines = M.GetNakedEdges();

for (int i = 0; i < plines.Length; i++) {
  nNakedEdges += plines[i].Count - 1;
}



string meshReport = String.Format("Mesh is {0}, {1}, {2}. Mesh has {3} disjoint Pieces, {4} naked edges", validText, closedText, manifoldText, nbDisjointPieces, nNakedEdges);
Report = meshReport;

GetNakedEdges() returns null (not an empty array) for meshes without naked edges. So to iterate over the array of polylines you need to first check if the returned value is null before proceeding with the iteration. Here’s how to do it:

int nNakedEdges = 0;
Polyline[] plines = M.GetNakedEdges();
if (plines != null)
{
  for (int i = 0; i < plines.Length; i++)
    nNakedEdges += plines[i].Count - 1;
}

Ah, finally! Thank you for saving me @Mahdiyar :grin:
Happy new year!

Here is a shorter version of your code:

private void RunScript(Mesh M, ref object Report)
{
  var validText = M.IsValid ? "valid" : "not valid";
  var closedText = M.IsClosed ? "closed" : "not closed";
  bool _;
  var manifoldText = M.IsManifold(true, out _, out _) ? "manifold" : "not manifold";
  var nbDisjointPieces = M.DisjointMeshCount;
  var nNakedEdges = 0;
  var[] plines = M.GetNakedEdges();
  if (plines != null)
    for (int i = 0; i < plines.Length; i++)
      nNakedEdges += plines[i].Count - 1;
   Report = String.Format("Mesh is {0}, {1}, {2}. Mesh has {3} disjoint Pieces, {4} naked edges", validText, closedText, manifoldText, nbDisjointPieces, nNakedEdges);
}

Mesh Report Debug V0.gh (810.6 KB)

1 Like

OMG, this is so elegant!
Thank you so much!