Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Seeking basic transform examples for sketch objects

fliesblindthroughcrapstorms
Participant

Seeking basic transform examples for sketch objects

fliesblindthroughcrapstorms
Participant
Participant

I attempting to create repetative parameter driven objects which exceed what can be done with parameteric sketching. This is my first foray into the API, but I am familiar with Python. I can't seem to find any examples of how to set up the matrix to do basic transforms (rotation/translation) of sketch curves. Can anyone point me in the right direction?

0 Likes
Reply
Accepted solutions (2)
414 Views
3 Replies
Replies (3)

kandennti
Mentor
Mentor
Accepted solution

Hi @fliesblindthroughcrapstorms -San.

 

The following sample rotates the sketch curve of a given sketch.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import math


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

        msg: str = 'Select Sketch'
        selFilter: str = 'Sketches'
        sel: core.Selection = selectEnt(msg, selFilter)
        if not sel:
            return

        exec_rotation_sketchCurves(sel.entity, 45)

        ui.messageBox('Done')

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


def exec_rotation_sketchCurves(
    skt: fusion.Sketch,
    angle: float, # unit deg
) -> None:

    crvs = [c for c in skt.sketchCurves]
    if len(crvs) < 1:
        return

    mat: core.Matrix3D = core.Matrix3D.create()
    mat.setToRotation(
        math.radians(angle),
        core.Vector3D.create(0, 0, 1),
        core.Point3D.create(0, 0, 0),
    )

    skt.move(
        core.ObjectCollection.createWithArray(crvs),
        mat,
    )


def selectEnt(
    msg: str,
    filterStr: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filterStr)
        return sel
    except:
        return None
0 Likes

kandennti
Mentor
Mentor
Accepted solution

This is how it works for translations.

 

・・・
        # exec_rotation_sketchCurves(sel.entity, 45)
        exec_translation_sketchCurves(
            sel.entity,
            core.Vector3D.create(10, 10, 0),
        )
・・・

def exec_translation_sketchCurves(
    skt: fusion.Sketch,
    vec: core.Vector3D,
) -> None:

    crvs = [c for c in skt.sketchCurves]
    if len(crvs) < 1:
        return

    mat: core.Matrix3D = core.Matrix3D.create()
    mat.translation = vec

    skt.move(
        core.ObjectCollection.createWithArray(crvs),
        mat,
    )
0 Likes

fliesblindthroughcrapstorms
Participant
Participant

Thank you very much.

1 Like