abaqus - 如何使用 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")
解决方案
您可以调整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
以便从这一点开始执行下一次搜索。
希望这会有所帮助
推荐阅读
- laravel - DOMPDF 在 Ubuntu 服务器上已弃用?
- sql - 使用 Logstash 和 Kibana 从两个不同的数据库中提取数据
- ragel - 在 ragel 中扫描“引用字符串”的正确方法是什么?
- java - 如何从 corba idl 获取 JAVA 对象
- php - 您可以通过为您网站中不同类型的文件制作单独的文件来使您的 public_html 更有条理吗?
- c# - 多个数据网格在 WPF 应用程序中,但一次只显示一个
- go - 如何在Go中逐行打印没有括号的切片中的元素?
- godot - Godot 3.1 变量未在范围错误中声明
- php - WAMP 服务器中的 SAP 和 PHP 连接
- selenium - 带有 Chrome 浏览器的 Selenium Webdriver 不显示身份验证弹出窗口