NX Journal 図面を開いてレイヤを非表示

2020/01/18 categories:NX Journal| tags:NX Journal|Python|

CSVからID、リビジョン、図面名を取得して図面を開く、レイヤを非表示を繰り返すプログラムを作成しました。

Pythonコード

import NXOpen
import NXOpen.Annotations
import NXOpen.Layer
import NXOpen.UF
import csv

def main():
    theSession  = NXOpen.Session.GetSession()
    
    # csvを開く
    dicts = open_csv()

    # 開けなければ終了
    if dicts is None:
        return
    
    # 文字列入力
    ret = input_string("Hidden layer number", "Hidden layer number")
    layer_number = int(ret[0])

    for d in dicts:

        # 図面のファイル名を作成
        dwg_filename = "@DB/" + d["ID"] + "/" + d["リビジョン"] + "/specification/"  + d["図面名"]
        
        # ファイルを開く
        workPart = openPart(theSession, dwg_filename)
        
        # アプリケーションの切り替え
        theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")

        # レイヤを変更
        hiddenLayer(workPart, layer_number)

def hiddenLayer(workPart, layer_number):
    # レイヤを非表示にする
    stateArray = [NXOpen.Layer.StateInfo(layer_number, NXOpen.Layer.State.Hidden)]
    workPart.Layers.ChangeStates(stateArray, False)

def input_string(title, default_string):
    # ret[0] : input string
    # ret[1] : 1 = Back, 2 = Cancel, 3 = OK (Accept default ), 5 = Data entered, 8 = Disallowed state
    theUfSession  = NXOpen.UF.UFSession.GetUFSession()
    theUI = NXOpen.UI.GetUI()
    theUI.LockAccess()
    ret = theUfSession.Ui.AskStringInput(title, default_string)
    theUI.UnlockAccess()
    return ret

def viewUpdate(workPart):
    # ビューの更新
    try:
        workPart.Views.WorkView.UpdateCustomSymbols()
        views = [ view for view in workPart.DraftingViews ]
        workPart.DraftingViews.UpdateViews(views)
    except Exception as e:
        pass

def openPart(theSession, id):
    try:
        # 既にファイルを開いていれば表示パートを変える
        part1 = theSession.Parts.FindObject(id)
        status1, partLoadStatus1 = theSession.Parts.SetActiveDisplay(
            part1, 
            NXOpen.DisplayPartOption.AllowAdditional, 
            NXOpen.PartDisplayPartWorkPartOption.UseLast
        )
        partLoadStatus1.Dispose()
    except Exception as e:
        # 見つからなければファイルを開く
        status1 = theSession.Parts.OpenBaseDisplay(id)
    return theSession.Parts.Work

def open_csv():
    try:
        # ファイル選択ダイアログ表示
        theUfSession  = NXOpen.UF.UFSession.GetUFSession()
        theUI = NXOpen.UI.GetUI()
        ret = theUfSession.Ui.CreateFilebox("", "CSVファイルを選択", "csv", "*.csv")

        # CSVを辞書のリストに変換
        with open(ret[1]) as f:
            reader = csv.DictReader(f)
            dicts = [row for row in reader]
        
        return dicts
    except:
        return None
    
if __name__ == '__main__':
    main()

Share post

Related Posts

コメント