一个入门的基本例子,这里提供完整代码,遍历所有部件,其他同理。
//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 );
}
欢迎交流与讨论。