多组件 装配
int ZiDongWanTou::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----+
UF_initialize();
char msg[100];
Session *theSession = Session::GetSession();
Part *displayPart(theSession->Parts()->Display());
Part *workPart(theSession->Parts()->Work());
tag_t parentPart = UF_PART_ask_display_part();
cycleLine();
/*theSession->ListingWindow()->Open();
sprintf(msg,"LIne数量:%d",lineNum.size());
theSession->ListingWindow()->WriteLine(msg);*/
//////////添加组件
///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义
std::vector<Assemblies::Component*> compNum;
for(int i=0;i<lineNum.size()-1;i++)
{
Assemblies::Component *component0;
compNum.push_back(component1);
}
///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义*/
for(int i=0;i<lineNum.size()-1;i++)
{
line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
//添加组件
Point3d basePoint1(0.0, 0.0, 0.0);
Matrix3x3 orientation1;
Point3d p11(0.0, 0.0, 0.0);
Point3d p12(0.0, 0.0, 0.0);
Point3d p21(0.0, 0.0, 0.0);
Point3d p22(0.0, 0.0, 0.0);
p11 = line1->StartPoint();
p12 = line1->EndPoint();
p21 = line2->StartPoint();
p22 = line2->EndPoint();
basePoint1 = p12;
orientation1.Xx = p12.X-p11.X;
orientation1.Xy = p12.Y-p11.Y;
orientation1.Xz = p12.Z-p11.Z;
orientation1.Yx = p21.X-p22.X;
orientation1.Yy = p21.Y-p22.Y;
orientation1.Yz = p21.Z-p22.Z;
orientation1.Zx = 0.0;
orientation1.Zy = 0.0;
orientation1.Zz = 0.0;
PartLoadStatus *partLoadStatus1;
/*///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义
std::vector<Assemblies::Component*> compNum;
Assemblies::Component *component1;
compNum.push_back(component1);
///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义*/
//Assemblies::Component *component1;////原函数
compNum[i] = workPart->ComponentAssembly()->AddComponent("E:\\bgl\\0lib\\WT57-10H0-001.prt", "model", "1", basePoint1, orientation1, -1, &partLoadStatus1);
delete partLoadStatus1;
component1 = compNum[i];
assemPart();
//tag_t part_tag;
//tag_t occ_tag;
//occ_tag = component1[0]->Tag(); //但是此处只是一个occ Tag.不是原零件的tag
//part_tag = UF_ASSEM_ask_prototype_of_occ(occ_tag);
//char msg[111];
//
///////获取选择对象的进行动态转换(这个是成功的)
///*Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT WT57-10H0-001 1")));
//std::vector<TaggedObject*>objects1 = this->selection0->GetProperties()->GetTaggedObjectVector("SelectedObjects");
//Line *line7=NULL;
//line7 = dynamic_cast<Line *>(objects1[0]);
//theSession->ListingWindow()->Open();
//sprintf(msg,"数量tag:%d",line7->Tag());
//theSession->ListingWindow()->WriteLine(msg);*/
//////////////
////Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT WT57-10H0-001 1")));
//tag_t feature= NULL_TAG ;
//tag_t feature1= NULL_TAG ;
//std::vector<tag_t> line7Num;
//std::vector<tag_t> line8Num;
//int type = UF_line_type; //可以查询line, sketch,其他的edge,feature,solid摸不清规律
//const char * name7= "LINE7" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
//UF_OBJ_cycle_by_name_and_type(parentPart,name7,type, TRUE, &feature);
//
////while ( feature != NULL_TAG )
////{
//// line7Num.push_back(feature);
//// UF_OBJ_cycle_by_name_and_type(part_tag, name7,type, TRUE, &feature);//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
////
////}
///*theSession->ListingWindow()->Open();
//sprintf(msg,"数量tag:%d",line7Num[i]);
//theSession->ListingWindow()->WriteLine(msg);*/
//
//Line *line7 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
//
//const char * name8= "LINE8" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
//feature= NULL_TAG;
//UF_OBJ_cycle_by_name_and_type(parentPart,name8,type, TRUE, &feature);
////while ( feature != NULL_TAG )
////{
//// line8Num.push_back(feature);
//// UF_OBJ_cycle_by_name_and_type(part_tag, name8,type, TRUE, &feature);//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
////}
//Line *line8 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
}
UF_terminate();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}
void WuCanJieMian::cycleLine()
{
Session *theSession = Session::GetSession();
Part *displayPart(theSession->Parts()->Display());
Part *workPart(theSession->Parts()->Work());
Features::Feature *fcList;
std::vector<NXObject *> entitiesArray; //std::vector是一种数组定义方式
Features::FeatureCollection *fc=workPart->Features();//迭代
Features::FeatureCollection::iterator itr=fc->begin();//获得第一个特征
for (;itr!=fc->end();++itr) //循环获得所有特征
{
fcList=(*itr); //获得特征赋值给fcList
NXString featType = NULL;
featType = fcList->FeatureType();
/*theSession->ListingWindow()->WriteLine(fcList->FeatureType());*/
if(!strcmp(featType.GetLocaleText(),"LINE"))
{
//lineNum.push_back(fcList->Tag());
entitiesArray = fcList->GetEntities(); //GetEntities方法,实体下0对象,Lineg下1对对象,基准下7对象,点集下对象最多
lineNum.push_back(entitiesArray[0]->Tag());
}
}
//line1 =dynamic_cast<Line *>(entitiesArray[0]);
/*for(int i=0;i<lineNum.size();i++)
//{
// //NXObjectManager::Get(lineNum[i]);//通过tag得到 TaggedObject 对象指针.
////line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
}*/
/*line1 = dynamic_cast<Line*>(entitiesArray[0]);
line2 = dynamic_cast<Line*>(entitiesArray[0]); */
//line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[0])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
//line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
}
void WuCanJieMian::assemPart()
{
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());
tag_t parentPart = UF_PART_ask_display_part();
/////////////////获取Line7,Line8
tag_t feature= NULL_TAG ;
int type = UF_line_type; //可以查询line, sketch,其他的edge,feature,solid摸不清规律
const char * name7= "LINE7" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
UF_OBJ_cycle_by_name_and_type(parentPart,name7,type, TRUE, &feature);
Line *line7 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
feature= NULL_TAG;
const char * name8= "LINE8" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
UF_OBJ_cycle_by_name_and_type(parentPart,name8,type, TRUE, &feature);
Line *line8 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
///////////////////////////////////////
Positioning::ComponentPositioner *componentPositioner1;
componentPositioner1 = workPart->ComponentAssembly()->Positioner();
componentPositioner1->ClearNetwork();
componentPositioner1->BeginAssemblyConstraints();
Positioning::Network *network1;
network1 = componentPositioner1->EstablishNetwork();
Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<Positioning::ComponentNetwork *>(network1));
componentNetwork1->SetMoveObjectsState(true);
Assemblies::Component *nullAssemblies_Component(NULL);
componentNetwork1->SetDisplayComponent(nullAssemblies_Component);
//第一组
Positioning::Constraint *constraint1;
constraint1 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<Positioning::ComponentConstraint *>(constraint1));
componentConstraint1->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);
componentConstraint1->SetConstraintType(Positioning::Constraint::TypeTouch);
/////定义约束对象
/////获取选择对象的进行动态转换(这个是成功的)
/*std::vector<TaggedObject*>objects1 = this->selection01->GetProperties()->GetTaggedObjectVector("SelectedObjects");
Line *line3=NULL;
line3 = dynamic_cast<Line *>(objects1[0]); */
////装配进来的件就不用选择了
Positioning::ConstraintReference *constraintReference1;
constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line7, false, false, false);
/*////获取选择对象的进行动态转换(这个是成功的)
std::vector<TaggedObject*>objects2 = this->edge_select01->GetProperties()->GetTaggedObjectVector("SelectedObjects");
//objects2[0]->Tag();
Line *line4=NULL;
line4 = dynamic_cast<Line *>(objects2[0]);*/
///定义约束对象
/*Positioning::ConstraintReference *constraintReference1;
constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line3, false, false, false);*/
Positioning::ConstraintReference *constraintReference2;
constraintReference2 = componentConstraint1->CreateConstraintReference(workPart->ComponentAssembly(), line1, false, false, false);
constraintReference2->SetFixHint(true);
componentNetwork1->Solve();
//第二组
Positioning::Constraint *constraint2;
constraint2 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint2(dynamic_cast<Positioning::ComponentConstraint *>(constraint2));
componentConstraint2->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);
componentConstraint2->SetConstraintType(Positioning::Constraint::TypeTouch);
/////获取选择对象的进行动态转换(这个是成功的)
Positioning::ConstraintReference *constraintReference3;
constraintReference3 = componentConstraint2->CreateConstraintReference(component1, line8, false, false, false);
Positioning::ConstraintReference *constraintReference4;
constraintReference4 = componentConstraint2->CreateConstraintReference(workPart->ComponentAssembly(), line2, false, false, false);
constraintReference4->SetFixHint(true);
componentNetwork1->Solve();
componentPositioner1->ClearNetwork();
componentPositioner1->EndAssemblyConstraints();
}