Siemens-NXUG二次开发-创建块(长方体)特征、圆柱特征、圆锥或圆台特征、球体特征、管道特征[Python UF][20240504]

1.python uf函数

1.1 NXOpen.UF.ModlFeatures.CreateBlock1

# 内部和外部模式可用
"""
官方文档翻译:创建块。角点指示要创建的块的原点,X、Y和Z长度控制边长度的大小,sign参数控制执行的布尔运算,此函数的输出是与块相关联的对象标识符,原点是相对于绝对CSYS的,但是,边长度的方向是相对于WCS的。
返回值:一个tag,元素类型为python的int类型,块特征的feature tag标识。
"""             
def NXOpen.UF.ModlFeatures.CreateBlock1	(self, sign, corner_pt, medge_len)
	'''
	sign(NXOpen.UF.Modl.FeatureSigns) 指示创建的同时执行某种布尔运算, UF_NULLSIGN:创建新的目标体、UF_POSITIVE:合并、target solid UF_NEGATIVE:减去、UF_UNSIGNED:相交
	corner_pt:float list-块的角点位置(角点是值距离原点最近的点)
	edge_len:str list-分别在XYZ三个轴上的尺寸长度
	'''

1.2 NXOpen.UF.ModlFeatures.CreateCyl1

# 内部和外部模式可用
官方文档翻译:创建圆柱体,原点指示圆柱体底面的中心所在的位置,方向矢量控制在其上创建圆柱体的轴,直径和高度控制圆柱体的大小,sign参数控制执行的布尔运算,此函数的输出是与圆柱体相关联的对象标识符。
返回值:一个tag,元素类型为python的int类型,圆柱特征的feature tag标识。
"""             
def NXOpen.UF.ModlFeatures.CreateCyl1(self, sign, origin, height, diam, direction)
	'''
	sign(NXOpen.UF.Modl.FeatureSigns) 指示创建的同时执行某种布尔运算, UF_NULLSIGN:创建新的目标体、UF_POSITIVE:合并、target solid UF_NEGATIVE:减去、UF_UNSIGNED:相交
	origin:float list-底面圆心位置
	height:str-圆柱高度
	diam:str-圆柱直径
	direction:float list-圆柱建模方向(朝向:从底面朝着某个方向建立高度为height的圆柱特征)
	'''

1.3 NXOpen.UF.ModlFeatures.CreateCone1

# 内部和外部模式可用
官方文档翻译:创建圆锥体或圆台体,原点指示圆锥体底面的中心所在的位置,方向矢量控制在哪个轴上创建圆锥体,底部和顶部直径(直径)以及高度控制圆锥体的大小,sign参数控制执行的布尔运算,此函数的输出是与该圆锥体相关联的对象标识符。
返回值:一个tag,元素类型为python的int类型,圆锥或圆台特征的feature tag标识。
"""             
def NXOpen.UF.ModlFeatures.CreateCyl1(self, sign, origin, height, diam, direction)
	'''
	sign(NXOpen.UF.Modl.FeatureSigns) 指示创建的同时执行某种布尔运算, UF_NULLSIGN:创建新的目标体、UF_POSITIVE:合并、target solid UF_NEGATIVE:减去、UF_UNSIGNED:相交
	origin:float list-底面圆心位置
	height:str-圆锥或圆台高度
	diam:str list-[0]圆锥底面直径(大直径)、[1]圆台上表面直径(小直径),其中小直径为'0.0'时绘制出来的是圆锥否则是圆台
	direction:float list-圆柱建模方向(朝向:从底面朝着某个方向建立高度为height的圆锥或圆台特征)
	'''

1.4 NXOpen.UF.ModlFeatures.CreateSphere1

# 内部和外部模式可用
官方文档翻译:创建球体,“原点”表示球体的中心,Diameter控制大小,sign参数控制执行的布尔运算,此函数的输出是与球体关联的特征标记。
返回值:一个tag,元素类型为python的int类型,球体特征的feature tag标识。
"""             
def NXOpen.UF.ModlFeatures.CreateSphere1(self, sign, center, diam)
	'''
	sign(NXOpen.UF.Modl.FeatureSigns) 指示创建的同时执行某种布尔运算, UF_NULLSIGN:创建新的目标体、UF_POSITIVE:合并、target solid UF_NEGATIVE:减去、UF_UNSIGNED:相交
	center:float list-球心位置
	diam:str-球直径
	'''

1.5 NXOpen.UF.Modl.CreateTube

# 内部和外部模式可用
官方文档翻译:根据指定的内径和外径创建卡套管或电缆,如果内径设置为零,则会产生电缆(实心体无空心部分),此函数的输出是一个特征对象标识符列表,限制变量的类型为字符,以允许表达式使用。
返回值:一个tag,元素类型为python的int类型,管道特征的feature tag标识。
def NXOpen.UF.Modl.CreateTube(self, path_list, limit, sign)
	'''
	path_list-int list:构成管道中心线的边或曲线类型tag列表
	limit:str lis-[0]管道外直、 [1]管道内直径,其中当[1]为‘0.0’时创建的就是实心圆柱体管道
	sign(NXOpen.UF.Modl.FeatureSigns) 指示创建的同时执行某种布尔运算, UF_NULLSIGN:创建新的目标体、UF_POSITIVE:合并、target solid UF_NEGATIVE:减去、UF_UNSIGNED:相交
	'''

2.示例代码

2.1 pyuf_block_cylinder_cone_sphere_tube.py

import NXOpen
import NXOpen.UF as UF


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_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 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
	modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
	return uf_modlFeatures_instance.CreateBlock1(modl_feature_signs, corner_point, size)

def createCylinder(the_pyuf_session, origin, height, diam, direction, signs = 0):
	"""
	python uf创建圆柱

	origin-float list[x,y,z]:圆柱底端面中心点坐标,height-str:圆柱高度,diam-str:圆柱直径,
	direction-float list[x,y,z]:圆柱建模方向向量(从中心点开始朝某个方向建模)

	返回值一个整数:圆柱的feature tag标识
	"""

	uf_modlFeatures_instance = the_pyuf_session.ModlFeatures
	modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
	return uf_modlFeatures_instance.CreateCyl1(modl_feature_signs, origin, height, diam, direction)


def createCone(the_pyuf_session, origin, height, diam, direction, signs = 0):
	"""
	[python uf创建圆锥或圆锥台

	origin-float list[x,y,z]:圆锥底端面中心点坐标,height-str:圆锥高度,
	diam-str list[大直径,小直径(圆锥-0.0,圆锥台-大于0.0)]:圆锥直径,direction-float list[x,y,z]:圆锥建模方向向量(从中心点开始朝某个方向建模)

	返回值一个整数:圆锥或圆锥台的feature tag标识
	"""
	uf_modlFeatures_instance = the_pyuf_session.ModlFeatures
	modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
	return uf_modlFeatures_instance.CreateCone1(modl_feature_signs, origin, height, diam, direction)

def createSphere(the_pyuf_session, origin, diam, signs = 0):
	"""
	python uf创建球

	origin-float list[x,y,z]:球中心点坐标,diam-str:球直径

	返回值一个整数:球的feature tag标识
	"""
	uf_modlFeatures_instance = the_pyuf_session.ModlFeatures
	modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
	return uf_modlFeatures_instance.CreateSphere1(modl_feature_signs, origin, diam)

def createTube(the_pyuf_session, path_list, diam, signs = 0):
	"""
	创建管体
	center  (list of float) Center of sphere to be created.
	diam    (str) Diameter of sphere to be created.
	"""
	uf_modl_instance = the_pyuf_session.Modl
	modl_feature_signs = UF.Modl.FeatureSigns.ValueOf(signs)
	return uf_modl_instance.CreateTube(path_list, diam, modl_feature_signs)

def getFeatureBody(the_pyuf_session, feature_tag):
	"""
	查询特征所属body的tag
	"""
	uf_modeling_instance = the_pyuf_session.Modeling
	return uf_modeling_instance.AskFeatBody(feature_tag)


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_block_cylinder_cone_sphere_tube.prt'
    
    new_prt_file_tag = pyuf_new_prt(the_pyuf_session, new_prt_file_name)

    # 外部模式运行,会在命令行中打印,但内部模式运行则无反应,因在UF.UI类中操作,才可在NX/UG界面显示数据
    # 由于与NX/UG界面UI操作的方法都在UI类中,因此先获取UF.UI示例对象
    pyuf_ui_instance = the_pyuf_session.Ui

    # 判断信息窗口是否打开
    if pyuf_ui_instance.IsListingWindowOpen() == False:
        # 信息窗口未打开,先要将其打开
        # 打开信息窗口
        pyuf_ui_instance.OpenListingWindow()
        
    # 创建长方体
    block_feature_tag = createBlock(the_pyuf_session, [100.0, 100.0, 100.0], ['200.0', '400.0', '50.0'])
    
    # 创建圆柱体(朝X轴正向建模)
    createCylinder(the_pyuf_session, [50.0, 50.0, 50.0], '55.0', '100.0', [1.0, 0.0, 0.0])
    
    # 创建圆锥(朝Z轴负向建模)(带布尔减去,会自动将与之接触的实体中减去该圆锥体)
    createCone(the_pyuf_session, [50.0, 50.0, 50.0], '120.0', ['100.0', '0.0'], [0.0, 0.0, -1.0], 2)
    
    # 创建圆台(朝Z轴正向建模)
    createCone(the_pyuf_session, [150.0, 50.0, 50.0], '120.0', ['100.0', '55.0'], [0.0, 0.0, 1.0])
    
    # 创建球体
    createSphere(the_pyuf_session, [200.0, 300.0, 125.0], '100.0')
    
    # 从某个特征上查询该特征所属的实体
    block_body_tag = getFeatureBody(the_pyuf_session, block_feature_tag)
    block_body_face_tag_list = get_solid_body_face_tags(the_pyuf_session, block_body_tag)
    temp_face_edge_tag_list = get_solid_body_face_edge_tags(the_pyuf_session, block_body_face_tag_list[0])
    
    # 创建管道(控制外壁直径和内孔壁直径)
    createTube(the_pyuf_session, [temp_face_edge_tag_list[0]], ['10.0', '5.0'])
    
    # 创建管道(控制外壁直径和内孔壁直径)
    createTube(the_pyuf_session, [temp_face_edge_tag_list[1]], ['20.0', '0.0'])
    
    # 向信息窗口写入内容
    pyuf_ui_instance.WriteListingWindow('\nnew_prt_file_tag is:' + str(new_prt_file_tag))
    # 将信息窗口的内容保存到指定文件中
    pyuf_ui_instance.SaveListingWindow('D:\\pyuf_listingwindow_content_test.txt')

    # 关闭信息窗口
    #pyuf_ui_instance.ExitListingWindow()
    
    pyuf_save_prt(the_pyuf_session)
    
    pyuf_close_prt(the_pyuf_session, new_prt_file_tag, 0, 1)

3.运行结果

3.1 内部模式

选中要运行的.py文件后,点击“管道通路”即可。
在这里插入图片描述

运行结果:
在这里插入图片描述

3.2 外部模式

cmd命令:“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_block_cylinder_cone_sphere_tube.py
cmd命令:“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_block_cylinder_cone_sphere_tube.py

powershell命令:&“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_block_cylinder_cone_sphere_tube.py
powershell命令:&“D:\Siemens\NX 12.0\NXBIN\run_journal.exe” pyuf_block_cylinder_cone_sphere_tube.py

运行结果:
在这里插入图片描述

同上

  • 21
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值