NX Journal 図面の穴寸法の追加テキストを置換する2

Share on:

前回作成した穴寸法のテキスト置換のプログラムを少し改造しました。検索文字列と置換文字列をダイアログから入力できるようにして、置換する追加テキストの位置は上下前後の4種類としました。

Pythonコード

 1import NXOpen
 2import NXOpen.Annotations
 3import NXOpen.UF
 4
 5def main():
 6
 7    theSession  = NXOpen.Session.GetSession()
 8    workPart = theSession.Parts.Work
 9
10    # 文字列入力
11    ret = input_string("Search string", "Search string")
12    search_string = ret[0]
13    ret = input_string("Replace string", "Replace string")
14    replace_string = ret[0]
15
16    # 寸法に対して処理
17    for dimension in workPart.Dimensions:
18        
19        # 穴寸法か
20        if not type(dimension) is NXOpen.Annotations.HoleDimension:
21            continue
22        
23        appendText = dimension.GetAppendedText()
24
25        # 後
26        replaced = [ s.replace(search_string, replace_string) for s in appendText.GetAfterText() ]
27        appendText.SetAfterText( replaced )
28        # 前
29        replaced = [ s.replace(search_string, replace_string) for s in appendText.GetBeforeText() ]
30        appendText.SetBeforeText( replaced )
31        # 上
32        replaced = [ s.replace(search_string, replace_string) for s in appendText.GetAboveText() ]
33        appendText.SetAboveText( replaced )
34        # 下
35        replaced = [ s.replace(search_string, replace_string) for s in appendText.GetBelowText() ]
36        appendText.SetBelowText( replaced )
37
38        dimension.SetAppendedText( appendText )
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    theUI = NXOpen.UI.GetUI()
44    theUI.LockAccess()
45    ret = theUfSession.Ui.AskStringInput(title, default_string)
46    theUI.UnlockAccess()
47    return ret
48
49if __name__ == '__main__':
50    main()

関連記事