愿与您一同,用代码提升效率!




import hmimport hm.entities as entfrom hwx import guidef modify_component_name_replace (): model = hm.Model()# 获取所有comp name,供后续判断 listallcomponentname = [] allcomponentcol = hm.Collection(model, ent.Component)for com in allcomponentcol: listallcomponentname.append(com.name)# selectcomponentcol = hm.CollectionByInteractiveSelection(model, ent.Component) ostring = str(lineeditfind.text) nstring = str(lineeditreplace.text)# 如果没有选择component 弹出一个消息框if len(selectcomponentcol) == 0: msg = gui.MessageDialog(message="Please select component") msg.show()# 如果有选择component 执行下面程序if len(selectcomponentcol) > 0:for com in selectcomponentcol:if com.name.replace(ostring,nstring) in listallcomponentname: def buttonClickCommand(buttonId):if buttonId == 0:# gui.tellUser("Accept button clicked.") passelse:# gui.tellUser("Reject button clicked.") pass gui.tellUser(f"{com.name.replace(ostring,nstring)} already exist in model.")else: com.name=com.name.replace(ostring,nstring)def select_component_name_replace (): global selectcomponentcol model = hm.Model() selectcomponentcol = hm.CollectionByInteractiveSelection(model, ent.Component)# GUIlabelfind = gui.Label(text="Find What")labelreplace = gui.Label(text="Replace With")lineeditfind = gui.LineEdit()lineeditreplace = gui.LineEdit()button = gui.Button("Replace", command=modify_component_name_replace)button1 = gui.Button("Components", command=select_component_name_replace)mainFrametop = gui.GridFrame( (labelfind,lineeditfind), (labelreplace,lineeditreplace))mainFramebottom = gui.GridFrame( (button1,button))mainFrame = gui.VFrame( mainFrametop, mainFramebottom)dialog = gui.Dialog(caption="Modify Component Name Replace", width=400, height=120)dialog.addChildren(mainFrame)dialog.show()💡提示
如有问题,欢迎留言指正与交流。 本文已收录进合集,进入文末合集可查看更多笔记。