part design boolean operation in macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

part design boolean operation in macro

Post by smktec »

is it possible to perform part design boolean operation in a macro?
imm
Posts: 250
Joined: Wed Nov 03, 2021 1:00 pm

Re: part design boolean operation in macro

Post by imm »

This would be a starting point.

Code below is from PartDesign TestBoolean.py

Code: Select all

    def testBooleanCutCase(self):
        self.Body = self.Doc.addObject('PartDesign::Body','Body')
        self.Box = self.Doc.addObject('PartDesign::AdditiveBox','Box')
        self.Box.Length=10
        self.Box.Width=10
        self.Box.Height=10
        self.Body.addObject(self.Box)
        self.Doc.recompute()
        self.Body001 = self.Doc.addObject('PartDesign::Body','Body001')
        self.Box001 = self.Doc.addObject('PartDesign::AdditiveBox','Box001')
        self.Box001.Length=10
        self.Box001.Width=10
        self.Box001.Height=10
        self.Box001.Placement.Base = App.Vector(-5,0,0)
        self.Body001.addObject(self.Box001)
        self.Doc.recompute()
        self.BooleanCut = self.Doc.addObject('PartDesign::Boolean','BooleanCut')
        self.Body001.addObject(self.BooleanCut)
        self.Doc.recompute()
        self.BooleanCut.setObjects([self.Body,])
        self.BooleanCut.Type = 1
        self.Doc.recompute()
        self.assertAlmostEqual(self.BooleanCut.Shape.Volume, 500)
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: part design boolean operation in macro

Post by mario52 »

Hi

the code returned (PartDesign) by fuse box+cylinder in python console

Code: Select all

>>> # Gui.Selection.addSelection('Unnamed','Body')
>>> # Gui.Selection.addSelection('Unnamed','Body001')
>>> ### Begin command PartDesign_Boolean
>>> App.getDocument('Unnamed').getObject('Body001').newObject('PartDesign::Boolean','Boolean')
>>> App.getDocument('Unnamed').getObject('Boolean').addObjects([App.getDocument('Unnamed').getObject('Body'),])
>>> App.ActiveDocument.recompute()
>>> App.getDocument('Unnamed').getObject('Boolean').ViewObject.ShapeColor=getattr(App.getDocument('Unnamed').getObject('Cylinder').getLinkedObject(True).ViewObject,'ShapeColor',App.getDocument('Unnamed').getObject('Boolean').ViewObject.ShapeColor)
>>> App.getDocument('Unnamed').getObject('Boolean').ViewObject.LineColor=getattr(App.getDocument('Unnamed').getObject('Cylinder').getLinkedObject(True).ViewObject,'LineColor',App.getDocument('Unnamed').getObject('Boolean').ViewObject.LineColor)
>>> App.getDocument('Unnamed').getObject('Boolean').ViewObject.PointColor=getattr(App.getDocument('Unnamed').getObject('Cylinder').getLinkedObject(True).ViewObject,'PointColor',App.getDocument('Unnamed').getObject('Boolean').ViewObject.PointColor)
>>> App.getDocument('Unnamed').getObject('Boolean').ViewObject.Transparency=getattr(App.getDocument('Unnamed').getObject('Cylinder').getLinkedObject(True).ViewObject,'Transparency',App.getDocument('Unnamed').getObject('Boolean').ViewObject.Transparency)
>>> App.getDocument('Unnamed').getObject('Boolean').ViewObject.DisplayMode=getattr(App.getDocument('Unnamed').getObject('Cylinder').getLinkedObject(True).ViewObject,'DisplayMode',App.getDocument('Unnamed').getObject('Boolean').ViewObject.DisplayMode)
>>> Gui.getDocument('Unnamed').setEdit(App.getDocument('Unnamed').getObject('Body001'), 0, 'Boolean.')
>>> Gui.Selection.clearSelection()
>>> ### End command PartDesign_Boolean
>>> # Gui.Selection.clearSelection()
>>> App.getDocument('Unnamed').getObject('Boolean').setObjects( [App.getDocument('Unnamed').getObject('Body'),])
>>> App.getDocument('Unnamed').getObject('Boolean').Type = 0
>>> App.ActiveDocument.recompute()
>>> Gui.activeDocument().resetEdit()
>>> 
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: part design boolean operation in macro

Post by smktec »

thanks for the hints. I think I will have to further look into Part::Boolan class to move forward.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: part design boolean operation in macro

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: part design boolean operation in macro

Post by smktec »

super thanks
Post Reply