您的当前位置:首页正文

NX二次开发习题及源码

2023-10-17 来源:易榕旅网


1、创建NXhello界面

代码:

#include //包含UF_initialize()和UF_terminate()函数原型的头文件

#include //包含uc1601()函数原型的头文件

extern void ufusr(char *param, int *retcode, int param_len)

{

if (UF_initialize()!=0)//获取NX openapi的执行权限

{

return;

uc1601(\"Hello NX\弹出消息窗口,显示“Hello NX”

UF_terminate();//释放NX OPEN API的执行权限

}

}

1

运行结果截图:

2、创建一个模型,并在信息窗口显示模型的tag值。

代码:

#include

#include

#include

#include < uf_ui_ugopen.h>

#include

2

#include

static void do_ugopen_api(void)

{ typedef unsigned int tag_t;

UF_FEATURE_SIGN sign = UF_NULLSIGN;//无布尔运算

double cyl_orig[3] = {0,0,0};//圆柱的圆心坐标

char *cyl_height=\"100\";

char *cyl_diam=\"40\";

double direction[3]={0,0,1};//延Z轴正方向

tag_t obj=NULL_TAG;

UF_MODL_create_cyl1(sign, cyl_orig,cyl_height,cyl_diam,direction, &obj);

UF_UI_open_listing_window(); //显示信息框:tag

/*Returns the tag of the current display part. In a non-assembly part, this is the

same as the work part. If there currently isn't a displayed part, a NULL_TAG is returned. */

3

obj=UF_PART_ask_display_part();

if(obj == NULL_TAG) //没有部件的时候显示的信息提示框

{

uc1601 ( \"当前没有任何文件可供操作!\

}

else

{

char s[10];

sprintf(s, \"%d\功能:把格式化的数据写入某个字符串

UF_UI_write_listing_window(s);

return;

}

}

4

extern void ufusr( char *param, int *retcode, int rlen )//提供入口点

{

if((UF_initialize())!=0)

return;

do_ugopen_api ();

UF_terminate();

return;

}

extern int ufusr_ask_unload(void)

{

return(UF_UNLOAD_IMMEDIATELY);

}

运行结果截图:

5

(1)无部件时的输出

(2)有部件时的输出结果

3、open part 用户入口实例

6

创建open part user exit

在用户点击“open”时创建新部件,在其中创建sphere

详细说明user exit设置文件过程

代码如下:

#include < uf_assem.h>

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

7

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

8

UF_FEATURE_SIGN sign = UF_NULLSIGN;

tag_t part=null_tag;//如果没有赋值的话,直接就是\"tag_t part\",当你执行.dll文件时,就直接跑到建模环境了,没有信息提示框。当然你可以多申明几个tag_t

char part_name[13] = \"F:\\\\long.prt\";//设置part名字以及存储路径

double center [ 3 ]={0,0,0};//球心坐标

char * diam=\"100\";

int units =1;

uc1601(\"创建直径为100的球\

UF_PART_new (part_name, units, &part);//在当前的会话框中创建新的part,并把其作为工作部件

//试比较下面两个函数

UF_MODL_create_sphere(sign,part,center,diam, &part );

//UF_MODL_create_sphere1(sign,center,diam, &part);

}

9

extern void ufusr(char *param, int *retcode, int paramLen)

{ if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

extern int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

运行结果截图:

10

4、创建一个block,并指定某一特定面高亮显示。

代码如下图:

#include

11

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

12

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

UF_FEATURE_SIGN sign=UF_NULLSIGN;

tag_t obj=null_tag;

double center [3]={0,0,0} ;

char *edge_len[3]={\"10\

tag_t blk_feat;

13

int units=2;//此行代码的赋值,经过调试,貌似只有1和2可以,它们的区别就是生成模型的默认大小不同,可以自己调调看。

char part_name[13]=\"F:\\\\long.part\";//此段代码有点小重要,注意一定要保证完整性,不完整的话,估计会出现好多问题,绕好多路。

uc1601(\"创建一个正方体,并指定下表面高亮显示\

UF_PART_new(part_name,units,&obj);

//UF_MODL_create_block(sign,obj,center,edge_len,&blk_feat);

UF_MODL_create_block1(sign,center,edge_len,&blk_feat);

uf_list_p_t face_list;//链表

UF_MODL_create_list(&face_list);//创建链表

UF_MODL_ask_feat_faces(blk_feat,&face_list);//获取实体的个表面特征,并放入链表中

int count=0;

UF_MODL_ask_list_count(face_list,&count);

for(int i=0;i

14

{

UF_MODL_ask_list_item(face_list,i,&obj);//从链表中取出对象

int type;

double point [20] ;

double dir [20] ;

double box [6];

double radius;

double rad_data;

int norm_dir;

UF_MODL_ask_face_data(obj,&type,point,dir,box,&radius,&rad_data,&norm_dir);//这个函数非常重要,可以说是本段代码中最核心的部分。

if(dir[0]==0&&dir[1]==0&&dir[2]==-1)//确定高亮显示的那个面的法向量

{

UF_DISP_set_highlight(obj,1);

15

//UF_OBJ_set_color(obj, 186);

break;

}

}

}

/*ARGSUSED*/

extern void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

16

}

extern int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

运行结果截图:

17

注:和高亮显示还有一个类似的设置,就是给实体上色,所用到的函数是:

UF_OBJ_set_color(obj, 186);

上面的这个函数是把对象显示成红色,如下图:

18

“186”是红色的代号。这些数字可以在UG交互模式下的对象显示里查到,如下图:

5、用遍历的形式输出当前part所有面的tag至listing window

代码如下:

#include < uf_assem.h>

#include

#include

#include

19

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

20

printf(\"returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

UF_FEATURE_SIGN sign = UF_NULLSIGN;

double center [ 3 ] = {0,0,0};

char * edge_len [ 3 ]={\"100\

tag_t blk_feat =null_tag;

char * part_name=\"F:\\\\long\";

int units=1;

tag_t part =NULL_TAG;

21

uc1601(\"创建一个block,并显示每个面的tag\

UF_PART_new(part_name,units,&part);//在当前会话窗口中创建一个part,并使其成为工作部件

UF_CALL(UF_MODL_create_block1(sign,center,edge_len, &blk_feat));

if(blk_feat == NULL_TAG)

{

uc1601 ( \"当前没有任何文件可供操作!\

}

else

{

int type=70;

int subtype=2;

tag_t obj=null_tag;

tag_t display_part=UF_PART_ask_display_part();

22

UF_OBJ_cycle_objs_in_part(display_part,70,&obj);

while(obj!=null_tag)

{

UF_OBJ_ask_type_and_subtype(obj,&type,&subtype);//返回一个被标记的对象的对象类型和子类型

UF_OBJ_cycle_objs_in_part (display_part,UF_solid_type,&obj);//查询遍历对象

if(subtype== UF_solid_face_subtype)

{

char s[50];

sprintf(s, \"%d\

UF_CALL(UF_UI_open_listing_window());

UF_CALL(UF_UI_write_listing_window(s));

UF_CALL(UF_UI_write_listing_window(\"\\n\"));

}

23

}

}

}

void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

24

return (UF_UNLOAD_IMMEDIATELY);

}

运行结果截屏:

25

6、遍历当前part中所有features,输出其类型至listing window,UF_MODL_ask_feat_type()

代码如下:

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

26

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

char * part_name=\"F:\\\\NX\";

UF_FEATURE_SIGN sign = UF_NULLSIGN;

tag_t part=null_tag;

27

double center [ 3 ]={0,0,0};

char * diam=\"100\";

int units =1;

uc1601(\"创建直径为100的球,并显示其实体特征。\

UF_PART_new (part_name, units, &part);//在当前的会话框中创建新的part,并把其作为工作部件

UF_MODL_create_sphere1(sign,center,diam, &part);

int count = 0;

char* solid_feature;

tag_t found = NULL_TAG;

tag_t display_part = UF_PART_ask_display_part();

UF_UI_open_listing_window();

UF_OBJ_cycle_objs_in_part(display_part,UF_feature_type,&found);

while( found != NULL_TAG )

28

{

count++;

UF_CALL(UF_MODL_ask_feat_type(found,&solid_feature));//获取输入的特征的类型

UF_CALL(UF_UI_write_listing_window(\"feature:\"));

UF_CALL(UF_UI_write_listing_window(solid_feature));

UF_CALL(UF_UI_write_listing_window(\"\\n\"));

UF_CALL(UF_OBJ_cycle_objs_in_part(display_part,UF_feature_type,&found));

}

}

extern void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

29

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

extern int ufusr_ask_unload(void)

{

return(UF_UNLOAD_IMMEDIATELY);

}

运行结果截屏:

30

6、创建人机交互界面新建圆柱体,并可以随意选择原点和矢量方向。

代码如下:

#include

31

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include \"cylinder.h\"

#define CHANGE_CB_COUNT ( 3 + 1 ) /* Add 1 for the terminator */

tag_t blk_tag= null_tag;

32

double blk_orig[] = {0,0,0};

double direction [ 3 ];

char * part_name=\"F:\\\\long\";

int units=1;

static UF_STYLER_callback_info_t CHANGE_cbs[CHANGE_CB_COUNT] = {

{UF_STYLER_DIALOG_INDEX, UF_STYLER_APPLY_CB CHANGE_apply_cb},

{CHANGE_ACTION_2 , UF_STYLER_ACTIVATE_CB CHANGE_action_2_act_cb},

{CHANGE_ACTION_3 , UF_STYLER_ACTIVATE_CB CHANGE_action_3_act_cb},

{UF_STYLER_NULL_OBJECT, UF_STYLER_NO_CB, 0, 0 }

};

static UF_MB_styler_actions_t actions[] = {

。33

, 0, , 1, , 1,

{ \"cylinder.dlg\ NULL, CHANGE_cbs, UF_MB_STYLER_IS_NOT_TOP },

{ NULL, NULL, NULL, 0 } /* This is a NULL terminated list */

};

static int init_proc(UF_UI_selection_p_t select,void *user_data);

extern void ufusr (char *param, int *retcode, int rlen)

{

uc1601(\"创建一个圆柱,并指明原点和矢量\

UF_PART_new(part_name,units,&blk_tag);

int response = 0;

int error_code = 0;

if ( ( UF_initialize() ) != 0 )

return;

if ( ( error_code = UF_STYLER_create_dialog ( \"cylinder.dlg\

34

CHANGE_cbs, /* Callbacks from dialog */

CHANGE_CB_COUNT, /* number of callbacks*/

NULL, /* This is your client data */

&response ) ) != 0 )

{

char fail_message[133];

/* Get the user function fail message based on the fail code.*/

UF_get_fail_message(error_code, fail_message);

UF_UI_set_status (fail_message);

printf ( \"%s\\n\

}

UF_terminate();

return;

35

}

extern int ufusr_ask_unload (void)

{

return ( UF_UNLOAD_IMMEDIATELY );

}

extern void ufusr_cleanup (void)

{

return;

}

int CHANGE_apply_cb ( int client_data,UF_STYLER_item_value_type_p_t callback_data)

{

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

。36

dialog_id,void *

//直径的相关参数

UF_STYLER_item_value_type_t data;

data.item_id = \"REAL_0\";

data.item_attr = UF_STYLER_VALUE;

data.indicator = UF_STYLER_REAL_VALUE;

UF_STYLER_ask_value(dialog_id,&data);//用来查询对话框控件的属性值。

char diameter[133];

sprintf(diameter,\"%f\

//高度的相关参数

data.item_id = \"REAL_1\";

data.item_attr = UF_STYLER_VALUE;

data.indicator = UF_STYLER_REAL_VALUE;

UF_STYLER_ask_value(dialog_id,&data);

37

char height[133];

sprintf(height,\"%f\

UF_FEATURE_SIGN sign = UF_NULLSIGN;

//tag_t blk_tag= null_tag;

//UF_PART_new(part_name,units,&blk_tag);

UF_MODL_create_cyl1(sign,blk_orig,diameter,height,direction , &blk_tag);

UF_STYLER_free_value(&data);//释放字符串或数组申请的内存

UF_STYLER_ask_value(dialog_id,&data);

UF_MODL_update();

UF_STYLER_free_value(&data);

UF_terminate ();

return (UF_UI_CB_CONTINUE_DIALOG);

}

38

//选择圆柱中心点,选用点构造器UF_UI_point_construct()

int CHANGE_action_2_act_cb ( int dialog_id,void *

client_data,UF_STYLER_item_value_type_p_t callback_data)

{

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

char *cue = \"点构造器\";

UF_UI_POINT_base_method_t method = UF_UI_POINT_INFERRED;

tag_t point_tag = NULL_TAG;

//double sBasePoint[3];;

int response;

int error = UF_UI_point_construct(cue,

&method,

&point_tag,

39

blk_orig,

&response);

UF_terminate ();

return (UF_UI_CB_CONTINUE_DIALOG);

}

//选择圆柱方向(拾取向量对话框)

int CHANGE_action_3_act_cb ( int client_data,UF_STYLER_item_value_type_p_t callback_data)

{

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

double vec[3], pnt[3];

int mode = UF_UI_ZC_AXIS;//默认矢量方向

。40

dialog_id,void *

int disp_flag = UF_UI_DISP_NO_VECTOR;//显示临时坐标,是比较UF_UI_DISP_NO_VECTOR

int response = 0;

int ifail;

UF_initialize();

ifail = UF_UI_specify_vector( \"选择一个矢量\

&mode,

disp_flag,

vec,

pnt,

&response );

if ( ifail != 0 || response != UF_UI_OK )

printf( \"No vector selected \\n\" );

else

41

printf( \"Vect base (%f, %f, %f), direction (%f, %f, %f) \\n\

pnt[0], pnt[1], pnt[2], vec[0], vec[1], vec[2] );

fflush( stdout );

UF_terminate ();

//double direction[3];

for(int i=0;i<3;i++)

{

direction[i]=vec[i]-pnt[i];

}

/* Callback acknowledged, do not terminate dialog */

return (UF_UI_CB_CONTINUE_DIALOG);

}

static int init_proc(UF_UI_selection_p_t select,void* user_data)

42

{

//指明了mask_triple数组的长度

int num_triples = 1;

UF_UI_mask_t mask_triples[] = UF_UI_SEL_FEATURE_SOLID_BODY};

if((UF_UI_set_sel_mask(select,

UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,

num_triples,

mask_triples)) == 0)

{

return (UF_UI_SEL_SUCCESS);

}

else

{

。43

{UF_solid_type, 0,

return (UF_UI_SEL_FAILURE);

}

}

运行结果截屏

44

45

7、(1)新建一个对象,选择对象的面,返回这个面的tag值。

代码如下:

46

#include

#include

#include

#include

#include

static int sel_init_proc(UF_UI_selection_p_t select,void *user_data);

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

47

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

//类选择对话框相关变量

char cue[] = \"Select Objects\";

char title[] = \"Select faces\";

int scope=UF_UI_SEL_SCOPE_NO_CHANGE;

int response, count, i;

48

tag_p_t objects;

int error_code = 0;

char* str = (char*)UF_allocate_memory(sizeof(unsigned int)+1,&error_code);

UF_UI_open_listing_window();

if((UF_CALL(UF_UI_select_with_class_dialog(

cue, title, scope,sel_init_proc, NULL, &response, &count, &objects))) == 0)

{

printf(\"object count = %d\\n\

if (response == UF_UI_OK && count > 0)

{

for (i=0; i{

sprintf(str,\"%d\\n\

49

UF_UI_write_listing_window(str);

UF_DISP_set_highlight(objects[i], 0);//把选中的对象高亮显示

}

UF_free(objects);

}

}

}

/* selection initialization procedure */

static int sel_init_proc(UF_UI_selection_p_t select,void* user_data)

{

int num_triples=1;

UF_UI_mask_t mask_triples[] = {UF_solid_type, UF_UI_SEL_FEATURE_PLANAR_FACE};

/* enable only lines and edges */

。50

0,

if((UF_CALL(UF_UI_set_sel_mask(select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,

num_triples, mask_triples))) == 0)

{

return (UF_UI_SEL_SUCCESS);

}

else

{

return (UF_UI_SEL_FAILURE);

}

}

/*ARGSUSED*/

void ufusr(char *param, int *retcode, int param_len)

{

51

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

运行结果截屏:

52

(2)读取文本文件里的内容和往文本文件中写入信息。

代码如下:

53

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include \"Multiple_text.h\"

using namespace std;

。54

#define CHANGE_CB_COUNT ( 3 + 1 ) /* Add 1 for the terminator */

char prompt_string[133]=\"请选取文本文件\";

char title_string[133]=\"选取文本文件\";

char filter_string[133] = \"文本文件\";

char default_name[133]=\" \";

char filename[133];

int response=0;

static UF_STYLER_callback_info_t CHANGE_cbs[CHANGE_CB_COUNT] = {

{UF_STYLER_DIALOG_INDEX, UF_STYLER_APPLY_CB CHANGE_apply_cb},

{CHANGE_ACTION_1 , UF_STYLER_ACTIVATE_CB CHANGE_action_1_act_cb},

{CHANGE_ACTION_2 , UF_STYLER_ACTIVATE_CB CHANGE_action_2_act_cb},

。55

, 0, , 1, , 1,

{UF_STYLER_NULL_OBJECT, UF_STYLER_NO_CB, 0, 0 }

};

static UF_MB_styler_actions_t actions[] = {

{ \"Multiple_text.dlg\ NULL, CHANGE_cbs, UF_MB_STYLER_IS_NOT_TOP },

{ NULL, NULL, NULL, 0 } /* This is a NULL terminated list */

};

extern void ufusr(char *param, int *retcode, int rlen)

{

int response = 0;

int error_code = 0;

if ( ( UF_initialize() ) != 0 )

return;

if ( ( error_code = UF_STYLER_create_dialog ( \"Multiple_text.dlg\

56

CHANGE_cbs, /* Callbacks from dialog */

CHANGE_CB_COUNT, /* number of callbacks*/

NULL, /* This is your client data */

&response ) ) != 0 )

{

char fail_message[133];

/* Get the user function fail message based on the fail code.*/

UF_get_fail_message(error_code, fail_message);

UF_UI_set_status (fail_message);

printf ( \"%s\\n\

}

UF_terminate();

return;

57

}

extern int ufusr_ask_unload (void)

{

/* unload immediately after application exits*/

return ( UF_UNLOAD_IMMEDIATELY );

/*via the unload selection dialog... */

/*return ( UF_UNLOAD_SEL_DIALOG ); */

/*when UG terminates... */

/*return ( UF_UNLOAD_UG_TERMINATE ); */

}

extern void ufusr_cleanup (void)

{

return;

58

}

int CHANGE_apply_cb ( int dialog_id,

void * client_data,

UF_STYLER_item_value_type_p_t callback_data)

{

/* Make sure User Function is available. */

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

/* ---- Enter your callback code here ----- */

UF_terminate ();

/* Callback acknowledged, do not terminate dialog */

/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted */

/* for this callback type. You must respond to your apply button.*/

59

return (UF_UI_CB_CONTINUE_DIALOG);

}

int CHANGE_action_1_act_cb ( int dialog_id,

void * client_data,

UF_STYLER_item_value_type_p_t callback_data)

{

/* Make sure User Function is available. */

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

/* ---- Enter your callback code here ----- */

UF_UI_create_filebox ( prompt_string,

title_string,

filter_string,

60

default_name,

filename,

&response);

/* 从文件中读信息*/

int error;

fstream file1(filename);

char **file_contents = (char**)UF_allocate_memory(sizeof(char*)*16,&error);

int i=0;

while(!file1.eof())

{

file_contents[i] = (char*)UF_allocate_memory(MAX_LINE_SIZE+1,&error);

file1>>file_contents[i];

UF_UI_write_listing_window(file_contents[i]);

61

UF_UI_write_listing_window(\"\\n\");

i++;

}

UF_STYLER_item_value_type_t data;

data.item_id = \"MTXT_0\";

data.item_attr = UF_STYLER_VALUE;

data.indicator = UF_STYLER_STRING_PTR_VALUE;

data.count = i;

data.value.strings = file_contents;

UF_STYLER_set_value(dialog_id,&data);

UF_STYLER_free_value(&data);

file1.close();

UF_terminate ();

62

/* Callback acknowledged, do not terminate dialog */

return (UF_UI_CB_CONTINUE_DIALOG);

/* or Callback acknowledged, terminate dialog. */

/* return ( UF_UI_CB_EXIT_DIALOG ); */

}

int CHANGE_action_2_act_cb ( int dialog_id,

void * client_data,

UF_STYLER_item_value_type_p_t callback_data)

{

/* Make sure User Function is available. */

if ( UF_initialize() != 0)

return ( UF_UI_CB_CONTINUE_DIALOG );

/* ---- Enter your callback code here ----- */

63

UF_STYLER_item_value_type_t data;

data.item_id = \"MTXT_0\";

data.item_attr = UF_STYLER_VALUE;

UF_STYLER_ask_value(dialog_id,&data);

char prompt_string[]=\"Select File\";

char title_string[]=\"File Access\";

char filter_string[UF_CFI_MAX_PATH_NAME_SIZE+1]=\"*.txt\";

char default_name[133]=\"\";

char filename[UF_CFI_MAX_PATH_NAME_SIZE];

int response;

int error = UF_UI_create_filebox(prompt_string,

title_string,

filter_string,

64

default_name,

filename,

&response);

if(error !=0 )

{

return UF_UI_CB_CONTINUE_DIALOG;

}

fstream file2(filename);

for (int i=0;i{

if(!(file2<file2.close();

UF_STYLER_free_value(&data);

65

UF_terminate ();

/* Callback acknowledged, do not terminate dialog */

return (UF_UI_CB_CONTINUE_DIALOG);

/* or Callback acknowledged, terminate dialog. /* return ( UF_UI_CB_EXIT_DIALOG ); }

}

注意:这个项目需要在UG中创建一个人机交互界面8、(1)创建点收集器,创建参考点并返回点的坐标

代码如下:

#include

#include

#include

。66

*/

*/

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

67

static void do_ugopen_api(void)

{

//相关知识在P76

char message[]=\"点收集器\";

logical coincident_points = TRUE;

UF_UI_chained_points_p_t points;

int count;

int response;

UF_UI_select_point_collection(message,coincident_points,&points,&count,&response);

points[0].object;

UF_UI_open_listing_window();

char str [133];

for(int i=0;i<3;i++)

68

{

sprintf(str,\"%f\关键部分

UF_UI_write_listing_window(str);

UF_UI_write_listing_window(\"\\n\");

}

UF_free(points);

}

void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

69

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

/* Perform cleanup */

}

运行结果截屏:

70

71

(2)创建一个对象,用单对象选择对话框选择对象的边,并返回该边的长度。

72

代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

static int init_proc(UF_UI_selection_p_t select,void *user_data);

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

static int report( char *file, int line, char *call, int irc)

73

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

74

//单对象选择对话框变量参数

char cue[] = \"单对象选择对话框\";

char title[] = \"单对象选择对话框\";

int iScope=UF_UI_SEL_SCOPE_NO_CHANGE;

int response;

tag_t view;

tag_t object;

double cursor[3];

tag_t xform =null_tag;

tag_t feature_tag;

tag_t exp_tag ;

double length ;

char buf[UF_UI_MAX_STRING_LEN+1];

75

if(!UF_CALL(UF_UI_select_with_single_dialog(cue,title,

iScope, init_proc, NULL,

&response, &object, cursor, &view)))

{

if (response == UF_UI_OBJECT_SELECTED ||

response == UF_UI_OBJECT_SELECTED_BY_NAME)

{

UF_GEXP_create_length(object,xform,&feature_tag,&exp_tag);//创建一个测量长度的表达式

UF_CALL(UF_MODL_ask_exp_tag_value(exp_tag,&length));//调用表达式的tag值

sprintf(buf, \"The length of the linearedge is:%f\\n\

UF_UI_open_listing_window();

UF_UI_write_listing_window(buf);

76

}

/* unhighlight selected object */

UF_DISP_set_highlight(object,0);

}

}

/* selection initialization procedure */

static int init_proc(UF_UI_selection_p_t select,void* user_data)

{

int num_triples = 1;//1代表对象的边

UF_UI_mask_t mask_triples[] = {UF_solid_type, UF_UI_SEL_FEATURE_LINEAR_EDGE};

/* enable only lines and edges */

if((UF_CALL(UF_UI_set_sel_mask(select,

UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,

。77

0,

num_triples, mask_triples))) == 0)

{

return (UF_UI_SEL_SUCCESS);

}

else

{

return (UF_UI_SEL_FAILURE);

}

}

void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

78

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

/* Perform cleanup */

}

运行结果截屏如下:

79

9、访问外部数据源(哥们的电脑做不起来这个项目,所以没有截屏,但代码是正确的)

代码如下:

80

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

。81

#include

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char sFailMessage[133];

UF_get_fail_message(irc,sFailMessage);

char sWholeMessage[256];

sprintf(sWholeMessage,

\"File Name:%s\\nLine:%d\\nFunction:%s\\nError Code:%d\\nMessage:%s\

file,line,call,irc,sFailMessage);

82

MessageBox( NULL, sWholeMessage,

\"Error in Function\

}

return(irc);

}

static void error_return(char *msg)

{

fprintf(stderr,\"\\n+++ERROR : %s.\\n\\n\

exit(1);

}

static void report_error(int result,char *alt_msg)

{

char err_message[200];

83

int found = UF_get_fail_message(result,err_message);

if (found != 0)

error_return(alt_msg);

else

error_return(err_message);

}

static void create_blo( double *blo_length,char*blo_width,char*blo_height )

{

UF_FEATURE_SIGN sign = UF_NULLSIGN;

tag_t taget = NULL_TAG;

double blo_origin [3] = {0,0,0};

//double cyl_direction[ 3 ] = { 0.0, 0.0, 1.0 };

tag_t blk_feat = NULL_TAG;

。84

origin_y, char

char* edge_lengths[] ={blo_length,blo_width,blo_height};

blo_origin[1] = origin_y;

UF_MODL_create_block (sign,taget,blo_origin,edge_lengths,&blk_feat);

}

static void do_ugopen_api(void)

{

CDatabase Database; // 定义一个MFC的CDatabse数据库类对象Databse

CString SQLCommand = \"select * from screw\"; // SQL查询语句

DWORD dwOptions = 0; // 设置连接的建立方式

CDBVariant temp; // 数据库数据通用类型

int result; // 储存返回值的变量

char blo_length[133];

char blo_width[133];

85

char blo_height[133];

double *blo = NULL;

try

{

result = Database.OpenEx( \"DSN=screw\ // 打开数据库

if( result != 0 ) // 如果成功打开

{

CRecordset rs( &Database ); // 定义记录集

if ( rs.Open( CRecordset::snapshot, SQLCommand ) != false )

{

rs.MoveFirst( );

short nFields = rs.GetODBCFieldCount( ); // 获取记录的字段数目

while( !rs.IsEOF( ) )

86

{

blo =(double *)malloc( nFields * sizeof(double) );

for( short index = 0; index < nFields; index++ )

{

rs.GetFieldValue( index, temp );

blo[index] = temp.m_dblVal;

}

sprintf(blo_length,\"%f\

sprintf(blo_width,\"%f\

sprintf(blo_height,\"%f\

create_blo( blo[1]*3,blo_length,blo_width,blo_height);

rs.MoveNext( );

free(blo);

87

}

rs.Close( ); // 关闭记录集

}

Database.Close(); // 关门数据库连接

}

}

catch( CDBException *pe ) // 出错处理

{

AfxMessageBox( \"Exception!\" );

AfxMessageBox( pe->m_strError );

pe -> Delete( );

}

}

88

/*ARGSUSED*/

void ufusr(char *param, int *retcode, int param_len)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

/*----------------------------------------------------------------------------*/

extern int ufusr_ask_unload (void)

{

/* unload immediately after application exits*/

89

return ( UF_UNLOAD_IMMEDIATELY );

}

10、创建一个拉伸体。

代码如下:

#include

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

90

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

91

int i;

tag_t view_tag = NULL_TAG;

//定义三根线,作为扫描的截面线串

UF_CURVE_line_t sline1,sline2,sline3;

tag_t line1,line2,line3;

tag_t generators[3];

//定义拉特征参数

uf_list_p_t objects;

char *taper_angle = \"0.0\";

char *limit[ 2 ] = {\"0.0\拉伸起点和终点,及长度

double point[ 3 ] = {0.0,0.0,0.0};

double direction[ 3 ]={0.0,0.0,1.0}; //拉伸方向

UF_FEATURE_SIGN sign = UF_NULLSIGN;

92

uf_list_p_t features;

if(!UF_initialize())

{

//创建线1

sline1.start_point[ 0 ] = 0.0;

sline1.start_point[ 1 ] = 0.0;

sline1.start_point[ 2 ] = 0.0;

sline1.end_point[ 0 ] = 0.0;

sline1.end_point[ 1 ] = 5.0;

sline1.end_point[ 2 ] = 0.0;

UF_CURVE_create_line(&sline1,&line1);

//创建线2

sline2.start_point[ 0 ] = 0.0;

93

sline2.start_point[ 1 ] = 5.0;

sline2.start_point[ 2 ] = 0.0;

sline2.end_point[ 0 ] = 5.0;

sline2.end_point[ 1 ] = 5.0;

sline2.end_point[ 2 ] = 0.0;

UF_CURVE_create_line(&sline2,&line2);

//创建线3

sline3.start_point[ 0 ] = 5.0;

sline3.start_point[ 1 ] = 5.0;

sline3.start_point[ 2 ] = 0.0;

sline3.end_point[ 0 ] = 0.0;

sline3.end_point[ 1 ] = 0.0;

sline3.end_point[ 2 ] = 0.0;

94

UF_CURVE_create_line(&sline3,&line3);

//把三根线的tag放入数组

generators[0] = line1;

generators[1] = line2;

generators[2] = line3;

//创建一个链表,以存放扫描的截面线串

UF_MODL_create_list( &objects);

for(i=0;i<3;i++)

{

UF_MODL_put_list_item(objects,generators[i]);

}

UF_MODL_create_extruded(objects,taper_angle,limit,point,direction,sign,&features);

UF_MODL_delete_list(&objects);

95

UF_VIEW_ask_work_view(&view_tag);

UF_VIEW_fit_view(view_tag,0.7);

UF_PART_save();

UF_terminate();

}

}

void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

96

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

/* Perform cleanup */

}

运行结果截屏如下:

97

11、在两个平面的中间创建一个参考平面。

代码如下:

#include

#include

#include

#include

98

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

99

}

static int init_proc(UF_UI_selection_p_t select,void* user_data)

{

int num_triples = 2;//2代表面,1代表对象的边

UF_UI_mask_t mask_triples[] = {

UF_line_type, 0, 0,

UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE};

/* enable only lines and edges */

if((UF_CALL(UF_UI_set_sel_mask(select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples, mask_triples))) == 0)

{

return (UF_UI_SEL_SUCCESS);

}

else

100

{

return (UF_UI_SEL_FAILURE);

}

}

static void do_ugopen_api(void)

{

char cue[] = \"按类选择对话框\";

char title[] = \"按类选择对话框\";

int response, count, i;

tag_p_t objects;

/* Use multiple class selection with scope set to any object

in the work part and without using an initialization procedure.

*/

101

if((UF_CALL(UF_UI_select_with_class_dialog(cue, title,

UF_UI_SEL_SCOPE_NO_CHANGE ,init_proc, NULL, &response, &count, &objects))) == 0)

{

// printf(\"object count = %d\\n\

if (response == UF_UI_OK && count > 0)

{

for (i=0; i{

// printf(\"object tag = %d\\n\

UF_DISP_set_highlight(objects[i],1);

}

//相对参考平面的参数

int num_refs =2;//两个约束面,最大为3

102

int point_select [3] ;

int which_plane=1;

double reference_point [3];

char* angle_string=\"0\";

char* offset_string=\"0\";

int num_dplanes;

tag_t dplane_tag [2] ;

UF_MODL_create_relative_dplane(num_refs,objects ,point_select , which_plane, reference_point,angle_string, offset_string, &num_dplanes, dplane_tag);

UF_free(objects);

}

}

}

/*ARGSUSED*/

103

void ufusr(char *param, int *retcode, int param_len)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

运行结果截屏如下:

104

105

12、(1)创建一个圆柱体,并利用相对参考平面知识创建一个孔特征(P74)。

代码如下:

#include

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

{

if (irc)

106

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

//创建圆柱

UF_FEATURE_SIGN sign = UF_NULLSIGN;

107

tag_t target = NULL_TAG;

double cyl_origin[3] = {0.0,0.0,0.0};

char * cyl_height =\"100\";

char * cyl_diam =\"50\";

double cyl_direction[3] = {0.0,0.0,1.0};

tag_t cyl_obj;

UF_MODL_create_cylinder(sign,target,cyl_origin,cyl_height,cyl_diam,cyl_direction,&cyl_obj);

//创建相对基准平面的变量声明区

int num_refs = 1;

tag_t object_tags[3];

tag_t object_tags1[3];

int point_select[3] = {0,1,0};

108

int which_plane = 1;

char * offset_string = \"25\";

int num_dplanes;

tag_t dplane_tag[2];

tag_t dplane_tag1[2];

int layer_fea_nu;

tag_t * layer_fea_id;

//创建孔特征的变量声明区

double location[3] = {25.0,0.0,50.0};

double direction[3] = {-1.0,0.0,0.0};

char * diame =\"20\";

char * depth =\"80\";

char * angle =\"0.0\";

109

tag_t face_li;

tag_t face_t1;

tag_t hole_obj_id;

//获取圆柱体所有的面,存入链表

uf_list_p_t face_list;

UF_MODL_ask_feat_faces(cyl_obj,&face_list);

UF_MODL_ask_list_item(face_list,2,&object_tags[0]);

UF_MODL_delete_list(&face_list);

//设置61层为工作层,将以下创建的基准平面放到61层

UF_LAYER_set_status(61,UF_LAYER_WORK_LAYER);

//创建基准平面

UF_MODL_create_relative_dplane(num_refs,object_tags,point_select,which_plane,NULL,NULL,\"0\

//获取基准平面object tag

110

object_tags1[0]=dplane_tag[0];

UF_MODL_ask_feat_object(dplane_tag[0],&layer_fea_nu,&layer_fea_id);

UF_OBJ_set_blank_status(layer_fea_id[0],UF_OBJ_BLANKED);

UF_free(layer_fea_id);

//由第一个基准平面偏置出第二个基准平面,使第二个基准平面与圆柱平面相切

UF_MODL_create_relative_dplane(1,object_tags1,point_select,which_plane,NULL,NULL,offset_string,&num_dplanes,dplane_tag1);

//设置层1为工作层

UF_LAYER_set_status(1,UF_LAYER_WORK_LAYER);

//创建孔特征

face_li=dplane_tag1[0];

face_t1=object_tags[0];

UF_MODL_create_simple_hole(location,direction,diame,depth,angle,face_li,face_t1,&hole_obj_id);

111

UF_terminate();

}

void ufusr(char *param, int *retcode, int paramLen)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

112

}

void ufusr_cleanup(void)

{

/* Perform cleanup */

}

运行结果截屏:

。113

(2)在装配环境中导入一个工作部件。

代码如下:

#include

#include

#include

114

#include

#include

#include

#include

#include

#include

#include

void ufusr(char *param, int *retcode, int paramLen)

{

if(UF_initialize()!=0)

return;

tag_t parent_part=UF_ASSEM_ask_work_part();

char part[133]=\"E:\\\\workdir\\\\model1.prt\";

115

char refset_name[133]=\"\";

const char instance_name = null_tag;

double origin[3]={0.0,0.0,0.0};

double csys_matrix[6]={1,0,0,0,1,0};

int layer;

UF_LAYER_ask_work_layer(&layer);

tag_t instance;

UF_PART_load_status_t error_status;

UF_ASSEM_add_part_to_assembly(parent_part,part,refset_name,instance_name,origin,csys_matrix,layer,&instance,&error_status);

UF_free_string_array(error_status.n_parts,error_status.file_names);

UF_free(&error_status);

UF_terminate();

}

116

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

}

13、在装配环境下打开两个部件,并对其进行“对齐”约束。

代码如下:

#include

#include

#include

#include

117

#include

#include

#include

#include

#include

#include

using namespace std;

static int init_proc(UF_UI_selection_p_t select,void* user_data)

{

int num_triples = 1;

UF_UI_mask_t mask_triples[] = {UF_solid_type,

0,

UF_UI_SEL_FEATURE_PLANAR_FACE};

118

/* enable only lines and edges */

if(UF_UI_set_sel_mask(select,

UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,

num_triples,

mask_triples) == 0)

return (UF_UI_SEL_SUCCESS);

else

return (UF_UI_SEL_FAILURE);

}

extern DllExport void ufusr(char *param, int *retcode, int paramLen)

{

if(UF_initialize()!=0)

return;

119

tag_t display_part = NULL_TAG;

display_part = UF_PART_ask_display_part();/*Returns the tag of the current display part. In a non-assembly part, this is the

same as the work part. If there currently isn't a displayed part, a

NULL_TAG is returned. */

tag_t root_occ = UF_ASSEM_ask_root_part_occ(display_part);

if(root_occ==null_tag)//判断是否是装配环境

{

uc1601(\"Please return to the assembly environment\

return;

}

int response,count=0;

tag_p_t objs;

UF_UI_select_with_class_dialog(\"Select planar faces\

120

\"Align\

UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY,

init_proc,

NULL,

&response,

&count,

&objs);

if(response!=UF_UI_OK||count<2)

{

uc1601(\"Please select faces to create align constraint!\

return;

}

tag_t tool_body=null_tag,target_body=NULL_TAG;

121

UF_MODL_ask_face_body(objs[0],&target_body);

UF_MODL_ask_face_body(objs[1],&tool_body);

if( tool_body == target_body )

{

uc1601(\"Please select create!\

return;

}

//创建配合关系

UF_ASSEM_mating_condition_t mc;

tag_t from_part_occ = NULL_TAG;

tag_t to_part_occ = NULL_TAG;

//初始化约束

UF_ASSEM_init_mc(&mc);

122

//Return the tag of the part_occurrence

UF_ASSEM_ask_parent_component(objs[0],&from_part_occ);

UF_ASSEM_ask_parent_component(objs[1],&to_part_occ);

mc.mated_object = UF_ASSEM_ask_inst_of_part_occ(from_part_occ);//Returns the instance tag of a part occurrence

mc.part_occurrence = UF_ASSEM_ask_root_part_occ(display_part);//Returns the tag of the root part occurrence

mc.num_constraints = 1;//约束关系的个数

mc.constraints[0].from_part_occ = from_part_occ;

mc.constraints[0].from_status = UF_ASSEM_ok;

mc.constraints[0].from = UF_ASSEM_ask_prototype_of_occ( objs[0] );

mc.constraints[0].from_type = UF_ASSEM_planar_face;

mc.constraints[0].to = UF_ASSEM_ask_prototype_of_occ( objs[1] );

mc.constraints[0].to_part_occ = to_part_occ;

123

mc.constraints[0].to_status = UF_ASSEM_ok;

mc.constraints[0].to_type = UF_ASSEM_planar_face;

mc.constraints[0].mate_type = UF_ASSEM_align;

UF_ASSEM_mc_status_t status;

UF_ASSEM_dof_t dof;

double transform_matrix[4][4];

//求解约束

int result = UF_ASSEM_solve_mc(&mc,&status,&dof,transform_matrix);

if( result == 0 && status == UF_ASSEM_mc_solved )

{

UF_ASSEM_mc_structure_state_t struct_state;

UF_ASSEM_apply_mc_data(&mc,&struct_state,&status);

//更新模型

124

UF_DISP_refresh();

UF_MODL_update();

}

UF_terminate();

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

}

14(1)创建一个圆柱,并打一个穿孔,倒圆角。

125

代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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

static int report( char *file, int line, char *call, int irc)

126

{

if (irc)

{

char messg[133];

printf(\"%s, line %d: %s\\n\

(UF_get_fail_message(irc, messg)) ?

printf(\" returned a %d\\n\

printf(\" returned error %d: %s\\n\

}

return(irc);

}

static void do_ugopen_api(void)

{

127

UF_FEATURE_SIGN sign = UF_NULLSIGN;

tag_t target = NULL_TAG;

//创建圆柱的变量声明区

double origin [] = {0.0,0.0,0.0};

char* height = \"100\";

char* cyl_diam = \"80\";

double cyl_direction [] = {0.0,0.0,1.0};

tag_t cyl_feat_tag = NULL_TAG;

UF_MODL_create_cylinder(sign,target,origin,height,cyl_diam,cyl_direction,&cyl_feat_tag);

double value;

tag_t target1;

UF_MODL_create_exp_tag(height,&target1);//Creates a parameter expression, based upon the input string.

128

UF_MODL_ask_exp_tag_value(target1,&value);//Asks expression tag.

the value of an

//创建孔特征的变量声明区

double location[3]={0.0,0.0,0.0} ;

double direction [] = {0.0,0.0,1.0};

tag_t obj=null_tag;

char *diam=\"30\";

char *angle=\"0\";

tag_t bottom,top;

tag_t feature_obj_id ;

//链表,把圆柱所有的面放进去

uf_list_p_t face_list;

UF_MODL_create_list(&face_list);//Retrieves an object from a linked list of objects

129

UF_MODL_ask_feat_faces(cyl_feat_tag,&face_list);

int count=0;

UF_MODL_ask_list_count(face_list,&count);//Retrieves the count from a linked list of objects

for(int i=0;i{

UF_MODL_ask_list_item(face_list,i,&obj);

int type;

double point [3] ;

double dir [3] ;

double box [6] ;

double radius;

double rad_data;

130

int norm_dir ;

UF_MODL_ask_face_data(obj,&type,point,dir,box,&radius,&rad_data,&norm_dir);//Queries the data associated with a face

if(type==22)//“22”代表有界平面

{

if(fabs(point[2]-origin[2])<0.0005)

{

bottom=obj;

}

if(fabs(point[2]-value)<0.0001)

{

UF_MODL_ask_feat_location(obj,location);

top=obj;

131

}

}

}

UF_MODL_create_simple_hole(location,direction,diam,height,angle,top,bottom,&feature_obj_id);

UF_MODL_ask_feat_faces(feature_obj_id,&face_list);

UF_MODL_ask_list_count(face_list,&count);

for(int i=0;i{

UF_MODL_ask_list_item(face_list,i,&obj);//Retrieves an object from a linked list of objects

int typel;

UF_MODL_ask_face_type(obj,&typel);

//倒圆角

132

if(typel== UF_MODL_CYLINDRICAL_FACE )

{

uf_list_p_t facesl;

UF_MODL_create_list(&facesl);

UF_MODL_ask_shared_edges(top,obj,&facesl);

const char * radius=\"3\";

int smooth_overflow=0;

int cliff_overflow=0;

int notch_overflow=0;

double vrb_tool=3;

tag_t feature_obj_idl;

UF_MODL_create_blend(radius, facesl, smooth_overflow, cliff_overflow, notch_overflow, vrb_tool, &feature_obj_id );

}

133

}

}

void ufusr(char *param, int *retcode, int param_len)

{

if (!UF_CALL(UF_initialize()))

{

do_ugopen_api();

UF_CALL(UF_terminate());

}

}

int ufusr_ask_unload(void)

{

return (UF_UNLOAD_IMMEDIATELY);

134

}

运行结果截屏如下:

(2)创建一个对象,使其沿着X轴平移100,以Z轴为旋转轴旋转90度。

代码如下:

#include

#include

135

#include

#include

#include

#include

#include

extern void ufusr(char *param, int *retcode, int paramLen)

{

if(UF_initialize()!=0)

return;

//获取实体

tag_t display_part = UF_PART_ask_display_part();

tag_t objects = NULL_TAG;

UF_OBJ_cycle_objs_in_part(display_part,UF_solid_type,&objects);

136

//返回平移的矩阵

double translation[3]={100.0,0.0,0.0};

double matrix1[16];

uf5943(translation,matrix1);

//返回旋转的矩阵

double origin[3]={0.0,0.0,0.0};

double direction[3]={0.0,0.0,1.0};

double degrees_rotation=90.0;

double matrix2[16];

int status1;

uf5945(origin,direction,°rees_rotation,matrix2,&status1);

//实现平移及旋转

const int n_objects=1 ;

137

const int move_or_copy=1;

const int dest_layer=0 ;

const int trace_curves=2 ;

tag_t copies;

tag_t trace_curve_group;

int status2;

uf5947(matrix1,&objects,&n_objects,&move_or_copy,&dest_layer,

&trace_curves,&copies,&trace_curve_group,&status2);

uf5947(matrix2,&objects,&n_objects,&move_or_copy,&dest_layer,

&trace_curves,&copies,&trace_curve_group,&status2);

UF_terminate();

}

int ufusr_ask_unload(void)

138

{

return (UF_UNLOAD_IMMEDIATELY);

}

void ufusr_cleanup(void)

{

}

。139

欢迎您的下载,

资料仅供参考!

致力为企业和个人提供合同协议,策划案计划书,学习资料等等

打造全网一站式需求

140

因篇幅问题不能全部显示,请点此查看更多更全内容