C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

上側の面を判断する2

こちらの続きです。
上側の面を判断する1 - C#ATIA


前回のものがイマイチなので根本的に作戦を変更したい。
任意の面のサイズをブロックとして取得する方法があります。
こちらのboundingBoxプロパティです。
Fusion 360 Help
これCATIAにも欲しい・・・。但し、各XYZ平面の向き方向にしか取得できません。

この向きが指定出来ない欠点を説明付きで補うための記述を、
Ekins氏がブログに記載していたのですが、どうも削除したっぽいです。
(Fusion360とInventorで説明していた記憶あり)


今回のテーマは任意の方向での上の面の為、元のBodyをXYZ平面の向きの
なるように一時的に変換して利用しようかな?とも思っていたのですが、
類似したものを見つけました。
Fusion 360 Help
恐らくOrientedBoundingBox3Dオブジェクトは、BoundingBox3Dオブジェクトの
向き指定可能版のようです。

上方向となる平面かBodyの平坦な面を指定後、Bodyの面をクリックする度に
可視化したOrientedBoundingBox3Dを作ります。

# Fusion360API Python script
# OrientedBoundingBox test

import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_baseFeat = adsk.fusion.BaseFeature.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # 向きの選択
        msg :str = '上向きを決める平面か、サーフェスの平らな面を選んで!'
        selFiltter :str = 'ConstructionPlanes,PlanarFaces'

        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # 上向きと横向きベクトル
        vec3D = adsk.core.Vector3D
        upVec :vec3D = sel.entity.geometry.normal
        upVec.normalize()

        tmpVec = vec3D.create(1,0,0)
        if upVec.isParallelTo(tmpVec):
            tmpVec = vec3D.create(0,1,0)

        sideVec :vec3D = upVec.crossProduct(tmpVec)

        # 面の選択-無限ループ
        msg :str = '面を選んで!!'
        selFiltter :str = 'Faces'
        measureMgr :adsk.core.MeasureManager = _app.measureManager
        tmpMgr = adsk.fusion.TemporaryBRepManager.get()

        while True:
            # 面選択
            sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
            if not sel: break

            # 面
            face = sel.entity

            # OrientedBoundingBox
            orientedBox = measureMgr.getOrientedBoundingBox(
                face, upVec, sideVec)

            # 向きが一致しているとエラーになるので
            if orientedBox.length < 0.001:
                _ui.messageBox('上向きと一致する為、作成を止めておきます')
                continue

            # 見える化
            box = tmpMgr.createBox(orientedBox)

            global _baseFeat
            if not _baseFeat:
                _baseFeat = root.features.baseFeatures.add()
                _baseFeat.startEdit()

            box = root.bRepBodies.add(box,_baseFeat)
            box.opacity = 0.5

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

    finally:
        try:
            if _baseFeat:
                _baseFeat.finishEdit()
        except:
            pass

def selectEnt(
        msg :str, 
        filtterStr :str) -> adsk.core.Selection :

    try:
        sel = _ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None

・・・作るまではこれでイケル! と思ったのですが、
出来上がってみると、方向性が違っている気がする。