how to cut a wire?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: how to cut a wire?

Post by Chris_G »

Would you like to try this :

Code: Select all

def get_wire(edges, e1, e2):
    _tmp = edges + [e1]
    se = Part.sortEdges(_tmp)
    if len(se) == 1:  # e1 is indeed touching previous edges
        w1 = Part.Wire(se[0])
        return w1, e2
    else:  # e1 is not touching previous edges, let's try with e2
        se = Part.sortEdges(edges + [e2])
        if len(se) == 1:  # all edges connected
            w1 = Part.Wire(se[0])
            return w1, e1
    return None, None

def split_wire(wire, line, tol=1e-7):
    wires = []
    edges = []
    for i, oe in enumerate(wire.OrderedEdges):
        d, pts, info = oe.distToShape(line)
        if d > tol:
            edges.append(oe)
        else:
            for data in info:
                if data[0] == "Edge":
                    e1, e2 = oe.split(data[2]).Edges
                    w, e = get_wire(edges, e1, e2)
                    if w:
                        wires.append(w)
                        edges = [e]
                elif data[0] == "Vertex":
                    print("Wire is cut exactly on a vertex")
                    print("We should do something also, but I'm getting lazy ;-)")
    if edges:
        wires.append(Part.Wire(Part.sortEdges(edges)[0]))
    if wire.isClosed():
        join = Part.Wire(Part.sortEdges(wires[0].Edges + wires[-1].Edges)[0])
        wires = [join] + wires[1:-1]
    return wires

It sems to be working good.
wire_cut.png
wire_cut.png (9.62 KiB) Viewed 444 times
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: how to cut a wire?

Post by onekk »

Great work, many many thanks, your code is more polished and "elegant".

It works flawlessy, I have to study some more :D.

Can I bother you more about your Curves WB in the other thread?

Many many thanks again and Best 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/
Post Reply