SubD to BRep Invalid Issue by Using Rhino.Inside C#

I am trying to convert a mesh to subd to brep. I am getting below error while converting subd to brep.

Without using brep.compact():

ON_Brep.m_L[16203] loop has trim vertex mismatch:
m_T[loop.m_ti[0]=64812].m_vi[1] = 16998 != m_T[loop.m_ti[1]=64813].m_vi[0]=16999.

With using brep.compact():

ON_BrepEdge.m_vi[0] = -1 (should be >= 0 )
brep.m_E[11189] edge is not valid.
edge is not a valid.
ON_Brep.m_E[11189] is invalid.

Additional Information:

  1. My native mesh is valid but it’s a bad mesh. When I convert to subd then the subd become invalid then I am trying to convert it into brep then I am getting the invalid polysurface Using Rhino.Inside Api C#
  2. While doing this steps manually in Rhino UI, my native mesh is valid but a bad mesh, when I convert to subd then the subd become invalid but if I convert subd to brep then the brep become valid. So the manual approach working good, only Rhino.Inside api approach is failing.

Thanks in Advance

It looks like you’re using code, so I’ve set the category to Developer. It will certainly help if you post your code and the mesh that is giving you problems.