[solved]About the usage of "Part"- "SliceApart" tool?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

[solved]About the usage of "Part"- "SliceApart" tool?

Post by xianyu »

hi,
I have a surface, and I want it to cut a cube, and end up with the solid I want(from this thread).
https://forum.freecadweb.org/viewtopic. ... 61#p627261

In the GUI, I can easily get what I want, But I want to do it on a script.
https://wiki.freecadweb.org/Part_SliceApart#Scripting
https://forum.freecadweb.org/viewtopic. ... 22#p424022

Code: Select all

import BOPTools.SplitFeatures
j = BOPTools.SplitFeatures.makeSlice(name= 'Slice')
j.Base = base_shape
j.Tools = tool_shapes

#BOPTools.SplitAPI.slice(base_shape, tool_shapes, mode, tolerance = 0.0)   #or
Can someone teach me how to use it?It has two split shapes, I only need one of them(how can i get it)

Thanks in advance :)
Last edited by xianyu on Fri Sep 23, 2022 6:20 am, edited 1 time in total.
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: About the usage of "Part"- "SliceApart" tool?

Post by onekk »

The output shape occupies the same space as the original. But it is split where it intersects with other shapes. The split pieces are put into a compound (or compsolid), so the object appears to remain in one piece. You need to explode the compound to get the individual pieces.
Probably this is the point.

Code: Select all

import FreeCAD
from FreeCAD import Placement, Rotation, Vector
import Part
import BOPTools.SplitFeatures

def setview():
    """Rearrange View."""
    FreeCAD.Gui.SendMsgToActiveView("ViewFit")
    FreeCAD.Gui.activeDocument().activeView().viewAxometric()

DOC_NAME = "slice"

if FreeCAD.ActiveDocument is not None:
    if FreeCAD.ActiveDocument.Name== DOC_NAME:
        FreeCAD.closeDocument(DOC_NAME)

DOC = FreeCAD.newDocument(DOC_NAME)


VZOR = Vector(0,0,0)
ROT0 = Rotation(0,0,0)

shape = Part.makeBox(10,10,10)
tool = Part.makePlane(30,30)
tool.Placement = Placement(Vector(-15,-15,5), ROT0)

# mode could be "Standard", "Split", and "CompSolid"
# "Standard" and "Split" differ by the action of the tool on wires, shells and compsolids:
# if "Split", those are separated;
# if "Standard", they are kept together (get extra segments). 
# if "CompSolid", return a Compsolid (a set of solids connected by faces)

slice = BOPTools.SplitAPI.slice(shape, [tool], "Split", 1.0)
Part.show(slice, "SliceTS") 

print(slice.ShapeType)

shapes = slice.SubShapes

part1 = shapes[0]
part1.Placement = Placement(Vector(50,0,0), ROT0)

Part.show(part1, "slice1")

part2 = shapes[1]
part2.Placement = Placement(Vector(50,0,10), ROT0)
Part.show(part2, "slice2")

setview()
Hope it helps

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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: About the usage of "Part"- "SliceApart" tool?

Post by xianyu »

onekk wrote: Thu Sep 22, 2022 11:40 am

Code: Select all

slice = BOPTools.SplitAPI.slice(shape, [tool], "Split", 1.0)
Thank you very much for your generous answer, the code works!
I have a little question.

When I use "tool", I get the error: 'Part.Face' object is not iterable
Use "[tool]", normal
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: About the usage of "Part"- "SliceApart" tool?

Post by onekk »

xianyu wrote: Fri Sep 23, 2022 6:16 am I have a little question.

When I use "tool", I get the error: 'Part.Face' object is not iterable
Use "[tool]", normal

simply because Tools is expecting a list, so with [tool] you will supply a list (with only one element) and he is happy.

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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: About the usage of "Part"- "SliceApart" tool?

Post by xianyu »

onekk wrote: Fri Sep 23, 2022 7:03 am
xianyu wrote: Fri Sep 23, 2022 6:16 am I have a little question.

When I use "tool", I get the error: 'Part.Face' object is not iterable
Use "[tool]", normal

simply because Tools is expecting a list, so with [tool] you will supply a list (with only one element) and he is happy.

Regards

Carlo D.
If my shapes are solids and solids, not faces and solids, use "tools"?
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: About the usage of "Part"- "SliceApart" tool?

Post by onekk »

xianyu wrote: Fri Sep 23, 2022 7:13 am ...
If my shapes are solids and solids, not faces and solids, use "tools"?

Code: Select all

j = BOPTools.SplitFeatures.makeSlice(name= 'Slice')
j.Base = base_shape
j.Tools = tool_shapes
The functions will operate on "DocumentObjects" and from:

https://wiki.freecadweb.org/Part_Slice

You will see.
The Part Slice.svg Part Slice also known as Slice to compound tool is used to split shapes by intersection with other shapes.
So you could use shapes (in the GUI terminology there is no distinction between DocumentObjects and TopoShapes as everyting is shown on the screen.)

Sadly some reworking on the wiki pages should be done to uniform the terminology, but it is not a big hassle, until you have to dig in the "bowels" of FC, like when you are using scripting where the distinction between Curves, TopoShapes and DocumentObjects is important.

It happens that I have used a Plane that is simply a Face so the error is simply telling me that as the tool is expecting an iterable and a Face is not an iterable.

Hope it helps

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/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: [solved]About the usage of "Part"- "SliceApart" tool?

Post by xianyu »

@onekk Thank you for your patient answer, at least now I don't need to study it so deeply :D
Freecad novice, A Python enthusiast
Post Reply