Skip to main content
12-Amethyst
July 29, 2022
Solved

Replace the component part in a assembly via OTK C++/OTK Java

  • July 29, 2022
  • 1 reply
  • 4382 views

What i suppoesed to do if i need replace a component part in a assembly in Creo 7.0.2.0 OTK C++/Java OTK
In old version the method pfcSolid::ExecuteFeatureOps(pfcFeatureOperations_ptr, optional pfcRegenInstructions_ptr) works well,however e-support technique file tells me OTK C++/Java OTK have anther mehod with same function in this version, but i can't find the method.
The only mehod i found in help file is pfcCompModelReplace_ptr CreateReplaceOp (pfcModel_ptr NewModel), but what i should do after i have created this option,which mehod can receive this argument and execute the replacement action.
Urgent issue need your help ,million thanks!

Best answer by MurphyMin
#pragma once
#include <iostream>
#include <iostream>
#include <string>
#include <pfcAsyncConnection.h>
#include <pfcSession.h>
#include <pfcGlobal.h>
#include <Locale.h>
#include <wfcSession.h>

int main()
{
	pfcAsyncConnection_ptr conn = pfcAsyncConnection::Connect("", "", "", 1);
	if (conn == NULL)
	{
		throw "Asyn Connection pointer is NULL!";
	}
	pfcSession_ptr session = conn->GetSession();
	session->UIShowMessageDialog("Asynchronous Mode Test!", NULL);
	wfcWSession_ptr wSession = wfcWSession::cast(session);

	pfcModel_ptr model = wSession->GetCurrentModel();
	pfcSolid_ptr solid = pfcSolid::cast(model);

	wfcWSolid_ptr wSolid = wfcWSolid::cast(model);

	cout << model->GetFullName() << endl;

	pfcFeatures_ptr feas = solid->ListFeaturesByType(false, pfcFEATTYPE_COMPONENT);
	pfcFeature_ptr fea = feas->get(0);
	pfcComponentFeat_ptr comFea = pfcComponentFeat::cast(fea);

	xstring modelNameN = model->GetFullName().ToLower();
	xstring modelNameX = modelNameN.Substring(1, modelNameN.GetLength());

	cStringT modelName = modelNameX.operator cStringT();

	cout << modelName << endl;
	cout << comFea->GetName() << endl;

	pfcModel_ptr model1 = wSession->GetModel(modelNameX, pfcMDL_PART);

	cout << model1->GetFullName() << endl;

	pfcSolid_ptr solid1 = pfcSolid::cast(model1);
	cout << solid1->GetFullName() << endl;

	pfcFamilyMember_ptr famMember = pfcFamilyMember::cast(solid1);
	pfcFamilyTableRows_ptr famRows = famMember->ListRows();

	pfcWindow_ptr win = wSession->GetCurrentWindow();

	//pfcRegenInstructions_ptr regeIns = pfcRegenInstructions::Create(false, true, true);
	/*regeIns->SetResolveModeRegen(true);
	regeIns->SetRefreshModelTree(true);*/

	/*wfcWRegenInstructions_ptr wRegeIns = wfcWRegenInstructions::Create();
	wRegeIns->SetNoResolveMode(true);
	wRegeIns->SetForceRegen(true);
	wRegeIns->SetRefreshModelTree(true);*/

	for (int i = 0; i < famRows->getarraysize(); i++)
	{
		pfcFamilyTableRow_ptr famRow = famRows->get(i);
		xstring famName = famRow->GetInstanceName();
		pfcModel_ptr famModel = famRow->CreateInstance();

		pfcCompModelReplace_ptr modelReplace = comFea->CreateReplaceOp(famModel);

		cout << modelReplace->GetNewModel()->GetFullName() << endl;
		//solid->ExecuteFeatureOps(, NULL);

		/*pfcFeatureOperation_ptr feaOpt = pfcFeatureOperation::cast(modelReplace);

		pfcFeatureOperations_ptr feaOpts = pfcFeatureOperations::create();
		feaOpts->append(feaOpt);
		
		solid->ExecuteFeatureOps(feaOpts, 0);*/

		/*solid->Regenerate();
		win->Refresh();
		win->Repaint();*/

		cout << "Test " << endl;
	}

}
 

VB API Test via old method ComponentFeat.CreateReplaceOp() & Solid.ExecuteFeatureOps() 

 static void Main(string[] args)
 {
 CCpfcAsyncConnection cCpfcAsync = new CCpfcAsyncConnection();
 IpfcAsyncConnection conn = cCpfcAsync.Connect(null, null, null, null);

 CMpfcSession cMpfc = new CMpfcSession();
 IpfcSession session = cMpfc.GetCurrentSessionWithCompatibility((int)EpfcCreoCompatibility.EpfcC4Compatible);
 IpfcBaseSession baseSession = session as IpfcBaseSession;
 Console.WriteLine(baseSession.GetCurrentDirectory());

 IpfcModel model = baseSession.CurrentModel;
 IpfcSolid solid = model as IpfcSolid;//获取MC组件 的Solid

 //获取NC solid下的组件特征
 IpfcFeatures feas= solid.ListFeaturesByType(true, (int)EpfcFeatureType.EpfcFEATTYPE_COMPONENT);
 IpfcComponentFeat comFea = feas[0] as IpfcComponentFeat;//转化为组件特征
 

 string modelName = model.FullName.ToLower().Replace("n", "");

 IpfcModel model1 = baseSession.GetModel(modelName, (int)EpfcModelType.EpfcMDL_PART);//NC组件下的部件
 IpfcSolid solid1 = model1 as IpfcSolid;//NC组件下的部件 的Solid,用于取族表


 IpfcFamilyMember familyMember = solid1 as IpfcFamilyMember;////NC组件下的部件 下的族表实例
 IpfcFamilyTableRows famRows = familyMember.ListRows();

 IpfcRegenInstructions regeIns =new CCpfcRegenInstructions().Create(true,null,null);
 regeIns.RefreshModelTree = true;
 regeIns.ResolveModeRegen = true;
 regeIns.ForceRegen = true;

 foreach (IpfcFamilyTableRow famRow in famRows)
 {
 string famName = famRow.InstanceName;
 IpfcModel famModel = famRow.CreateInstance();
 //Console.WriteLine(famModel.CommonName);

 IpfcCompModelReplace compModelReplace = comFea.CreateReplaceOp(famModel);//创建替换操作类
 //compModelReplace.NewModel = famModel;//指名替换后的新模型

 CpfcFeatureOperations replaceOpts =new CpfcFeatureOperations();
 replaceOpts.Append(compModelReplace);

 solid.ExecuteFeatureOps(replaceOpts, regeIns);

 
 }

 conn.Disconnect(1000);
 }
 

Seems don't work , or maybe some mistakes i had, would you mind to show me the right method?

1 reply

21-Topaz I
July 29, 2022

By chance did you go through this article : https://www.ptc.com/en/support/article/cs130146 

The usage is restricted to the actual Replace command from the UI meaning that it applies to Interchange Assemblies and Family Table change.

 

Replacing a component by an unrelated component is equivalent to deleting a component and assembling a new one. It is basically the behavior of from UI perspective.

MurphyMin12-AmethystAuthor
12-Amethyst
July 31, 2022

Actually , i have already tested this method but seems don't work to replace the family table member part.

MurphyMin_0-1659282406229.png

 

MurphyMin12-AmethystAuthor
12-Amethyst
July 31, 2022

OTK C++ Test  via interface CompModelReplace

#pragma once
#include <iostream>
#include <iostream>
#include <string>
#include <pfcAsyncConnection.h>
#include <pfcSession.h>
#include <pfcGlobal.h>
#include <Locale.h>
#include <wfcSession.h>

int main()
{
	pfcAsyncConnection_ptr conn = pfcAsyncConnection::Connect("", "", "", 1);
	if (conn == NULL)
	{
		throw "Asyn Connection pointer is NULL!";
	}
	pfcSession_ptr session = conn->GetSession();
	session->UIShowMessageDialog("Asynchronous Mode Test!", NULL);
	wfcWSession_ptr wSession = wfcWSession::cast(session);

	pfcModel_ptr model = wSession->GetCurrentModel();
	pfcSolid_ptr solid = pfcSolid::cast(model);

	wfcWSolid_ptr wSolid = wfcWSolid::cast(model);

	cout << model->GetFullName() << endl;

	pfcFeatures_ptr feas = solid->ListFeaturesByType(false, pfcFEATTYPE_COMPONENT);
	pfcFeature_ptr fea = feas->get(0);
	pfcComponentFeat_ptr comFea = pfcComponentFeat::cast(fea);

	xstring modelNameN = model->GetFullName().ToLower();
	xstring modelNameX = modelNameN.Substring(1, modelNameN.GetLength());

	cStringT modelName = modelNameX.operator cStringT();

	cout << modelName << endl;
	cout << comFea->GetName() << endl;

	pfcModel_ptr model1 = wSession->GetModel(modelNameX, pfcMDL_PART);

	cout << model1->GetFullName() << endl;

	pfcSolid_ptr solid1 = pfcSolid::cast(model1);
	cout << solid1->GetFullName() << endl;

	pfcFamilyMember_ptr famMember = pfcFamilyMember::cast(solid1);
	pfcFamilyTableRows_ptr famRows = famMember->ListRows();

	pfcWindow_ptr win = wSession->GetCurrentWindow();

	//pfcRegenInstructions_ptr regeIns = pfcRegenInstructions::Create(false, true, true);
	/*regeIns->SetResolveModeRegen(true);
	regeIns->SetRefreshModelTree(true);*/

	/*wfcWRegenInstructions_ptr wRegeIns = wfcWRegenInstructions::Create();
	wRegeIns->SetNoResolveMode(true);
	wRegeIns->SetForceRegen(true);
	wRegeIns->SetRefreshModelTree(true);*/

	for (int i = 0; i < famRows->getarraysize(); i++)
	{
		pfcFamilyTableRow_ptr famRow = famRows->get(i);
		xstring famName = famRow->GetInstanceName();
		pfcModel_ptr famModel = famRow->CreateInstance();

		pfcCompModelReplace_ptr modelReplace = comFea->CreateReplaceOp(famModel);

		cout << modelReplace->GetNewModel()->GetFullName() << endl;
		//solid->ExecuteFeatureOps(, NULL);

		/*pfcFeatureOperation_ptr feaOpt = pfcFeatureOperation::cast(modelReplace);

		pfcFeatureOperations_ptr feaOpts = pfcFeatureOperations::create();
		feaOpts->append(feaOpt);
		
		solid->ExecuteFeatureOps(feaOpts, 0);*/

		/*solid->Regenerate();
		win->Refresh();
		win->Repaint();*/

		cout << "Test " << endl;
	}

}

 

VB API Test   via old method ComponentFeat.CreateReplaceOp() & Solid.ExecuteFeatureOps() 

 static void Main(string[] args)
 {
 CCpfcAsyncConnection cCpfcAsync = new CCpfcAsyncConnection();
 IpfcAsyncConnection conn = cCpfcAsync.Connect(null, null, null, null);

 CMpfcSession cMpfc = new CMpfcSession();
 IpfcSession session = cMpfc.GetCurrentSessionWithCompatibility((int)EpfcCreoCompatibility.EpfcC4Compatible);
 IpfcBaseSession baseSession = session as IpfcBaseSession;
 Console.WriteLine(baseSession.GetCurrentDirectory());

 IpfcModel model = baseSession.CurrentModel;
 IpfcSolid solid = model as IpfcSolid;//获取MC组件 的Solid

 //获取NC solid下的组件特征
 IpfcFeatures feas= solid.ListFeaturesByType(true, (int)EpfcFeatureType.EpfcFEATTYPE_COMPONENT);
 IpfcComponentFeat comFea = feas[0] as IpfcComponentFeat;//转化为组件特征
 

 string modelName = model.FullName.ToLower().Replace("n", "");

 IpfcModel model1 = baseSession.GetModel(modelName, (int)EpfcModelType.EpfcMDL_PART);//NC组件下的部件
 IpfcSolid solid1 = model1 as IpfcSolid;//NC组件下的部件 的Solid,用于取族表


 IpfcFamilyMember familyMember = solid1 as IpfcFamilyMember;////NC组件下的部件 下的族表实例
 IpfcFamilyTableRows famRows = familyMember.ListRows();

 IpfcRegenInstructions regeIns =new CCpfcRegenInstructions().Create(true,null,null);
 regeIns.RefreshModelTree = true;
 regeIns.ResolveModeRegen = true;
 regeIns.ForceRegen = true;

 foreach (IpfcFamilyTableRow famRow in famRows)
 {
 string famName = famRow.InstanceName;
 IpfcModel famModel = famRow.CreateInstance();
 //Console.WriteLine(famModel.CommonName);

 IpfcCompModelReplace compModelReplace = comFea.CreateReplaceOp(famModel);//创建替换操作类
 //compModelReplace.NewModel = famModel;//指名替换后的新模型

 CpfcFeatureOperations replaceOpts =new CpfcFeatureOperations();
 replaceOpts.Append(compModelReplace);

 solid.ExecuteFeatureOps(replaceOpts, regeIns);

 
 }

 conn.Disconnect(1000);
 }

 

Seems don't work , or maybe some mistakes i had, would you mind to show me the right method?