小白学UG二次开发_UG/Open UIStlyer对话框设计
本例参考《UG/Open API、MFC和COM开发实例精解》 黄勇,本例基本参照书中内容操作,没有做太大修改,能正常运行。第一步,准备工作在UGII_USER_DIR目录里新建startup,application两个文件夹。书中是这么说的,但是我只用到了application,startup我没有用到。怎么设置环境变量我就不说了。
第二步,设计UI.
UG开始-》所有应用模块-》NX 6 之前版本UI样式编辑器选之前的版本主要为了降低开发难度。进入UIStyler 对话框设计环境,新建文件。
里面有很多控件,自己试着点点看,探索下。
里面有对象浏览器,对话框和资源编辑器,对话框的标题和提示如下图所示,前缀名比较重要,直接关系到后续程序生成后的一些参数名称,我们这边设置为DIALOG_ACCESS。
设置如图所示的整数、实数、字符串和按钮控件,导航按键中按钮样式选项里选择的不同项会影响到后续的回调函数。这里选择关闭。整数,实数,字符串修改下标签和标识符。这个会在对象浏览器里反应出来。目前我还不会修改类型,就放着没管,如果有知道的,请帮忙解释下类型。
另外,我对回调函数的名称生成机制也不是很清楚,只是知道Back_cb应该是关闭对话框时触发的回调函数,action_3_act_cb是按钮空间触发的回调函数,名字和书上不太一样,但是能正常实现功能。
完成后,另存为对话框到application,语言选C,对话框名称为access_dialog,在目录下会生成access_dialog.dlg,access_dialog.h,access_dialog_template.c三个文件,进入文件夹,将最后一个文件改成access_dialog.cpp并保存。
按之前帖子的方法建立一个DDL工程,注意目录放在UGII_USER_DIR下,名称也为Dialog_Access.将access_dailog.h和 access_dialog.cpp复制到工程中,添加到头文件和源文件中。接下来是虐心的代码。。。。
/*=============================================================================
WARNING!!This file is overwritten by the UIStyler each time the Styler
file is saved.
Filename:access_dialog_template.c
This file was generated by the NX User Interface Styler
Created by: Daniel
Version: NX 8.5
Date: 03-23-2015
Time: 19:32
This template file is overwritten each time the UIStyler dialog is
saved.Any modifications to this file will be lost.
==============================================================================*/
/*==============================================================================
Purpose:This TEMPLATE file contains C source and static structures to
guide you in the construction of your NX Open application dialog.
The generation of your dialog file (.dlg extension) is the first step towards
dialog construction within Unigraphics.You must now create a UGOpen
application that utilizes this file (.dlg).
生成对话框文件dig是第一步,然后要利用dlg文件创建一个UGOpen应用
The information in this file provides you with the following:
1.Help on the use of the functions, UF_MB_add_styler_actions and
UF_STYLER_create_dialog in your NX Open application.These functions
will load and display your UIStyler dialog in Unigraphics.
两个重要函数
An example of the function, UF_MB_add_styler_actions to associate your
dialog to the menubar is shown below (Search on Example 1).
这个example1 和2 在哪里?谁知道?
An example of a invoking a dialog from a callback utilizing
UF_STYLER_create_dialog is also shown below (Search for Example 2).
An example of a user exit utilizing UF_STYLER_create_dialog is also
shown below (Search for Example 3).
2.The callback structure(回调结构体?不知道是不是这么翻译的): --- DIALOG_ACCESS_cbs ---
This structure is VERY important if you have callbacks associated with
your dialog.It correlates the dialog items in your dialog with the
callback functions you must supply.You should not modify this
structure since it MUST match up to the information stored in your
dialog file (.dlg).用以链接对话框项目(item)和回调函数,其必须和dlg文件匹配,不要修改。Any attempt to do so will cause an error while
constructing your dialog. If you wish to modify the association of
your callbacks to your dialog, please reload your dialog file (.dlg) into
the UIStyler and regenerate your files.
You do not need to be concerned about this structure, simply pass it as
an argument to the function, UF_STYLER_create_dialog along with your
dialog file (.dlg).
Example 1 displays the actual call you may make for this particular
dialog.
3.The empty callback functions (stubs) associated with your dialog items
have also been placed in this file.These empty functions have been
created simply to start you along with your coding requirements.
The function name, argument list and possible return values have already
been provided for you.
文件中也已经生成了空的回调函数,后续要实现啥功能,就要在里面编程。
NOTE:Each callback must be wrappered with the functions UF_initialize()
and UF_terminate().
==============================================================================*/
/* These include files are needed for the following template code. */
#include <stdio.h>
#include <uf.h>
#include <uf_defs.h>
#include <uf_exit.h>
#include <uf_ui.h>
#include <uf_styler.h>
#include <uf_mb.h>
#include "access_dialog.h"//这里要用引号
/* The following definition defines the number of callback entries */
/* in the callback structure: */
/* UF_STYLER_callback_info_t DIALOG_ACCESS_cbs */
#define DIALOG_ACCESS_CB_COUNT ( 3 + 1 ) /* Add 1 for the terminator */
/*--------------------------------------------------------------------------
The following structure defines the callback entries used by the
styler file.This structure MUST be passed into the user function,
UF_STYLER_create_dialog along with DIALOG_ACCESS_CB_COUNT.
--------------------------------------------------------------------------*/
static UF_STYLER_callback_info_t DIALOG_ACCESS_cbs =
{
{UF_STYLER_DIALOG_INDEX, UF_STYLER_APPLY_CB , 0, DIALOG_ACCESS_apply_cb},
{UF_STYLER_DIALOG_INDEX, UF_STYLER_BACK_CB , 0, DIALOG_ACCESS_back_cb},
{DIALOG_ACCESS_BUTTON, UF_STYLER_ACTIVATE_CB , 0, DIALOG_ACCESS_action_3_act_cb},
{UF_STYLER_NULL_OBJECT, UF_STYLER_NO_CB, 0, 0 }
};
这里我还没怎么搞明白,先不管。不过应该是和后续回调函数调用时需要用到的一些结构体。
/*--------------------------------------------------------------------------
UF_MB_styler_actions_t contains 4 fields.These are defined as follows:
Field 1 : the name of your dialog that you wish to display.
Field 2 : any client data you wish to pass to your callbacks.
Field 3 : your callback structure.
Field 4 : flag to inform menubar of your dialog location.This flag MUST
match the resource set in your dialog!Do NOT ASSUME that changing
this field will update the location of your dialog.Please use the
UIStyler to indicate the position of your dialog.
--------------------------------------------------------------------------*/
static UF_MB_styler_actions_t actions[] = {
{ "access_dialog.dlg",NULL, DIALOG_ACCESS_cbs,UF_MB_STYLER_IS_NOT_TOP },
{ NULL,NULL,NULL,0 } /* This is a NULL terminated list */
};
这个我也没特别闹明白,但是应该是菜单栏调用UI需要采用该结构体数组。
/*---------------- MENUBAR HOOKUP HELP Example -------------------
这里是从菜单栏调用UI的方法,本例忽略,后续会去试一试
To launch this dialog from a Unigraphics menubar, you must follow
the steps below.
1)Add the following lines to your MenuScript file in order to
associate a menu bar button with your dialog.In this
example, a cascade menu will be created and will be
located just before the Help button on the main menubar.
The button, ACCESS_DIALOG_BTN is set up to launch your dialog and
will be positioned as the first button on your pulldown menu.
If you wish to add the button to an existing cascade, simply
add the 3 lines between MENU LAUNCH_CASCADE and END_OF_MENU
to your menuscript file.
The MenuScript file requires an extension of ".men".
Make sure that you add the extension to the file and place
the file in your startup directory:
$UGII_USER_DIR/startup or
$UGII_SITE_DIR/startup or
$UGII_VENDOR_DIR/startup directory
Move the contents between the dashed lines to your Menuscript file.
! ----------------------------------------------------------------
VERSION 120
EDIT UG_GATEWAY_MAIN_MENUBAR
BEFORE UG_HELP
CASCADE_BUTTON UISTYLER_DLG_CASCADE_BTN
LABEL Dialog Launcher
END_OF_BEFORE
MENU UISTYLER_DLG_CASCADE_BTN
BUTTON ACCESS_DIALOG_BTN
LABEL Display access_dialog dialog
ACTIONS access_dialog.dlg
END_OF_MENU
! ---------------------------------------------------------------
2) Issue a call to the function, UF_MB_add_styler_actions from the ufsta
user exit as shown below.To use this call, remove the conditional
definitions:#ifdef MENUBAR_COMMENTED_OUT
#endif MENUBAR_COMMENTED_OUT
The static structure, actions, will allow you to associate ALL of your
dialogs and callback functions to themenubar at once.For example, if you
wish to have 10 dialogs associated to 10 different buttons on the menubar,
you may enter each dialog and callback list into the actions structure. Make
sure that you have created a corresponding button in your MenuScript file.
You may also have separate shared libraries, each with a ufsta user exit
for each individual dialog.
3) Place your compiled and linked ufsta user function in
$UGII_USER_DIR/startup or
$UGII_SITE_DIR/startup or
$UGII_VENDOR_DIR/startup directory.
NOTE:The user function must contain the proper extension .so, .sl or .dll
to make ensure that it is recognized by the MenuScript.If it does not
have the proper extension, it will NOT be recognized by MenuScript.
The action name you have provided in your MenuScript must correspond to
to the dialog name provided in the action structure.This MUST match inorder
to bind your dlg file to your MenuScript button.
4) Copy your UIStyler dialog file to the proper directory.
All dialog files (.dlg) must be located in
$UGII_USER_DIR/application or
$UGII_SITE_DIR/application or
$UGII_VENDOR_DIR/application directory
------------------------------------------------------------*/
#ifdef MENUBAR_COMMENTED_OUT
extern void ufsta (char *param, int *retcode, int rlen)
{
interror_code;
if ( (UF_initialize()) != 0)
return;
if ( (error_code = UF_MB_add_styler_actions ( actions ) ) != 0 )
{
char fail_message;
UF_get_fail_message(error_code, fail_message);
printf ( "%s\n", fail_message );
}
UF_terminate();
return;
}
#endif /*MENUBAR_COMMENTED_OUT*/
/*-------DIALOG CREATION FROM A CALLBACK HELP Example ----------
从其他UI对话框调用这个UI对话框,本例不涉及
If you wish to have this dialog displayed from the callback of
another UIStyler dialog, you should:
1) Make sure that the callback of your UIStyler dialog is
designated as a dialog building callback.
2) Remove the conditional definitions:
#ifdef DISPLAY_FROM_CALLBACK
#endif DISPLAY_FROM_CALLBACK
3) Your callback should issue a call to this function.
4) You should also add the funcitonal prototype to your header file
(access_dialog.h) and ensure that the file is properly included.
All dialog files must be located in
$UGII_USER_DIR/application or
$UGII_SITE_DIR/application or
$UGII_VENDOR_DIR/application directory
--------------------------------------------------------------*/
#ifdef DISPLAY_FROM_CALLBACK
extern int <enter the name of your function> ( int *response )
{
interror_code = 0;
if ( ( error_code = UF_initialize() ) != 0 )
return (0) ;
if ( ( error_code = UF_STYLER_create_dialog ( "access_dialog.dlg",
DIALOG_ACCESS_cbs, /* Callbacks from dialog */
DIALOG_ACCESS_CB_COUNT, /* number of callbacks*/
NULL, /* This is your client data */
response ) ) != 0 )
{
char fail_message;
/* 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", fail_message );
}
UF_terminate();
return (error_code);
}
#endif /* DISPLAY_FROM_CALLBACK */
/*-------DIALOG CREATION FROM A USER EXIT HELP Example --------
从USER EXIT生成这个对话框,调用NX Open API,UF_STYLER_create_dialog,
To create this dialog from a user exit, you must invoke a
call to the NX Open API, UF_STYLER_create_dialog.An example
is shown below.
All dialog files must be located in 这个是讲LIALOG文件存放路径的,之前已经存过了,问题不大。
$UGII_USER_DIR/application or
$UGII_SITE_DIR/application or
$UGII_VENDOR_DIR/application directory
1) Remove the conditional definitions:
这个要求把这两行注释掉,或者在之前#define DISPLAY_FROM_USER_EXIT
#ifdef DISPLAY_FROM_USER_EXIT
#endif DISPLAY_FROM_USER_EXIT
2) Add a user exit to the function name below, for example, ufusr.
3) Consider how your shared library will be unloaded.Take a look
at the generated function ufusr_ask_unload.
--------------------------------------------------------------*/
//#define DISPLAY_FROM_USER_EXIT 如果这里定义了 DISPLAY_FROM_USER_EXIT,代码也会有效。
//#ifdef DISPLAY_FROM_USER_EXIT
extern void ufusr (char *param, int *retcode, int rlen)//这里的ufusr是后续添进去的,
{
intresponse = 0;
interror_code = 0;
if ( ( UF_initialize() ) != 0 )
return;
if ( ( error_code = UF_STYLER_create_dialog ( "access_dialog.dlg",
DIALOG_ACCESS_cbs, /* Callbacks from dialog */
DIALOG_ACCESS_CB_COUNT, /* number of callbacks*/
NULL, /* This is your client data */
&response ) ) != 0 )
{
char fail_message;
/* 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", fail_message );
}
UF_terminate();
return;
}
/*--------------------------------------------------------------------------
This function specifies how a shared image is unloaded from memory
within Unigraphics. This function gives you the capability to unload an
internal NX Open application or userexit from Unigraphics.You can
specify any one of the three constants as a return value to determine
the type of unload to perform:immediately after user function
execution, via an unload selection dialog, or when Unigraphics terminates
terminates.If you choose UF_UNLOAD_SEL_DIALOG, then you have the
option to unload your image by selectingFile->Utilities->Unload Shared
Image.
NOTE:A program which associates NX Open applications with the menubar
MUST NOT use this option since it will UNLOAD your NX Open application image
--------
from the menubar.
--------------------------------------------------------------------------*/
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 ); */
}
这个函数先不管
/*--------------------------------------------------------------------------
You have the option of coding the cleanup routine to perform any housekeeping
chores that may need to be performed.If you code the cleanup routine, it is
automatically called by Unigraphics.
--------------------------------------------------------------------------*/
extern void ufusr_cleanup (void)
{
return;
}
//#endif /* DISPLAY_FROM_USER_EXIT */
这个也不用管,目前我也不是特明白
/*-------------------------------------------------------------------------*/
/*---------------------- UIStyler Callback Functions ----------------------*/
/*-------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------
* Callback Name: DIALOG_ACCESS_apply_cb
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with.The dialog id is a dynamic,
* unique id and should not be stored.It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog.Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int DIALOG_ACCESS_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.*/
return (UF_UI_CB_CONTINUE_DIALOG);
}
/* -------------------------------------------------------------------------
* Callback Name: DIALOG_ACCESS_back_cb
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with.The dialog id is a dynamic,
* unique id and should not be stored.It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog.Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int DIALOG_ACCESS_back_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 ----- */
uc1601("Hello,boy,you will exit this dialog!",1);//这句是自己添加的,当关闭对话框时,会运行这个函数
UF_terminate ();
/* Callback acknowledged, terminate dialog. */
/* It is STRONGLY recommended that you exit your */
/* callback with UF_UI_CB_EXIT_DIALOG in a back call */
/* back rather than UF_UI_CB_CONTINUE_DIALOG. */
return (UF_UI_CB_EXIT_DIALOG);
}
/* -------------------------------------------------------------------------
* Callback Name: DIALOG_ACCESS_action_3_act_cb
* This is a callback function associated with an action taken from a
* UIStyler object.
*按button的回调函数,会有一系列的动作。
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with.The dialog id is a dynamic,
* unique id and should not be stored.It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog.Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int DIALOG_ACCESS_action_3_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_STYLER_item_value_type_t data;
char info;
data.item_attr=UF_STYLER_VALUE;
data.item_id=DIALOG_ACCESS_INTEGER;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=DIALOG_ACCESS_REAL;
UF_STYLER_ask_value(dialog_id,&data);
data.item_attr=UF_STYLER_VALUE;
data.item_id=DIALOG_ACCESS_STRING;
UF_STYLER_ask_value(dialog_id,&data);
//将格式化的数据写入字符缓冲区
sprintf(info,"整数为:%d\n实数为:%f\n字符串为:%s\n",data.value.integer,data.value.real,data.value.string);
uc1601(info,1);
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 ); */
}
最后的效果
沙发我来坐,非常感谢楼主的分享学习!~{:victory:} 老叶 发表于 2015-3-23 23:05 static/image/common/back.gif
沙发我来坐,非常感谢楼主的分享学习!~
谢谢支持,这也是对我自己学习UG二次开发的积累和激励。 接下来想学下菜单栏的二次开发,还有从菜单栏调用UIStyler,还有对话框调用对话框等几种不同的方式,后续还会继续发帖。 奇塔车饰 发表于 2015-3-24 11:59 static/image/common/back.gif
接下来想学下菜单栏的二次开发,还有从菜单栏调用UIStyler,还有对话框调用对话框等几种不同的方式,后续还 ...
楼主,关于NX和vs配置方面有没有遇到问题啊,我现在是不能创建项目 zyxiu 发表于 2015-4-18 21:08 static/image/common/back.gif
楼主,关于NX和vs配置方面有没有遇到问题啊,我现在是不能创建项目
我另外有帖子讲到,请参看 我想问一下楼主,后面说语言选择c,怎么设置啊?我的默认是vb啊
这个我不太清楚哈 wx804207051 发表于 2015-12-3 16:27
我想问一下楼主,后面说语言选择c,怎么设置啊?我的默认是vb啊
就在旁边,首选项里也有
页:
[1]
2