# -*- coding: utf-8 -*-
from abaqusGui import *
from abaqusConstants import *
class MyCustomTool(AFXForm):
def __init__(self, owner):
AFXForm.__init__(self, owner)
self.owner = owner
# 定义调用内核函数的命令
self.cmd = AFXGuiCommand(
mode=self,
method='createSets', # 内核函数名
objectName='function', # 模块名(kernel/function.py)
registerQuery=False
)
# 定义输入参数
self.numSetsKw = AFXIntKeyword(self.cmd, 'numSets', True, 2)
self.setNamesKw = AFXStringKeyword(self.cmd, 'setNames', True)
self.setRatiosKw = AFXStringKeyword(self.cmd, 'setRatios', True)
def doCustomChecks(self):
"""输入验证逻辑"""
numSets = self.numSetsKw.getValue()
setNamesStr = self.setNamesKw.getValue().strip()
setRatiosStr = self.setRatiosKw.getValue().strip()
# 检查空输入
if not setNamesStr or not setRatiosStr:
showAFXErrorDialog(self.getCurrentDialog(), 'Input cannot be empty!')
return False
# 分割输入
setNames = [name.strip() for name in setNamesStr.split(',') if name.strip()]
setRatios = [ratio.strip() for ratio in setRatiosStr.split(',') if ratio.strip()]
# 检查数量匹配
if len(setNames) != numSets or len(setRatios) != numSets:
showAFXErrorDialog(
self.getCurrentDialog(),
'Expected {} sets, but got {} names and {} ratios!'.format(numSets, len(setNames), len(setRatios))
)
return False
# 检查比例格式
try:
setRatios = [float(x) for x in setRatios]
except ValueError as e:
showAFXErrorDialog(self.getCurrentDialog(), 'Invalid ratio format: {}'.format(e))
return False
# 检查比例总和
if sum(setRatios) != 100:
showAFXErrorDialog(self.getCurrentDialog(), 'Sum of ratios must be 100%!')
return False
return True
def getFirstDialog(self):
return MyCustomDialog(self)
class MyCustomDialog(AFXDataDialog):
def __init__(self, form):
self.form = form
AFXDataDialog.__init__(self, form, 'Create Sets', self.OK | self.APPLY | self.CANCEL, DIALOG_ACTIONS_SEPARATOR)
# 输入参数分组框
GroupBox_1 = FXGroupBox(p=self, text='Input Parameters', opts=FRAME_GROOVE | LAYOUT_FILL_X)
frame = AFXVerticalAligner(GroupBox_1)
# 输入字段
AFXTextField(p=frame, ncols=10, labelText='Number of Sets:', tgt=self.form.numSetsKw, sel=0)
AFXTextField(p=frame, ncols=20, labelText='Set Names (comma-separated):', tgt=self.form.setNamesKw, sel=0)
AFXTextField(p=frame, ncols=20, labelText='Set Ratios (comma-separated):', tgt=self.form.setRatiosKw, sel=0)