NX二次開發測量體的表面積\體積\重量(部件和實體都可以測)


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, "程序錯誤,請檢查代碼");
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM