Python function for Union

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
orxshi
Posts: 21
Joined: Thu Dec 19, 2019 4:50 pm

Python function for Union

Post by orxshi »

What is the python function for Part -> Union? I managed to union/fuse two shells in GUI but cannot find corresponding python function?
chrisb
Veteran
Posts: 54207
Joined: Tue Mar 17, 2015 9:14 am

Re: Python function for Union

Post by chrisb »

The function is Fusion. Most commands are logged in Python console, so it's always worth a try to do something from the GUI while watching the console.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
orxshi
Posts: 21
Joined: Thu Dec 19, 2019 4:50 pm

Re: Python function for Union

Post by orxshi »

But Python complains that

module 'Part' has no attribute 'Fusion'

It also complains if I try MultiFuse instead of Fusion
chrisb
Veteran
Posts: 54207
Joined: Tue Mar 17, 2015 9:14 am

Re: Python function for Union

Post by chrisb »

Sorry for being short/wrong. Let's assume you have a Box and a Cylinder in your document. Then you can do the following:

Code: Select all

doc = App.ActiveDocument
doc.addObject("Part::MultiFuse","Fusion")
doc.Fusion.Shapes = [doc.Box,doc.Cylinder]
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Python function for Union

Post by vocx »

orxshi wrote: Fri Dec 20, 2019 5:33 pm What is the python function for Part -> Union? I managed to union/fuse two shells in GUI but cannot find corresponding python function?
Keep the Python console open. Then perform the actions that you want in the graphical interface, that is, grab two objects, and press Part_Union. You will see the commands that produce that object recorded in the Python console. Now you can take that code and adapt it to your needs. This is what chrisb did. It's the same process that is done when you record macros.

It's the best way to learn Python scripting with FreeCAD. Just perform operations with the buttons, see the generated code, and then repeat those same steps.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Python function for Union

Post by DeepSOIC »

in python, it's more commonly needed to operate on shapes, without creating an object in a project for every step. Then, use fuse method of Part.Shape objects :

Code: Select all

sh1 = Part.makeCylinder(10,3)
sh2 = Part.makeSphere(5)
fused = sh1.fuse(sh2)
Part.show(fused)
If you do want to have every step as a project object, as if you made it all in GUI, see chrisb's answer above. Just watch and bend what you see printed to py console when you do the gui stuff.
freedman
Veteran
Posts: 3466
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: Python function for Union

Post by freedman »

Since we are on this topic, could you add either approach for a loft. I can't get the correct keywords.
Thank you
Post Reply