C#ATIA

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

選択した面の周辺の面も、選択状態にする2

こちらの続きです。

選択した面の周辺の面も、選択状態にする1 - C#ATIA

Pythonの内包表記、For文の前に記述するのは他の言語には見られない
異様さを最初は感じたものの、案外書きやすくて使いまくってますが、
処理速度がちょっと気になったので、時間も測定しました。

#FusionAPI_python
#Author-kantoku
#Description-Surface_Around_Select_time-Test
#選択した面と接する周囲の面を選択状態にする

import adsk.core, adsk.fusion, traceback
from itertools import chain

_app = adsk.core.Application.get()
_ui = _app.userInterface

def run(context):
    try:
        global _ui
        
        #面選択
        selFilters = 'Faces'
        sel = Sel('面を選択/ESC-中止', selFilters )
        if sel is None: return
        face = sel.entity
        
        sw = StopWatchLite()
        
        #選択状態クリア
        _ui.activeSelections.clear()
        
        #取得
        sw.start()
        faces = typeA(face)#ここを書き換える
        
        #選択状態化
        [_ui.activeSelections.add(f) for f in faces]
        
        _ui.messageBox('Time:{}\n Count:{}'.format(sw.stop(),len(faces)))

    except:
        if _ui:
            _ui.messageBox('エラー\n{}'.format(traceback.format_exc()))

#最初の処理方法
def typeA(face):
    #全ループ取得
    loops = face.loops
    
    #全エッジ取得
    edges = [loop.edges for loop in loops]
    edgelist = list(chain.from_iterable([edge for edge in edges]))
    
    #エッジを共有している面の取得
    facelist = list(chain.from_iterable([edge.faces for edge in edgelist]))
    
    #重複している面を削除 - tempIdをキーとしたdictで
    facedict ={}
    [facedict.update({f.tempId:f}) for f in facelist]
    
    return facedict.values()

#多重ループな残念な処理
def typeB(face):
    facedict = {}
    for loop in face.loops:
        for edge in loop.edges:
            for surf in edge.faces:
                facedict.update({surf.tempId:surf})
    return facedict.values()

#残念な処理で重複したリストをそのまま返す処理
def typeC(face):
    faces = []
    for loop in face.loops:
        for edge in loop.edges:
            for surf in edge.faces:
                faces.append(surf)
    return faces
    
#選択
#param: msg-string, selFilter-SelectionFilters
#return: selection
def Sel(msg, selFilter):
    global _ui
    try:
        return _ui.selectEntity(msg, selFilter)
    except:
        return None

#http://qiita.com/yuu116atlab/items/b79ba36aa99550f25b8b
import time
class StopWatchLite(object):

    def __init__(self) :
        self.make = time.time()
        return

    def start(self) :
        self.st = time.time()
        return self

    def stop(self) :
        return time.time()-self.st

    def reset(self) :
        self.st = self.make
        return self

    def __str__(self) :
        return str( self.stop() )

ちょっとカッコ悪いのですが

        faces = typeA(face)#ここを書き換える

の部分を書き換えます。 続けて3タイプ処理すると
後から実行されたものの方が好結果になってしまったので。

200枚程選択される面を処理したところ、typeAとBはほとんど
大差なくtypeCは倍ぐらいの時間がかかりました。
typeCの場合、既に選択されている面を再度選択するような処理に
なる為、何となくそうなるだろうとは思っていましたが。

内包表記で十分だな。