C#ATIA

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

表示されているボディ毎にセットアップを作る

最近はがんばってAPIフォーラムで答えています。

こちらで回答したものをそのまま転載ですが、表示されている
ボディ毎にセットアップとNCプログラムを作成します。

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam
import sys

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        camObj: cam.CAM = get_cam_product()

        rootOcc: fusion.Occurrence = camObj.designRootOccurrence
        bodies = get_show_bodies(rootOcc.component)
        if len(bodies) < 1: return

        for body in bodies:
            create_nc_program(
                camObj,
                create_setup(
                    camObj,
                    body
                )
            )

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


def create_nc_program(
        camObj: cam.CAM,
        targetSetup: cam.Setup,
) -> cam.NCProgram:
    
    ncPrograms: cam.NCPrograms = camObj.ncPrograms
    ncIpt: cam.NCProgramInput = ncPrograms.createInput()
    ncIpt.displayName = targetSetup.name

    set_cam_parameter(
        ncIpt,
        "nc_program_filename",
        targetSetup.name,
    )

    set_cam_parameter(
        ncIpt,
        "nc_program_openInEditor",
        True,
    )

    ncIpt.operations = [targetSetup]
    ncPrograms.add(ncIpt)


def get_show_bodies(
        root: fusion.Component
) -> list[fusion.BRepBody]:
    
    bodies = root.findBRepUsingPoint(
        core.Point3D.create(0,0,0),
        fusion.BRepEntityTypes.BRepBodyEntityType,
        sys.float_info.max,
        True,
    )

    return list(bodies)


def create_setup(
        camObj: cam.CAM,
        targetBody: fusion.BRepBody,
) -> cam.Setup:

    setups: cam.Setups = camObj.setups
    setupIpt: cam.SetupInput = setups.createInput(
        cam.OperationTypes.MillingOperation
    )
    setupIpt.models = [targetBody]
    setup: cam.Setup = setups.add(setupIpt)
    setup.name = targetBody.name

    set_cam_parameter(
        setup,
        'job_stockMode',
        'default',
    )

    set_cam_parameter(
        setup,
        'job_stockOffsetMode',
        'keep',
    )

    return setup


def get_cam_parameter(
        camEntity,
        name: str,
) -> cam.CAMParameter:
    try:
        prm: cam.CAMParameter = camEntity.parameters.itemByName(
            name
        )
        if not prm: return None

        return prm.value.value

    except:
        return None


def set_cam_parameter(
        camEntity,
        name: str,
        value: str,
) -> bool:
    
    try:
        prm: cam.CAMParameter = camEntity.parameters.itemByName(
            name
        )
        if not prm: return False

        prm.value.value = value

        return True

    except:
        return False


def get_cam_product() -> cam.CAM:

    app: core.Application = core.Application.get()
    activete_cam_env()

    return app.activeProduct


def activete_cam_env() -> None:

    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface

    camWS: core.Workspace = ui.workspaces.itemById('CAMEnvironment') 
    camWS.activate()

これはデザインから実行しても、製造に切り替えて作ります。

まぁ正直に書くと、本当に基本的な事しか行っていない為、
色々と設定が不足しています。

段々思い出してきましたよ。