列文章目录
文章目录
前言
随着工业智能化的不断发展,UG二次开发的需求越来越多,也吸引了大批的二开从业人员,本人作为一名资深IT从业者(10年+)也毅然加入二次开发大军。
然而,和流行IT行业(互联网、金融、医疗等)相比,工业智能化的门槛显得更高一点,专业的工业软件,相对封闭的开发理念和更小的开发圈子,让刚进入二开的从业者有点举步维艰。边学边整理,希望通过这系列文章的整理能给二开的生态增添一叶绿。
一、UI样式文件分析
1. 样式文件目录
目录位置:
UGOPEN\SampleNXOpenApplications\C++\BlockStyler\UDB_CreateCylinder\CreateCylinder_UsingUDB.dlx
UGOPEN\SampleNXOpenApplications\C++\BlockStyler\UDB_CreateCylinder\DimensionUDB.dlx
UGOPEN\SampleNXOpenApplications\C++\BlockStyler\UDB_CreateCylinder\DimensionUDB.udx
2. 样式文件导入预览
3. 样式文件解析
本实例主要用到的控件如下 : 指定点、指定矢量、UDB控件
如上图,DimensionUDB.dlx文件生成UDB控件只需要在文件生成类型选择用户定义UI块,然后生成的DimensionUDB.udx文件需要放到开发目录 {UGII_USER_DIR}/application
二、源码文件解析
1. 主程序分析
1.1 加载模块
extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
{
CreateCylinder_UsingUDB *theCreateCylinder_UsingUDB = NULL;
try
{
theCreateCylinder_UsingUDB = new CreateCylinder_UsingUDB();
// The following method shows the dialog immediately
theCreateCylinder_UsingUDB->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
CreateCylinder_UsingUDB::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
if(theCreateCylinder_UsingUDB != NULL)
{
delete theCreateCylinder_UsingUDB;
theCreateCylinder_UsingUDB = NULL;
}
}
CreateCylinder_UsingUDB::CreateCylinder_UsingUDB()
{
try
{
// Initialize the NX Open C++ API environment
CreateCylinder_UsingUDB::theSession = NXOpen::Session::GetSession();
CreateCylinder_UsingUDB::theUI = UI::GetUI();
theDlxFileName = "CreateCylinder_UsingUDB.dlx";
theDialog = CreateCylinder_UsingUDB::theUI->CreateDialog(theDlxFileName);
// Registration of callback functions
theDialog->AddApplyHandler(make_callback(this, &CreateCylinder_UsingUDB::apply_cb));
theDialog->AddOkHandler(make_callback(this, &CreateCylinder_UsingUDB::ok_cb));
theDialog->AddUpdateHandler(make_callback(this, &CreateCylinder_UsingUDB::update_cb));
theDialog->AddInitializeHandler(make_callback(this, &CreateCylinder_UsingUDB::initialize_cb));
theDialog->AddDialogShownHandler(make_callback(this, &CreateCylinder_UsingUDB::dialogShown_cb));
diameter = DimensionUDB::RegisterUserDefinedUIBlock(theDialog, "diameter");
height = DimensionUDB::RegisterUserDefinedUIBlock(theDialog, "height");
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
throw;
}
}
用户定义控件的使用方法和通用控件有些许差别:
diameter = DimensionUDB::RegisterUserDefinedUIBlock(theDialog, "diameter");
height = DimensionUDB::RegisterUserDefinedUIBlock(theDialog, "height");设置用户定义控件的方法如下,此方法需要自己写:
diameter->SetLabel("Diameter");
diameter->SetValue(200);height->SetLabel("Height");
height->SetValue( 500);
1.2 提交模块
int CreateCylinder_UsingUDB::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----
CreateCylinder();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
CreateCylinder_UsingUDB::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}
void CreateCylinder_UsingUDB::CreateCylinder()
{
Part* workPart(theSession->Parts()->Work());
Features::CylinderBuilder* cylinderBuilder1 = NULL;
cylinderBuilder1 = workPart->Features()->CreateCylinderBuilder(NULL);
//Get the point
PropertyList* pointPropertyList = point0->GetProperties();
Point3d originPoint = pointPropertyList->GetPoint("Point");
delete pointPropertyList;
pointPropertyList = NULL;
//Get the direction
PropertyList* vectorPropertyList = vector0->GetProperties();
Vector3d direction = vectorPropertyList->GetVector("Vector");
delete vectorPropertyList;
vectorPropertyList = NULL;
Direction* direction1 = NULL;
direction1 = workPart->Directions()->CreateDirection(originPoint, direction, SmartObject::UpdateOptionWithinModeling);
//Set diameter and height
std::stringstream diameterValue;
diameterValue << diameter->GetValue();
std::stringstream heightValue;
heightValue << height->GetValue();
cylinderBuilder1->Diameter()->SetRightHandSide(diameterValue.str());
cylinderBuilder1->Height()->SetRightHandSide(heightValue.str());
//Set the axis
Axis* axis1 = NULL;
axis1 = cylinderBuilder1->Axis();
axis1->Point()->SetCoordinates(originPoint);
axis1->SetDirection(direction1);
//Commit
cylinderBuilder1->Commit();
cylinderBuilder1->Destroy();
cylinderBuilder1 = NULL;
}
这里通过CylinderBuilder类实现了生成圆柱体方法,需要关注获取圆柱直径和高度的方法,用户控件的方法都需要在用户控件里面定义和实现
std::stringstream diameterValue;
diameterValue << diameter->GetValue();
std::stringstream heightValue;
heightValue << height->GetValue();
2. UDB分析
//Sets the label on the double block
void DimensionUDB::SetLabel(const char* label)
{
PropertyList* prop = Dimension->GetProperties();
prop->SetString("Label", label);
delete prop;
prop = NULL;
}
//Sets the value of the double block
void DimensionUDB::SetValue(const double value)
{
PropertyList* prop = Dimension->GetProperties();
prop->SetDouble("Value", value);
delete prop;
prop = NULL;
}
//Sets the value of the double block
double DimensionUDB::GetValue() const
{
PropertyList* prop = Dimension->GetProperties();
double value = prop->GetDouble("Value");
delete prop;
prop = NULL;
return value;
}
//Enables/Disables the double block based on toggle. If toggled on, the double-field is disabled
void DimensionUDB::EnableDimensionField()
{
PropertyList* prop = sensitiveFlag->GetProperties();
bool flag = prop->GetLogical("Value");
delete prop;
prop = NULL;
//Disable double block if toggle field is on
PropertyList* dimensionPropertyList = Dimension->GetProperties();
dimensionPropertyList->SetLogical("Enable", !flag);
delete dimensionPropertyList;
dimensionPropertyList = NULL;
}
编写用户定义控件的时候,只需要关注我们需要的内容,这里我们要用到3个功能:
1、切换开关的时候,输入框可用/不可用
2、设置输入框的默认值
3、获取输入框的内容
3. 运行结果截图
总结
官方实例UDB_CreateCylinder
本节主要讲解UDB的生成和使用方法,主要知识点如下:
1)编辑用户定义UI块,生成udx文件
2)UDB的调用方法
3)UDB的后台开发,用户定义方法的开发及调用方法