首页/文章/ 详情

Python_occ 学习记录 | 尺寸标注

22小时前浏览1

标注样式

from OCC.Core.gp import gp_Dir, gp_Ax2, gp_Circ, gp_Pnt
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_RadiusDimension
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Core.Prs3d import Prs3d_DimensionAspect
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Display.SimpleGui import init_display

display, start_display, add_menu, add_function_to_menu = init_display()

# 几何对象
c = gp_Circ(gp_Ax2(gp_Pnt(200.0200.00.0), gp_Dir(0.00.01.0)), 80)
ec = BRepBuilderAPI_MakeEdge(c).Edge()
# AIS_Shape:显示层对象,把拓扑边/面/体包装成可交互图元(可选中、着色、隐藏、变透明等)
ais_shp = AIS_Shape(ec)
display.Context.Display(ais_shp, True)

# 创建半径标注
rd = PrsDim_RadiusDimension(ec)
the_aspect = Prs3d_DimensionAspect() # 尺寸标注的“样式表”(颜色、文字、箭头、延长线等外观)
the_aspect.SetCommonColor(Quantity_Color(Quantity_NOC_BLACK))
rd.SetDimensionAspect(the_aspect)

display.Context.Display(rd, True)
display.FitAll()
start_display()

SetArrowAspect

arr = the_aspect.ArrowAspect()
arr.SetLength(15.0# 箭头长度
arr.SetAngle(0.5# 箭头开角

ArrowOrientation

from OCC.Core.Prs3d import Prs3d_DAO_External

the_aspect.SetArrowOrientation(Prs3d_DAO_External)  # 外部箭头

SetLineAspect

将箭头、线、标注文字,样式分开设置

from OCC.Core.Prs3d import Prs3d_LineAspect
from OCC.Core.Aspect import Aspect_TOL_SOLID

# 线:红色
line_asp = Prs3d_LineAspect(Quantity_Color(Quantity_NOC_RED), Aspect_TOL_SOLID, 2.0)
the_aspect.SetLineAspect(line_asp)

# 箭头:蓝色
arr_asp = the_aspect.ArrowAspect()
arr_asp.SetColor(Quantity_Color(Quantity_NOC_BLUE1))

# 文字:黑色
txt_asp = the_aspect.TextAspect()
txt_asp.SetColor(Quantity_Color(Quantity_NOC_BLACK))
txt_asp.SetHeight(14)
the_aspect.SetTextAspect(txt_asp)

SetValueStringFormat

the_aspect.SetValueStringFormat("%.2f mm")

MakeArrows3d

the_aspect.MakeArrows3d(True)

MakeText3d

the_aspect.MakeText3d(True)

MakeTextShaded

the_aspect.MakeTextShaded(True)

标注方法

直径标注 DiameterDimension

from OCC.Core.PrsDim import PrsDim_DiameterDimension
dim = PrsDim_DiameterDimension(edge_circ)
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Circ, gp_Lin
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.Prs3d import Prs3d_DimensionAspect
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_RED, Quantity_NOC_BLUE1
from OCC.Display.SimpleGui import init_display

p0 = gp_Pnt(200.0200.00.0)
circ = gp_Circ(gp_Ax2(p0, gp_Dir(001)), 80.0)
edge_circ = BRepBuilderAPI_MakeEdge(circ).Edge()

display, start_display, _, _ = init_display()

display.Context.Display(AIS_Shape(edge_circ), True)

the_aspect = Prs3d_DimensionAspect()
the_aspect.SetCommonColor(Quantity_Color(Quantity_NOC_BLACK))

from OCC.Core.PrsDim import PrsDim_DiameterDimension
dim = PrsDim_DiameterDimension(edge_circ)

dim.SetDimensionAspect(the_aspect)
display.Context.Display(dim, True)
display.FitAll()
start_display()

长度标注 LengthDimension

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_LengthDimension
from OCC.Core.Prs3d import Prs3d_DimensionAspect
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeVertex
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

# 统一的工作平面(用于标注的放置/投影)
work_pln = gp_Pln(gp_Pnt(000), gp_Dir(001))

# 统一的简单样式:只设为黑色
dim_aspect = Prs3d_DimensionAspect()
dim_aspect.SetCommonColor(Quantity_Color(Quantity_NOC_BLACK))

# ---------------- 两点之间长度 ----------------
A = gp_Pnt(1202600)
B = gp_Pnt(3202600)
vA = BRepBuilderAPI_MakeVertex(A).Vertex()
vB = BRepBuilderAPI_MakeVertex(B).Vertex()

# 显示两点与连线
ais_vA = AIS_Shape(vA); display.Context.Display(ais_vA, False)
ais_vB = AIS_Shape(vB); display.Context.Display(ais_vB, False)

# 标注
dim_A = PrsDim_LengthDimension(A, B, work_pln)
dim_A.SetDimensionAspect(dim_aspect)
display.Context.Display(dim_A, False)

# ---------------- 单条边的长度 ----------------
P1 = gp_Pnt(1202000)
P2 = gp_Pnt(2602000)
edge_seg = BRepBuilderAPI_MakeEdge(P1, P2).Edge()

ais_seg = AIS_Shape(edge_seg); display.Context.Display(ais_seg, False)

dim_B = PrsDim_LengthDimension(edge_seg, work_pln)
dim_B.SetDimensionAspect(dim_aspect)
display.Context.Display(dim_B, False)

# 视图
display.FitAll()

start_display()
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_LengthDimension
from OCC.Core.Prs3d import Prs3d_DimensionAspect
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

# 工作平面(定义标注的放置/投影方向):XY 平面
work_pln = gp_Pln(gp_Pnt(000), gp_Dir(001))

p1 = gp_Pnt(1202000); p2 = gp_Pnt(3202000)
q1 = gp_Pnt(1202400); q2 = gp_Pnt(3202400)
edge_a = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
edge_b = BRepBuilderAPI_MakeEdge(q1, q2).Edge()

ais_a = AIS_Shape(edge_a); display.Context.Display(ais_a, False)
ais_b = AIS_Shape(edge_b); display.Context.Display(ais_b, False)

dim = PrsDim_LengthDimension(edge_a, edge_b, work_pln)

asp = Prs3d_DimensionAspect()
asp.SetCommonColor(Quantity_Color(Quantity_NOC_BLACK))
dim.SetDimensionAspect(asp)

display.Context.Display(dim, False)
display.FitAll()

start_display()

角度标注 AngleDimension

from OCC.Core.gp import gp_Pnt
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_AngleDimension
from OCC.Core.Prs3d import Prs3d_DimensionAspect
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

p0 = gp_Pnt(2002000)
p1 = gp_Pnt(3202000)  
p2 = gp_Pnt(2603000)  

e1 = BRepBuilderAPI_MakeEdge(p0, p1).Edge()
e2 = BRepBuilderAPI_MakeEdge(p0, p2).Edge()

ais_e1 = AIS_Shape(e1); display.Context.Display(ais_e1, False)
ais_e2 = AIS_Shape(e2); display.Context.Display(ais_e2, False)

dim = PrsDim_AngleDimension(e1, e2)

asp = Prs3d_DimensionAspect()
asp.SetCommonColor(Quantity_Color(Quantity_NOC_BLACK))
dim.SetDimensionAspect(asp)

display.Context.Display(dim, False)
display.FitAll()

start_display()

偏移距离标注 OffsetDimension

from OCC.Core.gp import gp_Pnt
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape, AIS_TextLabel
from OCC.Core.PrsDim import PrsDim_OffsetDimension
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_BLUE1, Quantity_NOC_RED
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

# —— 两条平行边:y=200 和 y=240(偏移量 40)——
p1 = gp_Pnt(1202000); p2 = gp_Pnt(3202000)
q1 = gp_Pnt(1202400); q2 = gp_Pnt(3202400)
edge1 = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
edge2 = BRepBuilderAPI_MakeEdge(q1, q2).Edge()

# 显示参与几何(蓝、红)
ais1 = AIS_Shape(edge1); display.Context.SetColor(ais1, Quantity_Color(Quantity_NOC_BLUE1), True)
ais2 = AIS_Shape(edge2); display.Context.SetColor(ais2, Quantity_Color(Quantity_NOC_RED),   True)
display.Context.Display(ais1, False)
display.Context.Display(ais2, False)

dist_val = 40.0
label_text = f"{dist_val:.1f} mm"
offset_rel = PrsDim_OffsetDimension(edge1, edge2, dist_val, label_text)
# 注意:offset_rel 不是 AIS_InteractiveObject,不能直接 Context.Display

xm = (p1.X() + p2.X()) * 0.5
lead_a = gp_Pnt(xm, p1.Y(), 0)
lead_b = gp_Pnt(xm, q1.Y(), 0)
lead_edge = BRepBuilderAPI_MakeEdge(lead_a, lead_b).Edge()
ais_lead = AIS_Shape(lead_edge)
display.Context.SetColor(ais_lead, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(ais_lead, False)

txt = AIS_TextLabel()
txt.SetPosition(gp_Pnt(xm + 10, (p1.Y()+q1.Y())*0.50))
txt.SetColor(Quantity_Color(Quantity_NOC_BLACK))
txt.SetText(label_text)
display.Context.Display(txt, False)

display.FitAll()

start_display()

同心约束 ConcentricRelation

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Circ, gp_Pln
from OCC.Core.Geom import Geom_Plane
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_ConcentricRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLUE1, Quantity_NOC_RED, Quantity_NOC_BLACK
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

center = gp_Pnt(2002000)
ax2 = gp_Ax2(center, gp_Dir(001))
circ_outer = gp_Circ(ax2, 60)
circ_inner = gp_Circ(ax2, 35)

edge_outer = BRepBuilderAPI_MakeEdge(circ_outer).Edge()
edge_inner = BRepBuilderAPI_MakeEdge(circ_inner).Edge()

ais_outer = AIS_Shape(edge_outer); display.Context.SetColor(ais_outer, Quantity_Color(Quantity_NOC_BLUE1), True)
ais_inner = AIS_Shape(edge_inner); display.Context.SetColor(ais_inner, Quantity_Color(Quantity_NOC_RED),   True)
display.Context.Display(ais_outer, False)
display.Context.Display(ais_inner, False)

pln = gp_Pln(gp_Pnt(000), gp_Dir(001))
geom_plane = Geom_Plane(pln)

rel = PrsDim_ConcentricRelation(edge_outer, edge_inner, geom_plane)
display.Context.SetColor(rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(rel, False)

display.FitAll()

start_display()

平行关系 ParallelRelation

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax3, gp_Pln
from OCC.Core.Geom import Geom_Plane
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_ParallelRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLUE1, Quantity_NOC_RED, Quantity_NOC_BLACK
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

p1 = gp_Pnt(1002000); p2 = gp_Pnt(3002000)
q1 = gp_Pnt(1002400); q2 = gp_Pnt(3002400)

edge1 = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
edge2 = BRepBuilderAPI_MakeEdge(q1, q2).Edge()

ais1 = AIS_Shape(edge1); display.Context.SetColor(ais1, Quantity_Color(Quantity_NOC_BLUE1), True)
ais2 = AIS_Shape(edge2); display.Context.SetColor(ais2, Quantity_Color(Quantity_NOC_RED),   True)
display.Context.Display(ais1, False)
display.Context.Display(ais2, False)

pln = gp_Pln(gp_Ax3(gp_Pnt(0,0,0), gp_Dir(0,0,1)))
geom_plane = Geom_Plane(pln)

par_rel = PrsDim_ParallelRelation(edge1, edge2, geom_plane)
display.Context.SetColor(par_rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(par_rel, False)

display.FitAll()

start_display()

垂直关系 PerpendicularRelation

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax3, gp_Pln
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_PerpendicularRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_BLUE1, Quantity_NOC_RED
from OCC.Core.Geom import Geom_Plane
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

e_h = BRepBuilderAPI_MakeEdge(gp_Pnt(1402000), gp_Pnt(3002000)).Edge()
e_v = BRepBuilderAPI_MakeEdge(gp_Pnt(2001400), gp_Pnt(2003000)).Edge()

ais_h = AIS_Shape(e_h); display.Context.SetColor(ais_h, Quantity_Color(Quantity_NOC_BLUE1), True)
ais_v = AIS_Shape(e_v); display.Context.SetColor(ais_v, Quantity_Color(Quantity_NOC_RED),   True)
display.Context.Display(ais_h, False)
display.Context.Display(ais_v, False)

pln = gp_Pln(gp_Ax3(gp_Pnt(000), gp_Dir(001)))
geom_plane = Geom_Plane(pln)

rel = PrsDim_PerpendicularRelation(e_h, e_v, geom_plane)
display.Context.SetColor(rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(rel, False)

display.FitAll()
start_display()

相切关系 TangentRelation

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Circ, gp_Pln
from OCC.Core.Geom import Geom_Plane
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_TangentRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_BLUE1, Quantity_NOC_RED
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

# 圆(中心 (200,200,0),半径 80)
center = gp_Pnt(2002000)
circ = gp_Circ(gp_Ax2(center, gp_Dir(001)), 80.0)
edge_circ = BRepBuilderAPI_MakeEdge(circ).Edge()

# 与圆上切点 (200, 280, 0) 相切的水平线段
p1 = gp_Pnt(1202800)
p2 = gp_Pnt(2802800)
edge_line = BRepBuilderAPI_MakeEdge(p1, p2).Edge()

# 显示参与几何
ais_circ = AIS_Shape(edge_circ)
ais_line = AIS_Shape(edge_line)
display.Context.SetColor(ais_circ, Quantity_Color(Quantity_NOC_BLUE1), True)
display.Context.SetColor(ais_line, Quantity_Color(Quantity_NOC_RED), True)
display.Context.Display(ais_circ, False)
display.Context.Display(ais_line, False)

# 参考平面:XY 平面
pln = gp_Pln(gp_Pnt(000), gp_Dir(001))
geom_plane = Geom_Plane(pln)

# 相切关系标注
tangent_rel = PrsDim_TangentRelation(edge_line, edge_circ, geom_plane)

display.Context.SetColor(tangent_rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(tangent_rel, True)

display.FitAll()

start_display()

相等距离 EqualDistanceRelation

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.Geom import Geom_Plane
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex, BRepBuilderAPI_MakeEdge
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_EqualDistanceRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

pln = gp_Pln(gp_Pnt(000), gp_Dir(001))
gpln = Geom_Plane(pln) 

A1, B1 = gp_Pnt(1002000), gp_Pnt(2002000)
A2, B2 = gp_Pnt(1001600), gp_Pnt(2001600)

vA1 = BRepBuilderAPI_MakeVertex(A1).Vertex()
vB1 = BRepBuilderAPI_MakeVertex(B1).Vertex()
vA2 = BRepBuilderAPI_MakeVertex(A2).Vertex()
vB2 = BRepBuilderAPI_MakeVertex(B2).Vertex()

e1 = BRepBuilderAPI_MakeEdge(A1, B1).Edge()
e2 = BRepBuilderAPI_MakeEdge(A2, B2).Edge()
ais1 = AIS_Shape(e1); display.Context.Display(ais1, False)
ais2 = AIS_Shape(e2); display.Context.Display(ais2, False)

rel = PrsDim_EqualDistanceRelation(vA1, vB1, vA2, vB2, gpln)

display.Context.SetColor(rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(rel, False)

display.FitAll()
start_display()

相等半径

from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Circ, gp_Pln
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.Geom import Geom_Plane
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_EqualRadiusRelation
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_BLUE1, Quantity_NOC_RED
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

R = 60.0
c1 = gp_Circ(gp_Ax2(gp_Pnt(2002000), gp_Dir(001)), R)
c2 = gp_Circ(gp_Ax2(gp_Pnt(3802000), gp_Dir(001)), R)

e1 = BRepBuilderAPI_MakeEdge(c1).Edge()
e2 = BRepBuilderAPI_MakeEdge(c2).Edge()

ais1 = AIS_Shape(e1); display.Context.SetColor(ais1, Quantity_Color(Quantity_NOC_BLUE1), True)
ais2 = AIS_Shape(e2); display.Context.SetColor(ais2, Quantity_Color(Quantity_NOC_RED),   True)
display.Context.Display(ais1, False)
display.Context.Display(ais2, False)

pln = gp_Pln(gp_Pnt(000), gp_Dir(001))
geom_plane = Geom_Plane(pln)

eq_rel = PrsDim_EqualRadiusRelation(e1, e2, geom_plane)

display.Context.SetColor(eq_rel, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.Display(eq_rel, False)

display.FitAll()
start_display()

最大/小半径

MinRadiusDimension、MaxRadiusDimension

 
 
from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2, gp_Elips
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.BRep import BRep_Tool
from OCC.Core.Geom import Geom_Ellipse, Geom_Circle
from OCC.Core.AIS import AIS_Shape
from OCC.Core.PrsDim import PrsDim_MinRadiusDimension, PrsDim_MaxRadiusDimension
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK, Quantity_NOC_BLUE1
from OCC.Display.SimpleGui import init_display

display, start_display, _, _ = init_display()

center = gp_Pnt(000)
ax2 = gp_Ax2(center, gp_Dir(001))
el = gp_Elips(ax2, 90.040.0)
edge_el = BRepBuilderAPI_MakeEdge(el).Edge()

ais_el = AIS_Shape(edge_el)
display.Context.SetColor(ais_el, Quantity_Color(Quantity_NOC_BLUE1), True)
display.Context.Display(ais_el, False)

# -------- 自动获取最小/最大半径 --------
curv_h, f, l = BRep_Tool.Curve(edge_el)

min_val = None
max_val = None

ell = Geom_Ellipse.DownCast(curv_h)
if ell isnotNone:
    max_val = ell.MajorRadius()
    min_val = ell.MinorRadius()
else:
    cir = Geom_Circle.DownCast(curv_h)
    if cir isnotNone:
        max_val = min_val = cir.Radius()

if min_val isNoneor max_val isNone:
    raise RuntimeError("This edge is not an ellipse or a circle, so the minimum/maximum radius cannot be determined automatically.")


# -------- 创建最小/最大半径标注(文本用 str) --------
dim_min = PrsDim_MinRadiusDimension(edge_el, float(min_val), f"{min_val:.1f} mm")
dim_max = PrsDim_MaxRadiusDimension(edge_el, float(max_val), f"{max_val:.1f} mm")

display.Context.SetColor(dim_min, Quantity_Color(Quantity_NOC_BLACK), True)
display.Context.SetColor(dim_max, Quantity_Color(Quantity_NOC_BLACK), True)

display.Context.Display(dim_min, False)
display.Context.Display(dim_max, False)

display.FitAll()
display.View_Top()
start_display()


来源:易木木响叮当
ACTpythonUM
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2025-09-07
最近编辑:22小时前
易木木响叮当
硕士 有限元爱好者
获赞 264粉丝 380文章 412课程 2
点赞
收藏
作者推荐
未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习计划 福利任务
下载APP
联系我们
帮助与反馈