【NX】NX二次开发中遍历所有部件完整范例

一个入门的基本例子,这里提供完整代码,遍历所有部件,其他同理。

 

//author:autumoon
//邮箱:9506@163.com
//日期:2023-08-03 
/*****************************************************************************
**
** AssembleTraversal.cpp
**
** Description:
**     Contains Unigraphics entry points for the application.
**
*****************************************************************************/

#include "uf_assem.h"
#include "NXOpen/ListingWindow.hxx"

#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/AttributeManager.hxx>
#include <NXOpen/AttributePropertiesBaseBuilder.hxx>
#include <NXOpen/AttributePropertiesBuilder.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/Body.hxx>
#include "NXOpen/PartCollection.hxx"

/* Include files */
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
#   include <strstream>
#   include <iostream>
    using std::ostrstream;
    using std::endl;    
    using std::ends;
    using std::cerr;
#else
#   include <strstream.h>
#   include <iostream.h>
#endif
#include <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
    if (irc)
    {
        char err[133],
             msg[133];

        sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
            irc, line, file);
        UF_get_fail_message(irc, err);

        UF_print_syslog(msg, FALSE);
        UF_print_syslog(err, FALSE);
        UF_print_syslog("\n", FALSE);
        UF_print_syslog(call, FALSE);
        UF_print_syslog(";\n", FALSE);

        if (!UF_UI_open_listing_window())
        {
            UF_UI_write_listing_window(msg);
            UF_UI_write_listing_window(err);
            UF_UI_write_listing_window("\n");
            UF_UI_write_listing_window(call);
            UF_UI_write_listing_window(";\n");
        }
    }

    return(irc);
}

static void find_all_parts(tag_t root)
{
	NXOpen::Session *theSession = NXOpen::Session::GetSession();
	NXOpen::ListingWindow* lw = theSession->ListingWindow();
	lw->Open();


	tag_t* child_part_occs;
	int part_num=UF_ASSEM_ask_part_occ_children(root,&child_part_occs);
	for(int i=0;i<part_num;i++)
	{
		char part_name[ 256 + 1 ];
		char refset_name[ 30 + 1 ];
		char instance_name[ 30 + 1 ];
		double origin[ 3 ];
		double csys_matrix[ 9 ];
		double transform[ 4 ][ 4 ];
		UF_CALL(UF_ASSEM_ask_component_data(child_part_occs[i],
			part_name,
			refset_name,
			instance_name,
			origin,
			csys_matrix,
			transform));

		char szInfo[256];
		sprintf(szInfo, "Ocurrence tag=%d,instance name=%s\n",child_part_occs[i],instance_name);
		lw->WriteLine(szInfo);

		find_all_parts(child_part_occs[i]);
	}
	UF_free(child_part_occs);
}

/*****************************************************************************
**  Activation Methods
*****************************************************************************/
/*  Unigraphics Startup
**      This entry point activates the application at Unigraphics startup */
extern DllExport void ufsta( char *param, int *returnCode, int rlen )
{
    /* Initialize the API environment */
    if( UF_CALL(UF_initialize()) ) 
    {
        /* Failed to initialize */
        return;
    }
    
    /* TODO: Add your application code here */

	// 获取根部件
	NXOpen::Session *theSession = NXOpen::Session::GetSession();
	NXOpen::Part *workPart(theSession->Parts()->Work());

	tag_t workTag = workPart->Tag();

	tag_t root = UF_ASSEM_ask_root_part_occ(workTag);

	tag_t* child_part_occs;

	int part_num = UF_ASSEM_ask_part_occ_children(root, &child_part_occs);

	find_all_parts(root);

    /* Terminate the API environment */
    UF_CALL(UF_terminate());
}

/*****************************************************************************
**  Utilities
*****************************************************************************/

/* Unload Handler
**     This function specifies when to unload your application from Unigraphics.
**     If your application registers a callback (from a MenuScript item or a
**     User Defined Object for example), this function MUST return
**     "UF_UNLOAD_UG_TERMINATE". */
extern int ufusr_ask_unload( void )
{
    return( UF_UNLOAD_IMMEDIATELY );
}

欢迎交流与讨论。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值