cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

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

MurphyMin
12-Amethyst

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

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!

ACCEPTED SOLUTION

Accepted Solutions
MurphyMin
12-Amethyst
(To:remy)

#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?

View solution in original post

8 REPLIES 8
remy
21-Topaz I
(To:MurphyMin)

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.

MurphyMin
12-Amethyst
(To:remy)

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

MurphyMin_0-1659282406229.png

 

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?

remy
21-Topaz I
(To:MurphyMin)

First and foremost, did you try your scenario from the User Interface perspective ie from Creo Parametric "pick and clicks commands"?

Can you confirm it works?

If yes, can you attach the model to the thread?

MurphyMin
12-Amethyst
(To:remy)

This operation is okay in Creo parametric UI , and have done this operation many times manually,In Creo 5.0 the operation is also okay by otk c++/vb api/

MurphyMin_1-1659408948921.png

 

 

remy
21-Topaz I
(To:MurphyMin)

Our thread has remained idle for weeks. Apologies for delay in coming back to you.

 

thank you for your confirmation. After searching CreateReplaceOp in the knowledge base, i found this article ; https://www.ptc.com/en/support/article/cs367218 

 

It introduces the method wfcWAssembly::AutoInterchange() 

 

Did you try it?

MurphyMin
12-Amethyst
(To:remy)

Thanks, Already done, the method wfcWAssembly::AutoInterchange()  is okay foy my solution.

MurphyMin
12-Amethyst
(To:remy)

#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?
Announcements


Top Tags