可以通过AIS_ColorScale
给视图中加入colorbar,如上图所示:
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.AIS import AIS_ColorScale
from OCC.Core.Aspect import Aspect_TOTP_LEFT_LOWER
from OCC.Core.Graphic3d import (
Graphic3d_TMF_2d,
Graphic3d_TransformPers,
)
from OCC.Core.gp import gp_XY, gp_Pnt
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
myBox = BRepPrimAPI_MakeBox(60, 60, 50).Shape()
colorscale = AIS_ColorScale()
# colorscale properties
aMinRange = colorscale.GetMin()
aMaxRange = colorscale.GetMax()
aNbIntervals = colorscale.GetNumberOfIntervals()
aTextHeight = colorscale.GetTextHeight()
labPosition = colorscale.GetLabelPosition()
position = gp_XY(colorscale.GetXPosition(), colorscale.GetYPosition())
title = colorscale.GetTitle()
# colorscale display
colorscale.SetSize(300, 300) # 设置标尺宽度和高度(像素)
colorscale.SetRange(0.0, 10.0) # 设置数值范围(最小0.0,最大10.0)
colorscale.SetNumberOfIntervals(10) # 设置颜色分段数
colorscale.SetXPosition(50) # 向右偏移50像素
colorscale.SetYPosition(50) # 向上偏移50像素
colorscale.SetZLayer(-5) # 设置渲染层级(负值确保标尺在几何体上方)
# Graphic3d_TMF_2d 标尺始终以 2D 形式渲染(不受视角影响)
# Aspect_TOTP_LEFT_LOWER 固定在视窗左下角
colorscale.SetTransformPersistence(
Graphic3d_TransformPers(Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER)
)
colorscale.SetToUpdate()
display.Context.Display(colorscale, True)
display.DisplayShape(myBox, update=True)
start_display()
可以将colorbar的设置封装为一个方法,直接在主程序内调用即可,比如做一个类似Abaqus的colorbar
colorbar.py
from typing import Tuple, Optional
from OCC.Core.Graphic3d import Graphic3d_Vec2i
from OCC.Core.AIS import AIS_ColorScale
from OCC.Core.Aspect import (
Aspect_TOTP_LEFT_LOWER,
Aspect_TOCSD_USER,
Aspect_SequenceOfColor,
)
from OCC.Core.Graphic3d import Graphic3d_TMF_2d, Graphic3d_TransformPers
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
import matplotlib.cm as mcm
from OCC.Core.Prs3d import Prs3d_Drawer, Prs3d_TextAspect
def add_color_scale(
ctx,
vmin: float,
vmax: float,
*,
intervals: int = 11,
size: Tuple[int, int] = (220, 360),
anchor=Aspect_TOTP_LEFT_LOWER,
title: Optional[str] = "S, Mises\n(Avg: 75%)",
label_format: str = "%+.3e",
text_height: int = 24,
z_layer: int = -5,
cmap: str = "rainbow",
reverse: bool = False,
offset: Tuple[int, int] = (0, 0),
text_color: Optional[Tuple[float, float, float]] =(0, 0, 0),
text_font: str = "Times"
) -> AIS_ColorScale:
cs = AIS_ColorScale()
cs.SetRange(float(vmin), float(vmax))
cs.SetNumberOfIntervals(int(intervals))
cs.SetSize(int(size[0]), int(size[1]))
tp = Graphic3d_TransformPers(Graphic3d_TMF_2d, anchor)
tp.SetOffset2d(Graphic3d_Vec2i(int(offset[0]), int(offset[1])))
cs.SetTransformPersistence(tp)
if title:
cs.SetTitle(title)
if hasattr(cs, "SetTextHeight"):
cs.SetTextHeight(int(text_height))
if z_layer is not None:
cs.SetZLayer(int(z_layer))
if hasattr(cs, "SetFormat"):
cs.SetFormat(label_format)
if text_color is not None:
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
r, g, b = map(float, text_color)
ctx.SetColor(cs, Quantity_Color(r, g, b, Quantity_TOC_RGB), True)
if text_font:
drw = cs.Attributes() or Prs3d_Drawer()
ta = drw.TextAspect() or Prs3d_TextAspect()
ta.SetFont(text_font)
drw.SetTextAspect(ta)
cs.SetAttributes(drw)
cs.SetToUpdate()
# 用户自定义色条(用 matplotlib)
cs.SetColorType(Aspect_TOCSD_USER)
n = max(1, int(intervals))
cm = mcm.get_cmap(cmap, n)
seq = Aspect_SequenceOfColor()
for i in range(n):
t = i / (n - 1) if n > 1 else 0.0
if reverse:
t = 1.0 - t
r, g, b, _ = cm(t)
seq.Append(Quantity_Color(float(r), float(g), float(b), Quantity_TOC_RGB))
cs.SetColors(seq)
cs.SetToUpdate()
ctx.Display(cs, True)
return cs
主程序:
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from colorbar import add_color_scale
from OCC.Display.SimpleGui import init_display
from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB
display, start_display, add_menu, add_function_to_menu = init_display()
myBox = BRepPrimAPI_MakeBox(60, 60, 50).Shape()
data_min, data_max = 0.0, 10.0
cs = add_color_scale(
display.Context,
data_min, data_max,
intervals=12,
size=(200, 400),
title="S, Mises\n(Avg: 75%)",
label_format="%+.2e",
text_height=24,
text_color = (0, 0, 0),
text_font = "Times",
cmap="coolwarm",
reverse=False,
offset=(20, 10),
)
display.DisplayShape(myBox, update=True)
start_display()