|
楼主 |
发表于 2019-4-30 08:49:51
|
显示全部楼层
//---- Enter your callback code here -----
std::vector<NXOpen::TaggedObject *> selobjs = selection0->GetSelectedObjects();//获取选定对象
NXOpen::NXString pathNXstring = nativeFileBrowser0->Path().GetLocaleText();
char cName[256] = "";
std::string strName = pathNXstring.GetText();
strcpy(cName, strName.c_str());
char *ptr = strrchr(cName, '\\');
sprintf(cName, "%s", ptr + 1);
strName = cName;
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
NXOpen::PartLoadStatus *partLoadStatus1 = NULL;//部分加载状态
NXOpen::Features::DatumCsys * sys1 = NULL;
NXOpen::Assemblies::Component *component1 = NULL;// Assemblies 组件 Component 成分
std::vector<NXOpen::NXObject *> objects1(0);
std::vector<NXOpen::DatumAxis *> ovectordatumAxis1;
std::vector<NXOpen::DatumAxis *> daxes;
//NXOpen::CartesianCoordinateSystem * CartesianCoordinateSystemsys=NULL;
char num[100];
UF_initialize();
for (int i = 0; i < selobjs.size(); i++)
{
sys1 = dynamic_cast<NXOpen::Features::DatumCsys *> (selobjs);
getDatumAxis(sys1, ovectordatumAxis1);
sprintf(num, "ovectordatumAxis1=%d", ovectordatumAxis1.size());//3
uc1601(num, 1);
NXOpen::Point3d basePoint1(0.0, 0.0, 0.0);//这里换成坐标系的点,下面换成坐标系的轴
NXOpen::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;
//tag_t work_part = NULL_TAG;
double origin[3] =
{
0.0, 0.0, 0.0
};
double csys[6] =
{
1.0, 0.0, 0.0, 0.0, 1.0, 0.0
};
UF_PART_load_status_t error_status;
tag_t tempinstance = NULL_TAG;
tag_t partInstance = NULL_TAG;
//修改时间:2018/09/11 ,修改人:konghaijiao
//修改原因:如果iszRefName为空,则NX会自动采用Entire part引用集
UF_ASSEM_add_part_to_assembly(workPart->Tag(), pathNXstring.GetLocaleText(), "MODEL", NULL, origin, csys, -1, &tempinstance, &error_status);
if (error_status.n_parts != 0)
{
UF_PART_free_load_status(&error_status);
}
partInstance = tempinstance;
tag_t* part_occs = NULL;
int occCount = UF_ASSEM_ask_part_occs_of_inst(partInstance, &part_occs);
if (occCount <= 0)
{
return 3;
}
tag_t partPro = UF_ASSEM_ask_prototype_of_occ(part_occs[0]);
NXOpen::Part* pAssemPart = dynamic_cast<NXOpen::Part*>(NXOpen::NXObjectManager::Get(partPro));
if (NULL == pAssemPart)
{
return 4;
}
component1 = dynamic_cast<NXOpen::Assemblies::Component*>(NXObjectManager::Get(part_occs[0]));
//component1 = workPart->ComponentAssembly()->AddComponent(pathNXstring,"MODEL",strName, basePoint1,
// orientation1, -1, &partLoadStatus1, true);
std::vector<NXOpen::Assemblies::Component*> tempComps;
tempComps.push_back(component1);
//HFHZUpdateMgr->SetDefaultUpdateFailureAction(NXOpen::Update::FailureOptionAcceptAll);
component1->UpdateStructure(tempComps, 2, true);
NXOpen::Part * component1Part = pAssemPart/*dynamic_cast<NXOpen::Part*>(component1->Prototype())*/;
string name=component1Part->Name().GetLocaleText();
NXOpen::DatumAxis *datumAxisPart;
//getSonPartCoordinateSystem(component1, daxes);
Features::FeatureCollection * feacol = component1Part->Features();
for (auto i : *feacol)
{
getDatumAxis(dynamic_cast<Features::DatumCsys*>(i), daxes);
}
//for (auto &i : daxes)
//{
// if (i->IsOccurrence())
// {
// }
// else
// {
// auto*nxobj = i->Prototype();
// //i = dynamic_cast<NXOpen::DatumAxis *>(component1->FindOccurrence(i));
// i = dynamic_cast<NXOpen::DatumAxis *>(component1->FindObject("PROTO#HANDLE R-714"));
// }
// //tag_t * occurrences = NULL;
// //UF_ASSEM_ask_occs_of_entity(i->Tag(), &occurrences);
// //i = dynamic_cast<NXOpen::DatumAxis *>(NXObjectManager::Get(occurrences[0]));
// HFHZN::HFHZNXAttr::SetUserAttribute(i, "test", "test","fff");
//
// //NXObject*obj=HFHZN::HFHZNXPart::GetOccObject(component1, i);
// //i = dynamic_cast<NXOpen::DatumAxis *>(obj);
//}
////NXOpen::DatumCollection * aDatumCollection = component1Part->Datums();
////for (DatumCollection::iterator i = aDatumCollection->begin(); i != aDatumCollection->end(); i++)
////{
//// auto m = dynamic_cast<NXOpen::DatumAxis*> (*i);
//// if (m != NULL)
//// {
//// int inum = 0;//计数
//// NXOpen::Point3d mPoint = m->Origin();
//// NXOpen::Vector3d mVector = m->Direction();
//// if (mPoint.X == 0 && mPoint.Y == 0 && mPoint.Z == 0)//判断坐标系是否是原点
//// {
//// if (mVector.X == 1 && mVector.Y == 0 && mVector.Z == 0)//判断是否是X轴
//// {
//// inum++;
//// sprintf(num, "daxes=%d,x", inum);//3
//// uc1601(num, 1);
//// }
//// if (mVector.X == 0 && mVector.Y == 1 && mVector.Z == 0)
//// {
//// inum++;
//// sprintf(num, "daxes=%d,y", inum);
//// uc1601(num, 1);
//// }
//// if (mVector.X == 0 && mVector.Y == 0 && mVector.Z == 1)
//// {
//// inum++;
//// sprintf(num, "daxes=%d,z", inum);
//// uc1601(num, 1);
//// }
//// }
//// if (inum>0)
//// {
//// daxes.push_back(m);
//// }
//// inum = 0;//清空
//// }
////}
sprintf(num, "daxes=%d", daxes.size());//3
uc1601(num, 1);
NXOpen::Positioning::ComponentPositioner *componentPositioner1; //Positioning 定位 ComponentPositioner 组件定位器
componentPositioner1 = workPart->ComponentAssembly()->Positioner();//定位器
componentPositioner1->ClearNetwork();//清除网络
NXOpen::Assemblies::Arrangement *arrangement1(dynamic_cast<NXOpen::Assemblies::Arrangement *>(workPart->ComponentAssembly()->Arrangements()->FindObject("Arrangement 1")));//Arrangement 排列
componentPositioner1->SetPrimaryArrangement(arrangement1);//设置主要排列
componentPositioner1->BeginAssemblyConstraints();//开始装配训练
bool allowInterpartPositioning1;//允许部件间定位
allowInterpartPositioning1 = theSession->Preferences()->Assemblies()->InterpartPositioning();
/*
Preferences 偏好
Assemblies 组件
InterpartPositioning 关节间定位
*/
NXOpen::Positioning::Network *network1;//网络
network1 = componentPositioner1->EstablishNetwork();//建立网络
NXOpen::Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<NXOpen::Positioning::ComponentNetwork *>(network1));//组件网络
componentNetwork1->SetMoveObjectsState(true);//设置移动对象状态
NXOpen::Assemblies::Component *nullNXOpen_Assemblies_Component(NULL);//空nxopen程序集组件
componentNetwork1->SetDisplayComponent(nullNXOpen_Assemblies_Component);//设置显示组件
componentNetwork1->SetNetworkArrangementsMode(NXOpen::Positioning::ComponentNetwork::ArrangementsModeExisting);//SetNetworkArrangementsMode 设置网络排列模式 ArrangementsModeExisting 现有布置方式
componentNetwork1->SetMoveObjectsState(true);//设置移动对象状态
NXOpen::Positioning::Constraint *constraint1;//约束
constraint1 = componentPositioner1->CreateConstraint(true);//创建约束
NXOpen::Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<NXOpen::Positioning::ComponentConstraint *>(constraint1));//组件约束
componentConstraint1->SetConstraintAlignment(NXOpen::Positioning::Constraint::AlignmentInferAlign);//SetConstraintAlignment 设置约束 AlignmentInferAlign 对齐推断对齐
componentConstraint1->SetConstraintType(NXOpen::Positioning::Constraint::TypeTouch); //SetConstraintType 设置约束类型 TypeTouch 触控式 //这里给我的感觉是在代码上设置约束,要设置对齐约束,还要设置接触约束,才是装配约束中的对齐接触
/*NXOpen::TaggedObject * DatumAxis = NXOpen::NXObjectManager::Get(daxes[0]);
NXOpen::DatumAxis *datumAxis1(dynamic_cast<NXOpen::DatumAxis *>(DatumAxis));*/
NXOpen::Positioning::ConstraintReference *constraintReference1;//约束引用
constraintReference1 = componentConstraint1->CreateConstraintReference(component1, daxes[0], false, false, false);//创建约束引用
//NXOpen::DatumAxis *datumAxis2(dynamic_cast<NXOpen::DatumAxis *>(workPart->Datums()->FindObject("DATUM_CSYS(1) X axis")));//基准面
//NXOpen::DatumAxis *datumAxis2(dynamic_cast<NXOpen::DatumAxis *>(xDirection));
NXOpen::Positioning::ConstraintReference *constraintReference2;
constraintReference2 = componentConstraint1->CreateConstraintReference(workPart->ComponentAssembly(), ovectordatumAxis1[0], false, false, false);
上面是部分代码,图片是录宏得到的代码,需要的就是将录宏红色框里面的换掉,代码不可能只执行这一个prt
C:\Users\luxin\Desktop\TIM图片20190430084345.png |
|