NXOPEN 选择材料库对实体进行缩放比例

VS2015 NX12

 

//关键代码

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

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

void YN_Shrinkage::SetUI() //设置UI
{
try
{
enumTypeValue= enumType->GetProperties()->GetEnum("Value");
if (enumTypeValue == 0)
{
expressionShrinkage->SetShow(true);
expressionShrinkageX->SetShow(false);
expressionShrinkageY->SetShow(false);
expressionShrinkageZ->SetShow(false);
}
else if (enumTypeValue == 1)
{
expressionShrinkage->SetShow(false);
expressionShrinkageX->SetShow(true);
expressionShrinkageY->SetShow(true);
expressionShrinkageZ->SetShow(true);
}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("设置UI", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::RedMaterialLibrary() //读材料库
{
try
{

MaterialAndShrinkage1.clear();
Material1.clear();
Shrinkage1.clear();

UF_initialize();

//获取模板文档路径
char *translationDateInfofile;
UF_translate_variable("UGII_SITE_DIR", &translationDateInfofile);
string MaterialDensityDIR1 = translationDateInfofile;
string MaterialDensityDIR2 = "\\data\\MaterialShrinkage.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 MaterialShrinkage1;
while (getline(infile, MaterialShrinkage1))
{
if (MaterialShrinkage1.size())
{
MaterialAndShrinkage1.push_back(MaterialShrinkage1);
Material1.push_back(MaterialShrinkage1.substr(0, MaterialShrinkage1.find("-")));
Shrinkage1.push_back(MaterialShrinkage1.substr(MaterialShrinkage1.find("-收缩率:") + 1, 19));
}
}
infile.close(); //关闭文件输入流


}

stringMaterial->SetValue(Material1[1]); //设置stringMaterial默认值
stringMaterial->SetListItems(Material1); //塞字符到stringMaterial

UF_terminate();

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("读材料库", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::SetShrinkage() //设置收缩率
{
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()))

{
string ShrinkageA = Shrinkage1[i].GetLocaleText();
//theSession->ListingWindow()->WriteLine(ShrinkageA);
string ShrinkageA1= ShrinkageA.substr(8, ShrinkageA.find("~")-8);
//theSession->ListingWindow()->WriteLine("最小:"+ShrinkageA1);
string ShrinkageA2= ShrinkageA.substr(ShrinkageA.find("~") + 1, ShrinkageA.find(" "));
//theSession->ListingWindow()->WriteLine("最大:"+ShrinkageA2);
expressionShrinkage->SetValue((atof(ShrinkageA1.c_str())+ atof(ShrinkageA2.c_str()))/2);//设置收缩率
expressionShrinkageX->SetValue((atof(ShrinkageA1.c_str()) + atof(ShrinkageA2.c_str())) / 2);//设置收缩率
expressionShrinkageY->SetValue((atof(ShrinkageA1.c_str()) + atof(ShrinkageA2.c_str())) / 2);//设置收缩率
expressionShrinkageZ->SetValue((atof(ShrinkageA1.c_str()) + atof(ShrinkageA2.c_str())) / 2);//设置收缩率
}
}
UF_terminate(); //入口终止代码

displayMaterialRemak(); //显示材料信息

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("设置收缩率", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::displayMaterialRemak() //显示材料信息
{
try
{
MaterialRemak.clear();
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()))

{
string MaterialNote = MaterialAndShrinkage1[i].GetLocaleText();
//theSession->ListingWindow()->WriteLine(MaterialNote);

string MaterialName = MaterialNote.substr(0, MaterialNote.find("-"));
MaterialRemak.push_back(MaterialName);
//theSession->ListingWindow()->WriteLine(MaterialName);
string MaterialScale = MaterialNote.substr(MaterialNote.find("-收缩率:") + 1, MaterialNote.find("-熔料温度:")- MaterialNote.find("-收缩率:")-1);
MaterialRemak.push_back(MaterialScale);
//theSession->ListingWindow()->WriteLine(MaterialScale);
string MaterialTemperature = MaterialNote.substr(MaterialNote.find("-熔料温度:") + 1, MaterialNote.find("-模具温度:") - MaterialNote.find("-熔料温度:") - 1);
MaterialRemak.push_back(MaterialTemperature);
//theSession->ListingWindow()->WriteLine(MaterialTemperature);
string ToolingTemperature = MaterialNote.substr(MaterialNote.find("-模具温度:") + 1, MaterialNote.find("-注射压力:") - MaterialNote.find("-模具温度:") - 1);
MaterialRemak.push_back(ToolingTemperature);
//theSession->ListingWindow()->WriteLine(ToolingTemperature);
string MaterialPressure = MaterialNote.substr(MaterialNote.find("-注射压力:") + 1, MaterialNote.find("-注射速度:") - MaterialNote.find("-注射压力:") - 1);
MaterialRemak.push_back(MaterialPressure);
//theSession->ListingWindow()->WriteLine(MaterialPressure);
string MaterialSpeed = MaterialNote.substr(MaterialNote.find("-注射速度:") + 1, MaterialNote.find(" ") - MaterialNote.find("-注射速度:") - 1);
MaterialRemak.push_back(MaterialSpeed);
//theSession->ListingWindow()->WriteLine(MaterialSpeed);
}
}
//multiline_string0->GetProperties()->SetLogical("Enable", false);//设置控件不可更改
multiline_string0->SetValue(MaterialRemak);

UF_terminate(); //入口终止代码

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("显示材料信息", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::EditMaterialLibrary() //编辑材料库
{
try
{
UF_initialize();

//获取模板文档路径
char *translationDateInfofile;
UF_translate_variable("UGII_SITE_DIR", &translationDateInfofile);
string MaterialDensityDIR1 = translationDateInfofile;
string MaterialDensityDIR2 = "\\data\\MaterialShrinkage.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_Shrinkage::theUI->NXMessageBox()->Show("编辑材料库", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::ShrinkageBody() //缩放体
{
try
{
if (selectObjs.size() == 0)
{
return;
}

//获取比例
if (enumTypeValue == 0)
{
ShrinkageX = expressionShrinkage->Value();
ShrinkageY = expressionShrinkage->Value();
ShrinkageZ = expressionShrinkage->Value();
}
else if (enumTypeValue == 1)
{
ShrinkageX = expressionShrinkageX->Value();
ShrinkageY = expressionShrinkageY->Value();
ShrinkageZ = expressionShrinkageZ->Value();
}

if (isShrinkageCreat == true)
{
EditShrinkageBody(); //编辑缩放体
return;
}

//缩放体
NXOpen::Features::Feature *nullNXOpen_Features_Feature(NULL);
NXOpen::Features::ScaleBuilder *scaleBuilder1;
scaleBuilder1 = workPart->Features()->CreateScaleBuilder(nullNXOpen_Features_Feature);

scaleBuilder1->SetType(NXOpen::Features::ScaleBuilder::TypesGeneral);//三方向缩放体
//scaleBuilder1->UniformFactor()->SetRightHandSide("1.005");
//scaleBuilder1->ScaleAlongAxis()->SetRightHandSide("1");
//scaleBuilder1->ScaleOtherDirection()->SetRightHandSide("1");
scaleBuilder1->ScaleXdirection()->SetRightHandSide(doubleToNXString(ShrinkageX));//X方向比例
scaleBuilder1->ScaleYdirection()->SetRightHandSide(doubleToNXString(ShrinkageY));//Y方向比例
scaleBuilder1->ScaleZdirection()->SetRightHandSide(doubleToNXString(ShrinkageZ));//Y方向比例
NXOpen::ScCollector *scCollector1;
scCollector1 = workPart->ScCollectors()->CreateCollector();

std::vector<NXOpen::Body *> bodies1(selectObjs.size());
for (int i = 0; i < selectObjs.size(); i++)
{
Body *body1 = dynamic_cast<Body *>(selectObjs[i]);
bodies1[i] = body1;
}
//theSession->ListingWindow()->WriteLine("测试:"+ intToNXString(bodies1.size()));
NXOpen::BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1, true);

std::vector<NXOpen::SelectionIntentRule *> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector1->ReplaceRules(rules1, false);
scaleBuilder1->SetBodyCollector(scCollector1);

NXOpen::Point *point1 = workPart->Points()->CreatePoint(ShrinkagePoint);

scaleBuilder1->SetPoint(point1);
NXOpen::Vector3d vector1(0.0, 0.0, 1.0);
NXOpen::Direction *direction1;
direction1 = workPart->Directions()->CreateDirection(ShrinkagePoint, vector1, NXOpen::SmartObject::UpdateOptionWithinModeling);
scaleBuilder1->SetVector(direction1);
NXOpen::Vector3d xDirection1(1.0, 0.0, 0.0);
NXOpen::Vector3d yDirection1(0.0, 1.0, 0.0);
NXOpen::Xform *xform1;
xform1 = workPart->Xforms()->CreateXform(ShrinkagePoint, xDirection1, yDirection1, NXOpen::SmartObject::UpdateOptionWithinModeling, 1.0);

NXOpen::CartesianCoordinateSystem *cartesianCoordinateSystem1;
cartesianCoordinateSystem1 = workPart->CoordinateSystems()->CreateCoordinateSystem(xform1, NXOpen::SmartObject::UpdateOptionWithinModeling);

scaleBuilder1->SetCsys(cartesianCoordinateSystem1);

//NXOpen::NXObject *nXObject1;
//nXObject1 = scaleBuilder1->Commit();
feature1 = scaleBuilder1->CommitFeature();

scaleBuilder1->Destroy();

isShrinkageCreat = true;


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("缩放体", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

void YN_Shrinkage::EditShrinkageBody() //编辑缩放体
{
try
{
//theSession->ListingWindow()->WriteLine("编辑缩放体");
//缩放体
NXOpen::Features::Scale *EditShrinkageBody1 = dynamic_cast<NXOpen::Features::Scale *>(feature1);
NXOpen::Features::ScaleBuilder *scaleBuilder1;
scaleBuilder1 = workPart->Features()->CreateScaleBuilder(EditShrinkageBody1);

scaleBuilder1->SetType(NXOpen::Features::ScaleBuilder::TypesGeneral);//三方向缩放体
scaleBuilder1->ScaleXdirection()->SetRightHandSide(doubleToNXString(ShrinkageX));//X方向比例
scaleBuilder1->ScaleYdirection()->SetRightHandSide(doubleToNXString(ShrinkageY));//Y方向比例
scaleBuilder1->ScaleZdirection()->SetRightHandSide(doubleToNXString(ShrinkageZ));//Y方向比例
NXOpen::ScCollector *scCollector1;
scCollector1 = workPart->ScCollectors()->CreateCollector();

std::vector<NXOpen::Body *> bodies1(selectObjs.size());
for (int i = 0; i < selectObjs.size(); i++)
{
Body *body1 = dynamic_cast<Body *>(selectObjs[i]);
bodies1[i] = body1;
}

NXOpen::BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1, true);

std::vector<NXOpen::SelectionIntentRule *> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector1->ReplaceRules(rules1, false);
scaleBuilder1->SetBodyCollector(scCollector1);

NXOpen::Point *point1 = workPart->Points()->CreatePoint(ShrinkagePoint);

scaleBuilder1->SetPoint(point1);
NXOpen::Vector3d vector1(0.0, 0.0, 1.0);
NXOpen::Direction *direction1;
direction1 = workPart->Directions()->CreateDirection(ShrinkagePoint, vector1, NXOpen::SmartObject::UpdateOptionWithinModeling);
scaleBuilder1->SetVector(direction1);
NXOpen::Vector3d xDirection1(1.0, 0.0, 0.0);
NXOpen::Vector3d yDirection1(0.0, 1.0, 0.0);
NXOpen::Xform *xform1;
xform1 = workPart->Xforms()->CreateXform(ShrinkagePoint, xDirection1, yDirection1, NXOpen::SmartObject::UpdateOptionWithinModeling, 1.0);

NXOpen::CartesianCoordinateSystem *cartesianCoordinateSystem1;
cartesianCoordinateSystem1 = workPart->CoordinateSystems()->CreateCoordinateSystem(xform1, NXOpen::SmartObject::UpdateOptionWithinModeling);

scaleBuilder1->SetCsys(cartesianCoordinateSystem1);

markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "Shrinkagefeature1");

feature1 = scaleBuilder1->CommitFeature();

scaleBuilder1->Destroy();

isShrinkageCreat = true;


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_Shrinkage::theUI->NXMessageBox()->Show("编辑缩放体", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

 

NXOPEN 选择材料库对实体进行缩放比例

 

上一篇:NXOpen绝对座标值转为WCS座标值


下一篇:NXOpen 求点到边的垂线并结合UF求起始点终点交点