唐老师 你好 看了你的nx创建部件课程,自己尝试通过UI 选择体创建部件,如果是在单个部件界面是可以创建部件成功,但是如果是在有多个装配部件环境下创建部件始终不成功,很是困惑也找不到原因,想请教你,感谢!
int creat_edit_attr::CreateNewComponentcd(std::vector<TaggedObject*> sheetbody, string strsheetname, tag_t &part_tag)
{
try
{
ListingWindow *lw=theSession->ListingWindow();
lw->Open();
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
// ----------------------------------------------
// Gets the file path and name
// ----------------------------------------------
//判断文件的路径
string m_filepath = workPart->FullPath().GetLocaleText();
string s = m_filepath;
std::size_t found1 = s.find_last_of("\\");
m_filepath = s.substr(0, found1 + 1);
string filename = m_filepath + strsheetname + ".prt";
lw->WriteLine("进入CreateNewComponentcd 1");
// TODO: add your code here
UF_initialize();
size_t ui = sheetbody.size();
int iconut= static_cast<int>(ui);
const char* new_part_name = filename.c_str();
//
//
tag_t * tagObjects = (tag_t *) malloc(iconut * sizeof(tag_t));
for (int i = 0; i < iconut; i++)
{
NXOpen::DisplayableObject *body1 = (NXOpen::DisplayableObject *)(NXOpen::NXObjectManager::Get(sheetbody[i]->Tag()));
tagObjects[i] = body1->Tag();
char chtag[256];
sprintf(chtag,"%d",tagObjects[i]);
lw->WriteLine("选择Body的 tag:");
lw->WriteLine(chtag);
}
//新建组件
tag_t parent_part = UF_ASSEM_ask_work_part();
lw->WriteLine("进入CreateNewComponentcd UF_ASSEM_ask_work_part()");
char cht[256];
sprintf(cht,"%d",parent_part);
lw->WriteLine("workpart tag:");
lw->WriteLine(cht);
tag_t instance = NULL_TAG;
double origin [3] = { 0.0, 0.0, 0.0 };
double csys_matrix [6] = { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 };
lw->WriteLine("进入CreateNewComponentcd 3");
int n_objects = iconut;
UF_ASSEM_create_component_part( parent_part,new_part_name, "MODEL", strsheetname.c_str(), 1, -1, origin, csys_matrix, n_objects, tagObjects, &instance );
lw->WriteLine("进入CreateNewComponentcd 4");
char ch[256];
sprintf(ch,"%d",instance);
lw->WriteLine("新建部件的tag:");
lw->WriteLine(ch);
//UF_free(objects);
UF_terminate();
return 1;
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
creat_edit_attr::theUI->NXMessageBox()->Show("CD创建部件错误提示", NXOpen::NXMessageBox::DialogTypeError, ex.what());
return 0;
}
}