使用UFUN进行UG NX二次开发: 装配设计技巧及步骤
使用UFUN进行UG NX二次开发: 装配设计技巧及步骤1、装入部件
通过函数UF_ASSEM_add_part_to_assembly()指定部件装入的位置 、姿态等信息,实现在装配体中装入一个部件功能;
2、创建配合关系
a.通过函数UF_ASSEM_init_mc()初始化约束;
b.定义约束关系,即填写结构UF_ASSEM_constraint_s中信息;
装配中各部件间的配合关系是通过装配约束关系来实现的。装配约束是指一个部件到另一个部件的约束,将部件A配合到部件B上,则部件A称为主动件(from),部件B称为被动件(to),在约束求解过程中,被动件在装配空间中的位置固定,而主动件将变换姿态到满足装配约束位置上。组成配合关系的约束之间不能存在几何关系上的矛盾,也不能形成循环定义。
c.通过函数UF_ASSEM_solve_mc()求解约束;
d.通过函数UF_ASSEM_apply_mc_data将约束应用到装配体上;
e.通过函数UF_MODL_update更新模型。
#include <stdio.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_obj.h>
#include <uf_disp.h>
#include <uf_modl.h>
#include <uf_part.h>
#include <uf_assem.h>
#define UF_CALL( X ) ( report( __FILE__, __LINE__, #X, ( X ) ) )
int report( char *file, int line, char *call, int irc );
void DoAssembly( void );
int report( char *file, int line, char *call, int irc )
{
if ( irc )
{
char err_msg[ UF_UI_MAX_STRING_LEN + 1 ];
char msg[ 133 ];
char fail_message[ 133 ];
intlen;
UF_get_fail_message( irc, fail_message );
sprintf( err_msg, "%s, line %d: error %d: %s", file, line, irc, fail_message );
len = strlen( err_msg );
len = ( len < 131 ) ? len : 131;
strncpy ( msg, err_msg, len );
msg[ len + 1 ] = '\000';
uc1601( err_msg, TRUE );
}
return( irc );
}
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
if ( UF_initialize() ) return;
DoAssembly();
UF_terminate();
}
void DoAssembly( void )
{
if ( NULL_TAG == UF_PART_ask_display_part() )
{
uc1601( "请新建一个文件再进行本操作!", 1 );
return;
}
tag_t tagInsCyl = NULL_TAG;
tag_t tagInsVBlk= NULL_TAG;
tag_t tagOccCyl = NULL_TAG;
tag_t tagOccVBlk= NULL_TAG;
UF_PART_load_status_t ErrorStatus;
double dblzOrigin[ 3 ] = { 0, 0, 0 };
double dblzCsysMatrix[ 6 ] = { 1, 0, 0, 0, 1, 0 };
/* 装入部件GB2208_80_Vblock.prt */
UF_ASSEM_add_part_to_assembly(
UF_PART_ask_display_part(),
"f:\\ug\\chapter4_1\\GB2208_80_Vblock.prt",
NULL,
"VBLOCK",
dblzOrigin,
dblzCsysMatrix,
-1,
&tagInsVBlk,
&ErrorStatus );
/* 装入部件Cylinder.prt */
dblzOrigin[ 1 ] = 50;
UF_ASSEM_add_part_to_assembly(
UF_PART_ask_display_part(),
"f:\\ug\\chapter4_1\\Cylinder.prt",
NULL,
"CYLINDER",
dblzOrigin,
dblzCsysMatrix,
-1,
&tagInsCyl,
&ErrorStatus );
tagOccCyl = UF_ASSEM_ask_part_occ_of_inst( NULL_TAG, tagInsCyl );
tagOccVBlk= UF_ASSEM_ask_part_occ_of_inst( NULL_TAG, tagInsVBlk );
/* 创建配合关系 */
UF_ASSEM_mating_condition_t McData;
tag_t tagObjectFrom = NULL_TAG;
tag_t tagObjectTo = NULL_TAG;
tag_t tagPrototypeFrom = NULL_TAG;
tag_t tagPrototypeTo = NULL_TAG;
/* 初始化约束*/
UF_ASSEM_init_mc( &McData );
/* 定义约束,构造配合关系*/
McData.mated_object = tagInsCyl;
McData.num_constraints = 3;
McData.name = NULL;
McData.user_name = false;
McData.override = false;
McData.suppressed = false;
McData.implicit = false;
tagObjectFrom = NULL_TAG;
tagObjectTo = NULL_TAG;
UF_OBJ_cycle_by_name( "CYL_FACE_3", &tagObjectFrom );
UF_OBJ_cycle_by_name( "P1", &tagObjectTo );
tagPrototypeFrom= UF_ASSEM_ask_prototype_of_occ( tagObjectFrom );
tagPrototypeTo = UF_ASSEM_ask_prototype_of_occ( tagObjectTo );
McData.constraints[ 0 ].from = tagPrototypeFrom;
McData.constraints[ 0 ].from_part_occ = tagOccCyl;
McData.constraints[ 0 ].from_status = UF_ASSEM_ok;
McData.constraints[ 0 ].from_type = UF_ASSEM_cylindrical_face;
McData.constraints[ 0 ].to = tagPrototypeTo;
McData.constraints[ 0 ].to_part_occ = tagOccVBlk;
McData.constraints[ 0 ].to_status = UF_ASSEM_ok;
McData.constraints[ 0 ].to_type = UF_ASSEM_planar_face;
McData.constraints[ 0 ].mate_type = UF_ASSEM_tangent;
McData.constraints[ 0 ].offset = NULL_TAG;
McData.constraints[ 0 ].name = NULL;
McData.constraints[ 0 ].user_name = false;
McData.constraints[ 0 ].inherited = false;
McData.constraints[ 0 ].suppressed = false;
McData.constraints[ 0 ].has_help_point = false;
UF_DISP_set_highlight( tagObjectFrom, 1 );
UF_DISP_set_highlight( tagObjectTo, 1 );
uc1601( "这两个面进行相切操作", 1 );
UF_DISP_set_highlight( tagObjectFrom, 0 );
UF_DISP_set_highlight( tagObjectTo, 0 );
tagObjectFrom = NULL_TAG;
tagObjectTo = NULL_TAG;
UF_OBJ_cycle_by_name( "CYL_FACE_3", &tagObjectFrom );
UF_OBJ_cycle_by_name( "P2", &tagObjectTo );
tagPrototypeFrom= UF_ASSEM_ask_prototype_of_occ( tagObjectFrom );
tagPrototypeTo = UF_ASSEM_ask_prototype_of_occ( tagObjectTo );
McData.constraints[ 1 ].from = tagPrototypeFrom;
McData.constraints[ 1 ].from_part_occ = tagOccCyl;
McData.constraints[ 1 ].from_status = UF_ASSEM_ok;
McData.constraints[ 1 ].from_type = UF_ASSEM_cylindrical_face;
McData.constraints[ 1 ].to = tagPrototypeTo;
McData.constraints[ 1 ].to_part_occ = tagOccVBlk;
McData.constraints[ 1 ].to_status = UF_ASSEM_ok;
McData.constraints[ 1 ].to_type = UF_ASSEM_planar_face;
McData.constraints[ 1 ].mate_type = UF_ASSEM_tangent;
McData.constraints[ 1 ].offset = NULL_TAG;
McData.constraints[ 1 ].name = NULL;
McData.constraints[ 1 ].user_name = false;
McData.constraints[ 1 ].inherited = false;
McData.constraints[ 1 ].suppressed = false;
McData.constraints[ 1 ].has_help_point = false;
UF_DISP_set_highlight( tagObjectFrom, 1 );
UF_DISP_set_highlight( tagObjectTo, 1 );
uc1601( "这两个面进行相切操作", 1 );
UF_DISP_set_highlight( tagObjectFrom, 0 );
UF_DISP_set_highlight( tagObjectTo, 0 );
tagObjectFrom = NULL_TAG;
tagObjectTo = NULL_TAG;
UF_OBJ_cycle_by_name( "CYL_FACE_1", &tagObjectFrom );
UF_OBJ_cycle_by_name( "P3", &tagObjectTo );
tagPrototypeFrom= UF_ASSEM_ask_prototype_of_occ( tagObjectFrom );
tagPrototypeTo = UF_ASSEM_ask_prototype_of_occ( tagObjectTo );
McData.constraints[ 2 ].from = tagPrototypeFrom;
McData.constraints[ 2 ].from_part_occ = tagOccCyl;
McData.constraints[ 2 ].from_status = UF_ASSEM_ok;
McData.constraints[ 2 ].from_type = UF_ASSEM_planar_face;
McData.constraints[ 2 ].to = tagPrototypeTo;
McData.constraints[ 2 ].to_part_occ = tagOccVBlk;
McData.constraints[ 2 ].to_status = UF_ASSEM_ok;
McData.constraints[ 2 ].to_type = UF_ASSEM_planar_face;
McData.constraints[ 2 ].mate_type = UF_ASSEM_align;
McData.constraints[ 2 ].offset = NULL_TAG;
McData.constraints[ 2 ].name = NULL;
McData.constraints[ 2 ].user_name = false;
McData.constraints[ 2 ].inherited = false;
McData.constraints[ 2 ].suppressed = false;
McData.constraints[ 2 ].has_help_point = false;
UF_DISP_set_highlight( tagObjectFrom, 1 );
UF_DISP_set_highlight( tagObjectTo, 1 );
uc1601( "这两个面进行对齐操作", 1 );
UF_DISP_set_highlight( tagObjectFrom, 0 );
UF_DISP_set_highlight( tagObjectTo, 0 );
double dblzTransform[ 4 ][ 4 ];
UF_ASSEM_dof_t Dof;
UF_ASSEM_mc_status_t Status;
/* 求解约束*/
UF_CALL( UF_ASSEM_solve_mc( &McData, &Status, &Dof, dblzTransform ) );
if ( UF_ASSEM_mc_solved == Status )
{
uc1601( "求解约束成功!", 1 );
UF_ASSEM_mc_structure_state_t StructStatus;
/* 将约束应用到装配体上*/
UF_CALL( UF_ASSEM_apply_mc_data( &McData, &StructStatus, &Status ) );
if ( UF_ASSEM_structure_ok == StructStatus )
{
uc1601( "应用装配成功!", 1 );
/* 更新模型*/
UF_MODL_update();
UF_DISP_refresh();
}
else uc1601( "应用装配失败!", 1 );
}
else uc1601( "求解约束失败!", 1 );
}
}
没人顶啊
页:
[1]