Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:booleandifference [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Boolean Difference =====
 +
 +===== C# =====
 +<code c#>
 +public static Rhino.Commands.Result BooleanDifference(Rhino.RhinoDoc doc)
 +{
 +  Rhino.Commands.Result rc;
 +  Rhino.DocObjects.ObjRef[] objrefs;
 +  rc = Rhino.Input.RhinoGet.GetMultipleObjects("​Select first set of polysurfaces",​
 +    false, Rhino.DocObjects.ObjectType.PolysrfFilter,​ out objrefs);
 +  if (rc != Rhino.Commands.Result.Success)
 +    return rc;
 +  if (objrefs == null || objrefs.Length < 1)
 +    return Rhino.Commands.Result.Failure;​
 +
 +  List<​Rhino.Geometry.Brep>​ in_breps0 = new List<​Rhino.Geometry.Brep>​();​
 +  for (int i = 0; i < objrefs.Length;​ i++)
 +  {
 +    Rhino.Geometry.Brep brep = objrefs[i].Brep();​
 +    if (brep != null)
 +      in_breps0.Add(brep);​
 +  }
 +
 +  doc.Objects.UnselectAll();​
 +  rc = Rhino.Input.RhinoGet.GetMultipleObjects("​Select second set of polysurfaces",​
 +    false, Rhino.DocObjects.ObjectType.PolysrfFilter,​ out objrefs);
 +  if (rc != Rhino.Commands.Result.Success)
 +    return rc;
 +  if (objrefs == null || objrefs.Length < 1)
 +    return Rhino.Commands.Result.Failure;​
 +
 +  List<​Rhino.Geometry.Brep>​ in_breps1 = new List<​Rhino.Geometry.Brep>​();​
 +  for (int i = 0; i < objrefs.Length;​ i++)
 +  {
 +    Rhino.Geometry.Brep brep = objrefs[i].Brep();​
 +    if (brep != null)
 +      in_breps1.Add(brep);​
 +  }
 +
 +  double tolerance = doc.ModelAbsoluteTolerance;​
 +  Rhino.Geometry.Brep[] breps = Rhino.Geometry.Brep.CreateBooleanDifference(in_breps0,​ in_breps1, tolerance);
 +  if (breps.Length < 1)
 +    return Rhino.Commands.Result.Nothing;​
 +  for (int i = 0; i < breps.Length;​ i++)
 +    doc.Objects.AddBrep(breps[i]);​
 +  doc.Views.Redraw();​
 +  return Rhino.Commands.Result.Success;​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function BooleanDifference(ByVal doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  Dim rc As Rhino.Commands.Result
 +  Dim objrefs As Rhino.DocObjects.ObjRef()
 +  rc = Rhino.Input.RhinoGet.GetMultipleObjects("​Select first set of polysurfaces",​ False, Rhino.DocObjects.ObjectType.PolysrfFilter,​ objrefs)
 +  If rc <> Rhino.Commands.Result.Success Then
 +    Return rc
 +  End If
 +  If objrefs Is Nothing OrElse objrefs.Length < 1 Then
 +    Return Rhino.Commands.Result.Failure
 +  End If
 +
 +  Dim in_breps0 As New List(Of Rhino.Geometry.Brep)()
 +  For i As Integer = 0 To objrefs.Length - 1
 +    Dim brep As Rhino.Geometry.Brep = objrefs(i).Brep()
 +    If brep IsNot Nothing Then
 +      in_breps0.Add(brep)
 +    End If
 +  Next
 +
 +  doc.Objects.UnselectAll()
 +  rc = Rhino.Input.RhinoGet.GetMultipleObjects("​Select second set of polysurfaces",​ False, Rhino.DocObjects.ObjectType.PolysrfFilter,​ objrefs)
 +  If rc <> Rhino.Commands.Result.Success Then
 +    Return rc
 +  End If
 +  If objrefs Is Nothing OrElse objrefs.Length < 1 Then
 +    Return Rhino.Commands.Result.Failure
 +  End If
 +
 +  Dim in_breps1 As New List(Of Rhino.Geometry.Brep)()
 +  For i As Integer = 0 To objrefs.Length - 1
 +    Dim brep As Rhino.Geometry.Brep = objrefs(i).Brep()
 +    If brep IsNot Nothing Then
 +      in_breps1.Add(brep)
 +    End If
 +  Next
 +
 +  Dim tolerance As Double = doc.ModelAbsoluteTolerance
 +  Dim breps As Rhino.Geometry.Brep() = Rhino.Geometry.Brep.CreateBooleanDifference(in_breps0,​ in_breps1, tolerance)
 +  If breps.Length < 1 Then
 +    Return Rhino.Commands.Result.[Nothing]
 +  End If
 +  For i As Integer = 0 To breps.Length - 1
 +    doc.Objects.AddBrep(breps(i))
 +  Next
 +  doc.Views.Redraw()
 +  Return Rhino.Commands.Result.Success
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import scriptcontext
 +
 +def BooleanDifference():​
 +    filter = Rhino.DocObjects.ObjectType.PolysrfFilter
 +    rc, objrefs = Rhino.Input.RhinoGet.GetMultipleObjects("​Select first set of polysurfaces",​ False, filter)
 +    if rc != Rhino.Commands.Result.Success:​ return rc
 +    if not objrefs: return Rhino.Commands.Result.Failure
 +
 +    in_breps0 = []
 +    for objref in objrefs:
 +        brep = objref.Brep()
 +        if brep: in_breps0.append(brep)
 +
 +    scriptcontext.doc.Objects.UnselectAll()
 +    rc, objrefs = Rhino.Input.RhinoGet.GetMultipleObjects("​Select second set of polysurfaces",​ False, filter)
 +    if rc != Rhino.Commands.Result.Success:​ return rc
 +    if not objrefs: return Rhino.Commands.Result.Failure
 +
 +    in_breps1 = []
 +    for objref in objrefs:
 +        brep = objref.Brep()
 +        if brep: in_breps1.append(brep)
 +
 +    tolerance = scriptcontext.doc.ModelAbsoluteTolerance
 +    breps = Rhino.Geometry.Brep.CreateBooleanDifference(in_breps0,​ in_breps1, tolerance)
 +    if not breps: return Rhino.Commands.Result.Nothing
 +    for brep in breps: scriptcontext.doc.Objects.AddBrep(brep)
 +    scriptcontext.doc.Views.Redraw()
 +    return Rhino.Commands.Result.Success
 +
 +if __name__=="​__main__":​
 +    BooleanDifference()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/booleandifference.txt ยท Last modified: 2015/09/14 (external edit)