NX8.5 VS2010
//关键代码
void CreatLifter::AssemblyTemplateFile() //装配模板文件
{
    try
    {
UF_initialize();
		//if(selectObjs.size()==0)
		//{
		//	return;
		//}
GetUI();
		if ( isComponent1ASSEM==true )
		{
			Session::UndoMarkId markId1;
			markId1 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Delete");
    
			bool notifyOnDelete1;
			notifyOnDelete1 = theSession->Preferences()->Modeling()->NotifyOnDelete();
    
			theSession->UpdateManager()->ClearErrorList();
    
			Session::UndoMarkId markId2;
			markId2 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Delete");
    
			std::vector<NXObject *> objects1(1);
			//Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT YN_Lifter_1 1")));
			objects1[0] = ImportComponent1;
			int nErrs1;
			nErrs1 = theSession->UpdateManager()->AddToDeleteList(objects1);
    
			bool notifyOnDelete2;
			notifyOnDelete2 = theSession->Preferences()->Modeling()->NotifyOnDelete();
    
			int nErrs2;
			nErrs2 = theSession->UpdateManager()->DoUpdate(markId2);
    
			theSession->DeleteUndoMark(markId1, NULL);
		}
		Point3d theNewPoint = manip0->Origin();
		xDirection=manip0->XAxis();
		yDirection=manip0->YAxis();
		double yuandian[3] = { theNewPoint.X , theNewPoint.Y, theNewPoint.Z  }; //
		double ufun_X[3] = { xDirection.X, xDirection.Y , xDirection.Z };
		double ufun_Y[3] = { yDirection.X,  yDirection.Y , yDirection.Z };
		double ufun_Z[3];
		UF_VEC3_cross( ufun_X, ufun_Y, ufun_Z ); //通过叉乘求Z轴
		Vector3d manip0Z ( ufun_Z[0], ufun_Z[1], ufun_Z[2] ); //Z轴double型转Vector3d
		zDirection = manip0Z;
	
		LifterMatrix1.Xx = xDirection.X;
		LifterMatrix1.Xy = xDirection.Y;
		LifterMatrix1.Xz = xDirection.Z;
		LifterMatrix1.Yx = yDirection.X;
		LifterMatrix1.Yy = yDirection.Y;
		LifterMatrix1.Yz = yDirection.Z;
		LifterMatrix1.Zx = zDirection.X;
		LifterMatrix1.Zy = zDirection.Y;
		LifterMatrix1.Zz = zDirection.Z;
		PartLoadStatus *partLoadStatus1;
		ImportComponent1 = workPart->ComponentAssembly()->AddComponent(NXlifter1_Path.c_str(), "MODEL", "YN_Lifter_1", manip0->Origin(), LifterMatrix1, -1, &partLoadStatus1, true);
		delete partLoadStatus1;
		std::vector<NXObject *> objects1(0);
		int nErrs1;
		nErrs1 = theSession->UpdateManager()->AddToDeleteList(objects1);
isComponent1ASSEM=true;
		if (isUpdateExpression==false)
		{
			updateExpression();  //更新表达式
			//theSession->ListingWindow()->WriteLine("测试");
		}
isUpdateExpression=true;
UF_terminate();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreatLifter::theUI->NXMessageBox()->Show("装配模板文件", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
    }
}
void CreatLifter::updateExpression()  //更新表达式
{
    try
    {
		if ( isComponent1ASSEM == false )
		{
			return;
		}
GetUI(); //获取UI
		//切换工作部件
		PartLoadStatus *partLoadStatus1;
 		theSession->Parts()->SetWorkComponent(ImportComponent1, &partLoadStatus1);
		workPart = theSession->Parts()->Work();
		delete partLoadStatus1;
		std::vector<NXObject *> objects1(0);
		int nErrs1;
		nErrs1 = theSession->UpdateManager()->AddToDeleteList(objects1);
		Session::UndoMarkId markId1;
		markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Expression");
		Expression *expressionDisX1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("H1")));
		Unit *unitDisX1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
		workPart->Expressions()->EditWithUnits(expressionDisX1, unitDisX1, doubleToNXString( LifterH1 ));
		Expression *expressionDisY1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("W1")));
		Unit *unitDisY1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
		workPart->Expressions()->EditWithUnits(expressionDisY1, unitDisY1, doubleToNXString( LifterW1 ));
		Expression *expressionL1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("H3")));
		Unit *unitL1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
		workPart->Expressions()->EditWithUnits(expressionL1, unitL1, doubleToNXString( LifterH3 ));
		Expression *expressionW1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("W")));
		Unit *unitW1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
		workPart->Expressions()->EditWithUnits(expressionW1, unitW1, doubleToNXString( LifterW ));
		Expression *expressionH1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("T")));
		Unit *unitH1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
		workPart->Expressions()->EditWithUnits(expressionH1, unitH1, doubleToNXString( LifterT ));
		Expression *expressionA1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("A")));
		Unit *unitA1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("Degrees")));
		workPart->Expressions()->EditWithUnits(expressionA1, unitA1, doubleToNXString( LifterA ));
		int nErrs2;
		nErrs2 = theSession->UpdateManager()->DoUpdate(markId1);
		//恢复的部件
		Assemblies::Component *componentBasePart(dynamic_cast<NXOpen::Assemblies::Component *>(NXObjectManager::Get(basePartvectorTag[0])));//组件
		PartLoadStatus *partLoadStatus2;
		theSession->Parts()->SetWorkComponent(componentBasePart, &partLoadStatus2);
		workPart = theSession->Parts()->Work();
		delete partLoadStatus2;	
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreatLifter::theUI->NXMessageBox()->Show("更新表达式", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
    }
}
void CreatLifter::deletebodyparms() //移除参数和删除基准面
{
    try
    {
        //---- Enter your callback code here -----
		CylFeaturesFindEnd(); //遍历特征,找到最后一个特征
		LifterBodyfeature1=(dynamic_cast<Features::Feature *>(NXObjectManager::Get(FeatureVecotor[(int)FeatureVecotor.size()-1]))); 
		//sprintf (msg,"%d",lastfeature1->Tag());
		//theSession->ListingWindow()->WriteLine(msg);
RemoveParametersbody1 = (dynamic_cast<Body *>(workPart->Bodies()->FindObject(LifterBodyfeature1->JournalIdentifier())));
		//移除参数
		Features::RemoveParametersBuilder *removeParametersBuilder1;
		removeParametersBuilder1 = workPart->Features()->CreateRemoveParametersBuilder();
		bool added1;
		added1 = removeParametersBuilder1->Objects()->Add(RemoveParametersbody1);
		NXObject *nXObject1;
		nXObject1 = removeParametersBuilder1->Commit();
		removeParametersBuilder1->Destroy();//释放
		Features::Feature *lastfeature2;
		lastfeature2=(dynamic_cast<Features::Feature *>(NXObjectManager::Get(FeatureVecotor[(int)FeatureVecotor.size()-4]))); 
		//sprintf (msg,"%d",lastfeature2->Tag());
		//theSession->ListingWindow()->WriteLine(msg);
		//删除基准面
		std::vector<NXObject *> objects1(1);
		objects1[0] = lastfeature2;
		int nErrs1;
		nErrs1 = theSession->UpdateManager()->AddToDeleteList(objects1);
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreatLifter::theUI->NXMessageBox()->Show("移除参数和删除基准面", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}
void CreatLifter::simpleInterferenceLifter() //干涉得出斜顶
{
    try
    {
		CylFeaturesFindEnd(); //遍历特征,找到最后一个特征
LifterBodyfeature1=(dynamic_cast<Features::Feature *>(NXObjectManager::Get(FeatureVecotor[(int)FeatureVecotor.size()-1])));
		//简单干涉
		GeometricAnalysis::SimpleInterference *simpleInterference1;
		simpleInterference1 = workPart->AnalysisManager()->CreateSimpleInterferenceObject();
simpleInterference1->SetInterferenceType(GeometricAnalysis::SimpleInterference::InterferenceMethodInterferenceSolid);
		Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(objbodysTAG[0])));
		//sprintf (msg,"第一体%d",body1->Tag());
		//theSession->ListingWindow()->WriteLine(msg);
		simpleInterference1->FirstBody()->SetValue(body1);
		Body *body2(dynamic_cast<Body *>(workPart->Bodies()->FindObject(LifterBodyfeature1->JournalIdentifier())));//特征转换body
		//sprintf (msg,"第二体%d",body2->Tag());
		//theSession->ListingWindow()->WriteLine(msg);
		simpleInterference1->SecondBody()->SetValue(body2);
		GeometricAnalysis::SimpleInterference::Result result1;
		result1 = simpleInterference1->PerformCheck();
		NXObject *nXObject1;
		nXObject1 = simpleInterference1->Commit();
		vector<NXOpen::NXObject *> interferenceObj;
		interferenceObj = simpleInterference1->GetInterferenceResults();
simpleInterference1->Destroy();
		//得到的干涉体改色和放置图层
		DisplayModification *displayModification1;
		displayModification1 = theSession->DisplayManager()->NewDisplayModification();
displayModification1->SetApplyToAllFaces(false);
displayModification1->SetApplyToOwningParts(false);
		displayModification1->SetNewTranslucency(0); //设置透明度
		//displayModification1->SetNewLayer(integer0LayerValue);//设置图层
displayModification1->SetNewColor(122);//得到的色值是int类型,存在vector数组里,使用时取数组里的第一个值[0]
displayModification1->SetNewWidth(DisplayableObject::ObjectWidthOne);
		std::vector<DisplayableObject *> objects1(1);
		Body *body3(dynamic_cast<Body *>(NXObjectManager::Get(interferenceObj[0]->Tag())));
		objects1[0] = body3;
		displayModification1->Apply(objects1);
delete displayModification1;
		deleteLifter(); //删除斜顶假体
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        CreatLifter::theUI->NXMessageBox()->Show("干涉得出斜顶", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

原文:https://www.cnblogs.com/hqsalanhuang/p/14643739.html