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)-19extruded-20 、bounded plane-22 、fillet (blend)-23 、b-surface-43offset 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 UFimport mathdef 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_tagdef 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.ModelingedgeTagList = uf_modling_instance.AskBodyEdges(solid_body_tag)return edgeTagListdef get_solid_body_face_tags(the_pyuf_session, solid_body_tag):"""功能:獲取一個solidbody實體中的所有面的tag標識"""uf_modling_instance = the_pyuf_session.Modelingface_tag_list = uf_modling_instance.AskBodyFaces(solid_body_tag)return face_tag_listdef get_solid_body_face_edge_tags(the_pyuf_session, solid_body_face_tag):"""功能:獲取一個實體面中的所有實體邊的tag標識"""uf_modling_instance = the_pyuf_session.Modelingedge_tag_list = uf_modling_instance.AskFaceEdges(solid_body_face_tag)return edge_tag_listdef get_solid_body_edge_type(the_pyuf_session, solid_body_edge_tag):"""功能:獲取一個實體邊的類型"""uf_modling_instance = the_pyuf_session.Modelingedge_type = uf_modling_instance.AskEdgeType(solid_body_edge_tag)return edge_typedef get_solid_body_face_edge_points(the_pyuf_session, solid_body_face_egde_tag):"""功能:獲取一個邊中的所有點的坐標"""uf_modling_instance = the_pyuf_session.Modelingedge_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.Modelingreturn uf_modeling_instance.AskFeatBody(feature_tag)def get_uf_modl_edge_string(uf_modl_edge_type):"""功能:根據類型標識,獲取UG MODL Edge對象的字符串形式描述,UF_MODL_LINEAR_EDGE 3001UF_MODL_CIRCULAR_EDGE 3002UF_MODL_ELLIPTICAL_EDGE 3003UF_MODL_INTERSECTION_EDGE 3004UF_MODL_SPLINE_EDGE 3005UF_MODL_SP_CURVE_EDGE 3006UF_MODL_FOREIGN_EDGE 3007UF_MODL_CONST_PARAMETER_EDGE 3008UF_MODL_TRIMMED_CURVE_EDGE 3009UF_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)-19extruded-20 、bounded plane-22 、fillet (blend)-23 、b-surface-43offset surface-65 、foreign surface-66、Convergent surface-67"""uf_modeling_instance = the_pyuf_session.Modelingreturn 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 = 0create new target solidUF_POSITIVE = 1add to target solidUF_NEGATIVE = 2subtract from target solidUF_UNSIGNED = 3intersect with target solidUF_NO_BOOLEAN = 4feature has not been booleanedUF_TOP_TARGET = 5feature is the "top target" feature, it has no"parent" features but does have tool featuresUF_UNITE = 6feature has been united to target solidUF_SUBTRACT = 7feature has been subtracted from target solidUF_INTERSECT = 8feature has been intersected with target solidUF_DEFORM_POSITIVE = 9feature used to deform the positive sideof the target sheetUF_DEFORM_NEGATIVE = 10feature used to deform the negative sideof the target sheet"""uf_modlFeatures_instance = the_pyuf_session.ModlFeaturesuf_modl_instance = the_pyuf_session.Modlmodl_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.Objreturn 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.Modelingreturn 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.ModlFeaturesreturn uf_modlFeatures_instance.CreateBlend(radius, edge_list, smooth_overflow, cliff_overflow, notch_overflow, vrb_tool)if __name__ == '__main__':# 獲取uf sessionthe_pyuf_session = get_uf_session()# 獲取python sessionthe_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)# 獲取實體上所有邊tagblock_body_edge_tag_list = get_solid_body_edge_tags(the_pyuf_session, block_body_tag)# 平行于Z軸的豎直邊tagparallel_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軸的豎直邊倒圓角半徑20mmparallel_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)# 獲取實體上所有面tagblock_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軸的平面tagvertical_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 = 0for 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)# 保存.prtpyuf_save_prt(the_pyuf_session)# 關閉.prtpyuf_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。
運行結果:
同上
其中,檢查輸出內容: