"Brep.CreateBooleanDifference" returns null

The first set of breps has intersection with the second set of breps,but it still returns null.May this api go well on some conditions? And what’s the conditions?I find this api goes well sometime,and sometime goes wrong.I’m so confused.

Just as with _BooleanDifference command the Brep.CreateBooleanDifference Method follows the same constraints. The API call not returning anything means most likely that your input is not correct.

If you find a case where _BooleanDifference works but the API call not, then please provide the BReps and code you are using to create the boolean difference.

boolean_difference.3dm (260.9 KB)
On the bottom the object has goes well,but on the top the object goes wrong.And the codes is same.
The code shows as the pic at the below.

On the top the brep has transformed ,and on the bottom the brep has not transformed.There is an only difference between them.

Hi @duzhengjie,

The top object is not a Brep but a Block that contains a Brep. Explode the block first. Then take the resulting Brep and use it as input.

– Dale

Sorry,the file I did was some wrong.I upload a new file,this file is correct.Please check it again,thanks!
boolean_difference.3dm (273.0 KB)

Assuming I understood correctly what booleans how this appears to be working just fine. It still works with using ModelAbsoluteTolerance in the place of 0.001:

body.txt (19.1 KB)
parameter.txt (19.1 KB)

var body = JsonConvert.DeserializeObject<Brep>(File.ReadAllText(@"D:\Desktop\body.txt"));
                var parameters = JsonConvert.DeserializeObject<Brep[]>(File.ReadAllText(@"D:\Desktop\parameter.txt"));
                foreach (var item in parameters)
                var r = Brep.CreateBooleanDifference(new[] { body }, parameters, 1);

Boolean result should have value but actually returns null, works fine with GrassHopper and _BooleanDifference command

I see indeed no result with the free floating geometries when used straight after parsing from JSON. But it works when using the geometry added to the document:

import Rhino
import scriptcontext as sc
import System.Collections.Generic as scg

bodylist = scg.List[Rhino.Geometry.Brep]()
paramlist = scg.List[Rhino.Geometry.Brep]()

co = Rhino.Runtime.CommonObject

with open("/Users/nathan/Downloads/body.txt") as f:
    l = f.readlines()[0]
    body = co.FromJSON(l)
    body_id = sc.doc.Objects.Add(body)
    body_object = sc.doc.Objects.FindId(body_id)
    body_object.Name = "A"

with open("/Users/nathan/Downloads/parameter.txt") as f2:
    l2 = f2.readlines()[0][1:-2]
    param = co.FromJSON(l2)
    param_id = sc.doc.Objects.Add(param)
    param_object = sc.doc.Objects.FindId(param_id)
    param_object.Name = "B"

result = Rhino.Geometry.Brep.CreateBooleanDifference(
# OR:
#result = Rhino.Geometry.Brep.CreateBooleanDifference(
#    bodylist,
#    paramlist,
#    sc.doc.ModelAbsoluteTolerance

print "Brep.CreateBooleanDifference using geometry directly from JSON:", result

alist = scg.List[Rhino.Geometry.Brep]()
blist = scg.List[Rhino.Geometry.Brep]()

Alist = [o.Geometry for o in sc.doc.Objects if o.Name=='A']
Blist = [o.Geometry for o in sc.doc.Objects if o.Name=='B']
for o in Alist:
for o in Blist:

Results = Rhino.Geometry.Brep.CreateBooleanDifference(
           Alist, Blist, sc.doc.ModelAbsoluteTolerance)

print "Brep.CreateBooleanDifference using geometry added to document:", Results



Brep.CreateBooleanDifference using geometry directly from JSON: None
Brep.CreateBooleanDifference using geometry added to document: Array[Brep]((<Rhino.Geometry.Brep object at 0x0000000000000051 [Rhino.Geometry.Brep]>))

I’ll ask around why this is happening.

It’s true, it’s amazing. However, in some cases, our current approach is fine. Also, our current design cannot support the modifications you provided because we use Rhino Compute.

I am merely investigating what is happening, I was not proposing this as a workaround or modification for your script!

@chuck can you please have a look to see what the difference is between the geometry before adding to the document, and the geometry after adding to the document?

Hi @ken_zhang,

Depending on how you construct your Brep, you might need to do a few things to make it more compatible with our Boolean solver.

Please review this and let me know if you have any questions.

– Dale

nice! Working fine

Sorry, I tried it and found that it didn’t completely solve the problem. If I read it directly from the json and then call brep.Faces.SplitKinkyFaces it works fine. However, if I brep.Faces.SplitKinkyFaces on a Brep before saving it into json, I hope I don’t need to brep.Faces.SplitKinkyFaces again when reading, but it doesn’t seem to be the case? Will this problem exist? Because I’m currently not completely sure that I actually call brep.Faces.SplitKinkyFaces completely before serializing to json.

But I did make this call in the code. I can’t make sure that there are some places where I missed writing, so I would like to ask if the data read from json must be brep.Faces.SplitKinkyFaces first?

It’s Ok, The problem with the boolean operation itself seems to be solved, thanks