C#ATIA

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

NURBS面を円筒面に変換する4

こちらの続きです。
NURBS面を円筒面に変換する3 - C#ATIA
ハイペースです。

前回フッと気が付いたのですが、面上の点から接線ベクトルと
法線ベクトルが得られるので、外積で軸ベクトル取れるじゃん と。

で作りました。

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import random

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        body: fusion.BRepBody = root.bRepBodies[0]

        nurbss = get_nurbs_faces(body)
        group = group_by_faces(nurbss)

        for faces in group:
            get_axis_test(faces)

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


def get_axis_test(faces: list):

    face: fusion.BRepFace = None
    for face in faces:

        eva: core.SurfaceEvaluator = face.evaluator

        prmRange: core.BoundingBox2D = eva.parametricRange()
        prms = [
            core.Point2D.create(
                random.uniform(
                    prmRange.minPoint.x,
                    prmRange.maxPoint.x,
                ),
                random.uniform(
                    prmRange.minPoint.y,
                    prmRange.maxPoint.y,
                ),
            ) for _ in range(10)
        ]

        # 面上の点
        _, pnts = eva.getPointsAtParameters(prms)

        # 接線
        _, tangents, _, _ = eva.getCurvatures(prms)
        pntSets = create_pointSets(pnts, tangents)
        # dump_lines(pntSets, 'tangent')

        # 法線
        _, normals = eva.getNormalsAtParameters(prms)
        pntSets = create_pointSets(pnts, normals)
        # dump_lines(pntSets, 'normal')

        # 軸-外積
        axiss = [t.crossProduct(n) for t, n in zip(tangents, normals)]
        pntSets = create_pointSets(pnts, axiss)
        # dump_lines(pntSets, 'axis')

        # 軸の合成
        vec: core.Vector3D = axiss.pop()
        [vec.add(v) for v in axiss]
        vec.normalize()
        pntSets = create_pointSets(pnts, [vec])
        dump_lines(pntSets, 'add_axis')
        print(vec.asArray())


def create_pointSets(points, vectors):
    v: core.Vector3D = None
    p1: core.Point3D = None
    pntSets = []
    for p1, v in zip(points, vectors):
        p2: core.Point3D = p1.copy()
        v.normalize
        p2.translateBy(v)
        pntSets.append((p1, p2))

    return pntSets


def dump_lines(pntSets, name = ''):
    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    skt: fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
    if len(name) > 0:
        skt.name = name
    sktLines: fusion.SketchLines = skt.sketchCurves.sketchLines

    skt.isComputeDeferred = True
    [sktLines.addByTwoPoints(p1, p2) for p1, p2 in pntSets]
    skt.isComputeDeferred = False

出力をコメントにして止めている部分は、出来上がりがあまりに
邪魔だった為です。

実行結果はこんな感じです。

見た目だけじゃなく、数値的にも

(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)
(0.0, 0.0, -1.0)

完璧じゃないですか!

こちらも

(0.0, 0.0, 1.0)
(0.0, 0.0, 1.0)

他人には絶対に伝わらないとは思ってます。

軸ベクトルの取得問題は解決としておきたい。