C#ATIA

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

スケッチのスピードテスト

先日の迷路や凸包の遅さを改善したい為、方法を模索することにしました。
今回はスケッチです。

スケッチに関しては、3D曲線インポートスプリクトを作成した際に色々と
試したので、どちらかと言うと覚書のようなものです。

こちらをベースにします。

#FusionAPI_python スケッチの速度テスト
#Author-kantoku

import adsk.core, adsk.fusion, traceback
import time, inspect
        
DEBUG = False
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        unit_size = 2.0
        lst = [((v*3, 0.0, 0.0),(unit_size+v*3, unit_size, 0.0)) for v in range(100)]
        
        #test1
        for i in range(5):
            doc = NewDoc(app)
            root = app.activeProduct.rootComponent
            
            t = time.time()
            
            funcname = test1(root,lst)#ここ書き換え
            
            msg='{}-time:{:.2f}s'.format(funcname,time.time()- t)
            print(msg)
            
            if not DEBUG:
                doc.close(False)
                
        if DEBUG:
            ui.messageBox(msg)
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def NewDoc(app):
    return app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

def initBox(skt,ary1,ary2):
    lines = skt.sketchCurves.sketchLines
    p1 = adsk.core.Point3D.create(ary1[0],ary1[1],ary1[2])
    p2 = adsk.core.Point3D.create(ary2[0],ary2[1],ary2[2])
    box = lines.addTwoPointRectangle(p1,p2)
    return box

比較を行うための関数を用意し "test1" の部分を書き換えて速度を測定します。
スケッチの四角の対角線となる2点の座標値を持つ、100個のリストをテスト関数に投げ込みます。
結果的にこんな感じの四角が100個出来上がります。
(実際はドキュメントを新作し、処理後閉じている為何も残りません)
f:id:kandennti:20180926153642p:plain
これを各5回行います。

○テスト1
APIのHelpのサンプルコードにもありそうな、オーソドックスな感じのものです。

#test1
def test1(root,lst):
    skt = root.sketches.add(root.xYConstructionPlane)
    [initBox(skt,a1,a2) for (a1,a2) in lst]
    
    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

比較するものも無いので、これが基準です。

test1-time:5.63s
test1-time:5.60s
test1-time:5.58s
test1-time:5.62s
test1-time:5.61s


○テスト2
スケッチオブジェクトのisComputeDeferredプロパティを利用すると、
スケッチ内の演算処理を停止する事が出来、速度向上のメリットかあるようです。

#test2
def test2(root,lst):
    skt = root.sketches.add(root.xYConstructionPlane)
    skt.isComputeDeferred = True
    [initBox(skt,a1,a2) for (a1,a2) in lst]
    skt.isComputeDeferred = False
    
    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

結果はこちら。効果絶大です。

test2-time:0.33s
test2-time:0.33s
test2-time:0.33s
test2-time:0.33s
test2-time:0.33s


○テスト3
スケッチオブジェクトのareProfilesShownプロパティは、プロファイルの
表示です。手動ではこちらのチェックの有無の操作になり、OFFにすることで
速度向上のメリットかあるようです。
f:id:kandennti:20180926153656p:plain

#test3
def test3(root,lst):
    skt = root.sketches.add(root.xYConstructionPlane)
    skt.areProfilesShown = False
    [initBox(skt,a1,a2) for (a1,a2) in lst]
    skt.areProfilesShown = True
    
    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

結果はこちら。効果大です。

test3-time:0.50s
test3-time:0.48s
test3-time:0.48s
test3-time:0.48s
test3-time:0.48s


○テスト4
通常であれば、履歴有りで使用することの方が多いはずですが
履歴なしに切り替えることで、少し効果が有るような記述を見つけました。

#test4
def test4(root,lst):
    des = adsk.fusion.Design.cast(root.parentDesign)
    des.designType = adsk.fusion.DesignTypes.DirectDesignType
    
    skt = root.sketches.add(root.xYConstructionPlane)
    [initBox(skt,a1,a2) for (a1,a2) in lst]
    
    des.designType = adsk.fusion.DesignTypes.ParametricDesignType
    
    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

結果はこちら。効果小です。
デメリットの方が大きい気がします。

test4-time:5.31s
test4-time:5.32s
test4-time:5.31s
test4-time:5.32s
test4-time:5.36s


○テスト5
スケッチのareDimensionsShownプロパティは、拘束の表示です。
手動ではこちらのチェックの有無の操作になり、OFFにすることで
速度向上のメリットかあるようです。
f:id:kandennti:20180926153709p:plain

#test5
def test5(root,lst):
    skt = root.sketches.add(root.xYConstructionPlane)
    skt.areDimensionsShown = False
    
    [initBox(skt,a1,a2) for (a1,a2) in lst]
       
    skt.areDimensionsShown = True

    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

結果はこちら。今回拘束を使わなかった為、効果は無さそうです。

test5-time:5.66s
test5-time:5.59s
test5-time:5.59s
test5-time:5.61s
test5-time:5.65s


○テスト6
現実的なものと考えテスト2,3,5を一緒にしてみました。

#test6
def test6(root,lst):
    skt = root.sketches.add(root.xYConstructionPlane)
    
    skt.isComputeDeferred = True
    skt.areProfilesShown = False
    skt.areDimensionsShown = False
    
    [initBox(skt,a1,a2) for (a1,a2) in lst]
    
    skt.areDimensionsShown = True
    skt.areProfilesShown = True
    skt.isComputeDeferred = False      

    f = inspect.currentframe()
    return inspect.getframeinfo(f)[2]

結果はこちら。テスト2と遜色無い為、isComputeDeferredプロパティの
効果が一番大きいようです。

test6-time:0.33s
test6-time:0.32s
test6-time:0.33s
test6-time:0.33s
test6-time:0.33s

3D曲線インポートスプリクトを作っていた頃、フォーラムで出ていた話題で
こちらの ”点の表示” をOFFにすることで速度向上することがわかっており
APIで操作出来ないか? と言うの出ていたのですが、未だに機能が追加されて
いないですね。(スプライン等、通過点が大量に必要な場合は効果が大きいです)
f:id:kandennti:20180926153733p:plain

少し時間に余裕があるので、色々と試しているのですが上手く行かない・・・。