今日分享的主要内容:如何将Matplotlib的colormap添加至Abaqus的云梯配色方案?
随着使用Abaqus的频率越来越高,已经不满足于已有的配色方案,在后处理的过程中更加倾向于Paraview的默认配色,个人觉得比Rainbow更加清爽。可以做一下对比:
(当然不排除某些领导偏爱花里胡哨的配色~)
于是乎有了个想法:是否可以将paraview的colormap或者其他个人觉得舒服的配色方案添加至Abaqus呢?
答案是肯定的,之前分享过如何将Matlab的colormap添加至Abaqus中,以及前段时间刚分享关于有限元后处理过程如何应用Matplotlib的推文,建议读者先阅读一下两篇历史推文:
探索Colormap:Matlab、Paraview和Python在有限元后处理中的应用
【Python二次开发】如何在Abaqus中更改云图配色风格
具体如何实现呢?直接上干货:
# -* - coding:UTF-8 -*-
import os
from abaqus import *
from abaqusConstants import *
# -----------------------------定义存放颜色表的类(请勿更改)------------------------------
class ColorMap_List:
def __init__(self, ColorMap_Name, ColorMap_Value):
self.ColorMap_Name = ColorMap_Name
self.ColorMap_Value = ColorMap_Value
self.ColorMap_Validated = True
# 修改颜色数量的检查逻辑,允许更多的颜色
if len(self.ColorMap_Value) < 2:
self.ColorMap_Validated = False
print "%s has less than 2 colors" % self.ColorMap_Name
return
# 遍历检查每个颜色的 RGB 值是否在 [0, 255] 之间
for Color in self.ColorMap_Value:
if len(Color) != 3:
self.ColorMap_Validated = False
print "%s has invalid color format" % self.ColorMap_Name
return
for Value in Color:
if not (0 <= Value <= 255):
self.ColorMap_Validated = False
print "%s has values out of range" % self.ColorMap_Name
return
# ------------------------定义载入颜色表至Abaqus的函数(请勿更改)--------------------------
def Abaqus_Spectrum_Generator(My_ColorMap):
if My_ColorMap.ColorMap_Validated == False:
Error_Info = My_ColorMap.ColorMap_Name + " is not a valid color map"
print Error_Info
return
# 转换RGB颜色为16进制颜色
My_ColorMap.ColorMap_Value_Hex = []
for RGB in My_ColorMap.ColorMap_Value:
Color_Hex = '#{:02x}{:02x}{:02x}'.format(*RGB)
My_ColorMap.ColorMap_Value_Hex.append(Color_Hex)
My_ColorMap.ColorMap_Value_Hex = tuple(My_ColorMap.ColorMap_Value_Hex)
# 载入颜色表至Abaqus
session.Spectrum(name=My_ColorMap.ColorMap_Name,
colors=My_ColorMap.ColorMap_Value_Hex)
Remind_Info = My_ColorMap.ColorMap_Name + " has been loaded."
print Remind_Info
# -------------------------定义从txt文件加载颜色表的函数-------------------------
def load_colormap_from_txt(file_path):
ColorMap_Value = []
with open(file_path, 'r') as f:
for line in f.readlines():
values = line.strip().split()[:3] # 只取前三列(RGB)
try:
# 检查颜色值是否在 [0, 1] 范围内,然后转换为 [0, 255] 的整数
rgb = [int(float(v) * 255) for v in values] # 直接乘以255,转换为[0, 255]范围
ColorMap_Value.append(rgb)
except ValueError:
print "Error reading line in file %s: %s" % (file_path, line)
continue
return ColorMap_Value
# -------------------------批量导入txt文件的colormap至Abaqus------------------------
def import_colormaps_to_abaqus(folder_path):
for file_name in os.listdir(folder_path):
if file_name.endswith('.txt'):
file_path = os.path.join(folder_path, file_name)
colormap_name = "Matplotlib_" + os.path.splitext(file_name)[0] # 在名字前加上Matplotlib_
colormap_values = load_colormap_from_txt(file_path)
My_ColorMap = ColorMap_List(colormap_name, colormap_values)
Abaqus_Spectrum_Generator(My_ColorMap)
# -------------------------设置文件夹路径并调用导入函数----------------------------
folder_path = r"C:\temp\MatplotlibColomap"
import_colormaps_to_abaqus(folder_path)
以上代码中的C:\temp\MatplotlibColomap
是我将matpotlib的colormapRGB色值导出至txt文件中,可在公众 号后台回复:AbaqusMatplotlib
,即可自动获取该文件。
将以上脚本在abaqus中运行即可,然后在Spectrum中选取相应配色即可。
这个方法有个小小的缺陷,就是每次重新打开Abaqus后,需要重新加载该脚本,为了方便起见,可以借助之前分享过的免费插件:Abaqus-Python内核代码调试工具【免费】,每次只需轻轻点击插件按钮,即可自动运行脚本。