NX二次开发新建组件工具(单个,批量)

VS2015 NX12

HPP:

#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_Label.hxx>
#include <NXOpen/BlockStyler_StringBlock.hxx>
#include <NXOpen/BlockStyler_Toggle.hxx>

//用户代码
#include <NXOpen/ListingWindow.hxx>
#include <uf.h>
#include <uf_ui.h>
#include <NXOpen/Part.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Assemblies_AssemblyManager.hxx>
#include <NXOpen/Assemblies_CreateNewComponentBuilder.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/SelectDisplayableObjectList.hxx>
#include <NXOpen/View.hxx>
#include <NXOpen/ModelingView.hxx>
#include <NXOpen/ModelingViewCollection.hxx>
#include <NXOpen/NXObjectManager.hxx>

//用户代码
char msg[256];
Part *workPart;
Part *displayPart;
BasePart *basedisplaypart;
int enum0Value;
std::vector<TaggedObject *> selectBodys;//选择控件选择的体

string displaypartMoldNo;//显示部件得到的模号
NXString partfullpath1;//显示部件的全路径
string symboltxt1;
string PartPath1;//显示部件路径
void setUIValueandPath();//设置UI值和获取路径

std::vector<string> NewName;//新建部件的名字
std::vector<string> NewNamePath;//新建部件全路径

void getUIValueandPath();//获取UI值和得到新部件名字和路径

std::vector<string> samepartname1;
std::vector<string> ALLsamenamePart;//所有同名的新建部件
bool ispartnamecreate;
void Cylmemory();//遍历内存

void CreateSinglPart();//创建单个部件
void CreateMultiplePart();//批量创建部件
void CreateNewAssmPart();//新模装配树
void SaveNewPart();//保存新建部件
void setASM();//设置装配树结构

CPP

void CreateNewComponent1::initialize_cb()
{
try
{
group01 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group01"));
enum0 = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("enum0"));
group02 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group02"));
selection0 = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selection0"));
label01 = dynamic_cast<NXOpen::BlockStyler::Label*>(theDialog->TopBlock()->FindBlock("label01"));
label02 = dynamic_cast<NXOpen::BlockStyler::Label*>(theDialog->TopBlock()->FindBlock("label02"));
label03 = dynamic_cast<NXOpen::BlockStyler::Label*>(theDialog->TopBlock()->FindBlock("label03"));
stringMoldNo = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringMoldNo"));
stringName = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringName"));
stringBeginName = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringBeginName"));
toggle0 = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("toggle0"));
//------------------------------------------------------------------------------
//Registration of StringBlock specific callbacks
//------------------------------------------------------------------------------
//stringMoldNo->SetKeystrokeCallback(make_callback(this, &CreateNewComponent1::KeystrokeCallback));

//------------------------------------------------------------------------------
//用户代码

theSession->ListingWindow()->Open();

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

workPart = theSession->Parts()->Work();//获取工作部件
displayPart = theSession->Parts()->Display();//获取显示部件
basedisplaypart = theSession->Parts()->BaseDisplay();//获取顶层显示部件

enum0Value = enum0->GetProperties()->GetEnum("Value");

selectBodys.clear();

ispartnamecreate = false;

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

void CreateNewComponent1::dialogShown_cb()
{
try
{
//---- Enter your callback code here -----
enum0Value = enum0->GetProperties()->GetEnum("Value");
//设置UI值和获取路径
setUIValueandPath();

stringMoldNo->SetValue(displaypartMoldNo);

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

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int CreateNewComponent1::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----
//获取切换开关
int toggle0Value = toggle0->Value();

if (ispartnamecreate == false)
{
if (enum0Value == 0)
{
//创建单个部件
CreateSinglPart();
}
else if (enum0Value == 1)
{
//批量创建部件
CreateMultiplePart();
}
else if (enum0Value == 2)
{
//设置UI值和获取路径
setUIValueandPath();

getUIValueandPath();//获取UI值和得到新部件名字和路径
//新模装配树
CreateNewAssmPart();
//设置装配树结构
setASM();
}


//保存部件
if (toggle0Value == 1)
{
SaveNewPart();//保存新建部件
}
}
else if (ispartnamecreate == true)
{
for (int i = 0; i < ALLsamenamePart.size(); i++)
{
theSession->ListingWindow()->WriteLine(ALLsamenamePart[i] + " 部件已存在请重新输入工件名或起始工件名");
}
return 0;
}


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

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int CreateNewComponent1::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if (block == enum0)
{
//---------Enter your code here-----------
//获取枚举值
enum0Value = enum0->GetProperties()->GetEnum("Value");
//设置UI值和获取路径
setUIValueandPath();

getUIValueandPath();//获取UI值和得到新部件名字和路径

}
else if (block == selection0)
{
//---------Enter your code here-----------
//获取选择控件选择的体
selectBodys = selection0->GetSelectedObjects(); //得选择的体

//设置UI值和获取路径
setUIValueandPath();

getUIValueandPath();//获取UI值和得到新部件名字和路径


}
else if (block == label01)
{
//---------Enter your code here-----------
}
else if (block == label02)
{
//---------Enter your code here-----------
}
else if (block == label03)
{
//---------Enter your code here-----------
}
else if (block == stringMoldNo)
{
//---------Enter your code here-----------
}
else if (block == stringName)
{
//---------Enter your code here-----------
getUIValueandPath();//获取UI值和得到新部件名字和路径

}
else if (block == stringBeginName)
{
//---------Enter your code here-----------
getUIValueandPath();//获取UI值和得到新部件名字和路径
}
else if (block == toggle0)
{
//---------Enter your code here-----------
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//设置UI值和获取路径
void CreateNewComponent1::setUIValueandPath()
{
try
{

//获取显示部件的全路径
partfullpath1 = basedisplaypart->FullPath();
//theSession->ListingWindow()->WriteLine("显示部件全路径:"+partfullpath1);

//分割字符串得到显示部件名
string basedisplaypartFullPath = partfullpath1.GetLocaleText();
int basedpartstard = (int)basedisplaypartFullPath.find_last_of("\");
int basedpartEnd = (int)basedisplaypartFullPath.find_last_of(".");
string basedisplaypartName = basedisplaypartFullPath.substr(basedpartstard + 1, basedpartEnd - basedpartstard - 1);//分割
//theSession->ListingWindow()->WriteLine("显示部件的名字:"+basedisplaypartName);

//分割字符串得到显示部件路径
PartPath1 = basedisplaypartFullPath.substr(0, basedpartstard);//分割
//theSession->ListingWindow()->WriteLine("显示部件路径:"+PartPath1);

//分割字符串得到模号
int sta = 0;
if ((sta = (int)basedisplaypartName.find_first_of("_")) > 0)
{
//theSession->ListingWindow()->WriteLine("下横杠");
symboltxt1 = "_";
sta = (int)basedisplaypartName.find_first_of("_");//找到工作部件全路径名中的第一下杠
}
else if ((sta = (int)basedisplaypartName.find_first_of("-")) > 0)
{
//theSession->ListingWindow()->WriteLine("横杠");
symboltxt1 = "-";
sta = (int)basedisplaypartName.find_first_of("-");//找到工作部件全路径名中的第一横杠
}
displaypartMoldNo = basedisplaypartName.substr(0, sta);//分割
//theSession->ListingWindow()->WriteLine("模号:"+displaypartMoldNo + " 分隔符号:" + symboltxt1);

if (enum0Value == 0)
{
selection0->SetShow(true);
stringMoldNo->SetShow(true);
stringName->SetShow(true);
stringBeginName->SetShow(false);
label01->SetShow(true);
label02->SetShow(true);
label03->SetShow(false);
}
else if (enum0Value == 1)
{
selection0->SetShow(true);
stringMoldNo->SetShow(true);
stringName->SetShow(false);
stringBeginName->SetShow(true);
label01->SetShow(true);
label02->SetShow(false);
label03->SetShow(true);
}
else if (enum0Value == 2)
{
selection0->SetShow(false);
stringMoldNo->SetShow(true);
stringName->SetShow(false);
stringBeginName->SetShow(false);
label01->SetShow(false);
label02->SetShow(false);
label03->SetShow(false);
}


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("设置UI值和获取路径", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}


//获取UI值和得到新部件名字和路径
void CreateNewComponent1::getUIValueandPath()
{
try
{

NewName.clear();
NewNamePath.clear();

//获取模号
PropertyList *stringMoldNoProps = stringMoldNo->GetProperties();
NXString theMoldNo = stringMoldNoProps->GetString("Value");
delete stringMoldNoProps;
stringMoldNoProps = NULL;

//获取工件号
PropertyList *stringNameProps = stringName->GetProperties();
NXString theName = stringNameProps->GetString("Value");//NXString theName
delete stringNameProps;
stringNameProps = NULL;

//获取起始工件号
PropertyList *stringBeginNameProps = stringBeginName->GetProperties();
NXString theBeginName = stringBeginNameProps->GetString("Value");
delete stringBeginNameProps;
stringBeginNameProps = NULL;

//转换
char NewMoldNo1[256];
sprintf(NewMoldNo1, "%s", theMoldNo.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
string NewMoldNo2 = NewMoldNo1;
//根据枚举转换新部件名和全路径
if (enum0Value == 0)
{
//转换

char NewName1[256];
sprintf(NewName1, "%s%s", symboltxt1, theName.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s.prt", NewMoldNo2, symboltxt1, theName.GetLocaleText()); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
}
else if (enum0Value == 1)
{

string theBeginNamestring1 = theBeginName.GetLocaleText();
int start1 = 0;

if ((start1 = (int)theBeginNamestring1.find_last_of("_")) > 0)
{
start1 = (int)theBeginNamestring1.find_last_of("_");//找到工作部件全路径名中的最后一个下杠
}
else if ((start1 = (int)theBeginNamestring1.find_last_of("-")) > 0)
{
start1 = (int)theBeginNamestring1.find_last_of("-");//找到工作部件全路径名中的最后一个横杠
}

int end1 = (int)theBeginNamestring1.find_last_of("");//找到工作部件全路径名中的最后一个空格
string theBeginNamestring2 = theBeginNamestring1.substr(start1 + 1, end1);//分割
string theBeginNamestring3 = theBeginNamestring1.substr(0, start1 + 1);//分割
int beginNo = atoi(theBeginNamestring2.c_str());//字符串转为整数型

//sprintf ( msg , "%d",beginNo );
//theSession->ListingWindow()->WriteLine(msg);
for (int i = 0; i < selectBodys.size(); i++)
{
if (beginNo < 10)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s00%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s00%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
else if (beginNo >= 10 && beginNo < 100)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s0%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s0%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
else if (beginNo >= 100 && beginNo < 1000)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
else if (beginNo >= 1000 && beginNo < 10000)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
else if (beginNo >= 10000 && beginNo < 100000)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
else if (beginNo >= 100000 && beginNo < 1000000)
{
//转换
char NewName1[256];
sprintf(NewName1, "%s%s%d", symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName2 = NewName1;
char NewName3[256];
sprintf(NewName3, "\%s%s%s%d.prt", NewMoldNo2, symboltxt1, theBeginNamestring3, beginNo); //NXString字符串转char,必须用.GetLocaleText()
string NewName4 = NewName3;
//新建部件的名字
string NewpartName1 = NewMoldNo2 + NewName2;
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
string NewNamePath1 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);
beginNo++;
}
}
}
else if (enum0Value == 2)
{
//新建部件的名字
string NewpartName1 = NewMoldNo2 + symboltxt1 + "Layout";
NewName.push_back(NewpartName1);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );
//新建部件全路径
char NewName3[256];
sprintf(NewName3, "\%s%sLayout.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath1 = PartPath1 + NewName3;
NewNamePath.push_back(NewNamePath1);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath1);

//新建部件的名字
string NewpartName2 = NewMoldNo2 + symboltxt1 + "Misc";
NewName.push_back(NewpartName2);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName2 );
//新建部件全路径
char NewName4[256];
sprintf(NewName4, "\%s%sMisc.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath2 = PartPath1 + NewName4;
NewNamePath.push_back(NewNamePath2);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath2);

//新建部件的名字
string NewpartName3 = NewMoldNo2 + symboltxt1 + "Ejector";
NewName.push_back(NewpartName3);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName3 );
//新建部件全路径
char NewName5[256];
sprintf(NewName5, "\%s%sEjector.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath3 = PartPath1 + NewName5;
NewNamePath.push_back(NewNamePath3);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath3);

//新建部件的名字
string NewpartName4 = NewMoldNo2 + symboltxt1 + "Cavity";
NewName.push_back(NewpartName4);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName4 );
//新建部件全路径
char NewName6[256];
sprintf(NewName6, "\%s%sCavity.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath4 = PartPath1 + NewName6;
NewNamePath.push_back(NewNamePath4);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath4);

//新建部件的名字
string NewpartName5 = NewMoldNo2 + symboltxt1 + "Core";
NewName.push_back(NewpartName5);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName5 );
//新建部件全路径
char NewName7[256];
sprintf(NewName7, "\%s%sCore.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath5 = PartPath1 + NewName7;
NewNamePath.push_back(NewNamePath5);
//theSession->ListingWindow()->WriteLine("新建部件全路径:" + NewNamePath5);

//新建部件的名字
string NewpartName6 = NewMoldNo2 + symboltxt1 + "Lifter";
NewName.push_back(NewpartName6);
//新建部件全路径
char NewName8[256];
sprintf(NewName8, "\%s%sLifter.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath6 = PartPath1 + NewName8;
NewNamePath.push_back(NewNamePath6);

//新建部件的名字
string NewpartName7 = NewMoldNo2 + symboltxt1 + "Slider";
NewName.push_back(NewpartName7);
//新建部件全路径
char NewName9[256];
sprintf(NewName9, "\%s%sSlider.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath7 = PartPath1 + NewName9;
NewNamePath.push_back(NewNamePath7);

//新建部件的名字
string NewpartName8 = NewMoldNo2 + symboltxt1 + "Cooling";
NewName.push_back(NewpartName8);
//新建部件全路径
char NewName10[256];
sprintf(NewName10, "\%s%sCooling.prt", NewMoldNo2, symboltxt1); //NXString字符串转char,必须用.GetLocaleText()
string NewNamePath8 = PartPath1 + NewName10;
NewNamePath.push_back(NewNamePath8);

}

Cylmemory();//遍历内存

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("获取UI值和得到新部件名字和路径", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}


//遍历内存
void CreateNewComponent1::Cylmemory()
{
try
{
ispartnamecreate = false;
//通过迭代器获取内存加载部件的名
samepartname1.clear();
ALLsamenamePart.clear();
PartCollection *Pc = theSession->Parts();
for (PartCollection::iterator ite = Pc->begin(); ite != Pc->end(); ite++)
{
NXOpen::BasePart *part = (*ite);
NXString partfullpath2 = part->FullPath();
//theSession->ListingWindow()->WriteLine("部件路径:"+partfullpath2);
//分割字符串得到内存加载的部件名字
string currmemorypart = partfullpath2.GetLocaleText();
int currmemorypartstard = (int)currmemorypart.find_last_of("\");
int currmemorypartEnd = (int)currmemorypart.find_last_of(".");
string currPartName1 = currmemorypart.substr(currmemorypartstard + 1, currmemorypartEnd - currmemorypartstard - 1);//分割
//theSession->ListingWindow()->WriteLine("内存部件名:"+currPartName1);
//新建的部件名字和内存加载部件的名字对比,相同就把内存加载部件的名字塞到容器
for (int Num = 0; Num < NewName.size(); Num++)
{
string newname1 = NewName[Num];

if (currPartName1 == newname1)
{
samepartname1.push_back(currPartName1);
//theSession->ListingWindow()->WriteLine("同名部件:"+currPartName1);
}
}

}

for (int i = 0; i < samepartname1.size(); i++)
{
string currPartName2 = samepartname1[i];
for (int Num = 0; Num < NewName.size(); Num++)
{
string newname3 = NewName[Num];
if (currPartName2 == newname3)
{
ispartnamecreate = true;
ALLsamenamePart.push_back(newname3);
theSession->ListingWindow()->WriteLine(newname3 + " 部件已存在请重新输入工件名或起始工件名");
}
}

}


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("遍历内存", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}

//创建单个部件
void CreateNewComponent1::CreateSinglPart()
{
try
{

for (int i = 0; i < NewName.size(); i++)
{
//新建部件的名字
string NewNamestr = NewName[i];
//新建部件全路径
string NewNamePathstr = NewNamePath[i];
//创建部件
FileNew *fileNew1;
fileNew1 = theSession->Parts()->FileNew();
fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
fileNew1->SetApplication(FileNewApplicationModeling);
fileNew1->SetUnits(Part::UnitsMillimeters);
fileNew1->SetRelationType("");
fileNew1->SetUsesMasterModel("No");
fileNew1->SetTemplateType(FileNewTemplateTypeItem);
fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
fileNew1->SetMasterFileName("");
fileNew1->SetUseBlankTemplate(false);
fileNew1->SetMakeDisplayedPart(false);
Assemblies::CreateNewComponentBuilder *createNewComponentBuilder1;
createNewComponentBuilder1 = workPart->AssemblyManager()->CreateNewComponentBuilder();
//createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeOther);
createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeModel);//引用集设置
createNewComponentBuilder1->SetReferenceSetName("MODEL");//设置引用集名字
createNewComponentBuilder1->SetNewComponentName(NewNamestr);//设置新建部件的名字

//把选择的体塞到objects1
std::vector<DisplayableObject *> objects1(selectBodys.size());
for (int j = 0; j < selectBodys.size(); j++)
{
Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(selectBodys[j]->Tag()))); //选择的目标转换body
objects1[j] = body1;
}

bool added1;
added1 = createNewComponentBuilder1->ObjectForNewComponent()->Add(objects1);

createNewComponentBuilder1->SetNewFile(fileNew1);

NXObject *nXObject1;
nXObject1 = createNewComponentBuilder1->Commit();

//fileNew1->Destroy();

createNewComponentBuilder1->Destroy();

}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("创建单个部件", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}


//批量创建部件
void CreateNewComponent1::CreateMultiplePart()
{
try
{

for (int i = 0; i < NewName.size(); i++)
{
//新建部件的名字
string NewNamestr = NewName[i];
//新建部件全路径
string NewNamePathstr = NewNamePath[i];
//创建部件
FileNew *fileNew1;
fileNew1 = theSession->Parts()->FileNew();
fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
fileNew1->SetApplication(FileNewApplicationModeling);
fileNew1->SetUnits(Part::UnitsMillimeters);
fileNew1->SetRelationType("");
fileNew1->SetUsesMasterModel("No");
fileNew1->SetTemplateType(FileNewTemplateTypeItem);
fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
fileNew1->SetMasterFileName("");
fileNew1->SetUseBlankTemplate(false);
fileNew1->SetMakeDisplayedPart(false);
Assemblies::CreateNewComponentBuilder *createNewComponentBuilder1;
createNewComponentBuilder1 = workPart->AssemblyManager()->CreateNewComponentBuilder();
//createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeOther);
createNewComponentBuilder1->SetReferenceSet(Assemblies::CreateNewComponentBuilder::ComponentReferenceSetTypeModel);//引用集设置
createNewComponentBuilder1->SetReferenceSetName("MODEL");//设置引用集名字
createNewComponentBuilder1->SetNewComponentName(NewNamestr);//设置新建部件的名字
//把选择的体塞到objects1
std::vector<DisplayableObject *> objects1(1);
Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(selectBodys[i]->Tag()))); //选择的目标转换body
objects1[0] = body1;
bool added1;
added1 = createNewComponentBuilder1->ObjectForNewComponent()->Add(objects1);

createNewComponentBuilder1->SetNewFile(fileNew1);

NXObject *nXObject1;
nXObject1 = createNewComponentBuilder1->Commit();

//fileNew1->Destroy();

createNewComponentBuilder1->Destroy();


}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("批量创建部件", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}

//新模装配树
void CreateNewComponent1::CreateNewAssmPart()
{
try
{

for (int i = 0; i < NewName.size(); i++)
{
//新建部件的名字
string NewNamestr = NewName[i];
//新建部件全路径
string NewNamePathstr = NewNamePath[i];
//创建部件
FileNew *fileNew1;
fileNew1 = theSession->Parts()->FileNew();
fileNew1->SetTemplateFileName("model-plain-1-mm-template.prt");
fileNew1->SetApplication(FileNewApplicationModeling);
fileNew1->SetUnits(Part::UnitsMillimeters);
fileNew1->SetRelationType("");
fileNew1->SetUsesMasterModel("No");
fileNew1->SetTemplateType(FileNewTemplateTypeItem);
fileNew1->SetNewFileName(NewNamePathstr);//设置新建部件的路径
fileNew1->SetMasterFileName("");
fileNew1->SetUseBlankTemplate(false);
fileNew1->SetMakeDisplayedPart(false);

NXObject *nXObject1;
nXObject1 = fileNew1->Commit();

fileNew1->Destroy();

//把新文件装配到显示部件
Point3d basePoint1(0.0, 0.0, 0.0);
Matrix3x3 orientation1;
orientation1.Xx = 1.0;
orientation1.Xy = 0.0;
orientation1.Xz = 0.0;
orientation1.Yx = 0.0;
orientation1.Yy = 1.0;
orientation1.Yz = 0.0;
orientation1.Zx = 0.0;
orientation1.Zy = 0.0;
orientation1.Zz = 1.0;
PartLoadStatus *partLoadStatus2;
Assemblies::Component *component1;
component1 = workPart->ComponentAssembly()->AddComponent(NewNamePathstr, "None", NewNamestr, basePoint1, orientation1, -1, &partLoadStatus2, true);

delete partLoadStatus2;


}

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("新模装配树", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}

//保存新建部件
void CreateNewComponent1::SaveNewPart()
{
try
{

PartSaveStatus *partSaveStatus1;
for (int i = 0; i < NewName.size(); i++)
{
Part *part1(dynamic_cast<Part *>(theSession->Parts()->FindObject(NewName[i])));
string savenewname = NewNamePath[i];
partSaveStatus1 = part1->Save(BasePart::SaveComponentsTrue, BasePart::CloseAfterSaveFalse);

}

delete partSaveStatus1;

//保存当前显示部件
//PartSaveStatus *partSaveStatus2;
//partSaveStatus2 = basedisplaypart->Save(BasePart::SaveComponentsTrue, BasePart::CloseAfterSaveFalse);
//delete partSaveStatus2;


}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("新模装配树", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}

//设置装配树结构
void CreateNewComponent1::setASM()
{
try
{

//新建部件的名字
string NewpartName1 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Layout 1";
string NewpartName2 = displaypartMoldNo + symboltxt1 + "Layout";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName2);
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName1 );

//新建部件的名字
string NewpartName3 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Ejector 1";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName3 );

//新建部件的名字
string NewpartName4 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Cavity 1";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName4 );

//新建部件的名字
string NewpartName5 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Core 1";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName5 );

//新建部件的名字
string NewpartName6 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Lifter 1";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName6 );

//新建部件的名字
string NewpartName7 = "COMPONENT " + displaypartMoldNo + symboltxt1 + "Slider 1";
//theSession->ListingWindow()->WriteLine("新建部件名:" + NewpartName7 );

Part *part1(dynamic_cast<Part *>(theSession->Parts()->FindObject(NewpartName2)));
std::vector<Assemblies::Component *> origComponents1(5);
Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName3)));
origComponents1[0] = component1;
Assemblies::Component *component2(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName4)));
origComponents1[1] = component2;
Assemblies::Component *component3(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName5)));
origComponents1[2] = component3;
Assemblies::Component *component6(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName6)));
origComponents1[3] = component6;
Assemblies::Component *component7(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName7)));
origComponents1[4] = component7;

Assemblies::Component *component4(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject(NewpartName1)));
std::vector<Assemblies::Component *> newComponents1;
ErrorList *errorList1;
part1->ComponentAssembly()->RestructureComponents(origComponents1, component4, true, newComponents1, &errorList1);

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
CreateNewComponent1::theUI->NXMessageBox()->Show("新模装配树", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}

}

怡宁塑胶模具设计
原文地址:https://www.cnblogs.com/hqsalanhuang/p/14902359.html