C#ATIA

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

CustomGraphicsTextの闇が深すぎる1

こちらの原因と対策を探ってます。
計測をもっと実用的にしたい2 - 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

        removeCG()

        billBoarding = False

        testData = [
            [
                'AB',
                adsk.core.Point3D.create(0,0,0),
                billBoarding
            ],
            [
                'CDE',
                adsk.core.Point3D.create(10,20,30),
                billBoarding
            ],
            [
                'FGHI',
                adsk.core.Point3D.create(-30,-20,-10),
                billBoarding
            ]
        ]

        for t, p, b in testData:
            initCGtext(root, t, p, b)

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

def initCGtext(
    comp: adsk.fusion.Component,
    txt: str,
    pnt: adsk.core.Point3D,
    isBillBoarding: bool
    ):

    vec: adsk.core.Vector3D = pnt.asVector()
    mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
    mat.translation = vec

    cgGroup: adsk.fusion.CustomGraphicsGroup = comp.customGraphicsGroups.add()
    cgTxt: adsk.fusion.CustomGraphicsText = cgGroup.addText(
        txt,
        'Arial',
        3,
        mat)
    cgTxt.isBold = True
    cgTxt.isSelectable = False

    if isBillBoarding:
        billBoard = adsk.fusion.CustomGraphicsBillBoard.create(pnt)
        billBoard.billBoardStyle = adsk.fusion.CustomGraphicsBillBoardStyles.ScreenBillBoardStyle
        cgTxt.billBoarding = billBoard


    drawBBox(comp, cgTxt.boundingBox, txt)

def drawBBox(
    comp: adsk.fusion.Component,
    bBox: adsk.core.BoundingBox3D,
    name: str):

    oriented = toOriented(bBox)

    tmpMgr: adsk.fusion.TemporaryBRepManager = adsk.fusion.TemporaryBRepManager.get()
    box: adsk.fusion.BRepBody = tmpMgr.createBox(oriented)

    baseFeat : adsk.fusion.BaseFeature = comp.features.baseFeatures.add()
    baseFeat.startEdit()
    body: adsk.fusion.BRepBody = comp.bRepBodies.add(box, baseFeat)
    body.opacity = 0.5
    body.name = name
    baseFeat.finishEdit()

def toOriented(
    bBox: adsk.core.BoundingBox3D) -> adsk.core.OrientedBoundingBox3D:

    pMax: adsk.core.Point3D = bBox.maxPoint
    pMin: adsk.core.Point3D = bBox.minPoint

    center = adsk.core.Point3D.create(
        (pMax.x + pMin.x) * 0.5,
        (pMax.y + pMin.y) * 0.5,
        (pMax.z + pMin.z) * 0.5
    )

    return adsk.core.OrientedBoundingBox3D.create(
        center,
        adsk.core.Vector3D.create(1,0,0),
        adsk.core.Vector3D.create(0,1,0),
        abs(pMax.x - pMin.x),
        abs(pMax.y - pMin.y),
        abs(pMax.z - pMin.z)
    )

def removeCG():
    app: adsk.core.Application = adsk.core.Application.get()
    des :adsk.fusion.Design = app.activeProduct
    cgs = [cmp.customGraphicsGroups for cmp in des.allComponents]
    cgs = [cg for cg in cgs if cg.count > 0]
    
    if len(cgs) < 1: return

    for cg in cgs:
        gps = [c for c in cg]
        gps.reverse()
        for gp in gps:
            gp.deleteMe()

これを実行するとこんなのが出来ます。
f:id:kandennti:20211021160930p:plain
"AB" 等の文字はCustomGraphicsTextで、近くの四角はサーフェスです。
この四角のサーフェスはCustomGraphicsTextのboundingBoxプロパティから
作り出しているので本当はこんな感じで出来上がるべきだと思ってます。
f:id:kandennti:20211021161552p:plain

自分の理解が足りないのか、バグなのか・・・。


続いて、一番悩んでいるのがBillBoardingです。
BillBoardingを利用すると、3D空間内をグルグル回しても文字が常に画面方向に
向いている状態を作ることが出来ます。
正に、計測時の数値がこの動きをします。

一部修正してこんな感じにします。

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

        billBoarding = True # <-ここ

        testData = [
・・・

修正後の物を実行するとこんな感じです。
f:id:kandennti:20211021162248p:plain
XY平面方向に見ると、さっきと変わらないんです。

ISO方向から見るとこんな感じ。
f:id:kandennti:20211021162428p:plain

原点に作成している "AB" は良いんです。画面グルグルしても大丈夫なんです。
"CDE" と "FGHI" は四角の位置からも離れちゃうんです。

別の角度から見てもおかしい。
(本当は動画の方が分かりやすい・・・)
f:id:kandennti:20211021162732p:plain

バグだと思いたい。何か計算式が有るのかな?


そもそも、ドキュメントに謎が多い。
・CustomGraphicsBillBoard.createメソッドのanchorPointの説明の
 "isViewDependentプロパティ" が見つからん。
Fusion 360 Help

・CustomGraphicsBillBoardオブジェクトのanchorPointの説明の
 "CustomGraphicsAnchorPointオブジェクトの静的createメソッドを使用して作成できます。"
 そんなオブジェクトは無い。(Point3Dになっている)
Fusion 360 Help

・CustomGraphicsText.boundingBoxプロパティの説明の
 ”グラフィックがモデル空間と画面空間のどちらで描画されているかに応じて、
 これはセンチメートル(モデル)またはピクセル(画面)のいずれかで
 バウンディングボックスを返します。” それどっちに描かれているかをどうやって知るの?
Fusion 360 Help


そもそもCustomGraphicsTextは、まだ不完全だったようなことが、以前フォーラムに
書いてあったような・・・。