Boolean operation

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Klikest
Posts: 7
Joined: Fri May 27, 2022 7:07 pm

Boolean operation

Post by Klikest »

Hi everyone!

How can I do a Boolean operation with keep instrument body (second operand) like in fusion 360 or any other cad?
https://wampi.ru/image/RZFWNRJ
Is the only way out is to make a copy of the body?

I will be glad of any help
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Boolean operation

Post by edwilliams16 »

Since FreeCAD obviously can do this, you'll have to be more explicit about what code you are trying. What workbench are you using?
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Boolean operation

Post by onekk »

Welcome.

Please remember also to tell us what version of FreeCAD are you susing, follow:

http://forum.freecadweb.org/viewtopic.php?f=3&t=2264

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Klikest
Posts: 7
Joined: Fri May 27, 2022 7:07 pm

Re: Boolean operation

Post by Klikest »

Hello. Sorry, I'm new to FreeCAD, and I'm still not good at it.

I'm using Version: 0.19.24366 (Git) and I work only on standard workbenches (Part and Part design)

I am a student and as part of my educational project I am writing a macro that simulates the cutting process of a cnc grinding machine. I am creating a model of a workpiece and a tool (Grinding wheel). The tool discretely moves along the specified coordinates, and at each step of the combat operation subtracts a circle from the workpiece.

I need the tool to remain in the scene after the boolean operation, and I don't have to constantly create it again. How can I do this?
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Boolean operation

Post by onekk »

As here we are dealing with macros, describing thing without some code is hard.

As programmers "speaks through code" probably some code will be more helpful than a bunch of word.

Usually answers are not too far from the what you have already done, so if its possible to post something, maybe a simpler code that explain your problem, probably someone will drive you in the right direction.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Klikest
Posts: 7
Joined: Fri May 27, 2022 7:07 pm

Re: Boolean operation

Post by Klikest »

My problem is in boolean operation:

App.activeDocument().addObject("Part::Cut","Cut")
App.activeDocument().Cut.Base = App.activeDocument().Body
App.activeDocument().Cut.Tool = App.activeDocument().Body001

After performing the operation, the second operand (tool or Body001) is deleted, and I need it to remain, because this body will still be used further.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Boolean operation

Post by openBrain »

Klikest wrote: Sun May 29, 2022 7:25 pm My problem is in boolean operation:

App.activeDocument().addObject("Part::Cut","Cut")
App.activeDocument().Cut.Base = App.activeDocument().Body
App.activeDocument().Cut.Tool = App.activeDocument().Body001

After performing the operation, the second operand (tool or Body001) is deleted, and I need it to remain, because this body will still be used further.
It's eventually hidden, but surely not deleted. In GUI, you'll find the objects nested under the Cut in the Tree view.
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Boolean operation

Post by edwilliams16 »

Works for me:
Screen Shot 2022-05-29 at 9.37.26 AM.png
Screen Shot 2022-05-29 at 9.37.26 AM.png (32.41 KiB) Viewed 951 times
You can of course keep a reference to the objects by assigning them to a variable.

Code: Select all

App.activeDocument().addObject("Part::Cut","Cut")
body = App.activeDocument().Body
App.activeDocument().Cut.Base = body
tool = App.activeDocument().Body001
App.activeDocument().Cut.Tool = tool
App.activeDocument().recompute()
Attachments
keeptool.FCStd
(11.59 KiB) Downloaded 13 times
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Boolean operation

Post by onekk »

Klikest wrote: Sun May 29, 2022 7:25 pm My problem is in boolean operation:

App.activeDocument().addObject("Part::Cut","Cut")
App.activeDocument().Cut.Base = App.activeDocument().Body
App.activeDocument().Cut.Tool = App.activeDocument().Body001

After performing the operation, the second operand (tool or Body001) is deleted, and I need it to remain, because this body will still be used further.
This is not a problem, is the way boolean operation will work.

You are using DocumentObjects and operate on them using "Part::Cut" DocumentObject, and this is the way it works.

You could ever copy the object and use a copy to do the cut so is the copy that go under DocumentObject "Cut", or use another approach:

I'm assuming you have two objects referred as Body and Body001 as "Names"

Code: Select all

doc = App.activeDocument()

# we need toposhapes so we are extracting using <.Shape>
body = doc.Body.Shape
tool = doc.Body001.Shape

cut = body.cut(tool).removeSplitter()

cut_dobj = doc.addObject("Part::FeaturePython", "boolean_cut")
cut_dobj.Shape = cut

doc.Recompute()
This way you have original objects and the cut as different things, cut is a standalone object as you have assigned the TopoShape to his Shape Property.

removeSplitter() is equivalent to put Refine = True in the "Part::Cut" DocumentObject.

NOTE: the code above is not tested, as I've written by hand so probable there are bugs or mistyping, iof anything go wrong please post an example the code, and I will make it work.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Klikest
Posts: 7
Joined: Fri May 27, 2022 7:07 pm

Re: Boolean operation

Post by Klikest »

Thank you so much!
Post Reply