Parametric including/excluding objects in the boolean operations

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
User avatar
nowayno
Posts: 25
Joined: Fri Jun 11, 2021 5:42 pm

Parametric including/excluding objects in the boolean operations

Post by nowayno »

Is it possible somehow, using some values in the Spreadsheet, to manage which object(s) should be included/excluded in the boolean operations?

For the simplest example - I have a box and cylinders on each side to make a hole through all four side walls. But I need to make the only one of them, depending on some calculations in the Spreadsheet. For sure, I can make a zero size for the hole diameter of height. However, if it will be not a cylinder, but a complex object/part - such a trick won't work.

Macro/python solution will also be ok for me.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric including/excluding objects in the boolean operations

Post by TheMarkster »

You would need to use a macro for this, and then manually run the macro to check the calculation result and update the model. Here is a skeleton setup to get you started.

The model is very simple. We have a cube with 4 cylinders, one attached to each of the 4 sides. The cylinders are fused together into a fusion object, which is cut from the cube. We can modify the Fusion property "Shapes", which is a list of document objects (in this case the 4 cylinders), via python macro based on the contents of the spreadsheet.
Snip macro screenshot-678690.png
Snip macro screenshot-678690.png (94.3 KiB) Viewed 1195 times
The spreadsheet only contains one alias, "calc_result". Make "calc_result" the alias of the cell containing your final calculation.
Snip macro screenshot-f8840b.png
Snip macro screenshot-f8840b.png (91.1 KiB) Viewed 1195 times
The macro looks like this:
Snip macro screenshot-cb453d.png
Snip macro screenshot-cb453d.png (117.14 KiB) Viewed 1195 times

Code: Select all

#take result of spreadsheet calculation and modify fusion shapes list based on result
doc = App.ActiveDocument
res = doc.ss.calc_result
#print(res)
c1 = doc.Cylinder
c2 = doc.Cylinder001
c3 = doc.Cylinder002
c4 = doc.Cylinder003

fus = doc.Fusion

#create a python dictionary and add some keys and values
#key is the result of the calculation, e.g. 3.14
#value is the list of cylinders to be used in the boolean fusion

dict = {
1: [c1,c2],
2: [c2,c3],
1.5: [c1,c2,c3],
3.14: [c1,c2,c3,c4]
#add more key/values here
}

#now parse the result to see if there is a matching key
if res in dict:
    fus.Shapes = dict[res]
    doc.recompute()
else:
    FreeCAD.Console.PrintError("Calculation result not in dictionary\n")
Attachments
calc_result.FCStd
(18.98 KiB) Downloaded 27 times
chrisb
Veteran
Posts: 53920
Joined: Tue Mar 17, 2015 9:14 am

Re: Parametric including/excluding objects in the boolean operations

Post by chrisb »

You could use a trick: Make a common of each object in question with a big blockl nuse the common for the booleans. If the object should be excluded, move the object via placement out of the block.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
nowayno
Posts: 25
Joined: Fri Jun 11, 2021 5:42 pm

Re: Parametric including/excluding objects in the boolean operations

Post by nowayno »

TheMarkster wrote: Mon Jul 26, 2021 3:41 pm You would need to use a macro for this, and then manually run the macro to ...
Thanks for the answer and code template. I think about a similar case but was not sure that this is correct. However, my idea was "from the opposite side": at first, create all objects, and cut them all, and then delete those ones, which are not needed in the current variant. It seems to me that the objects deleting is more "safe". And after exporting one desired case, undo deletion to restore full set for the next cases.

However, it will be nice to have property for any object like "Visible in model" or just "Used" and just skip invisible/unused object during model recomputations.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric including/excluding objects in the boolean operations

Post by TheMarkster »

I think I misunderstood your first post. I had thought it to mean you wanted various permutations of the 4 cylinders, e.g. sometimes c1 and c2, sometimes just c1, sometimes maybe all 4. After re-reading I see you only want 1 of them. That actually simplifies things. You can modify the Tool property to point to the desired cylinder and avoid the need for the fusion.

You could make all 4 cuts and then delete the ones you don't want, but it would be more efficient to just make one and modify it as needed, if needed. For example, make the cut using the cylinder you figure will be most commonly used, and then let the macro adjust the tool if needed based on the calculation result. But even if you just make all 4 there's no need to delete the unneeded ones. Just export the desired cut, which can also be done from python.

Leave the python console open and watch it as you do various things in the gui. Most of the time the code for doing the operation is displayed in the python console. This code can be copy/pasted into a macro and modified as needed.
User avatar
nowayno
Posts: 25
Joined: Fri Jun 11, 2021 5:42 pm

Re: Parametric including/excluding objects in the boolean operations

Post by nowayno »

TheMarkster wrote: Tue Jul 27, 2021 2:53 pm I think I misunderstood your first post. I had thought it to mean you wanted various permutations of the 4 cylinders, e.g. sometimes c1 and c2, sometimes just c1, sometimes maybe all 4. After re-reading I see you only want 1 of them.
Actually, I think about both cases. Let me clarify. I have a set of Arduino boards (dimensions). Also, I have different cases of some "external parts" for the boards. F.ex. I may pass wires from outside to inside to the board through the simple hole(s) for the wires, or using a special terminator, or whatever. So, I hoped to collect them all in one model, and later, "build" the box like a puzzle - just defining which parts I would like to combine. Like use "this" board and "this" connector in "those" wall(s). If I'll get a new board, I just add all required dimensions to my list in the spreadsheet and combine for it any variant of the possible puzzles.
User avatar
Shalmeneser
Veteran
Posts: 9474
Joined: Wed Dec 23, 2020 12:04 am
Location: Fr

Re: Parametric including/excluding objects in the boolean operations

Post by Shalmeneser »

You can create all the sketches you would need, but only use some for (simple ?) pocket.
User avatar
papyblaise
Veteran
Posts: 7868
Joined: Thu Jun 13, 2019 4:28 pm
Location: France

Re: Parametric including/excluding objects in the boolean operations

Post by papyblaise »

I do not see the interest: if you make one in three holes, that you modify in five holes (f.e) the one with three holes has disappeared :!:
Post Reply