元ネタはこちら
解決済み: ボディの中心(重心?)を基準に尺度変更したい - Autodesk Community
悩みに悩んで出来ました。 何となく後には引けない気がしたので。
#FusionAPI_python #Author-kantoku #Description-Create_Body_COGPoint ver0.02 import adsk.core, adsk.fusion, traceback def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface while True: body = SelBody(ui) if body is None: break comp = body.parentComponent cog = body.physicalProperties.centerOfMass if IsParametric(app) : baseFeature = comp.features.baseFeatures.add() baseFeature.startEdit() pnt = CreatePnt(comp, cog) baseFeature.finishEdit() else: pnt = CreatePnt(comp, cog) pnt.name = body.name + "_COG" ui.messageBox('終了') except: if ui: ui.messageBox('エラーで終了しました。\n{}'.format(traceback.format_exc())) #パラメトリックチェック def IsParametric(app): isPara = False design = adsk.fusion.Design.cast(app.activeProduct) if design.designType == adsk.fusion.DesignTypes.ParametricDesignType: isPara = True return isPara #点の作成 def CreatePnt(comp, cog): pnt = comp.constructionPoints.createInput() pnt.setByPoint(adsk.core.Point3D.create(cog.x, cog.y, cog.z)) return comp.constructionPoints.add(pnt) #Body選択 def SelBody(ui): try: selMsg = 'ボディを選択中。中止するにはEscキーを' selFilter = 'SolidBodies' ent = ui.selectEntity(selMsg, selFilter).entity return adsk.fusion.BRepBody.cast(ent) except: return None
スプリクト実行後、ボディを指定するたびに重心の位置に点を作成します。
・点はボディと同一コンポーネント内に作成されます。
・パラメトリック・ダイレクトどちらでも利用可能です。
・スプリクト実行後、異なるファイルに切り替えるとスプリクトは終了します。
未だ色々と問題は感じますが、最低限の動作はするはずです。
(インデントが深くてかっこ悪いなぁ)
色々サンプルコードを見たので
"動的型付けはあまり好きじゃないけど、こんな風に書くのか"
"あぁキャストしまくるんだなぁ"
"履歴を保持したまま、ボディをコピペ出来るんだ"
と色々と感じることも出来ました。
Helpを見る限り、CATIAのマクロより奥深い操作が出来る予感はしています。
追記です。
出来上がる点の名前が "(指定したボディ名)_COG"となる様に修正しました。