@hericson wrote:
I answer myself. I don't know if this code is bullet proof but it works in my tests.
private static bool IsBox(Autodesk.AutoCAD.DatabaseServices.Solid3d solid)
{
// A box have a single Complex, 6 faces (bottom, top, 4 sides) and 12 edges
using (Brep brep = new Brep(solid))
{
if (brep.Complexes.Count() != 1)
return false;
if (brep.Faces.Count() != 6)
return false;
if (brep.Edges.Count() != 12)
return false;
return
true;
}
}
A wedge have 1 Complex, 5 Faces and 9 Edges.
A pyramid have 1 Complex, 5 Faces and 8 Edges.
Very often ‘bulletproof’ is overkill and ‘serves the purpose’ can carry the day. The code snippet above is not bullet proof but could easily, and with great efficiency, work well with any file your workflow would need to process.
From my perspective, though, making code more bulletproof can be worth the effort. It’s always better to have more robust code, and the subtleties of the APIs can be brought into focus whilst dealing with a halted debugger.
See the file for objects that may cause hiccups. I’ve posted sample code that can handle much of the geometric oddities, but I wouldn’t even call that ‘bulletproof’.
private static bool IsBox(Brep solidBrep)
{
int EdgeCount = 0;
int FaceCount = 0;
try
{
FaceCount = solidBrep.Faces.Count();
EdgeCount = solidBrep.Edges.Count();
}
catch(Autodesk.AutoCAD.BoundaryRepresentation.Exception)
{
return false;
}
if (solidBrep.Complexes.Count() != 1) return false;
if (FaceCount != 6) return false;
if (EdgeCount != 12) return false;
BrepFaceCollection Faces = solidBrep.Faces;
bool isFirst = true;
Vector3d TestLin;
Vector3d FirstLin = new Vector3d();
foreach (Autodesk.AutoCAD.BoundaryRepresentation.Face fce in Faces)
{
ExternalBoundedSurface ebs = fce.Surface as ExternalBoundedSurface;
CurveBoundary[] cb = ebs.GetContours();
if (cb.Count() != 1) return false;
if(cb[0].NumElements !=4) return false;
PlanarEntity PlnEnt = ebs.BaseSurface as PlanarEntity;
if (PlnEnt == null) return false;
TestLin = PlnEnt.Normal;
if (isFirst)
{
FirstLin = TestLin;
isFirst = false;
continue;
}
if (!(FirstLin.IsParallelTo(TestLin) ^ FirstLin.IsPerpendicularTo(TestLin)))
{
return false;
}
}
return true;
}
************************************************************
May your cursor always snap to the location intended.