VS2019 NX1980
//关键代码
//用户代码
NXString YN_MeasurementBody::doubleToNXString(double value)//double转换NXString
{
stringstream tmpValue1;
tmpValue1 << setprecision(16) << value;
return NXString(tmpValue1.str());
}
NXString YN_MeasurementBody::intToNXString(int value)//int转换NXString
{
stringstream tmpValue2;
tmpValue2 << setprecision(16) << value;
return NXString(tmpValue2.str());
}
void YN_MeasurementBody::MeasurementSelectBody() //测量体
{
try
{
if (selectObjs.size() == 0)
{
return;
}
UF_initialize(); //初始化入口
std::vector <double> AllArea ;
std::vector <double> AllVolume;
std::vector <double> AllWeight;
AllArea.clear();
AllVolume.clear();
AllWeight.clear();
for (int i = 0; i < selectObjs.size(); i++)
{
Body *body1 = dynamic_cast<Body *>(selectObjs[i]);
BodyFindPart(body1); //体找部件
tag_t BodysPrototype = NULL_TAG;
if (isObjsComponent == true)
{
BodysPrototype = UF_ASSEM_ask_prototype_of_occ(selectObjs[i]->Tag());//如果是装配选的部件面,找面的原型
//theSession->ListingWindow()->WriteLine(intToNXString(BodysPrototype));
}
else if (isObjsComponent == false)
{
Body *body1 = dynamic_cast<Body *>(selectObjs[i]);
BodysPrototype = body1->Tag();
}
//获得体的表面积,体积(单位:克和厘米)
double density1 = expressionDensity->Value();
//theSession->ListingWindow()->WriteLine(doubleToNXString(density1));
double acc_val[11] = { 0.01,0,0,0,0,0,0,0,0,0,0 };
double mass_props[47];
double statistics[13];
UF_MODL_ask_mass_props_3d(&BodysPrototype, 1, 1, 3, density1, 1, acc_val, mass_props, statistics);
double weight1 = mass_props[1] * density1 * 0.001; //求重量 并由克转为千克
double area1 = mass_props[0] * 100; //面积转换 由平方厘米转为平方毫米
double volume1 = mass_props[1] * 1000; //体积转换 由立方厘米转为立方毫米
//theSession->ListingWindow()->WriteLine("表面积:"+doubleToNXString(area1));
//theSession->ListingWindow()->WriteLine("体积:"+ doubleToNXString(volume1));
//theSession->ListingWindow()->WriteLine("重量:" + doubleToNXString(weight1));
AllArea.push_back(area1);
AllVolume.push_back(volume1);
AllWeight.push_back(weight1);
}
double TotalArea = 0.00;
double TotalVolume = 0.00;
double TotalWeight = 0.00;
//计算总数
for (int i = 0; i<AllVolume.size(); i++)
{
TotalArea= TotalArea+ AllArea[i];
TotalVolume = TotalVolume + AllVolume[i];
TotalWeight = TotalWeight + AllWeight[i];
}
//转换为2位小数
char TotalAreaChar[256];
char TotalVolumeChar[256];
char TotalWeightChar[256];
sprintf(TotalAreaChar,"%.2f",TotalArea);
sprintf(TotalVolumeChar, "%.2f", TotalVolume);
sprintf(TotalWeightChar, "%.5f", TotalWeight);
//输入对应到UI
expressionSurfaceArea->SetValue(atof(TotalAreaChar));
expressionVolume->SetValue(atof(TotalVolumeChar));
expressionWeight->SetValue(atof(TotalWeightChar));
UF_terminate(); //入口终止代码
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("测量体", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}
void YN_MeasurementBody::CycleComponent() //遍历装配
{
try
{
//---- Enter your callback code here -----
//遍历装配
basedisplaypart = theSession->Parts()->BaseDisplay();//获取顶层显示部件
Assemblies::ComponentAssembly *componentassembly = basedisplaypart->ComponentAssembly();//获取装配底层(BaseWork为当前工作装配,basedisplaypart为当前显示的装配 两种结果是不一样的)
rootcomponent = componentassembly->RootComponent();//获取rootcomponent
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("遍历装配", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}
void YN_MeasurementBody::BodyFindPart(Body *body1) //体找部件
{
try
{
UF_initialize();
isObjsComponent = false;
objbodysTAG.clear();
objPartTAG.clear();
componentPartTAG.clear();
CycleComponent();//遍历装配
//判断是否为部件
objbodysTAG.push_back(body1->Tag());
int object_type, object_subtype;
tag_t parent = NULL;
UF_OBJ_ask_type_and_subtype(body1->Tag(), &object_type, &object_subtype); //获得对象类型
NXOpen::Assemblies::Component *component1;
if (object_type == UF_solid_type)
{
if (UF_ASSEM_is_occurrence(body1->Tag())) { UF_ASSEM_ask_parent_component(body1->Tag(), &parent); }
}
else if (object_type == UF_component_type) parent = body1->Tag();
{
component1 = (dynamic_cast<NXOpen::Assemblies::Component *>(NXObjectManager::Get(parent)));//组件
}
if (rootcomponent != NULL && component1 != NULL)
{
//获取部件名
Part *part1(dynamic_cast<Part *>(component1->Prototype())); //component强制转换为part
char part1_fspec[MAX_FSPEC_SIZE + 1];
UF_PART_ask_part_name(part1->Tag(), part1_fspec);
//theSession->ListingWindow()->WriteLine(part1_fspec);
//获取顶层部件名
char basepart1_fspec1[MAX_FSPEC_SIZE + 1];
UF_PART_ask_part_name(basePartvectorTag[0], basepart1_fspec1);
//theSession->ListingWindow()->WriteLine(basepart1_fspec1);
tag_t part1TAG = NULL_TAG;
UF_PART_load_status_t part1error_status;
if (!strcmp(basepart1_fspec1, part1_fspec) == 0)
{
isObjsComponent = true;
objPartTAG.push_back(part1->Tag());
componentPartTAG.push_back(component1->Tag());
}
}
if (objbodysTAG.size() == objPartTAG.size())
{
isObjsComponent = true;
}
UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("选体找部件", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
void YN_MeasurementBody::RedMaterialLibrary() //读材料库
{
try
{
MaterialAndDensity.clear();
Material1.clear();
Density1.clear();
UF_initialize();
//获取模板文档路径
char *translationDateInfofile;
UF_translate_variable("UGII_SITE_DIR", &translationDateInfofile);
string MaterialDensityDIR1 = translationDateInfofile;
string MaterialDensityDIR2 = "\\data\\MaterialDensity.dat";
string MaterialDensityPath = MaterialDensityDIR1 + MaterialDensityDIR2;
//theSession->ListingWindow()->WriteLine(translationColorManagementPath);
//指定本地数据文件的路径
int MaterialDensityStatus = 0;
//测试文件或目录是否存在
UF_CFI_ask_file_exist(MaterialDensityPath.c_str(), &MaterialDensityStatus);
if (MaterialDensityStatus == 1) //检测文件是否存在,0为存在,1为不存在
{
uc1601("请检查材料库文件是否存在.", 1);
return;
}
else if (MaterialDensityStatus == 0)//检测文件是否存在,0为存在,1为不存在
{
ifstream infile;
infile.open(MaterialDensityPath.c_str());
string MaterialDensity1;
while (getline(infile, MaterialDensity1))
{
if (MaterialDensity1.size())
{
MaterialAndDensity.push_back(MaterialDensity1);
Material1.push_back(MaterialDensity1.substr(0, MaterialDensity1.find("-")));
Density1.push_back(MaterialDensity1.substr(MaterialDensity1.find("-") + 1, MaterialDensity1.find(" ")));
}
}
infile.close(); //关闭文件输入流
}
stringMaterial->SetValue(Material1[1]); //设置stringMaterial默认值
stringMaterial->SetListItems(Material1); //塞字符到stringMaterial
UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("读材料库", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}
void YN_MeasurementBody::SetDensity() //设置密度
{
try
{
UF_initialize(); //初始化入口
//先获取里面的成员
std::vector <NXString> thestring01 = stringMaterial->GetListItems();
NXString str1 = stringMaterial->Value();
for (int i = 0; i < thestring01.size(); i++)
{
if (!strcmp(thestring01[i].GetLocaleText(), str1.GetLocaleText()))
{
expressionDensity->SetValue(atof(Density1[i].GetLocaleText()));//设置密度
}
}
UF_terminate(); //入口终止代码
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("设置密度", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}
void YN_MeasurementBody::EditMaterialLibrary() //编辑材料库
{
try
{
UF_initialize();
//获取模板文档路径
char *translationDateInfofile;
UF_translate_variable("UGII_SITE_DIR", &translationDateInfofile);
string MaterialDensityDIR1 = translationDateInfofile;
string MaterialDensityDIR2 = "\\data\\MaterialDensity.dat";
string MaterialDensityPath = MaterialDensityDIR1 + MaterialDensityDIR2;
//theSession->ListingWindow()->WriteLine(translationColorManagementPath);
//指定本地数据文件的路径
int MaterialDensityStatus = 0;
//测试文件或目录是否存在
UF_CFI_ask_file_exist(MaterialDensityPath.c_str(), &MaterialDensityStatus);
if (MaterialDensityStatus == 1) //检测文件是否存在,0为存在,1为不存在
{
return;
}
else if (MaterialDensityStatus == 0)//检测文件是否存在,0为存在,1为不存在
{
//打开文件
//ShellExecute(NULL, _T("open"), _T( translationColorManagementPath.c_str() ), NULL, NULL, SW_NORMAL);//SW_SHOW SW_SHOWNORMAL
//方法一 项目属性需使用多节字符集
//指定用记事本打开指定的文件
ShellExecute(NULL, "open", "NotePad.exe", MaterialDensityPath.c_str(), NULL, SW_SHOWNORMAL);//SW_SHOW SW_SHOWNORMAL
//方法二
//char filedirectorypath2[256];
//sprintf(filedirectorypath2, "start \"\" \"%s\"", translationColorManagementPath.c_str());
////theSession->ListingWindow()->WriteLine(filedirectorypath2);
//system(filedirectorypath2); //打开路径
}
UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_MeasurementBody::theUI->NXMessageBox()->Show("编辑材料库", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}