【Creo5.0二次开发参数化】选择装配

通过用户选择装配平面,自动装配

装配函数

/*------------------------------------------------------------------*\
Application includes
\*--------------------------------------   ---------------------------*/
#include <TestError.h>
#include <ProMessage.h>
#include <ProSolid.h>
#include "GetDimension.h"
#include <stdio.h>
#include "MCFdialog.h"
#include <ProModelitem.h>
#include <ProAsmcomp.h> // 新添的头文件
#include <ProWstring.h>
/// <summary>
/// 通过用户选择装配平面,自动装配
/// </summary>
/// <param name="asm_model"> 组装模型</param>
/// <param name="comp_model">装配元件模型</param>
/// <returns></returns>
ProError UserAssembleByDatums(ProAssembly asm_model,
	ProSolid comp_model)
{
	ProError status;
	ProName comp_datums[3];
	ProMatrix identity_matrix = { { 1.0, 0.0, 0.0, 0.0 },
	{0.0, 1.0, 0.0, 0.0},
	{0.0, 0.0, 1.0, 0.0},
	{0.0, 0.0, 0.0, 1.0} };
	ProAsmcomp asmcomp;
	ProAsmcompconstraint* constraints;
	ProAsmcompconstraint constraint;
	int i;
	ProBoolean interact_flag = PRO_B_FALSE;
	ProModelitem asm_datum, comp_datum;
	ProSelection asm_sel, comp_sel;
	ProAsmcomppath comp_path;
	ProIdTable c_id_table;
	c_id_table[0] = -1;
	//Set up the arrays of datum names  
	//参考平面,这里的名称要和自己的装配元件的平面名称一样,才能找到
	ProStringToWstring(comp_datums[0], "FRONT");
	ProStringToWstring(comp_datums[1], "TOP");

		ProStringToWstring(comp_datums[2], "RIGHT");
	//Package the component initially
		//把装配元件添加在组装元件之中,identity_matrix表示加载的初始位置,(在空间上)
	ProAsmcompAssemble(asm_model, comp_model, identity_matrix, &asmcomp);
	//Prepare the constraints array
	ProArrayAlloc(0, sizeof(ProAsmcompconstraint), 1,
		(ProArray*)&constraints);

	//用户选择装配平面
	//每选择一个装配平面,就和一个元件平面组成一个约束
	for (i = 0; i < 3; i++)
	{
		//Find the component datum  获取平面模型
		status = ProModelitemByNameInit(comp_model, PRO_SURFACE,
			comp_datums[i], &comp_datum);
		if (status != PRO_TK_NO_ERROR)
		{
			interact_flag = PRO_B_TRUE;
			continue;
		}
		//Allocate the references
		ProSelectionAlloc(NULL, &comp_datum, &comp_sel);
		int n_sel = 0;
		ProSelection* sel;
		//用户选择平面
		status = ProSelect("surface", 1, NULL, NULL, NULL, NULL, &sel, &n_sel);
		if (status != PRO_TK_NO_ERROR || n_sel < 0)
			return (PRO_TK_USER_ABORT);
		ProSelectionCopy(sel[0], &asm_sel);

		//Allocate and fill the constraint.填充约束,循环三次,三个元件平面和三个用户选择的平面
		ProAsmcompconstraintAlloc(&constraint);
		ProAsmcompconstraintTypeSet(constraint, PRO_ASM_ALIGN);
		ProAsmcompconstraintAsmreferenceSet(constraint, asm_sel,
			PRO_DATUM_SIDE_YELLOW);

			ProAsmcompconstraintCompreferenceSet(constraint, comp_sel,
				PRO_DATUM_SIDE_YELLOW);
		ProArrayObjectAdd((ProArray*)&constraints, -1, 1, &constraint);
	}  
	//把三个约束放进组装里完成装配

	status = ProAsmcompConstraintsSet(NULL, &asmcomp, constraints);
	ProSolidRegenerate((ProSolid)asmcomp.owner, PRO_REGEN_CAN_FIX);
	if (interact_flag)
	{
		ProAsmcompConstrRedefUI(&asmcomp);
	}
	return (PRO_TK_NO_ERROR);
}


测试函数

//选择装配,从当前内存或路径通过模型名称选择一个模型,调用装配函数
void  UserAssembleTest() {


// 在当前内存中或路径下找到 对应名称的模型的句柄
		ProError status;
		ProMdlType model_type = PRO_MDL_PART;
		ProMdl* session_mdls, find_mdl;
		int mdls_counts;
		ProName target_name;
		ProAssembly assembly;
		ProMdlCurrentGet((ProMdl*)&assembly);
		ProStringToWstring(target_name, "PRT0002");
		ProBoolean was_found = PRO_B_FALSE;
		//在当前内存中查找匹配的模型,工作区,找不到试试设置工作区
		status = ProSessionMdlList(model_type, &session_mdls, &mdls_counts);
		if (status == PRO_TK_NO_ERROR)
		{
			ProName mdl_name;
			for (int i = 0; i < mdls_counts; i++)
			{
				status = ProMdlNameGet(session_mdls[i], mdl_name);
				AfxMessageBox(mdl_name);
				int compare_result = 1;
				ProWstringCompare(target_name, mdl_name, PRO_VALUE_UNUSED,
					&compare_result);
				if (compare_result == 0)
				{
					//找到调用装配
					find_mdl = &session_mdls[i];
					UserAssembleByDatums(assembly, (ProSolid)session_mdls[i]);
				//	ProMdlDisplay(session_mdls[i]);
					was_found = PRO_B_TRUE;

				
				}
			}
			ProArrayFree((ProArray*)&session_mdls);

		}
		// if not found, try to retrieve it from disk
		if (was_found == PRO_B_FALSE)
		{
			status = ProMdlnameRetrieve(target_name, (ProMdlfileType)model_type, &find_mdl);
			if (status == PRO_TK_NO_ERROR)
			{
				//	ProMdlDisplay(find_mdl);
		
			}
		}

	}

猜你喜欢

转载自blog.csdn.net/qq_43886548/article/details/127746848