怡宁塑胶模具设计

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

VS2015 NX12

 

HPP

#include <iostream>
#include <algorithm>
#include <atlstr.h>
#include <sstream>
#include <fstream>
#include <string.h>
#include <sstream>
#include <Windows.h>
#include <iomanip>

#ifdef CreateDialog
#undef CreateDialog
#endif

#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Group.hxx>
#include <NXOpen/BlockStyler_Enumeration.hxx>
#include <NXOpen/BlockStyler_SelectObject.hxx>
#include <NXOpen/BlockStyler_Button.hxx>
#include <NXOpen/BlockStyler_StringBlock.hxx>
#include <NXOpen/BlockStyler_Toggle.hxx>

//用户代码
#include <uf_part.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_drf.h>
#include <NXOpen/ListingWindow.hxx> //打印信息
#include <NXOpen/NXException.hxx> //异常处理
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Annotations_Dimension.hxx>
#include <NXOpen/Annotations_DimensionCollection.hxx>
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Drawings_DraftingView.hxx>
#include <NXOpen/Drawings_DraftingViewCollection.hxx>

 

//用户代码
char msg[256];
Part *workPart;

std::vector<NXOpen::TaggedObject*> SelectObjDIM;

NXString doubleToNXString(double value);//double转换NXString
NXString intToNXString(int value);//int转换NXString

int EnumTypeValue;
int Enumtoggle01Value;
int Enumtoggle02Value;
void GetUI(); //获取UI
void SetUIshow(); //设置UI显示
void SetDimensionTolerance(int DIMmpi,int ToleranceCount, double DIMUpperTolerance, double DIMLowerTolerance); //设置公差(类型,小数位数,上公差,下公差)

 

CPP

 

void YN_Tolerance::initialize_cb()
{
try
{
group01 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group01"));
enumType = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("enumType"));
selection0 = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selection0"));
group02 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group02"));
button01 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button01"));
button02 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button02"));
button03 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button03"));
button04 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button04"));
button05 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button05"));
button06 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button06"));
button07 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button07"));
button08 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button08"));
button09 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button09"));
button10 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button10"));
button11 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button11"));
group03 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group03"));
button001 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button001"));
button002 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button002"));
button003 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button003"));
button004 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button004"));
button005 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button005"));
button006 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button006"));
button007 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button007"));
button008 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button008"));
button009 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button009"));
button010 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button010"));
button011 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button011"));
button012 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button012"));
group04 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group04"));
string01 = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("string01"));
toggle01 = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("toggle01"));
string02 = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("string02"));
toggle02 = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("toggle02"));
//------------------------------------------------------------------------------
//Registration of StringBlock specific callbacks
//------------------------------------------------------------------------------
//string01->SetKeystrokeCallback(make_callback(this, &YN_Tolerance::KeystrokeCallback));

//------------------------------------------------------------------------------
//用户代码
workPart = theSession->Parts()->Work();
theSession->ListingWindow()->Open();

//设置对象过虑(注释处改动即可使用)
Selection::SelectionAction actionOBJ = Selection::SelectionActionClearAndEnableSpecific;
std::vector<Selection::MaskTriple> maskArrayOBJ(1);
maskArrayOBJ[0] = Selection::MaskTriple(UF_dimension_type, 0/*此处控制选择的类型,右键点转到定义查开类型*/, 0 /*此处控制选择的类型,在头文件uf_ui_types.可以查到*/); // 尺寸
selection0/*此处blockID需按实际改*/->GetProperties()->SetSelectionFilter("SelectionFilter", actionOBJ, maskArrayOBJ);

SetUIshow(); //设置UI显示


}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

void YN_Tolerance::dialogShown_cb()
{
try
{
//---- Enter your callback code here -----

SetUIshow(); //设置UI显示

}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int YN_Tolerance::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if(block == enumType)
{
//---------Enter your code here-----------
SetUIshow(); //设置UI显示
}
else if(block == selection0)
{
//---------Enter your code here-----------
SelectObjDIM = selection0->GetSelectedObjects(); //获取选择对象
if (EnumTypeValue == 1)
{
SetDimensionTolerance(8,2, 0.01, 0.01); //设置基本(尺寸加框)
}
else if (EnumTypeValue == 2)
{
SetDimensionTolerance(9,2, 0.01, 0.01); //设置参考(尺寸加括号)
}
else if (EnumTypeValue == 6)
{
//获取上公差
PropertyList *string01Props = string01->GetProperties();
NXString theUpperTolerance = string01Props->GetString("Value");
delete string01Props;
//获取下公差
PropertyList *string02Props = string02->GetProperties();
NXString theLowerTolerance = string02Props->GetString("Value");
delete string02Props;
string02Props = NULL;
if (Enumtoggle01Value == 0 && Enumtoggle02Value == 0)
{
SetDimensionTolerance(5, 3, atof(theUpperTolerance.GetLocaleText()), -atof(theLowerTolerance.GetLocaleText())); //设置双向两行公差(自定义)
}
else if (Enumtoggle01Value == 1 && Enumtoggle02Value == 0)
{
SetDimensionTolerance(5, 3, -atof(theUpperTolerance.GetLocaleText()), -atof(theLowerTolerance.GetLocaleText())); //设置双向两行公差(自定义)
}
if (Enumtoggle01Value == 0 && Enumtoggle02Value == 1)
{
SetDimensionTolerance(5, 3, atof(theUpperTolerance.GetLocaleText()), atof(theLowerTolerance.GetLocaleText())); //设置双向两行公差(自定义)
}
else if (Enumtoggle01Value == 1 && Enumtoggle02Value == 1)
{
SetDimensionTolerance(5, 3, -atof(theUpperTolerance.GetLocaleText()), atof(theLowerTolerance.GetLocaleText())); //设置双向两行公差(自定义)
}

}
}
else if(block == button01)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 3,0.01,0.005); //设置公差
}
else if(block == button02)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 3,-0.005, -0.01); //设置公差
}
else if(block == button03)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 3,0.005, 0); //设置公差
}
else if(block == button04)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 3,0, -0.005); //设置公差
}
else if(block == button05)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 2,0.03,0.01); //设置公差
}
else if(block == button06)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 2,-0.01, -0.03); //设置公差
}
else if(block == button07)
{
//---------Enter your code here-----------
SetDimensionTolerance(5, 2,0.02, 0.01); //设置公差
}
else if(block == button08)
{
//---------Enter your code here-----------
SetDimensionTolerance(5,2, -0.01, -0.02); //设置公差
}
else if(block == button09)
{
//---------Enter your code here-----------
SetDimensionTolerance(5,2, 0.10, 0.09); //设置公差
}
else if(block == button10)
{
//---------Enter your code here-----------
SetDimensionTolerance(5,2, -0.09, -0.10); //设置公差
}
else if(block == button11)
{
//---------Enter your code here-----------
SetDimensionTolerance(1, 2, 0.01, 0.01); //设置公差
}
else if(block == button001)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6,2, 0.01, 0.01); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7,2, 0.01, 0.01); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4,2, 0.01, 0.01); //设置双向一行公差
}
}
else if(block == button002)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.02, 0.02); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.02, 0.02); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.02, 0.02); //设置双向一行公差
}
}
else if(block == button003)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.03, 0.03); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.03, 0.03); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.03, 0.03); //设置双向一行公差
}
}
else if(block == button004)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.04, 0.04); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.04, 0.04); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.04, 0.04); //设置双向一行公差
}
}
else if(block == button005)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.05, 0.05); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.05, 0.05); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.05, 0.05); //设置双向一行公差
}
}
else if(block == button006)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.10, 0.10); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.10, 0.10); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.10, 0.10); //设置双向一行公差
}
}
else if(block == button007)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.15, 0.15); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.15, 0.15); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.15, 0.15); //设置双向一行公差
}
}
else if(block == button008)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.20, 0.20); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.20, 0.20); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.20, 0.20); //设置双向一行公差
}
}
else if(block == button009)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.30, 0.30); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.30, 0.30); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.30, 0.30); //设置双向一行公差
}
}
else if(block == button010)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 0.50, 0.50); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 0.50, 0.50); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 0.50, 0.50); //设置双向一行公差
}
}
else if(block == button011)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 1.00, 1.00); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 1.00, 1.00); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 1.00, 1.00); //设置双向一行公差
}
}
else if(block == button012)
{
//---------Enter your code here-----------
if (EnumTypeValue == 3)
{
SetDimensionTolerance(6, 2, 1.50, 1.50); //设置单向正公差
}
else if (EnumTypeValue == 4)
{
SetDimensionTolerance(7, 2, 1.50, 1.50); //设置单向负公差
}
else if (EnumTypeValue == 5)
{
SetDimensionTolerance(4, 2, 1.50, 1.50); //设置双向一行公差
}
}
else if(block == string01)
{
//---------Enter your code here-----------
}
else if(block == toggle01)
{
//---------Enter your code here-----------
Enumtoggle01Value = toggle01->Value();

}
else if(block == string02)
{
//---------Enter your code here-----------
}
else if(block == toggle02)
{
//---------Enter your code here-----------
Enumtoggle02Value = toggle01->Value();
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//用户代码
NXString YN_Tolerance::doubleToNXString(double value)//double转换NXString
{
stringstream tmpValue1;
tmpValue1 << setprecision(16) << value;
return NXString(tmpValue1.str());
}

NXString YN_Tolerance::intToNXString(int value)//int转换NXString
{
stringstream tmpValue2;
tmpValue2 << setprecision(16) << value;
return NXString(tmpValue2.str());
}

void YN_Tolerance::GetUI() //获取UI
{
try
{
//用户代码
EnumTypeValue = enumType->GetProperties()->GetEnum("Value");
Enumtoggle01Value = toggle01->Value();
Enumtoggle02Value = toggle01->Value();


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("获取UI", NXOpen::NXMessageBox::DialogTypeInformation, ex.what());
}
}
void YN_Tolerance::SetUIshow() //设置UI显示
{
try
{

GetUI(); //获取UI

if (EnumTypeValue == 0)
{
group01->SetShow(true);
group02->SetShow(true);
group03->SetShow(false);
group04->SetShow(false);
}
else if (EnumTypeValue == 1)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(false);
group04->SetShow(false);
}
else if (EnumTypeValue == 2)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(false);
group04->SetShow(false);
}
else if (EnumTypeValue == 3)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(true);
group04->SetShow(false);
}
else if (EnumTypeValue == 4)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(true);
group04->SetShow(false);
}
else if (EnumTypeValue == 5)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(true);
group04->SetShow(false);
}
else if (EnumTypeValue == 6)
{
group01->SetShow(true);
group02->SetShow(false);
group03->SetShow(false);
group04->SetShow(true);
}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("设置UI显示", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

void YN_Tolerance::SetDimensionTolerance(int DIMmpi, int ToleranceCount, double DIMUpperTolerance, double DIMLowerTolerance) //设置公差
{
try
{
UF_initialize();
if (SelectObjDIM.size() == 0)
{
theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "请先选择尺寸");
return;
}

//设置标数时的默认公差
//int mpi[100];//mpi[6]=4为正负公差
//double mpr[70];
//char * radius_val;//半径符号
//char * diameter_val;//直径符号
//UF_DRF_set_preferences(mpi, mpr,radius_val,diameter_val);
//设置某个尺寸的公差

int mpi[100];//mpi[6]=4为正负公差
double mpr[70];//mpr[24]为上公差值 mpr[25]为下公差值
char radius_val[27];//半径符号
char diameter_val[27];//直径符号
for (int i = 0; i < SelectObjDIM.size(); i++)
{
NXOpen::Annotations::Dimension* Dimensions1 = dynamic_cast<NXOpen::Annotations::Dimension*>(SelectObjDIM[i]) ;
//theSession->ListingWindow()->WriteLine(intToNXString(Dimensions1->Tag()));
UF_DRF_ask_object_preferences(Dimensions1->Tag(), mpi, mpr, radius_val, diameter_val);
if (mpi[6] != DIMmpi || mpr[24] != DIMUpperTolerance || mpr[25] != DIMLowerTolerance )
{
mpi[6] = DIMmpi;//公差类型
mpi[4] = ToleranceCount;//公差小数点
mpr[24] = DIMUpperTolerance;//上公差
mpr[25] = DIMLowerTolerance;//下公差
UF_DRF_set_object_preferences(Dimensions1->Tag(), mpi, mpr, radius_val, diameter_val);
}

}

//清空选择控件
SelectObjDIM.clear();
selection0->SetSelectedObjects(SelectObjDIM);

UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Tolerance::theUI->NXMessageBox()->Show("设置公差", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

 

 

 

posted on 2021-06-22 18:14  怡宁塑胶模具设计  阅读(743)  评论(0编辑  收藏  举报