以下の内容はhttps://end0tknr.hateblo.jp/entry/20250103/1735889503より取得しました。


autodesk fusion python apiの練習 - 2コのボディをマウス選択し、ボディどうしの減算(切取り)

以下の内容で、動作しました。

コンポーネントとボディの関係により、動作しないケースがあるかもしれませんが、 手始めとしては十分かと思います。

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    app = adsk.core.Application.get()
    ui = app.userInterface
    design = app.activeProduct
    root_comp = design.rootComponent

    # 以下のメッセージがマウス付近に表示され、ユーザのボディ選択を待ちます
    prompt_cut_body = "カッターボディ(切り取る側)を選択してください"
    cut_body = select_body(ui,prompt_cut_body)
    if not cut_body:
        ui.messageBox("カッターボディが選択されていません")
        return

    # ターゲットボディの選択
    prompt_target_body = "ターゲットボディ(切り取られる側)を選択してください"
    target_body = select_body(ui,prompt_target_body)
    if not target_body:
        ui.messageBox("ターゲットボディが選択されていません")
        return

    # ObjectCollectionにカッターボディを追加

    try:
        # コピーを作成
        cut_body_name = cut_body.name
        copied_cut_body = copy_body(cut_body)
        cut_body_collection = adsk.core.ObjectCollection.create()
        cut_body_collection.add(cut_body)

        # CombineFeaturesで切り取りを設定
        combine_features = root_comp.features.combineFeatures
        combine_input = combine_features.createInput(target_body, cut_body_collection)
    
        combine_input.isKeepToolBodies = False  # ツールボディ保持の設定
        # 「切り取り」操作の設定
        combine_input.operation = adsk.fusion.FeatureOperations.CutFeatureOperation

        # Combine操作を実行
        combine_features.add(combine_input)

        copied_cut_body.name = "HOGE13"
        #copied_cut_body.name = cut_body_name

    except Exception as e:
        ui.messageBox(f"Error: {e}")
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))        

def select_body(ui, prompt_msg) -> adsk.fusion.BRepBody:
    body_result = ui.selectEntity(prompt_msg, "Bodies")

    if not body_result:
        return
    return body_result.entity

def copy_body(org_body) -> adsk.fusion.BRepBody:
    # ボディが属するコンポーネントを取得
    parent_occurrence = org_body.assemblyContext
    parent_component = parent_occurrence.component
    # ベースフィーチャーを作成
    base_feature = parent_component.features.baseFeatures.add()
    base_feature.startEdit()

    # TemporaryBRepManagerを使用してボディをコピー
    temp_brep_manager = adsk.fusion.TemporaryBRepManager.get()
    copied_body = temp_brep_manager.copy(org_body)

    # コピーされたボディをベースフィーチャーに追加
    new_body = parent_component.bRepBodies.add(copied_body, base_feature)
    # ベースフィーチャーの編集を終了
    base_feature.finishEdit()

    return new_body



以上の内容はhttps://end0tknr.hateblo.jp/entry/20250103/1735889503より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14