首页 > 解决方案 > 如何使用 python 脚本在步骤加载定义部分中插入关键字?

问题描述

我正在使用 python 将 *Include, Input=file.inp 插入到步骤加载定义部分中,以在节点上应用压力边界条件。这是我的脚本,但是,它被插入到零件级别部分。我想知道如何使用 python 控制插入位置。谢谢


def GetKeywordPosition(myModel, blockPrefix, occurrence=1):
    if blockPrefix == '':
        return len(myModel.keywordBlock.sieBlocks)+1
    pos = 0
    foundCount = 0
    for block in myModel.keywordBlock.sieBlocks:
        if string.lower(block[0:len(blockPrefix)])==\
           string.lower(blockPrefix):
            foundCount = foundCount + 1
            if foundCount >= occurrence:
                return pos
        pos=pos+1
    return +1

   position = GetKeywordPosition(myModel, '*step')+24
   myModel.keywordBlock.synchVersions(storeNodesAndElements=False)
   myModel.keywordBlock.insert(position, "\n*INCLUDE, INPUT=file.inp")

标签: abaqus

解决方案


您可以调整re模块。这应该工作

import re

# Get keywordBlock object
kw_block = myModel.keywordBlock
kw_block.synchVersions(storeNodesAndElements=False)

sie_blocks = kw_block.sieBlocks

# Define keywords for the search (don't forget to exclude special symbols with '\')
kw_list = ['\*Step, name="My Step"']

# Find index
idx = 0
for kw in kw_list:
    r = re.compile(kw)
    full_str = filter(r.match, sie_blocks[idx:])[0]
    idx += sie_blocks[idx:].index(full_str)

UPD:根据要求提供一些解释

由于 .inp 文件中的关键字可能有些重复,这里的主要思想是创建一个“搜索路线”,其中列表中的最后一个模式将对应于您要进行修改的位置(例如,如果您想要在特定的“*Instance”关键字之后找到“*End”关键字)。

所以我们通过我们的“搜索路线”==搜索模式列表迭代地进行:

  • 编译正则表达式;
  • sie_blocks从索引开始查找模式的第一次出现idx
  • 更新idx以便从这一点开始执行下一次搜索。

希望这会有所帮助


推荐阅读