Hello, I try to get some curves from BrepBrep intersection. When I create a box and I try the code everything works fine, but when I try it with a pyramid I get this error: “Index was outside the bounds of the array”. Does anyone know how to fix it? My code is:
Dim s As Surface = bbrep.Faces.Item(5)
ot.Add(s)
Dim sb As Brep = Brep.CreateFromSurface(s)
ot.Add(sb)
Dim supr As Boolean = Intersect.Intersection.BrepBrep(sb, brep, 0.001, crv, pts)
If crv(0) Is Nothing Then ----- here I get the error for crv(0)
RhinoApp.WriteLine("INVALID crv")
Return Rhino.Commands.Result.Failure
End If
cd.AddCurve(crv(0))
Do not access the array before testing if any intersction occured. At that point in time (when you test on an element of the expected result array) you can no longer avoid an error, because you might not have received any array at all! (this is why you get an error sometimes)
And exactly that is what the Boolean on the row above is good for - namely for testing if you get any array at all in return from the intersection function.
Therefore try like this instead:
Dim IntersectWasFound As Boolean = Intersect.Intersection.BrepBrep(sb, brep, 0.001, crv, pts)
If Not IntersectWasFound Then
' Msg about the fact that no intersection occured.
Dim supr As Boolean = Intersect.Intersection.BrepBrep(sb, brep, 0.01, crv, pts) If Not supr Then RhinoApp.WriteLine(" No intersection") Return Rhino.Commands.Result.Failure End If
I have not tested it but there are more intersections kinds than BrepBrep (probably for a good reason) so I would suggest that BrepBrep with a zero (0) height BoundingBox would return unpredictable results…
You have also BrepSurface, and BrepPlane alternatives which probably gives more predictable results.
Here’s the entire list of intersect options. Try until it works as expected.