Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:isbrepbox [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: IsBrepBox Test=====
 +
 +===== C# =====
 +<code c#>
 +public static bool IsBrepBox(Rhino.Geometry.Brep brep)
 +{
 +  const double zero_tolerance = 1.0e-6; // or whatever
 +  bool rc = brep.IsSolid;​
 +  if( rc )
 +    rc = brep.Faces.Count == 6;
 +
 +  var N = new Rhino.Geometry.Vector3d[6];​
 +  for (int i = 0; rc && i < 6; i++)
 +  {
 +    Rhino.Geometry.Plane plane;
 +    rc = brep.Faces[i].TryGetPlane(out plane, zero_tolerance);​
 +    if( rc )
 +    {
 +      N[i] = plane.ZAxis;​
 +      N[i].Unitize();​
 +    }
 +  }
 + 
 +  for (int i = 0; rc && i < 6; i++)
 +  {
 +    int count = 0;
 +    for (int j = 0; rc && j < 6; j++)
 +    {
 +      double dot = Math.Abs(N[i] * N[j]);
 +      if (dot <= zero_tolerance)
 +        continue;
 +      if (Math.Abs(dot - 1.0) <= zero_tolerance) ​
 +        count++;
 +      else
 +        rc = false;
 +    }
 + 
 +    if (rc)
 +    {
 +      if (2 != count)
 +        rc = false;
 +    }
 +  }
 +  return rc;
 +}
 +
 +public static Rhino.Commands.Result TestBrepBox(Rhino.RhinoDoc doc)
 +{
 +  Rhino.DocObjects.ObjRef obj_ref;
 +  var rc = Rhino.Input.RhinoGet.GetOneObject("​Select Brep", true, Rhino.DocObjects.ObjectType.Brep,​ out obj_ref);
 +  if (rc == Rhino.Commands.Result.Success)
 +  {
 +    var brep = obj_ref.Brep();​
 +    if (brep != null)
 +    {
 +      if (IsBrepBox(brep))
 +        Rhino.RhinoApp.WriteLine("​Yes it is a box");
 +      else
 +        Rhino.RhinoApp.WriteLine("​No it is not a box");
 +    }
 +  }
 +  return rc;
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function IsBrepBox(brep As Rhino.Geometry.Brep) As Boolean
 +  Const zero_tolerance As Double = 0.000001 ' or whatever
 +  Dim rc As Boolean = brep.IsSolid
 +  If rc Then
 +    rc = brep.Faces.Count = 6
 +  End If
 +
 +  Dim N = New Rhino.Geometry.Vector3d(5) {}
 +  Dim i As Integer = 0
 +  While rc AndAlso i < 6
 +    Dim plane As Rhino.Geometry.Plane
 +    rc = brep.Faces(i).TryGetPlane(plane,​ zero_tolerance)
 +    If rc Then
 +      N(i) = plane.ZAxis
 +      N(i).Unitize()
 +    End If
 +    i += 1
 +  End While
 +
 +  i = 0
 +  While rc AndAlso i < 6
 +    Dim count As Integer = 0
 +    Dim j As Integer = 0
 +    While rc AndAlso j < 6
 +      Dim dot As Double = Math.Abs(N(i) * N(j))
 +      If dot <= zero_tolerance Then
 +        Continue While
 +      End If
 +      If Math.Abs(dot - 1.0) <= zero_tolerance Then
 +        count += 1
 +      Else
 +        rc = False
 +      End If
 +      j += 1
 +    End While
 +
 +    If rc Then
 +      If 2 <> count Then
 +        rc = False
 +      End If
 +    End If
 +    i += 1
 +  End While
 +  Return rc
 +End Function
 +
 +Public Shared Function TestBrepBox(doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  Dim obj_ref As Rhino.DocObjects.ObjRef = Nothing
 +  Dim rc = Rhino.Input.RhinoGet.GetOneObject("​Select Brep", True, Rhino.DocObjects.ObjectType.Brep,​ obj_ref)
 +  If rc = Rhino.Commands.Result.Success Then
 +    Dim brep = obj_ref.Brep()
 +    If brep IsNot Nothing Then
 +      If IsBrepBox(brep) Then
 +        Rhino.RhinoApp.WriteLine("​Yes it is a box")
 +      Else
 +        Rhino.RhinoApp.WriteLine("​No it is not a box")
 +      End If
 +    End If
 +  End If
 +  Return rc
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +
 +def IsBrepBox(brep):​
 +    zero_tolerance = 1.0e-6 #or whatever
 +    rc = brep.IsSolid
 +    if rc: rc = brep.Faces.Count == 6
 +
 +    N = []
 +    for i in range(6):
 +        if not rc: break
 +        rc, plane = brep.Faces[i].TryGetPlane(zero_tolerance)
 +        if rc:
 +            v = plane.ZAxis
 +            v.Unitize()
 +            N.append(v)
 + 
 +    for i in range(6):
 +        count = 0
 +        for j in range(6):
 +            if not rc: break
 +            dot = abs(N[i] * N[j])
 +            if dot<​=zero_tolerance:​ continue
 +            if abs(dot-1.0)<​=zero_tolerance:​
 +              count += 1
 +            else:
 +              rc = False
 +
 +    if rc:
 +        if 2!=count: rc = False
 +    return rc
 +
 +
 +if __name__=="​__main__":​
 +    rc, objref = Rhino.Input.RhinoGet.GetOneObject("​Select Brep", True, Rhino.DocObjects.ObjectType.Brep)
 +    if rc==Rhino.Commands.Result.Success:​
 +        brep = objref.Brep()
 +        if brep:
 +            if IsBrepBox(brep):​ print "Yes it is a box"
 +            else: print "No it is not a box"
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/isbrepbox.txt ยท Last modified: 2015/09/14 (external edit)