Skip to content

How to filter the faces that are recognized as overlapped? #77

@Kiwi-XZ

Description

@Kiwi-XZ

Hi there,

I have found this example in this repository really helpful and tried it on my own CAD file 'three_puzzles_203.STEP' with the following code:

import os
from OCC.Display.WebGl import x3dom_renderer
from OCC.Extend.DataExchange import read_step_file_with_names_colors
from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.XCAFDoc import XCAFDoc_DocumentTool
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.TDF import TDF_LabelSequence, TDF_Label
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform
from OCC.Core.gp import gp_Pnt
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.BRepExtrema import BRepExtrema_ShapeProximity, BRepExtrema_DistShapeShape
from OCC.Display.SimpleGui import init_display
from OCC.Extend.ShapeFactory import make_vertex


def check_overlap_faces(shape_1, shape_2, tolerance=0, visualize=True):
    """
    Check the faces of two shapes that overlap.

    Parameters:
    -----------
    shape_1 : TopoDS_Shape
        The first shape to check for overlap.
    shape_2 : TopoDS_Shape
        The second shape to check for overlap.
    tolerance : float, optional
        The tolerance value for the proximity check. Default is 0.
    visualize : bool, optional
        If True, visualizes the shapes and their overlapping faces. Default is True.

    Returns:
    --------
    None
        This function does not return a value. It performs the overlap check and optionally visualizes the results.

    Notes:
    ------
    - This function uses the OpenCASCADE library for shape proximity checks.
    - The `BRepExtrema_ShapeProximity` class is used to determine overlapping sub-shapes.
    - For more details on the proximity algorithm, e.g. settings of tolerance, refer to the OpenCASCADE documentation:
        https://dev.opencascade.org/doc/refman/html/class_b_rep_extrema___shape_proximity.html

    Examples:
    ---------
    # Example usage:
    shape_1 = ...  # Load or create a TopoDS_Shape
    shape_2 = ...  # Load or create another TopoDS_Shape
    check_overlap(shape_1, shape_2, tolerance=0.01, visualize=True)

    """
    # Create meshes for the proximity algorithm
    deflection = 1e-3
    mesher1 = BRepMesh_IncrementalMesh(shape_1, deflection)
    mesher2 = BRepMesh_IncrementalMesh(shape_2, deflection)
    mesher1.Perform()
    mesher2.Perform()

    # Perform shape proximity check
    isect_test = BRepExtrema_ShapeProximity(shape_1, shape_2, tolerance)
    isect_test.Perform()

    # Get intersecting faces from Shape1
    overlaps1 = isect_test.OverlapSubShapes1()
    face_indices1 = overlaps1.Keys()
    shape_1_faces = []
    for ind in face_indices1:
        face = isect_test.GetSubShape1(ind)
        shape_1_faces.append(face)

    # Get intersecting faces from Shape2
    overlaps2 = isect_test.OverlapSubShapes2()
    face_indices2 = overlaps2.Keys()
    shape_2_faces = []
    for ind in face_indices2:
        face = isect_test.GetSubShape2(ind)
        shape_2_faces.append(face)

    # display both boxes and intersecting faces, in RED
    if visualize:
        display, start_display, add_menu, add_function_to_menu = init_display()
        display.DisplayShape(shape_1, transparency=0.5)
        display.DisplayShape(shape_2, transparency=0.5)
        display.DisplayShape(shape_1_faces + shape_2_faces, color="RED")
        display.FitAll()
        start_display()


if __name__ == "__main__":
    # Example usage
    file_path = 'path_to_three_puzzles_203.STEP'
    shapes = read_step_file_with_names_colors(file_path)
    shape_1 = list(shapes.keys())[1]
    shape_2 = list(shapes.keys())[3]
    check_overlap_faces(shape_1, shape_2, tolerance=0, visualize=True)

It works quite good, as you can see in the picture below (Numbers are for parts, letters are for surfaces), I have two parts 1 and 2 that are in touch, the surfaces that are in touch are visualized in red, the rest surfaces are transparent. But my aim is to find the surfaces that are really in overlap, not just "touched", in other words, I wish only that the surface 1-c and 2-c are recognized as overlapped, the rest not. Is there any hints about how to do this? The STEP file is also attached below. Thanks a lot!

Image

three_puzzles_203.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions