LoginSignup
2
3

More than 3 years have passed since last update.

Fusion 360 を Pythonで動かそう その4 スケッチにいろんな方法で線を描く

Last updated at Posted at 2020-07-24

はじめに

Fusion360 のAPIの理解を深めるために公式ドキュメント内のサンプルコード Create sketch lines in various ways API Sample の内容からドキュメントを読み込んでみたメモ書きです
スケッチの線や長方形を色々な方法で作成します。

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

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

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

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()))

2本のつながった線を書く

        # Draw two connected 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(line1.endSketchPoint, adsk.core.Point3D.create(1, 4, 0))

SketchCurves.sketchLines プロパティを取得しlinesに代入する
line1Point3D.create メソッドでXYZ座標を指定してポイントを作成し、SketchLines.addByTwoPoints メソッドで2点を指定して線を描いている
Line2line1.endSketchPointで取得したSketchLine オブジェクトのプロパティを使って始点を指定している

image.png

2点指定して長方形を描く

        # Draw a rectangle by two points.
        recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(4, 0, 0), adsk.core.Point3D.create(7, 2, 0))

SketchLines.addTwoPointRectangle メソッドで2点を指定して長方形を描く
image.png

スケッチに水平・垂直拘束をかける

        # Use the returned lines to add some constraints.
        sketch.geometricConstraints.addHorizontal(recLines.item(0))
        sketch.geometricConstraints.addHorizontal(recLines.item(2))
        sketch.geometricConstraints.addVertical(recLines.item(1))
        sketch.geometricConstraints.addVertical(recLines.item(3))

GeometricConstraints.addHorizontal メソッドで水平拘束 を GeometricConstraints.addVertical メソッドで垂直拘束をかけている

スケッチに寸法拘束をかける

        sketch.sketchDimensions.addDistanceDimension(
                        recLines.item(0).startSketchPoint, recLines.item(0).endSketchPoint,
                        adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation,
                        adsk.core.Point3D.create(5.5, -1, 0));

SketchDimensions.addDistanceDimension メソッドで2点を指定して寸法拘束をかけている。
recLines.item(0)の始点と終点を指定し、寸法の向きは水平とし、テキストの基準点を座標指定している

image.png

3点指定の長方形と中心点指定の長方形を描く

        # Draw a rectangle by three points.
        recLines = lines.addThreePointRectangle(adsk.core.Point3D.create(8, 0, 0), 
                      adsk.core.Point3D.create(11, 1, 0), adsk.core.Point3D.create(9, 3, 0))

        # Draw a rectangle by a center point.
        recLines = lines.addCenterPointRectangle(adsk.core.Point3D.create(14, 3, 0), 
                      adsk.core.Point3D.create(16, 4, 0))

ここは特に問題なし!
image.png

まとめ

少しづつだけど分かってきたような気がするけど、もう少しシンプルに書けないものだろうか?寸法の向きを指定するのにadsk.fusion.DimensionOrientations.HorizontalDimensionOrientationってのは長すぎ&階層深すぎのような???

前の記事 Fusion 360 を Pythonで動かそう その3 スケッチに3つの接線で円を描く
次の記事 Fusion 360 を Pythonで動かそう その5 最初と最後のおまじないを読んでみる

参考

Fusion 360 API Reference Manual

2
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
2
3