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


autodesk fusion for win へ fastapi for python を追加し、add in経由で作画

step1 - 新規アドインの追加

先日の autodesk fusion の新規add in作成直後の状態 - end0tknr's kipple - web写経開発 に記載した通りです。

step2 - pip install fastapi uvicorn pydantic

先日の autodesk fusion for win への外部moduleの pip install - end0tknr's kipple - web写経開発 に記載した通りですが、以下の通りです

C:\Users\end0t\AppData\Local\Autodesk\webdeploy\production\【略】
   \Python\Scripts\pip.exe install fastapi uvicorn pydantic

step3 - edit API/AddIns/NewAddIn3/NewAddIn3.py

defaultの NewAddIn3.py には様々、記載されていますが、 以下のように修正します。

from .lib import fusionAddInUtils as futil
from . import api_server

def run(context):
    try:
        api_server.start_server()
    except Exception as e:
        futil.handle_error( e )

def stop(context):
    try:
        api_server.stop_server()
    except:
        futil.handle_error( e )

step4 - edit API/AddIns/NewAddIn3/api_server.py

fastapi処理の中心として以下のように作成します。

from fastapi  import FastAPI
from pydantic import BaseModel
import adsk.core, adsk.fusion
import asyncio
import threading
import uvicorn

fapi = FastAPI()
server        = None
server_thread = None

class BoxData(BaseModel):
    width: float
    height: float
    depth: float

@fapi.post("/drawbox")
def draw_box(data: BoxData):
    create_box(data.width, data.height, data.depth)
    return {"status": "ok"}

def start_server():
    futil.log('start start_server()')
    global server, server_thread
    config = uvicorn.Config( app=fapi,
                             host="127.0.0.1",
                             port=5000,
                             log_level="info" )
    server = uvicorn.Server(config=config)

    def run():
        asyncio.run( server.serve() )

    server_thread = threading.Thread(target=run, daemon=True)
    server_thread.start()

def stop_server():
    futil.log('start stop_server()')
    if server and server.started:
        server.should_exit = True
        server_thread.join( timeout=5 )


def create_box(width, height, depth):
    app = adsk.core.Application.get()
    design = adsk.fusion.Design.cast(app.activeProduct)
    root = design.rootComponent

    # sketch作成
    sketches = root.sketches
    xyPlane = root.xYConstructionPlane
    sketch = sketches.add(xyPlane)
    lines = sketch.sketchCurves.sketchLines
    lines.addTwoPointRectangle(
        adsk.core.Point3D.create(0, 0, 0),
        adsk.core.Point3D.create(width, height, 0)
    )

    # 押し出し
    profile = sketch.profiles.item(0)
    extrudes = root.features.extrudeFeatures
    ext_input = extrudes.createInput(
        profile,
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(depth)
    ext_input.setDistanceExtent(False, distance)
    extrudes.add(ext_input)

step5 - C:\Windows\System32\curl.exe で動作確認

以下のように実行すると、fusion の画面に直方体が追加されます。

CONDA> where curl
C:\Windows\System32\curl.exe
CONDA> curl -X POST http://localhost:5000/drawbox
   -H "Content-Type: application/json"
   -d "{\"width\":2.0,\"height\":3.0,\"depth\":1.0}"

{"status":"ok"}



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

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