Siemens-NXUG二次开发-创建倒斜角特征、边倒圆角特征、设置对象颜色、获取面信息[Python UF][20240605]
- 1.python uf函数
- 1.1 NXOpen.UF.Modeling.AskFaceData
- 1.2 NXOpen.UF.Modeling.CreateChamfer
- 1.3 NXOpen.UF.ModlFeatures.CreateBlend
- 1.4 NXOpen.UF.Obj.SetColor
- 2.实体目标面边识别
- 2.1 识别平行于Z轴的竖直边(倒圆角)
- 2.1 识别垂直于Z轴的平面(倒斜角)
- 3.示例代码
- 3.1 pyuf_chamfer_blend.py
- 4.运行结果
- 4.1 内部模式
- 4.2 外部模式
1.python uf函数
1.1 NXOpen.UF.Modeling.AskFaceData
# 内部和外部模式可用
"""
返回值:一个元组,元素类型为python的int类型,块特征的feature tag标识。
"""
def NXOpen.UF.Modeling.AskFaceData(self, face_tag)
'''
face_tag:面的tag标识
[返回值]一个元组 (type-int,
face point-list of float,
dir-list of float,
Face boundary-list of float,
Face major radius-float,
Face minor radius-float,
Face normal direction-int)
其中元组0位置:
cylinder-16、cone-17 、sphere-18 、revolved (toroidal)-19
extruded-20 、bounded plane-22 、fillet (blend)-23 、b-surface-43
offset surface-65 、foreign surface-66、Convergent surface-67
'''
1.2 NXOpen.UF.Modeling.CreateChamfer
# 内部和外部模式可用
"""
返回值:一个tag,倒斜角特征tag。
"""
def NXOpen.UF.Modeling.CreateChamfer(self, subtype, offset1, offset2, theta, edges)
'''
subtype-int:1-单向偏置、2-双向偏置、3-偏置和角度、4-自由单向偏置、5-自由双向偏置,
offset1-str:偏置值1,
offset2-str:偏置值2,
theta-str:倒斜角角度值,
edges-int list:要倒斜角实体边的tag列表
[返回值]一个整数,倒角特征tag标识
'''
1.3 NXOpen.UF.ModlFeatures.CreateBlend
# 内部和外部模式可用
"""
返回值:一个tag,倒圆角特征tag。
"""
def NXOpen.UF.ModlFeatures.CreateBlend(self, radius, edge_list, smooth_overflow, cliff_overflow, notch_overflow, vrb_tool)
'''
radius-str:倒圆角半径,
edge_list-int list:要倒圆角实体边的tag列表,
smooth_overflow-int:倒圆角平滑溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
cliff_overflow-int:倒圆角峭壁溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
notch_overflow-int:倒圆角凹槽溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
vrb_tool-float:倒圆角公差
[返回值]一个整数,倒圆角特征tag标识
'''
1.4 NXOpen.UF.Obj.SetColor
# 内部和外部模式可用
"""
返回值:一个整数,0-成功执行,非零正整数-错误大代码。
"""
def NXOpen.UF.Obj.SetColor(self, object_tag, color_id)
'''
object_tag:正整数,对象tag标识
color_id:正整数-颜色号
'''
2.实体目标面边识别
2.1 识别平行于Z轴的竖直边(倒圆角)
- 从块特征tag获取该特征所属的实体tag
- 从实体tag获取所有的边tag
- 循环边tag,判断其所在向量是否平行于Z轴,即找到Z竖直边
识别开始时,当前3D实体状态:
识别完成后,倒圆角操作后,当前3D实体状态:
2.1 识别垂直于Z轴的平面(倒斜角)
- 从块特征tag获取该特征所属的实体tag
- 从实体tag获取所有的面tag
- 循环面tag,判断是否是平面且法线平行于Z轴,即平面垂直于Z轴,找到竖直边倒圆角后实体的上下两个平面
识别开始时,当前3D实体状态:
识别完成后,倒斜角操作后,当前3D实体状态:
3.示例代码
3.1 pyuf_chamfer_blend.py
import NXOpen
import NXOpen.UF as UF
import math
def get_uf_session():
# 获取当前python UF会话
return UF.UFSession.GetUFSession()
def get_py_session():
# 获取当前python会话
return NXOpen.Session.GetSession()
def pyuf_new_prt(the_pyuf_session, new_prt_file_name, units = 1):
"""
功能:创建一个指定文件路径和文件名的.prt文件,默认单位制是米(m)
"""
# 由于要对Part进行操作,因此需要获取Part实例对象
pyuf_part_instance = the_pyuf_session.Part
# New方法位于Part类对象中
new_prt_file_tag = pyuf_part_instance.New(new_prt_file_name, units)
return new_prt_file_tag
def pyuf_save_prt(the_pyuf_session):
"""
功能:保存当前工作part
"""
# 由于要对Part进行操作,因此需要获取Part实例对象
pyuf_part_instance = the_pyuf_session.Part
# Save方法位于Part类对象中
return pyuf_part_instance.Save()
def pyuf_close_prt(the_pyuf_session, part_tag, scope, mode):
"""
功能:关闭当前工作part
"""
# 由于要对Part进行操作,因此需要获取Part实例对象
pyuf_part_instance = the_pyuf_session.Part
# Close方法位于Part类对象中
return pyuf_part_instance.Close(part_tag, scope, mode)
def get_solid_body_edge_tags(the_pyuf_session, solid_body_tag):
"""
获取一个solidbody实体中的所有边的tag标识
"""
uf_modling_instance = the_pyuf_session.Modeling
edgeTagList = uf_modling_instance.AskBodyEdges(solid_body_tag)
return edgeTagList
def get_solid_body_face_tags(the_pyuf_session, solid_body_tag):
"""
功能:获取一个solidbody实体中的所有面的tag标识
"""
uf_modling_instance = the_pyuf_session.Modeling
face_tag_list = uf_modling_instance.AskBodyFaces(solid_body_tag)
return face_tag_list
def get_solid_body_face_edge_tags(the_pyuf_session, solid_body_face_tag):
"""
功能:获取一个实体面中的所有实体边的tag标识
"""
uf_modling_instance = the_pyuf_session.Modeling
edge_tag_list = uf_modling_instance.AskFaceEdges(solid_body_face_tag)
return edge_tag_list
def get_solid_body_edge_type(the_pyuf_session, solid_body_edge_tag):
"""
功能:获取一个实体边的类型
"""
uf_modling_instance = the_pyuf_session.Modeling
edge_type = uf_modling_instance.AskEdgeType(solid_body_edge_tag)
return edge_type
def get_solid_body_face_edge_points(the_pyuf_session, solid_body_face_egde_tag):
"""
功能:获取一个边中的所有点的坐标
"""
uf_modling_instance = the_pyuf_session.Modeling
edge_type = get_solid_body_edge_type(the_pyuf_session, solid_body_face_egde_tag)
edge_data = uf_modling_instance.AskEdgeVerts(solid_body_face_egde_tag)
edgeTypeString = get_uf_modl_edge_string(edge_type)
return [edge_type, edgeTypeString, edge_data[2], edge_data[0], edge_data[1]]
def get_feature_body(the_pyuf_session, feature_tag):
"""
查询特征所属body的tag
"""
uf_modeling_instance = the_pyuf_session.Modeling
return uf_modeling_instance.AskFeatBody(feature_tag)
def get_uf_modl_edge_string(uf_modl_edge_type):
"""
功能:根据类型标识,获取UG MODL Edge对象的字符串形式描述,
UF_MODL_LINEAR_EDGE 3001
UF_MODL_CIRCULAR_EDGE 3002
UF_MODL_ELLIPTICAL_EDGE 3003
UF_MODL_INTERSECTION_EDGE 3004
UF_MODL_SPLINE_EDGE 3005
UF_MODL_SP_CURVE_EDGE 3006
UF_MODL_FOREIGN_EDGE 3007
UF_MODL_CONST_PARAMETER_EDGE 3008
UF_MODL_TRIMMED_CURVE_EDGE 3009
UF_MODL_CONVERGENT_EDGE 100007
"""
if type(uf_modl_edge_type) != type(0):
return ""
if uf_modl_edge_type == UF.UFConstants.UF_MODL_LINEAR_EDGE:
return "3001-UF_MODL_LINEAR_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_CIRCULAR_EDGE:
return "3002-UF_MODL_CIRCULAR_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_ELLIPTICAL_EDGE:
return "3003-UF_MODL_ELLIPTICAL_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_INTERSECTION_EDGE:
return "3004-UF_MODL_INTERSECTION_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_SPLINE_EDGE:
return "3005-UF_MODL_SPLINE_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_SP_CURVE_EDGE:
return "3006-UF_MODL_SP_CURVE_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_FOREIGN_EDGE:
return "3007-UF_MODL_FOREIGN_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_CONST_PARAMETER_EDGE:
return "3008-UF_MODL_CONST_PARAMETER_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_TRIMMED_CURVE_EDGE:
return "3009-UF_MODL_TRIMMED_CURVE_EDGE-Type"
elif uf_modl_edge_type == UF.UFConstants.UF_MODL_CONVERGENT_EDGE:
return "100007-UF_MODL_CONVERGENT_EDGE-Type"
return "00-unknow-ModlEdgeType"
def get_face_data(the_pyuf_session, face_tag):
"""
查询面的数据
[返回值]一个元组 (type-int,
face point-list of float,
dir-list of float,
Face boundary-list of float,
Face major radius-float,
Face minor radius-float,
Face normal direction-int)
其中元组0位置:
cylinder-16、cone-17 、sphere-18 、revolved (toroidal)-19
extruded-20 、bounded plane-22 、fillet (blend)-23 、b-surface-43
offset surface-65 、foreign surface-66、Convergent surface-67
"""
uf_modeling_instance = the_pyuf_session.Modeling
return uf_modeling_instance.AskFaceData(face_tag)
def createBlock(the_pyuf_session, corner_point, size, signs = 0):
"""
python uf创建块(长方体)
corner_point-float list[x,y,z]:长方体角点坐标,size-str list[x_size, y_size,z_size]:块长宽高尺寸
返回值是一个整数:块的feature tag标识
signs意义:
UF_NULLSIGN = 0
create new target solid
UF_POSITIVE = 1
add to target solid
UF_NEGATIVE = 2
subtract from target solid
UF_UNSIGNED = 3
intersect with target solid
UF_NO_BOOLEAN = 4
feature has not been booleaned
UF_TOP_TARGET = 5
feature is the "top target" feature, it has no
"parent" features but does have tool features
UF_UNITE = 6
feature has been united to target solid
UF_SUBTRACT = 7
feature has been subtracted from target solid
UF_INTERSECT = 8
feature has been intersected with target solid
UF_DEFORM_POSITIVE = 9
feature used to deform the positive side
of the target sheet
UF_DEFORM_NEGATIVE = 10
feature used to deform the negative side
of the target sheet
"""
uf_modlFeatures_instance = the_pyuf_session.ModlFeatures
uf_modl_instance = the_pyuf_session.Modl
modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
return uf_modlFeatures_instance.CreateBlock1(modl_feature_signs, corner_point, size)
def setCorlor(the_pyuf_session, object_tag, color_id = 0):
"""
给UG对象设置颜色(面、特征、体等)
"""
uf_obj_instance = the_pyuf_session.Obj
return uf_obj_instance.SetColor(object_tag, color_id)
def createChafmer(the_pyuf_session, subtype, offset1, offset2, theta, edges):
"""
python uf创建边的倒斜角
subtype-int:1-单向偏置、2-双向偏置、3-偏置和角度、4-自由单向偏置、5-自由双向偏置,
offset1-str:偏置值1,offset2-str:偏置值2,theta-str:倒斜角角度值,edges-int list:要倒斜角实体边的tag列表
返回:倒斜角feature tag标识
"""
uf_modeling_instance = the_pyuf_session.Modeling
return uf_modeling_instance.CreateChamfer(subtype, offset1, offset2, theta, edges)
def createBlend(the_pyuf_session, radius, edge_list, smooth_overflow = 1, cliff_overflow = 1, notch_overflow = 1, vrb_tool = 0.0001):
"""
python uf创建边的倒圆角
radius-str:倒圆角半径,edge_list-int list:要倒圆角实体边的tag列表,
smooth_overflow-int:倒圆角平滑溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
cliff_overflow-int:倒圆角峭壁溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
notch_overflow-int:倒圆角凹槽溢出值、0-允许这种类型倒圆、1-防止这种类型倒圆,
vrb_tool-float:倒圆角公差
返回:倒圆角feature tag标识
"""
uf_modlFeatures_instance = the_pyuf_session.ModlFeatures
return uf_modlFeatures_instance.CreateBlend(radius, edge_list, smooth_overflow, cliff_overflow, notch_overflow, vrb_tool)
if __name__ == '__main__':
# 获取uf session
the_pyuf_session = get_uf_session()
# 获取python session
the_py_session = get_py_session()
# 新建prt文件路径与名称
new_prt_file_name = 'D:\\pyuf_chamfer_blend.prt'
new_prt_file_tag = pyuf_new_prt(the_pyuf_session, new_prt_file_name)
# 创建长方体
block_feature_tag = createBlock(the_pyuf_session, [100.0, 100.0, 100.0], ['250.0', '450.0', '80.0'])
"""
1.当前的3D模型是一个简单的长方体
"""
# 从某个特征上查询该特征所属的实体
block_body_tag = get_feature_body(the_pyuf_session, block_feature_tag)
# 获取实体上所有边tag
block_body_edge_tag_list = get_solid_body_edge_tags(the_pyuf_session, block_body_tag)
# 平行于Z轴的竖直边tag
parallel_z_edge_tag_list = []
# [edge_type, edgeTypeString, edge_data[2], edge_data[0], edge_data[1]]
# 长方体上下两个平面外轮廓边倒斜角 2mm 45°
for item_edge in block_body_edge_tag_list:
item_edge_point_info_list = get_solid_body_face_edge_points(the_pyuf_session, item_edge)
item_edge_dir = [item_edge_point_info_list[3][0] - item_edge_point_info_list[4][0],
item_edge_point_info_list[3][1] - item_edge_point_info_list[4][1],
item_edge_point_info_list[3][2] - item_edge_point_info_list[4][2],
]
#print("item_edge_dir:", item_edge_dir)
if math.fabs(item_edge_dir[0] - 0.000000) <= 1e-6 \
and math.fabs(item_edge_dir[1] - 0.000000) <= 1e-6 \
and item_edge_dir[2] != 0.000000:
# item_edge_dir平行于Z轴
parallel_z_edge_tag_list.append(item_edge)
print("parallel_z_edge_tag_list:", parallel_z_edge_tag_list)
# 垂平行于Z轴的竖直边倒圆角半径20mm
parallel_z_edge_blend_feature_tag = createBlend(the_pyuf_session, "20.0", parallel_z_edge_tag_list)
# 找到当前3D实体的tag(从特征上查询该特征所属的实体)
"""
2.当前的3D模型是一个4条平行于Z轴竖直边倒圆角半径20mm的长方体
"""
# 从某个特征上查询该特征所属的实体
block_body_tag = get_feature_body(the_pyuf_session, parallel_z_edge_blend_feature_tag)
# 获取实体上所有面tag
block_body_face_tag_list = get_solid_body_face_tags(the_pyuf_session, block_body_tag)
print("block_body_face_tag_list:", block_body_face_tag_list)
# 垂直于Z轴的平面tag
vertical_z_face_tag_list = []
for item_face in block_body_face_tag_list:
item_face_data_tuple = get_face_data(the_pyuf_session, item_face)
print("item_face_data_tuple:", item_face_data_tuple)
if item_face_data_tuple[0] == 22:
# 是平面类型
if math.fabs(math.fabs(item_face_data_tuple[2][0]) - 0.000000) <= 1e-6 \
and math.fabs(math.fabs(item_face_data_tuple[2][1]) - 0.000000) <= 1e-6 \
and math.fabs(item_face_data_tuple[2][2]) != 0.000000:
# 面的法线平行于Z轴即平面垂直于Z轴
vertical_z_face_tag_list.append(item_face)
print("vertical_z_face_tag_list:", vertical_z_face_tag_list)
vertical_z_face_edge_chafmer_feature_tag = 0
for item_face in vertical_z_face_tag_list:
item_face_edge_tag_list = get_solid_body_face_edge_tags(the_pyuf_session, item_face)
vertical_z_face_edge_chafmer_feature_tag = createChafmer(the_pyuf_session, 1, "2.000000", "2.000000", "45", item_face_edge_tag_list)
# 从某个特征上查询该特征所属的实体
block_body_tag = get_feature_body(the_pyuf_session, vertical_z_face_edge_chafmer_feature_tag)
setCorlor(the_pyuf_session, block_body_tag, 166)
# 保存.prt
pyuf_save_prt(the_pyuf_session)
# 关闭.prt
pyuf_close_prt(the_pyuf_session, new_prt_file_tag, 0, 1)
4.运行结果
4.1 内部模式
选中要运行的.py文件后,点击“管道通路”即可。
运行结果:
4.2 外部模式
cmd命令:“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_chamfer_blend.py。
powershell命令:&“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_chamfer_blend.py。
运行结果:
同上
其中,检查输出内容: