How to simulate a click on "view_align_to_view" over the API ?

How to simulate a click on "view_align_to_view" over the API ?

maurizio_manzi
Advocate Advocate
200 Views
3 Replies
Message 1 of 4

How to simulate a click on "view_align_to_view" over the API ?

maurizio_manzi
Advocate
Advocate

Hello,
I have oriented the part to the camera view over the API.
But how can I simulate a click on "view_align_to_view" over the API ?
"view_align_to_view" don't extsts as parameter.
See Pic.
Best regards
Maurizio

0 Likes
Accepted solutions (1)
201 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor
Accepted solution

Hi @maurizio_manzi -san.

 

I tried searching for the text command to click the “Align To View” button, but couldn't find it.

Instead, I created a function to calculate the Turn and Tilt values directly....
pic.png

# Fusion360API Python script

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

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

        turn, tilt = get_align_to_view_euler_angles()

        print(f"{turn}  :  {tilt}")
        app.log(f"\nTurn: {turn}\nTilt: {tilt}")

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


def get_align_to_view_euler_angles():

    def vector3d_to_matrix3d(
            input_vector, 
            tolerance=1e-6):
        # トレランス以内で(0,0,1)かチェック
        if (abs(input_vector.x) < tolerance and 
            abs(input_vector.y) < tolerance and 
            abs(abs(input_vector.z) - 1.0) < tolerance):
            return core.Matrix3D.create()

        z_axis = input_vector.copy()
        z_axis.normalize()

        world_z = core.Vector3D.create(0, 0, 1)
        
        if abs(z_axis.dotProduct(world_z)) < (1.0 - tolerance):
            y_axis = world_z.crossProduct(z_axis)
            y_axis.normalize()
            
            x_axis = y_axis.crossProduct(z_axis)
            x_axis.normalize()
        else:
            x_axis = core.Vector3D.create(1, 0, 0)
            y_axis = core.Vector3D.create(0, 1, 0)
            
            if z_axis.z < 0:
                x_axis.scaleBy(-1)
        
        origin = core.Point3D.create(0, 0, 0)
        matrix = core.Matrix3D.create()
        matrix.setWithCoordinateSystem(origin, x_axis, y_axis, z_axis)
        
        return matrix

    def compute_turn_tilt_from_matrices(
            matA: core.Matrix3D, 
            matB: core.Matrix3D,):
        _, xA, _, zA = matA.getAsCoordinateSystem()
        _, xB, _, zB = matB.getAsCoordinateSystem()

        tilt_rad = zA.angleTo(zB)
        tilt_deg = math.degrees(tilt_rad)

        def flatten_xy_and_check(v):
            vec = core.Vector3D.create(v.x, v.y, 0)
            if vec.length < 1e-6:
                return None  # Z軸とほぼ一致 → XY成分が無視できるほど小さい
            vec.normalize()
            return vec

        xA_xy = flatten_xy_and_check(xA)
        xB_xy = flatten_xy_and_check(xB)

        if xA_xy is None or xB_xy is None:
            turn_deg = 0.0
        else:
            turn_rad = xA_xy.angleTo(xB_xy)
            turn_deg = math.degrees(turn_rad)
            cross = xA_xy.crossProduct(xB_xy)
            if cross.z < 0:
                turn_deg = -turn_deg

        return (turn_deg, tilt_deg)

    app: core.Application = core.Application.get()

    camera: core.Camera = app.activeViewport.camera
    vecZ: core.Vector3D = camera.target.vectorTo(camera.eye)
    vecZ.normalize()
    
    return compute_turn_tilt_from_matrices(
        core.Matrix3D.create(),
        vector3d_to_matrix3d(vecZ)
    )


I used Claude to create the calculation part, but I think it should be fine.

0 Likes
Message 3 of 4

maurizio_manzi
Advocate
Advocate

Thank you very much

Message 4 of 4

kandennti
Mentor
Mentor

The unit of the function's return value is deg. Please note that we have not checked whether the CAM parameter is deg or rad.

0 Likes