通过class的代码形式,批量创建PART。
import ansafrom ansa import*class UserName ():def __init__(self):window=guitk.BCWindowCreate("Input_Values", guitk.constants.BCOnExitDestroy)grid=guitk.BCGridLayoutCreate(window, 1, 1)self.line_1=guitk.BCLineEditCreate(grid, 'name')guitk.BCGridLayoutAddWidget(grid, self.line_1, 0, 1, guitk.constants.BCAlignVCenter+guitk.constants.BCAlignLeft)self.label_1=guitk.BCLabelCreate(grid, "part name")guitk.BCGridLayoutAddWidget(grid, self.label_1, 0, 0, guitk.constants.BCAlignVCenter+guitk.constants.BCAlignLeft)guitk.BCDialogButtonBoxCreate(window)guitk.BCWindowSetAcceptFunction(window, self.AcceptFunc, None)guitk.BCWindowSetRejectFunction(window, self.RejectFunc, None)guitk.BCShow(window)def AcceptFunc (self, win, data):self.name=guitk.BCLineEditGetText(self.line_1)return 1def RejectFunc (self, win, data):return 1def createpart():window=UserName()for value in window.name.split() :base.CreateEntity(constants.NASTRAN,"ANSAPART",{"Name":value})createpart ()
# 基类/父类class Animal:def __init__(self, name):self.name = name # 封装:属性name被封装在Animal类中def speak(self): # 多态:不同的动物会以它们自己的方式发出声音raise NotImplementedError("Subclass must implement abstract method")# 子类继承自Animalclass Dog(Animal):def speak(self):return f"{self.name} says Woof!"class Cat(Animal):def speak(self):return f"{self.name} says Meow!"# 创建Animal类的实例animals = [Dog('Max'), Cat('Felix')]# 多态:调用相同的接口speak,执行不同的操作for animal in animals:print(animal.speak())