|  | 
 
| NX刮削后处理算法:有需要的可以下载参考看看。这些命令放在“PB_CMD_before_motion”下面 
 global mom_motion_type
 global angle
 
 # 初始化角度
 set angle 0.0
 switch $mom_motion_type {
 "FIRSTCUT" -
 "STEPOVER" -
 "CUT"   {
 
 
 
 
 # 定义两个路径点坐标
 global mom_mcs_goto  mom_nxt_mcs_goto mom_prev_mcs_goto mom_tool_axis
 set V1 {$mom_prev_mcs_goto(0) $mom_prev_mcs_goto(1) $mom_prev_mcs_goto(2)}
 set V2 {$mom_mcs_goto(0) $mom_mcs_goto(1) $mom_mcs_goto(2)}
 
 # 计算路径切线向量
 lassign $V1 p1(0) p1(1) p1(2)
 lassign $V2 p2(0) p2(1) p2(2)
 set T(0) [expr $p2(0) - $p1(0)]
 set T(1) [expr $p2(1) - $p1(1)]
 set T(2) [expr $p2(2) - $p2(2)] ;# 特别注意2维平面 Z 向 ,这个值为 0
 
 # 计算投影到 XY 平面后的向量
 set T_proj(0) $T(0)
 set T_proj(1) $T(1)
 set T_proj(2) $T(2)
 
 # 归一化路径切线向量
 set magnitude [expr sqrt($T_proj(0)*$T_proj(0) + $T_proj(1)*$T_proj(1) + $T_proj(2)*$T_proj(2))]
 set vector_axis(0) [expr $T_proj(0) / $magnitude]
 set vector_axis(1) [expr $T_proj(1) / $magnitude]
 set vector_axis(2) [expr $T_proj(2) / $magnitude]
 
 # 设置初始方向(安装刀片或镗刀初始朝向位置,1,0,0表示朝向 X+
 set direction(0) 1
 set direction(1) 0
 set direction(2) 0
 
 # 计算两个向量的点积
 set dot [expr ($vector_axis(0) * $direction(0) + $vector_axis(1) * $direction(1) + $vector_axis(2) * $direction(2))]
 
 # 计算夹角余弦值 0 ~ +180
 if {$dot > 1.0} {
 set angle 0.0
 } elseif {$dot < -1.0} {
 set angle 180.0
 } else {
 set angle [expr (90.0 / asin(1.0)) * acos($dot)]
 }
 
 # 向量叉乘
 set cross_product(0) [expr ($vector_axis(1) * $direction(2) - $vector_axis(2) * $direction(1))]
 set cross_product(1) [expr ($vector_axis(2) * $direction(0) - $vector_axis(0) * $direction(2))]
 set cross_product(2) [expr ($vector_axis(0) * $direction(1) - $vector_axis(1) * $direction(0))]
 
 # 判断反向角度 0 ~ -180
 set pot [expr ($cross_product(0) * $mom_tool_axis(0) + $cross_product(1) * $mom_tool_axis(1) + $cross_product(2) * $mom_tool_axis(2))]
 if {  $pot > 0.0 } {
 set angle [expr -1 * $angle]
 }
 
 # 控制角度在 0~ 360
 set ang [expr fmod($angle,360)]
 set angle [expr ($ang < 0) ? ($ang + 360) : $ang]
 
 MOM_output_literal "CCCC====$angle"
 
 }
 default {}
 }
 
 
 | 
 |