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

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

【NX】NX二次开发中遍历所有部件完整范例_第1张图片

 

//author:autumoon
//邮箱:[email protected]
//日期:2023-08-03 
/*****************************************************************************
**
** AssembleTraversal.cpp
**
** Description:
**     Contains Unigraphics entry points for the application.
**
*****************************************************************************/

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "NXOpen/PartCollection.hxx"

/* Include files */
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
#   include 
#   include 
    using std::ostrstream;
    using std::endl;    
    using std::ends;
    using std::cerr;
#else
#   include 
#   include 
#endif
#include 
#include 
#include 

#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;iWriteLine(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 );
}

欢迎交流与讨论。

你可能感兴趣的:(nx二次开发,c++,算法,开发语言)