基于NX平台的二次开发习题解答.doc_第1页
基于NX平台的二次开发习题解答.doc_第2页
基于NX平台的二次开发习题解答.doc_第3页
基于NX平台的二次开发习题解答.doc_第4页
基于NX平台的二次开发习题解答.doc_第5页
已阅读5页,还剩50页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于UG平台的二次开发习题解答盐城工学院优集学院二一年二月基于NX平台的二次开发课程习题解答课程代码:22021021学 时 数:32适用专业:机械设计制造及其自动化第1章 二次开发应用初步1、创建一个“Hello,你的姓名”消息对话框。要求:利用UG自带的向导创建二次开发工程项目;利用单按钮消息对话框(UC1601()创建“Hello”加你的姓名。源程序:extern C DllExport void ufusr( char *parm, int *returnCode, int rlen ) /* Initialize the API environment */ int errorCode = UF_initialize(); if ( 0 = errorCode ) /* TODO: Add your application code here */uc1601(Hello, 你的姓名,1); /* Terminate the API environment */ errorCode = UF_terminate(); /* Print out any error messages */ PrintErrorMessage( errorCode );第2章 二次开发应用框架1、创建圆柱体要求:创建一个Win32项目,并设置工程项目的属性,使其能够编译UG二次开发工程项目;创建基本体素特征圆柱体。源程序:#include #include #include #include static void do_ugopen_api(void) UF_FEATURE_SIGN sign = UF_NULLSIGN; double cyl_orig3 = 0.0,0.0,0.0; char *cyl_height=100;char *cyl_diam=100;double direction3=-1,0,0; tag_t cyl_obj; UF_MODL_create_cyl1(sign, cyl_orig, cyl_height,cyl_diam,direction, &cyl_obj);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);extern void ufusr_cleanup(void)return;2、创建一个凸台要求:凸台高H为你的学号,下底面直径B _diam为高h的两倍,上底面直径T _diam与高h相等。源程序:#include #include #include #include static void create_block(void)UF_FEATURE_SIGN sign = UF_NULLSIGN;UF_FEATURE_SIGN sign1 = UF_POSITIVE;double cone_orig3 = 0.0, 0.0, 20.0 ;char *height = “height=20.0”;char *diam2 = “.0”, “.0” ;double direction3 = 0.0,0.0,-1.0 ;tag_t cone_obj;UF_MODL_create_cone1(sign,cone_orig,height,diam,direction,&cone_obj);extern void ufusr( char *param, int *retcode, int rlen )if(UF_initialize()!=0)return;create_block();UF_terminate();return;extern int ufusr_ask_unload(void)return(UF_UNLOAD_IMMEDIATELY);extern void ufusr_cleanup(void)return;第3章 编程基础1、创建一个垫圈要求:采用表达式控制平垫圈的参数(建立垫圈,已知道d2=d1/2;h=d1/4。要求在执行程序后h=d1/2);改变平垫圈颜色为红色。源程序:#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 messg133; printf(%s, line %d: %sn, file, line, call); (UF_get_fail_message(irc, messg) ? printf( returned a %dn, irc) : printf( returned error %d: %sn, irc, messg); return(irc);static void create_gasket(void)UF_FEATURE_SIGN sign = UF_NULLSIGN;UF_FEATURE_SIGN sign1 = UF_NEGATIVE;double origin3 = 0.0, 0.0, 0.0 ;double direction3 = 0.0, 0.0, 1.0 ;UF_MODL_create_exp( length = 150);char *diam = length/3;char *diam1 = length/5;char *height = length/75;char *height1 = length/75;tag_t cyl_obj;tag_t cyl_obj1;tag_t gasket_obj_id;UF_CALL(UF_MODL_create_cyl1( sign, origin, height, diam, direction, &cyl_obj);UF_CALL(UF_MODL_create_cyl1( sign1, origin, height1, diam1, direction, &cyl_obj1);uc1601( 请按下OK键,实体的颜色将会变成红色, 1 ); UF_MODL_ask_feat_body( cyl_obj1, &gasket_obj_id );UF_OBJ_set_color( gasket_obj_id ,186 );/*ARGSUSED*/void ufusr(char *param, int *retcode, int paramLen) if (!UF_CALL(UF_initialize() create_gasket(); UF_CALL(UF_terminate(); int ufusr_ask_unload(void) return (UF_UNLOAD_IMMEDIATELY);2、创建表达式要求:创建两个矩形块两点间的距离表达式,并为距离表达式重命名,最后将当前部件中的表达式输出到信息窗口中。源程序:#include #include #include #include #include #include #include #include #include #include void ufusr(char *param, int *retcode, int param_len) if (!UF_initialize() /* 定义Block的参数 */ double block_orig3 = 0.0,0.0,0.0; double block_orig13 = 5.0,5.0,5.0; char *block_len3 = 1,2,3; tag_t blk_obj,blk_obj1; /* 定义表达式的参数 */ char *string; tag_t *exps; int number_of_exps=0,i=0; char bufUF_UI_MAX_STRING_LEN+1; tag_t part, feat_tag, exp_tag, solid, solid_body2; int type, subtype; /* 重命名距离表达式的参数 */ char *exp_string, *lhs_str, *rhs_str; char *exp_name; tag_t exp1_tag; /* 创建两个Block */ UF_MODL_create_block1(UF_NULLSIGN,block_orig, block_len,&blk_obj); UF_MODL_create_block1(UF_NULLSIGN,block_orig1, block_len,&blk_obj1); UF_UI_open_listing_window(); /* 找出所有的Solid实体 */ if(part=UF_PART_ask_display_part() = NULL_TAG) UF_UI_write_listing_window(Failed to get part tagn); return; solid=NULL_TAG; while (i 2) type=UF_solid_type; UF_OBJ_cycle_objs_in_part( part, type, &solid ); UF_OBJ_ask_type_and_subtype(solid, &type, &subtype); if (subtype = UF_solid_body_subtype) solid_bodyi+=solid; /* 创建一个距离表达式 */ UF_GEXP_create_distance(solid_body0, NULL_TAG, solid_body1, NULL_TAG, &feat_tag, &exp_tag); /* 为距离表达式重新命名 */ UF_MODL_ask_exp_tag_string( exp_tag, &exp_string ); UF_MODL_dissect_exp_string( exp_string, &lhs_str, &rhs_str, &exp1_tag ); UF_MODL_rename_exp( lhs_str, geo_exp ); UF_free( exp_string ); UF_free( lhs_str ); UF_free( rhs_str ); /* 获取表达式的数量 */ UF_MODL_ask_exps_of_part(part,&number_of_exps,&exps); sprintf(buf,当前部件文件一共有 %d 个表达式。n,number_of_exps); UF_UI_write_listing_window(buf); UF_UI_write_listing_window(其中几何表达式为:n); /* 获取距离表达式并显示在列表框中 */ for(i=0;inumber_of_exps;i+) logical flag; UF_MODL_is_geometric_expression(expsi,&flag); if (flag = TRUE) UF_MODL_ask_exp_tag_string(expsi,&string); sprintf(buf,距离表达式 = %sn,string); UF_UI_write_listing_window(buf); UF_free(string); UF_free(exps); /* 调整视图到合适的大小 */ tag_t view_tag = NULL_TAG; UF_VIEW_ask_work_view( &view_tag ); UF_VIEW_fit_view( view_tag, 0.7 ); UF_UI_write_listing_window( 请打开部件导航器, 选中DISTANCE_EXP(2),观察距离表达式。); /* 保存部件文件 */ UF_PART_save( ); UF_terminate(); 第4章 用户界面开发技术1、利用单选菜单列表对话框(UC1603()创建四个基本体素特征要求: 根据用户的不同选择,创建相应的基本体素特征;界面如下图所示。源程序:#include #include #include #include #include #include extern void ufusr( char * param, int * retcod, int param_len ) /* 对话框设置 */ char *cp1 = 请选择; int ip2 = 0; char cp3438 = 创建一个块(Block), 创建一个圆柱(Cylinder), 创建一个圆锥(Cone), 创建一个球体(Sphere) ; int ip4 = 4;int value; tag_t view_tag = NULL_TAG; UF_FEATURE_SIGN sign = UF_NULLSIGN; /* Block的变量声明区 */ double blk_corner 3 = 0.0, 0.0, 0.0 ; char *blk_edge_len 3 = 10.0, 20.0, 30.0 ; tag_t blk_obj_id; /* Cylinder的变量声明区 */ double cyl_origin 3 = 0.0, 0.0, 0.0 ; char *cyl_height = 20.0; char *cyl_diam = 10.0; double cyl_direction 3 = 0.0, 0.0, 1.0 ; tag_t cyl_obj_id; /* Cone的变量声明区 */ double cone_origin 3 = 0.0, 0.0, 0.0 ; char *cone_height = 20.0; char *cone_diam 2 = 30.0, 20.0 ; double cone_direction 3 = 0.0, 0.0, 1.0 ; tag_t cone_obj_id; /* Sphere的变量声明区 */ double sphere_center 3 = 0.0, 0.0, 0.0 ; char *sphere_diam = 30.0; tag_t sphere_obj_id; if ( !UF_initialize() ) /* 显示菜单 */ value = uc1603( cp1, ip2, cp3, ip4 ); switch( value ) case 5: /* 创建Block(块) */ UF_MODL_create_block1( sign, blk_corner, blk_edge_len, &blk_obj_id ); break; case 6: /* 创建Cylinder(圆柱) */ UF_MODL_create_cyl1( sign, cyl_origin, cyl_height, cyl_diam, cyl_direction, &cyl_obj_id ); break; case 7: /* 创建Cone(圆锥) */ UF_MODL_create_cone1( sign, cone_origin, cone_height, cone_diam, cone_direction, &cone_obj_id ); break; case 8: /* 创建Sphere(球体) */ UF_MODL_create_sphere1( sign, sphere_center, sphere_diam, &sphere_obj_id ); break; default: uc1601( 发生了错误!, 0 ); /* 调整视图到合适的大小 */ UF_VIEW_ask_work_view( &view_tag ); UF_VIEW_fit_view( view_tag, 0.7 ); /* 保存文件 */ UF_PART_save( ); UF_terminate( ); 2、利用UIStyler、Menu等工具创建一个对话框来生成垫圈。要求:对话框通过菜单和工具条激活;平垫圈的尺寸由用户通过对话框输入,对话框如下图如示: 源程序:(a)菜单文件xx.menVERSION 120!-EDIT UG_GATEWAY_MAIN_MENUBARTOP_MENUCASCADE_BUTTON CUSTOM_MYMENULABEL 实例END_OF_TOP_MENUMENU CUSTOM_MYMENUBUTTON MAKE_GASKETLABEL 创建垫圈.ACTIONS DianQuan.dlgEND_OF_MENU(b)工具条文件xx.tbrTITLE 实例VERSION 170!-BUTTON MAKE_GASKETLABEL 创建垫圈(c)主程序代码int Z_ok_cb ( int dialog_id, void * client_data, UF_STYLER_item_value_type_p_t callback_data)UF_STYLER_item_value_type_t date;UF_FEATURE_SIGN sign = UF_NULLSIGN;double origin3 = 0.0,0.0,0.0;char height10; /厚度char diam10; /外径double direction3 = 0.0,0.0,1.0;tag_t cyl_obj_id;double location3 = 0.0,0.0,0.0;double hole_dir3 = 0.0,0.0,-1.0;char hole_diam10; /内径const char * angle = 0;tag_t face_li;tag_t face_t1;tag_t hole_feat_id;uf_list_p_t face_list; /* Make sure User Function is available. */ if ( UF_initialize() != 0) return ( UF_UI_CB_CONTINUE_DIALOG ); /* - Enter your callback code here - */ date.item_attr = UF_STYLER_VALUE; date.item_id = Z_REAL_IN; UF_STYLER_ask_value(dialog_id , &date); sprintf( hole_diam , %f , date.value.real); UF_STYLER_free_value(&date); date.item_attr = UF_STYLER_VALUE; date.item_id = Z_REAL_OUT; UF_STYLER_ask_value(dialog_id , &date); sprintf( diam , %f , date.value.real); UF_STYLER_free_value(&date); date.item_attr = UF_STYLER_VALUE; date.item_id = Z_REAL_H; UF_STYLER_ask_value(dialog_id , &date); sprintf( height , %f , date.value.real); UF_STYLER_free_value(&date); UF_MODL_create_cyl1 (sign, origin, height, diam,direction, &cyl_obj_id ); UF_MODL_ask_feat_faces(cyl_obj_id , &face_list); UF_MODL_ask_list_item(face_list , 0 , &face_li); UF_MODL_ask_list_item(face_list , 1 , &face_t1); UF_MODL_create_simple_hole (location,direction, hole_diam, height, angle, face_li, face_t1, &hole_feat_id ); UF_terminate (); /* Callback acknowledged, terminate dialog */ /* It is STRONGLY recommended that you exit your */ /* callback with UF_UI_CB_EXIT_DIALOG in a ok callback.*/ /* return ( UF_UI_CB_EXIT_DIALOG ); */ return (UF_UI_CB_EXIT_DIALOG); 3、创建一个点收集器要求:能够收集用户所选择的点,并在单击“OK”和“Yes”按钮后,将用户所选的点的坐标信息在UG信息窗口中显示出来。源程序:void do_ugopen_api()char sMessage = 点收集器_朱佳乐;logical lCoincidentpoints = true;UF_UI_chained_points_p_t chained_points;int iCount;int iResponse;UF_UI_select_point_collection(sMessage,lCoincidentpoints,&chained_points,&iCount,&iResponse);char *str = new char100;if(iResponse = UF_UI_OK)UF_UI_open_listing_window();UF_UI_write_listing_window( );UF_UI_write_listing_window(X坐标:);UF_UI_write_listing_window( );UF_UI_write_listing_window(Y坐标:);UF_UI_write_listing_window( );UF_UI_write_listing_window(Z坐标:);UF_UI_write_listing_window(n);for( int i = 1; i pt0,chained_points-pt1,chained_points-pt2);UF_UI_write_listing_window(str);UF_UI_write_listing_window(n);chained_points+;delete str;第5章 外部数据访问1、访问外部Excel数据。要求:利用存贮在外部Excel中的数据创建圆柱。ab501050305050源程序:#include #include #include #include #include #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) if (irc) char sFailMessage133; UF_get_fail_message(irc,sFailMessage); char sWholeMessage256; sprintf(sWholeMessage, File Name:%snLine:%dnFunction:%snError Code:%dnMessage:%s, file,line,call,irc,sFailMessage); MessageBox( NULL, sWholeMessage, Error in Function, MB_OK|MB_ICONWARNING|MB_SYSTEMMODAL); return(irc); static void error_return(char *msg) fprintf(stderr,n+ERROR : %s.nn,msg); exit(1);static void report_error(int result,char *alt_msg) char err_message200; int found = UF_get_fail_message(result,err_message); if (found != 0) error_return(alt_msg); else error_return(err_message);static void create_cyl( double origin_y, char *cyl_height, char *cyl_diam ) UF_FEATURE_SIGN sign = UF_NULLSIGN; double cyl_origin 3 = 0.0, 0.0, 0.0 ; double cyl_direction 3 = 0.0, 0.0, 1.0 ; tag_t cyl_obj_id; cyl_origin1 = origin_y; UF_MODL_create_cyl1(sign, cyl_origin, cyl_height, cyl_diam, cyl_direction, &cyl_obj_id );static void do_ugopen_api(void) CDatabase Database; / 定义一个MFC的CDatabse数据库类对象Databse CString SQLCommand = select * from ppdc; / SQL查询语句 DWORD dwOptions = 0; / 设置连接的建立方式 CDBVariant temp; / 数据库数据通用类型 int result; / 储存返回值的变量 char cyl_height133; char cyl_diam133; double *cyl = NULL; try result = Database.OpenEx( DSN=ugsc, dwOptions ); / 打开数据库 if( result != 0 ) / 如果成功打开 CRecordset rs( &Database ); / 定义记录集 if ( rs.Open( CRecordset:snapshot, SQLCommand ) != false ) rs.MoveFirst( ); short nFields = rs.GetODBCFieldCount( ); / 获取记录的字段数目 while( !rs.IsEOF( ) ) cyl =(double *)malloc( nFields * sizeof(double) ); for( short index = 0; index m_strError ); pe - Delete( ); 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*/ return ( UF_UNLOAD_IMMEDIATELY );2、创建标准件六角头螺栓参数化设计系统。要求:六角头螺栓几何参数存储在外部Access数据库(.mdb)中,采用ODBC或ADO访问外部数据;根据用户选择的相应规格创建六角头螺栓,界面如下图所示。源程序:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SCREW_CB_COUNT ( 3 + 1 ) #define SCREW_BITMAP_SCREW (BITMAP_SCREW)#define SCREW_SEP_1 (SEP_1)#define SCREW_OPTION_CHOICE (OPTION_CHOICE)#define SCREW_DIALOG_OBJECT_COUNT ( 3 )#define SCREW_OPTION_CHOICE_COUNT 14#define LAYER_SOLID 1#define LAYER_SKETCH 21#define LAYER_CURVE 41class Cscrew : public CRecordsetpublic: Cscrew( CDatabase* pDatabase = NULL ); DECLARE_DYNAMIC( Cscrew ) CStringm_spec;/螺纹规格 doublem_d;/螺栓轴直径 doublem_b;/螺纹长度 doublem_a;/a(max) doublem_e;/螺栓帽外接圆直径 doublem_k;/公称 doublem_r;/r(min) doublem_s;/螺栓帽内接圆直径 doublem_l;/螺栓轴长度 doublem_weight;/100mm长的重量(kg)public: virtual CString GetDefaultSQL( ); virtual void DoFieldExchange

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论