Cant create Brep intersection

Hello, I try to use the CreateFromBox method and I cant get breps by doing that. This is part my code:

 Dim bbox = brep.GetBoundingBox(True)
 Dim corner = bbox.GetCorners()

For i = dt To corner(4).Z Step dt
            For j = 0 To 3 Step 1

       cor(j) = corner(j).Z + dt1
  Dim bb As New Rhino.Geometry.BoundingBox(corner(0).X, corner(0).Y, corner(0).Z, corner(j).X, corner(j).Y, cor(j))
  Dim b As New Geometry.Box(bb)                                                     
  Dim bbrep = Geometry.Brep.CreateFromBox(b)
  RhinoApp.WriteLine(bbrep.ToString)
  Dim inters As Brep() = Brep.CreateBooleanIntersection(brep, bbrep, 1)

 Next
            dt1 = dt1 + dt
        Next

I get this error: “Value cannot be null.” from Dim inters As Brep() = Brep.CreateBooleanIntersection(brep, bbrep, 1). And RhinoApp.WriteLine(bbrep.ToString) give me “Object reference not set to an instance of an object.”

Can someone tell me how to solve this problem please?
Thanks.

Basic debugging involves testing if you get what you expect after creating new things. See the text "' < Test for null/Nothing > which I injected here and there in your code.

Try that first and ask more if you don’t see where things goes wrong.

Dim bbox = brep.GetBoundingBox(True)
'  < Test for null/Nothing >
Dim corner = bbox.GetCorners()
'  < Test for null/Nothing >

For i = dt To corner(4).Z Step dt
    For j = 0 To 3 Step 1
        cor(j) = corner(j).Z + dt1
        Dim bb As New Rhino.Geometry.BoundingBox(corner(0).X, corner(0).Y, corner(0).Z, corner(j).X, corner(j).Y, cor(j))
        '  < Test for null/Nothing >
        Dim b As New Geometry.Box(bb)
        '  < Test for null/Nothing >
        Dim bbrep = Geometry.Brep.CreateFromBox(b)
        '  < Test for null/Nothing >
        RhinoApp.WriteLine(bbrep.ToString)
        '  < Test for null/Nothing >
        Dim inters As Brep() = Brep.CreateBooleanIntersection(brep, bbrep, 1)   
        '  < Test for null/Nothing >
    Next
    dt1 = dt1 + dt
Next

// Rolf

Yes, I tested it a lot of times. Everything before Dim bbrep = Geometry.Brep.CreateFromBox(b) is fine.

Is the b BoundingBox valid ?

http://developer.rhino3d.com/api/RhinoCommonWin/html/P_Rhino_Geometry_BoundingBox_IsValid.htm

The tests should be in your code. There are no test in your code.

// Rolf

Dim bb As New Rhino.Geometry.BoundingBox(corner(0).X, corner(0).Y, corner(0).Z, corner(j).X, corner(j).Y, cor(j))
If Not bb.IsValid Then
RhinoApp.WriteLine(“INVALID bb”)
Return Rhino.Commands.Result.Failure

            End If
            Dim b As New Geometry.Box(bb)
            If Not b.IsValid Then
                RhinoApp.WriteLine("INVALID b")
                Return Rhino.Commands.Result.Failure

            End If
            Dim bbrep = Geometry.Brep.CreateFromBox(b)
            If bbrep Is Nothing Then
                RhinoApp.WriteLine("INVALID bbrep")
                Return Rhino.Commands.Result.Failure

            End If

The result: “INVALID bbrep”

Then check the values you used in the constructor and see if something is not what you expect

Your debugging skills are improving. :wink:

Try also this before trying to create the bbrep

  if (bbox.IsDegenerate(doc.ModelAbsoluteTolerance) > 0) {
    RhinoApp.WriteLine("the curve's bounding box is degenerate (flat) in at least one direction so a box cannot be created.");

See this page:
http://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Brep_CreateFromBox.htm

// Rolf

The box is not degenerate

Have you done what Rolf told you ?

You might find that the box is really degenerate …

If I read your code correctly, the first iteration of the loop seems to build a box using the same vertex twice and simply changing the second Z … ( but obviously I may be wrong :confused: )

You can easily verify that by printing the values before passing them to the constructor … no ? :slight_smile:

Yes…That was the mistake, plus the second loop…
I think this is correct:
Dim bb As New Rhino.Geometry.BoundingBox(corner(0).X, corner(0).Y, corner(0).Z, corner(2).X, corner(2).Y, cor(j))

Thanks a lot guys