列文章目录
文章目录
目录
前言
随着工业智能化的不断发展,UG二次开发的需求越来越多,也吸引了大批的二开从业人员,本人作为一名资深IT从业者(10年+)也毅然加入二次开发大军。
然而,和流行IT行业(互联网、金融、医疗等)相比,工业智能化的门槛显得更高一点,专业的工业软件,相对封闭的开发理念和更小的开发圈子,让刚进入二开的从业者有点举步维艰。边学边整理,希望通过这系列文章的整理能给二开的生态增添一叶绿。
一、UI样式文件分析
1. 样式文件目录
目录位置:UGOPEN\SampleNXOpenApplications\C++\BlockStyler\MatrixOperations\MatrixOperations.dlx
2. 样式文件导入预览
3. 样式文件解析
本实例主要用到的控件如下:枚举、切换开关、指定矢量、双精度表、双精度数字
二、源码文件解析
1. 主程序分析
extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
{
try
{
theMatrixOperations = new MatrixOperations();
// The following method shows the dialog immediately
theMatrixOperations->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
MatrixOperations::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
if(theMatrixOperations != NULL)
{
delete theMatrixOperations;
}
}
MatrixOperations::MatrixOperations()
{
try
{
// Initialize the NX Open C++ API environment
MatrixOperations::theSession = NXOpen::Session::GetSession();
MatrixOperations::theUI = UI::GetUI();
theDialogName = "MatrixOperations.dlx";
theDialog = MatrixOperations::theUI->CreateDialog(theDialogName.c_str());
// Registration of callback functions
theDialog->AddApplyHandler(make_callback(this, &MatrixOperations::apply_cb));
theDialog->AddOkHandler(make_callback(this, &MatrixOperations::ok_cb));
theDialog->AddUpdateHandler(make_callback(this, &MatrixOperations::update_cb));
theDialog->AddInitializeHandler(make_callback(this, &MatrixOperations::initialize_cb));
theDialog->AddFocusNotifyHandler(make_callback(this, &MatrixOperations::focusNotify_cb));
theDialog->AddKeyboardFocusNotifyHandler(make_callback(this, &MatrixOperations::keyboardFocusNotify_cb));
theDialog->AddDialogShownHandler(make_callback(this, &MatrixOperations::dialogShown_cb));
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
MatrixOperations::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
throw;
}
}
构造函数里,我们需要关注dlx文件加载,回调方法里面主要关注:提交方法apply_cb、更新方法update_cb、初始化方法initialize_cb。
2. 处理模块分析
2.1 初始化方法
void MatrixOperations::initialize_cb()
{
try
{
SelectOperationsGroup = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("SelectOperationsGroup"));
SelectOperation = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("SelectOperation"));
Input = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Input"));
Matrix1Group = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Matrix1Group"));
Matrix1 = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Matrix1"));
Matrix2Group = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Matrix2Group"));
Matrix2 = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Matrix2"));
VectorGroup = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("VectorGroup"));
AllowSelection = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("AllowSelection"));
SelectVector = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("SelectVector"));
Vector1 = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Vector1"));
EnterAngle = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("EnterAngle"));
Angle1 = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Angle1"));
Angle = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("Angle"));
ResultGroup = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("ResultGroup"));
ResultMatrix = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("ResultMatrix"));
ResultVector = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("ResultVector"));
ResultDouble = dynamic_cast<NXOpen::BlockStyler::UIBlock* >(theDialog->TopBlock()->FindBlock("ResultDouble"));
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
MatrixOperations::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
2.2 控件更新/点击/激活方法
int MatrixOperations::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
//Getting properties of AllowSelection,SelectVector and Vector1 into propertylists
BlockStyler::PropertyList *AllowSelectionProperties = AllowSelection->GetProperties();
BlockStyler::PropertyList *SelectVectorProperties = SelectVector->GetProperties();
BlockStyler::PropertyList *Vector1Properties = Vector1->GetProperties();
NXOpen::Vector3d SelectedVec ;
std::vector<double> vector1values(3);
int nrows[1],ncols[3];
if(block == AllowSelection)
{
//---------Enter your code here-----------
vector1values = Vector1Properties->GetDoubleMatrix("Values",nrows, ncols);
SelectedVec.X = vector1values[0];
SelectedVec.Y = vector1values[1];
SelectedVec.Z = vector1values[2];
SelectVectorProperties->SetVector("Vector",SelectedVec);
SelectVectorProperties->SetLogical("Show",AllowSelectionProperties->GetLogical("Value"));
}
else if(block == SelectVector)
{
//---------Enter your code here-----------
SelectedVec = SelectVectorProperties->GetVector("Vector");
vector1values[0] = SelectedVec.X;
vector1values[1] = SelectedVec.Y;
vector1values[2] = SelectedVec.Z;
Vector1Properties->SetDoubleMatrix("Values", 1, 3, vector1values);
}
UpdateDialog();
//Deleting Property list after use
delete AllowSelectionProperties;
delete SelectVectorProperties;
delete Vector1Properties;
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
MatrixOperations::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}
这里主要是控件AllowSelection和SelectVector的update方法
1、AllowSelection,勾选了允许选择的时候,显示指定矢量控件,并把Vector1的值赋给SelectVector
vector1values = Vector1Properties->GetDoubleMatrix("Values",nrows, ncols);
SelectedVec.X = vector1values[0];
SelectedVec.Y = vector1values[1];
SelectedVec.Z = vector1values[2];
SelectVectorProperties->SetVector("Vector",SelectedVec);
SelectVectorProperties->SetLogical("Show",AllowSelectionProperties->GetLogical("Value"));2、SelectVector,使用指定矢量工具,获取矢量并返回矢量赋值给Vector1
SelectedVec = SelectVectorProperties->GetVector("Vector");
vector1values[0] = SelectedVec.X;
vector1values[1] = SelectedVec.Y;
vector1values[2] = SelectedVec.Z;
Vector1Properties->SetDoubleMatrix("Values", 1, 3, vector1values);
2.3 矩阵运算
//This method updates the dialog
void MatrixOperations::UpdateDialog()
{
// Getting Properties of Matrix1Group,Matrix2Group,EnterAngle and VectorGroup
BlockStyler::PropertyList *Matrix1GroupProperties = Matrix1Group->GetProperties();
BlockStyler::PropertyList *Matrix2GroupProperties = Matrix2Group->GetProperties();
BlockStyler::PropertyList *EnterAngleProperties = EnterAngle->GetProperties();
BlockStyler::PropertyList *VectorGroupProperties = VectorGroup->GetProperties();
// Getting Properties of Matrix1,Matrix2,Vector1 and Angle1
BlockStyler::PropertyList *Matrix1Properties = Matrix1->GetProperties();
BlockStyler::PropertyList *Matrix2Properties = Matrix2->GetProperties();
BlockStyler::PropertyList *Vector1Properties = Vector1->GetProperties();
BlockStyler::PropertyList *AngleProperties = Angle1->GetProperties();
// Getting Properties of ResultMatrix,ResultVector and ResultDouble
BlockStyler::PropertyList *ResultMatrixProperties = ResultMatrix->GetProperties();
BlockStyler::PropertyList *ResultVectorProperties = ResultVector->GetProperties();
BlockStyler::PropertyList *ResultDoubleProperties = ResultDouble->GetProperties();
int m1nrows[3],m1ncols[3];
int m2nrows[3],m2ncols[3];
int v1nrows[1],v2ncols[3];
std::vector<double > matrix1values = Matrix1Properties->GetDoubleMatrix("Values",m1nrows,m1ncols);
std::vector<double > matrix2values = Matrix2Properties->GetDoubleMatrix("Values", m2nrows, m2ncols);
std::vector<double> vector1values = Vector1Properties->GetDoubleMatrix("Values", v1nrows, v2ncols);
double anglevalue = AngleProperties->GetDouble("Value");
std::vector<double > matrixResultValues;
double mat1values[9], mat2values[9],matResultValues[9],vec1values[3],vecproductvalues[3];
int i = 0;
vector<double>::iterator it;
for ( it = matrix1values.begin(), i = 0 ; it < matrix1values.end(); it++ )
{
mat1values[i++] = *it;
}
for ( it = matrix2values.begin(), i = 0 ; it < matrix2values.end(); it++ )
{
mat2values[i++] = *it;
}
for ( it = vector1values.begin(), i = 0 ; it < vector1values.end(); it++ )
{
vec1values[i++] = *it;
}
double ResultValues[] = {0,0,0};
double determinant[1];
BlockStyler::PropertyList *SelectOperationProperties = SelectOperation->GetProperties();
NXString matrixOperation = SelectOperationProperties->GetEnumAsString("Value");
//If List box selection is Multiplication
if(!(strcmp(matrixOperation.GetText(),"Multiplication")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", true);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", true);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_multiply(mat1values ,mat2values,matResultValues);
matrixResultValues.insert(matrixResultValues.begin(),matResultValues,matResultValues+9);
ResultMatrixProperties->SetDoubleMatrix("Values", 3, 3, matrixResultValues);
}
//If List box selection is Addition
else if(!(strcmp(matrixOperation.GetText(),"Addition")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", true);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", true);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", false);
double *additionMatrix = MatrixAddition3x3(mat1values,mat2values);
for(i=0;i<9;i++)
{
matResultValues[i] = additionMatrix[i];
}
matrixResultValues.insert(matrixResultValues.begin(),matResultValues,matResultValues+9);
ResultMatrixProperties->SetDoubleMatrix("Values", 3, 3, matrixResultValues);
}
//If List box selection is Determinant
else if(!(strcmp(matrixOperation.GetText(),"Determinant")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", false);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", true);
UF_MTX3_determinant(mat1values ,determinant);
ResultDoubleProperties->SetDouble("Value", determinant[0]);
}
//If List box selection is Transpose
else if(!(strcmp(matrixOperation.GetText(),"Transpose")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", true);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_transpose(mat1values ,matResultValues);
matrixResultValues.insert(matrixResultValues.begin(),matResultValues,matResultValues+9);
ResultMatrixProperties->SetDoubleMatrix("Values", 3, 3, matrixResultValues);
}
//If List box selection is OrthoNormalize
else if(!(strcmp(matrixOperation.GetText(),"OrthoNormalize")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", true);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_ortho_normalize(mat1values);
matrixResultValues.insert(matrixResultValues.begin(),mat1values,mat1values+9);
ResultMatrixProperties->SetDoubleMatrix("Values", 3, 3, matrixResultValues);
}
//If List box selection is RotateAboutAxis
else if(!(strcmp(matrixOperation.GetText(),"RotateAboutAxis")))
{
Matrix1GroupProperties->SetLogical("Show", false);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", true);
VectorGroupProperties->SetLogical("Show", true);
ResultMatrixProperties->SetLogical("Show", true);
ResultVectorProperties->SetLogical("Show", false);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_rotate_about_axis(vec1values, anglevalue, matResultValues);
matrixResultValues.insert(matrixResultValues.begin(),matResultValues,matResultValues+9);
ResultMatrixProperties->SetDoubleMatrix("Values", 3, 3, matrixResultValues);
}
//If List box selection is VectorMultiply
else if(!(strcmp(matrixOperation.GetText(),"VectorMultiply")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", true);
ResultMatrixProperties->SetLogical("Show", false);
ResultVectorProperties->SetLogical("Show", true);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_vec_multiply(vec1values, mat1values, vecproductvalues);
matrixResultValues.insert(matrixResultValues.begin(),vecproductvalues,vecproductvalues+3);
ResultVectorProperties->SetDoubleMatrix("Values", 1, 3, matrixResultValues);
}
//If List box selection is X-direction Vector
else if(!(strcmp(matrixOperation.GetText(),"X-direction Vector")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", false);
ResultVectorProperties->SetLogical("Show", true);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_x_vec(mat1values, ResultValues);
matrixResultValues.insert(matrixResultValues.begin(),ResultValues,ResultValues+3);
ResultVectorProperties->SetDoubleMatrix("Values", 1, 3, matrixResultValues);
}
//If List box selection is Y-direction Vector
else if(!(strcmp(matrixOperation.GetText(),"Y-direction Vector")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", false);
ResultVectorProperties->SetLogical("Show", true);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_y_vec(mat1values, ResultValues);
matrixResultValues.insert(matrixResultValues.begin(),ResultValues,ResultValues+3);
ResultVectorProperties->SetDoubleMatrix("Values", 1, 3, matrixResultValues);
}
//If List box selection is Z-direction Vector
else if(!(strcmp(matrixOperation.GetText(),"Z-direction Vector")))
{
Matrix1GroupProperties->SetLogical("Show", true);
Matrix2GroupProperties->SetLogical("Show", false);
EnterAngleProperties->SetLogical("Show", false);
VectorGroupProperties->SetLogical("Show", false);
ResultMatrixProperties->SetLogical("Show", false);
ResultVectorProperties->SetLogical("Show", true);
ResultDoubleProperties->SetLogical("Show", false);
UF_MTX3_z_vec(mat1values, ResultValues);
matrixResultValues.insert(matrixResultValues.begin(),ResultValues,ResultValues+3);
ResultVectorProperties->SetDoubleMatrix("Values", 1, 3, matrixResultValues);
}
//Deleting all the proerties that were used
delete SelectOperationProperties;
delete Matrix1GroupProperties;
delete Matrix2GroupProperties;
delete EnterAngleProperties;
delete VectorGroupProperties;
delete Matrix1Properties;
delete Matrix2Properties;
delete Vector1Properties;
delete AngleProperties;
delete ResultMatrixProperties;
delete ResultVectorProperties;
delete ResultDoubleProperties;
}
这里涉及运算如下:Multiplication、Addition、Determinant、Transpose、OrthoNormalize、RotateAboutAxis、VectorMultiply、X-direction Vector、Y-direction Vector、Z-direction Vector。
运算都有内部函数,对应关系如下:
Multiplication:UF_MTX3_multiply
Addition:MatrixAddition3x3(加法比较简单,自己写)
Determinant:UF_MTX3_determinant
Transpose:UF_MTX3_transpose
OrthoNormalize:UF_MTX3_ortho_normalize
RotateAboutAxis:UF_MTX3_rotate_about_axis
VectorMultiply:UF_MTX3_vec_multiply
X-direction Vector:UF_MTX3_x_vec
Y-direction Vector:UF_MTX3_y_vec
Z-direction Vector:UF_MTX3_z_vec
3. 运行结果截图
总结
官方实例 MatrixOperations
1、前端BlockUI使用控件:枚举、切换开关、指定矢量、双精度表、双精度数字,主要知识点如下:
1)双精度表的初始化和赋值
2)指定矢量的使用,初始化和获取选择的矢量方向
2、后台通过前端获取的信息,实现矩阵运算