NX Journal 図面の穴寸法の追加テキストを置換する2
2020/01/11 categories:NX Journal| tags:NX Journal|Python|
前回作成した穴寸法のテキスト置換のプログラムを少し改造しました。検索文字列と置換文字列をダイアログから入力できるようにして、置換する追加テキストの位置は上下前後の4種類としました。
Pythonコード
import NXOpen
import NXOpen.Annotations
import NXOpen.UF
def main():
theSession = NXOpen.Session.GetSession()
workPart = theSession.Parts.Work
# 文字列入力
ret = input_string("Search string", "Search string")
search_string = ret[0]
ret = input_string("Replace string", "Replace string")
replace_string = ret[0]
# 寸法に対して処理
for dimension in workPart.Dimensions:
# 穴寸法か
if not type(dimension) is NXOpen.Annotations.HoleDimension:
continue
appendText = dimension.GetAppendedText()
# 後
replaced = [ s.replace(search_string, replace_string) for s in appendText.GetAfterText() ]
appendText.SetAfterText( replaced )
# 前
replaced = [ s.replace(search_string, replace_string) for s in appendText.GetBeforeText() ]
appendText.SetBeforeText( replaced )
# 上
replaced = [ s.replace(search_string, replace_string) for s in appendText.GetAboveText() ]
appendText.SetAboveText( replaced )
# 下
replaced = [ s.replace(search_string, replace_string) for s in appendText.GetBelowText() ]
appendText.SetBelowText( replaced )
dimension.SetAppendedText( appendText )
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
theUI = NXOpen.UI.GetUI()
theUI.LockAccess()
ret = theUfSession.Ui.AskStringInput(title, default_string)
theUI.UnlockAccess()
return ret
if __name__ == '__main__':
main()