C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)※記載されている内容は個人の意見であり、所属する団体等を代表する意見では御座いません・・・・よ!

OrientedBoundingBox3D内に面が完全に入っているか?

久々のFusion360です。

こちらを挑戦しました。
https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/is-a-brepface-lies-inside-the-orientedboundingbox3d/td-p/13709067

最初の回答では時間が無かったのでアイデアだけ書きましたが、
ちょっと時間を作り、アイデアを形にしました。

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        # OrientedBoundingBox3D
        occBBox: fusion.Occurrence = root.occurrences.item(0)
        bodyBBox: fusion.BRepBody = occBBox.bRepBodies[0]
        bBox: core.OrientedBoundingBox3D = bodyBBox.orientedMinimumBoundingBox

        # target faces
        occFaces: fusion.Occurrence = root.occurrences.item(1)
        bodyFaces: fusion.BRepBody = occFaces.bRepBodies[0]

        # Only BRepFace that are fully contained within 
        # the OrientedBoundingBox3D will be kept selected.
        sels: core.Selections = ui.activeSelections
        sels.clear

        for face in bodyFaces.faces:
            if is_fully_contained_in(face, bBox):
                sels.add(face)

        app.activeViewport.refresh()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def is_fully_contained_in(
        face: fusion.BRepFace,
        bBox: core.OrientedBoundingBox3D,) -> bool:

    def is_point_inside_obb(
            point: core.Point3D,
            obb: core.OrientedBoundingBox3D,) -> bool:
        center: core.Point3D = obb.centerPoint

        length_dir: core.Vector3D = obb.lengthDirection.copy()
        width_dir: core.Vector3D = obb.widthDirection.copy()
        length_dir.normalize()
        width_dir.normalize()

        height_dir: core.Vector3D = length_dir.crossProduct(width_dir)
        height_dir.normalize()

        vec: core.Vector3D = center.vectorTo(point)

        x: float = vec.dotProduct(length_dir)
        y: float = vec.dotProduct(width_dir)
        z: float = vec.dotProduct(height_dir)

        half_length: float = obb.length / 2.0
        half_width: float = obb.width / 2.0
        half_height: float = obb.height / 2.0

        return (
            -half_length <= x <= half_length and
            -half_width  <= y <= half_width and
            -half_height <= z <= half_height
        )
    

    return all(
        is_point_inside_obb(node, bBox) 
        for mesh in face.meshManager.displayMeshes 
        for node in mesh.nodeCoordinates
    )

"完全にAI使わずに書いたぜエエエエ"と言いたいところですが、is_point_inside_obb関数は
ChatGPT製です。
時間が無いので今回は無意味なイラスト無し。