3
3

More than 3 years have passed since last update.

Fusion 360 を Pythonで動かそう その3 スケッチに3つの接線で円を描く

Last updated at Posted at 2020-07-24

はじめに

Fusion360 のAPIの理解を深めるために公式ドキュメント内のサンプルコード Create circle by 3 tangents API Sample (3つの接線で円を作る APIサンプル) の内容からドキュメントを読み込んでみたメモ書きです
3本の線を作成し、その線に接する円を描きます。さらにその関係を維持するための接線拘束を作成します。

スクリプトの内容を確認する

最初と最後のおまじないとスケッチの作成まで

最初と最後のこの辺りはFusion360 APIのお決まりのパターンのようです。スケッチの作成については前回も触れたので今回は説明を省略します。

import adsk.core, adsk.fusion, traceback

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

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches;
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)

        #
        # ここにコードを追加していく
        #

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

線を3本描く

        # Draw three lines.
        lines = sketch.sketchCurves.sketchLines;
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 1, 0))
        line2 = lines.addByTwoPoints(adsk.core.Point3D.create(4, 3, 0), adsk.core.Point3D.create(2, 4, 0))
        line3 = lines.addByTwoPoints(adsk.core.Point3D.create(-1, 0, 0), adsk.core.Point3D.create(0, 4, 0))

linesという名前で sketchLines オブジェクトを作成する
Point3D.create メソッドでXYZ座標を指定してポイントを作成し、SketchLines.addByTwoPoints メソッドで始点・終点を指定して線を3本描いている

image.png

3本の線に接する円を描く

        # Draw circle tangent to the lines.
        circles = sketch.sketchCurves.sketchCircles
        circle1 = circles.addByThreeTangents(line1, line2, line3, adsk.core.Point3D.create(0,0,0))

circlesという名前で sketchCircles オブジェクトを作成する
SketchCircles.addByThreeTangents メソッドで3本の線とhintPointの座標値を指定して円を描いている
image.png

スケッチ拘束を作成

        # Apply tangent contstraints to maintain the relationship.
        constraints = sketch.geometricConstraints
        constraints.addTangent(circle1, line1)
        constraints.addTangent(circle1, line2)
        constraints.addTangent(circle1, line3)

sketch.geometricConstraints プロパティから sketch.geometricConstraints オブジェクトを取得しconstraints に代入
GeometricConstraints.addTangent メソッドで circle1 と line1, line2, line3 に接線拘束を作成
image.png

まとめ

これぐらいの内容なら読んで理解するのは問題ないけど、サンプルにはないものを作りたいときにドキュメントから必要な情報を探すのに苦労しそうな気がするなー:sob:

前の記事 Fusion 360 を Pythonで動かそう その2 スケッチに円を描いてみる
次の記事 Fusion 360 を Pythonで動かそう その4 スケッチにいろんな方法で線を描く

参考

Fusion 360 API Reference Manual

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3