[Solved] check if 2 faces overlap using Face.section method

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

[Solved] check if 2 faces overlap using Face.section method

Post by rluq »

I am trying to check if 2 parts overlap using the Face.section method. The attached images show the examples I refer to below.

In this example I have 3 cases, the first, where part1 and part2 overlap, the second, where part3 and part4 are in contact on 2 edges, and the third, where the parts do not overlap.

The following commands show how I have got the intersection (overlap1, overlap2, overlap3) between 2 parts using Face.section method:

Code: Select all

overlap1 = App.ActiveDocument.getObjectsByLabel('part1')[0].Shape.Faces[0].section(App.ActiveDocument.getObjectsByLabel('part2')[0].Shape.Faces[0])
Part.show(overlap1, "overlap1")
[case1.png]

Code: Select all

overlap2 = App.ActiveDocument.getObjectsByLabel('part3')[0].Shape.Faces[0].section(App.ActiveDocument.getObjectsByLabel('part4')[0].Shape.Faces[0])
Part.show(overlap2, "overlap2")
[case2.png]

Code: Select all

overlap3 = App.ActiveDocument.getObjectsByLabel('part5')[0].Shape.Faces[0].section(App.ActiveDocument.getObjectsByLabel('part6')[0].Shape.Faces[0])
[case3.png]

Considering that the 2 parts overlap only in the first case, I would like to find a rule that allows me to check if 2 parts overlap. I have tried to use the isValid method and the BoundBox property, but I don't know how to differentiate the different cases, especially the second one.

Code: Select all

>>> overlap1.isValid()
True
>>> overlap1.BoundBox
BoundBox (-692.421, 697.849, 0, -623.344, 750.705, 0)

>>> overlap2.isValid()
True
>>> overlap2.BoundBox
BoundBox (89.8585, 374.475, 0, 158.935, 703.134, 0)

>>> overlap3.isValid()
True
>>> overlap3.BoundBox
BoundBox (1.79769e+308, 1.79769e+308, 1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308)
Attachments
case1.png
case1.png (22.54 KiB) Viewed 778 times
case2.png
case2.png (23.39 KiB) Viewed 778 times
case3.png
case3.png (24.55 KiB) Viewed 778 times
test_overlap.FCStd
(22.6 KiB) Downloaded 12 times
Last edited by rluq on Tue May 31, 2022 3:28 pm, edited 1 time in total.
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: check if 2 faces overlap using Face.section method

Post by edwilliams16 »

Code: Select all

def testoverlap(overlap):
    if len(overlap.Edges) == 0:
        print("No overlap")
    else:
        w = Part.Wire(overlap.Edges)
        if w.isClosed():
            print("Shapes overlap")
        else:
            print("Shapes touch")

testoverlap(overlap1)
testoverlap(overlap2)
testoverlap(overlap3)
Works, but should fail in the pathological case where one shape completely surrounded and touched the other around its entire perimeter.
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: check if 2 faces overlap using Face.section method

Post by edwilliams16 »

Code: Select all

def testoverlap(overlap):
    if len(overlap.Edges) == 0:
        print("No overlap")
    else:
        se = Part.sortEdges(overlap.Edges)
        wires = [Part.Wire(s) for s in se]
        if any([w.isClosed() for w in wires]):
            print("Shapes overlap")
        else:
            print("Shapes touch")
I didn't think about the case where the overlap created multiple closed curves. This should fix that.
rluq
Posts: 14
Joined: Tue Apr 12, 2022 6:24 pm

Re: check if 2 faces overlap using Face.section method

Post by rluq »

Thank you edwilliams16 for your answer!
Post Reply