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

Share on:

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

Pythonコード

 1import NXOpen
 2import NXOpen.Annotations
 3import NXOpen.Layer
 4import NXOpen.UF
 5import csv
 6
 7def main():
 8    theSession  = NXOpen.Session.GetSession()
 9    
10    # csvを開く
11    dicts = open_csv()
12
13    # 開けなければ終了
14    if dicts is None:
15        return
16    
17    # 文字列入力
18    ret = input_string("Hidden layer number", "Hidden layer number")
19    layer_number = int(ret[0])
20
21    for d in dicts:
22
23        # 図面のファイル名を作成
24        dwg_filename = "@DB/" + d["ID"] + "/" + d["リビジョン"] + "/specification/"  + d["図面名"]
25        
26        # ファイルを開く
27        workPart = openPart(theSession, dwg_filename)
28        
29        # アプリケーションの切り替え
30        theSession.ApplicationSwitchImmediate("UG_APP_DRAFTING")
31
32        # レイヤを変更
33        hiddenLayer(workPart, layer_number)
34
35def hiddenLayer(workPart, layer_number):
36    # レイヤを非表示にする
37    stateArray = [NXOpen.Layer.StateInfo(layer_number, NXOpen.Layer.State.Hidden)]
38    workPart.Layers.ChangeStates(stateArray, False)
39
40def input_string(title, default_string):
41    # ret[0] : input string
42    # ret[1] : 1 = Back, 2 = Cancel, 3 = OK (Accept default ), 5 = Data entered, 8 = Disallowed state
43    theUfSession  = NXOpen.UF.UFSession.GetUFSession()
44    theUI = NXOpen.UI.GetUI()
45    theUI.LockAccess()
46    ret = theUfSession.Ui.AskStringInput(title, default_string)
47    theUI.UnlockAccess()
48    return ret
49
50def viewUpdate(workPart):
51    # ビューの更新
52    try:
53        workPart.Views.WorkView.UpdateCustomSymbols()
54        views = [ view for view in workPart.DraftingViews ]
55        workPart.DraftingViews.UpdateViews(views)
56    except Exception as e:
57        pass
58
59def openPart(theSession, id):
60    try:
61        # 既にファイルを開いていれば表示パートを変える
62        part1 = theSession.Parts.FindObject(id)
63        status1, partLoadStatus1 = theSession.Parts.SetActiveDisplay(
64            part1, 
65            NXOpen.DisplayPartOption.AllowAdditional, 
66            NXOpen.PartDisplayPartWorkPartOption.UseLast
67        )
68        partLoadStatus1.Dispose()
69    except Exception as e:
70        # 見つからなければファイルを開く
71        status1 = theSession.Parts.OpenBaseDisplay(id)
72    return theSession.Parts.Work
73
74def open_csv():
75    try:
76        # ファイル選択ダイアログ表示
77        theUfSession  = NXOpen.UF.UFSession.GetUFSession()
78        theUI = NXOpen.UI.GetUI()
79        ret = theUfSession.Ui.CreateFilebox("", "CSVファイルを選択", "csv", "*.csv")
80
81        # CSVを辞書のリストに変換
82        with open(ret[1]) as f:
83            reader = csv.DictReader(f)
84            dicts = [row for row in reader]
85        
86        return dicts
87    except:
88        return None
89    
90if __name__ == '__main__':
91    main()

関連記事