C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

Fusion360APIのVecter3Dの外積を確認1

こちらを確認!
メッシュの六角形分割に挑む11 - C#ATIA

外積を視覚的に確認したいのでテストしました。

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent
        
        mat = initMatrix3D_fromXZ(
            adsk.core.Vector3D.create(1, 2, 1),
            adsk.core.Vector3D.create(-1, 2, 2)
        )

        dumpMatrix(root, mat, 'Fusion API')

        mat = initMatrix3D_fromXZ(
            adsk.core.Vector3D.create(1, 2, 1),
            adsk.core.Vector3D.create(-1, 2, 2),
            cross3D
        )

        dumpMatrix(root, mat, 'non Fusion API')

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

def initMatrix3D_fromXZ(
    vecX: adsk.core.Vector3D,
    vecZ: adsk.core.Vector3D,
    crossFanc = None) -> adsk.core.Matrix3D:

    vecY: adsk.core.Vector3D 
    if crossFanc:
        vecY = cross3D(vecZ, vecX)
        vecX = cross3D(vecZ, vecY)
    else:
        vecY = vecZ.crossProduct(vecX)
        vecX = vecZ.crossProduct(vecY)

    vecX.normalize
    vecY.normalize
    vecZ.normalize

    mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
    mat.setWithCoordinateSystem(
        adsk.core.Point3D.create(0, 0, 0),
        vecX,
        vecY,
        vecZ
    )

    return mat

def cross3D(
    vec1: adsk.core.Vector3D,
    vec2: adsk.core.Vector3D) -> adsk.core.Vector3D:

    x = vec1.y * vec2.z - vec1.z * vec2.y
    y = vec1.z * vec2.x - vec1.x * vec2.z
    z = vec1.x * vec2.y - vec1.y * vec2.x

    return adsk.core.Vector3D.create(x, y, z)

# 長さ X10Cm Y15Cm Z20Cm
def dumpMatrix(
    comp: adsk.fusion.Component,
    mat: adsk.core.Matrix3D,
    name: str):

    skt: adsk.fusion.Sketch = comp.sketches.add(comp.xYConstructionPlane)
    skt.name = name

    pnt, vx, vy, vz = mat.getAsCoordinateSystem()
    vy.scaleBy(1.5)
    vz.scaleBy(2)
    for v in [vx, vy, vz]:
        p = pnt.copy()
        p.translateBy(v)
        skt.sketchCurves.sketchLines.addByTwoPoints(
            pnt, p
        )

2つのベクトルは、疑問に思った時のベクトルと同じです。
結果、
f:id:kandennti:20211008113944p:plain
ちゃんと右手系で返してます。
と言う事は、あちらで僕が何かを間違えてます・・・。