知识点
Pro/TOOLKIT的组装流程如下:
1. ProAsmcompAssemble
将元件模型组装到组立档案中,此时不包含任何约束。
ProError ProAsmcompAssemble(
ProAssembly owner_assembly, // 组立档案
ProSolid component_model, // 待组装的元件模型
ProMatrix init_pos, // 初始位置,一般传4阶单位矩阵
ProAsmcomp *feature); // 输出:元件特征
2. 定义组装的约束方式
Pro/TOOLKIT使用类型ProAsmcompconstraint来表示一个约束。
2.1. ProAsmcompconstraintAlloc:定义约束
ProError ProAsmcompconstraintAlloc(ProAsmcompconstraint *p_constraint);
2.2. ProAsmcompconstraintTypeSet:设置约束类型
ProError ProAsmcompconstraintTypeSet(ProAsmcompconstraint constraint,
ProAsmcompConstrType type);
约束类型有:配对、对齐、插入、相切、坐标系等等。
2.3. 设置约束的组立端参考和元件端参考
// 设置约束的组立端参考
ProError ProAsmcompconstraintAsmreferenceSet(
ProAsmcompconstraint constraint, // 约束
ProSelection asm_ref, // 组立端参考
ProDatumside asm_orient); // 约束方向,一般传PRO_DATUM_SIDE_YELLOW表示正向
// 设置约束的元件端参考
ProError ProAsmcompconstraintCompreferenceSet(
ProAsmcompconstraint constraint, // 约束
ProSelection comp_ref, // 元件端参考
ProDatumside comp_orient); // 约束方向,一般传PRO_DATUM_SIDE_YELLOW表示正向
组立端参考(ProSelection asm_ref)和元件端参考(ProSelection comp_ref)可以通过两种方式得到:
- 1.调用ProSelect函数让用户选取。
- 2.调用ProSelectionAlloc函数由程序创建。
很显然,如果要实现自动装配,则只能使用方法(2)。
ProError ProSelectionAlloc(
ProAsmcomppath *p_cmp_path, // 参考所在元件的路径
ProModelitem *p_mdl_itm, // 参考的模型项
ProSelection *p_selection); // 输出
类型ProAsmcomppath表示元件的路径信息,具体含义会在下一节详细阐述。
我们先举例来说明参数ProAsmcomppath p_cmp_path在此处的作用:
如上图所示:TEST.ASM下有两个A.PRT的元件特征,虽然它们所关联的模型相同,但是在TEST.ASM中组装的位置显然不同。现在要把B.PRT组装到TEST.ASM下,需要参考A.PRT中的基准面TOP,那怎样区分是用哪个A.PRT元件特征下的TOP呢?
可以用元件的特征标识(ID)来区分,如上图,元件特征A.PRT的ID分别为47和48。
ProAsmcomppath记录的就是从顶层组立开始的元件特征的ID路径,用以唯一确定组立档下不同的元件特征。
3. ProAsmcompConstraintsSet
将约束添加到元件特征。
ProError ProAsmcompConstraintsSet(
ProAsmcomppath* component_path, // 元件路径,一般传NULL即可
ProAsmcomp *component, // 元件特征
ProAsmcompconstraint *p_constraints); // 约束数组
ProAsmcomppath
ProAsmcomppath表示组立档下元件特征的ID路径,定义如下:
typedef struct pro_comp_path
{
ProSolid owner; // 组立档
ProIdTable comp_id_table; // int类型数组,存储ID路径。
int table_num; // comp_id_table中ID的数量
} ProAsmcomppath;
它可以唯一确定组立档下的元件特征。
如下图所示:
组立档ASM0001.ASM下有两个同名的元件特征PRT0001.PRT,其对应的ID路径分别为:
table_num = 3;
comp_id_table[0] = 39;
comp_id_table[1] = 1;
comp_id_table[2] = 5;
table_num = 2;
comp_id_table[0] = 40;
comp_id_table[1] = 2;
函数ProAsmcomppathInit用于初始化ProAsmcomppath。
ProError ProAsmcomppathInit(
ProSolid p_solid_handle,
ProIdTable memb_id_tab,
int table_size,
ProAsmcomppath *p_handle);
测试组装01代码
ProError UserAssembleByDatums(ProAssembly asm_model,
ProSolid comp_model)
{
ProError err;
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;
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;
// 参考特征名称
ProName asm_datums[3];
ProName comp_datums[3];
ProStringToWstring(asm_datums[0], "ASM_FRONT");
ProStringToWstring(asm_datums[1], "ASM_TOP");
ProStringToWstring(asm_datums[2], "ASM_RIGHT");
ProStringToWstring(comp_datums[0], "FRONT");
ProStringToWstring(comp_datums[1], "TOP");
ProStringToWstring(comp_datums[2], "RIGHT");
// 组装
ProAsmcompAssemble(asm_model, comp_model, identity_matrix, &asmcomp);
// 添加约束
ProArrayAlloc(0, sizeof(ProAsmcompconstraint), 1, (ProArray*)&constraints);
for (int i = 0; i < 3; i++)
{
err = ProModelitemByNameInit(asm_model, PRO_SURFACE, asm_datums[i], &asm_datum);
if (err != PRO_TK_NO_ERROR)
{
interact_flag = PRO_B_TRUE;
continue;
}
err = ProModelitemByNameInit(comp_model, PRO_SURFACE, comp_datums[i], &comp_datum);
if (err != PRO_TK_NO_ERROR)
{
interact_flag = PRO_B_TRUE;
continue;
}
// 元件路径
err = ProAsmcomppathInit(asm_model, c_id_table, 0, &comp_path);
// 参考
err = ProSelectionAlloc(&comp_path, &asm_datum, &asm_sel);
err = ProSelectionAlloc(NULL, &comp_datum, &comp_sel);
// 约束
err = ProAsmcompconstraintAlloc(&constraint);
err = ProAsmcompconstraintTypeSet(constraint, PRO_ASM_ALIGN);
err = ProAsmcompconstraintAsmreferenceSet(constraint, asm_sel, PRO_DATUM_SIDE_YELLOW);
err = ProAsmcompconstraintCompreferenceSet(constraint, comp_sel, PRO_DATUM_SIDE_YELLOW);
err = ProArrayObjectAdd((ProArray*)&constraints, -1, 1, &constraint);
}
// 设置元件特征的约束
err = ProAsmcompConstraintsSet(NULL, &asmcomp, constraints);
// 重生
ProSolidRegenerate((ProSolid)asmcomp.owner, PRO_REGEN_CAN_FIX);
if (interact_flag)
{
ProAsmcompConstrRedefUI(&asmcomp);
}
// 释放数组空间
ProArrayFree((ProArray*)&constraints);
return PRO_TK_NO_ERROR;
}
// 测试组装
int TestAssembly001(uiCmdCmdId command,
uiCmdValue *p_value,
void *p_push_command_data)
{
ProError err;
// 获取组立档案:ASM0001.ASM
ProMdl mdlAsm;
err = ProMdlInit(L"ASM0001", PRO_MDL_ASSEMBLY, &mdlAsm);
if (PRO_TK_E_NOT_FOUND == err)
{
MessageBoxW(NULL, L"未打开ASM0001.ASM", L"www.icaxdev.com", MB_OK);
return -1;
}
// 获取要组装的档案:PRT0001.PRT
ProMdl mdlComp;
err = ProMdlInit(L"PRT0001", PRO_MDL_PART, &mdlComp);
if (PRO_TK_E_NOT_FOUND == err)
{
MessageBoxW(NULL, L"未打开PRT0001.PRT", L"www.icaxdev.com", MB_OK);
return -1;
}
// 进行组装
UserAssembleByDatums((ProAssembly)mdlAsm, (ProSolid)mdlComp);
return 0;
}
测试组装02代码
ProError UserAssembly002(ProAssembly asm_model,
ProSolid comp_model_a,
ProSolid comp_model_b)
{
ProError err;
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 } };
ProAsmcomppath comp_path;
ProIdTable c_id_table;
c_id_table[0] = -1;
//--------------------------------------------------------
// 将A.PRT组装到TEST.ASM下
// 通过坐标系组装:(1)坐标系:ACS0(TEST.ASM) <> 坐标系:PRT_CSYS_DEF(A.PRT)
// (2)坐标系:ACS1(TEST.ASM) <> 坐标系:PRT_CSYS_DEF(A.PRT)
// TEST.ASM
// |__A.PRT
// |__A.PRT
//--------------------------------------------------------
ProAsmcomp asmcomp_a[2];
ProName asmCsysName[2];
ProStringToWstring(asmCsysName[0], "ACS0");
ProStringToWstring(asmCsysName[1], "ACS1");
for (int i = 0; i < 2; ++i)
{
ProModelitem asmCsys;
err = ProModelitemByNameInit(asm_model, PRO_CSYS, asmCsysName[i], &asmCsys);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
ProModelitem compCsys;
err = ProModelitemByNameInit(comp_model_a, PRO_CSYS, L"PRT_CSYS_DEF", &compCsys);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
// 元件路径
err = ProAsmcomppathInit(asm_model, c_id_table, 0, &comp_path);
// 参考
ProSelection asmCsysSel;
err = ProSelectionAlloc(&comp_path, &asmCsys, &asmCsysSel);
ProSelection compCsysSel;
err = ProSelectionAlloc(NULL, &compCsys, &compCsysSel);
// 约束
ProAsmcompconstraint* constraints;
err = ProArrayAlloc(0, sizeof(ProAsmcompconstraint), 1, (ProArray*)&constraints);
ProAsmcompconstraint constraint;
err = ProAsmcompconstraintAlloc(&constraint);
err = ProAsmcompconstraintTypeSet(constraint, PRO_ASM_CSYS);
err = ProAsmcompconstraintAsmreferenceSet(constraint, asmCsysSel, PRO_DATUM_SIDE_YELLOW);
err = ProAsmcompconstraintCompreferenceSet(constraint, compCsysSel, PRO_DATUM_SIDE_YELLOW);
err = ProArrayObjectAdd((ProArray*)&constraints, -1, 1, &constraint);
// 组装
err = ProAsmcompAssemble(asm_model, comp_model_a, identity_matrix, &asmcomp_a[i]);
err = ProAsmcompConstraintsSet(NULL, &asmcomp_a[i], constraints);
err = ProSolidRegenerate((ProSolid)asmcomp_a[i].owner, PRO_REGEN_NO_FLAGS);
// 释放数组空间
ProArrayFree((ProArray*)&constraints);
}
//--------------------------------------------------------
// 将B.PRT组装到TEST.ASM下
// 参考第一步中组装的第一个A.PRT的轴和面
// TEST.ASM
// |__A.PRT
// |__A.PRT
// |__B.PRT
//--------------------------------------------------------
ProModelitem compAxis_a;
err = ProModelitemByNameInit(comp_model_a, PRO_AXIS, L"A_2", &compAxis_a);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
ProModelitem compAxis_b;
err = ProModelitemByNameInit(comp_model_b, PRO_AXIS, L"A_1", &compAxis_b);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
ProModelitem compDatum_a;
err = ProModelitemByNameInit(comp_model_a, PRO_SURFACE, L"TOP", &compDatum_a);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
ProModelitem compDatum_b;
err = ProModelitemByNameInit(comp_model_b, PRO_SURFACE, L"TOP", &compDatum_b);
if (err != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}
// 元件路径
c_id_table[0] = asmcomp_a[0].id;
err = ProAsmcomppathInit(asm_model, c_id_table, 1, &comp_path);
// 参考
ProSelection asmAxisSel;
err = ProSelectionAlloc(&comp_path, &compAxis_a, &asmAxisSel);
ProSelection compAxisSel;
err = ProSelectionAlloc(NULL, &compAxis_b, &compAxisSel);
ProSelection asmDatumSel;
err = ProSelectionAlloc(&comp_path, &compDatum_a, &asmDatumSel);
ProSelection compDatumSel;
err = ProSelectionAlloc(NULL, &compDatum_b, &compDatumSel);
// 约束
ProAsmcompconstraint* constraints;
err = ProArrayAlloc(0, sizeof(ProAsmcompconstraint), 1, (ProArray*)&constraints);
ProAsmcompconstraint constraintAxis;
err = ProAsmcompconstraintAlloc(&constraintAxis);
err = ProAsmcompconstraintTypeSet(constraintAxis, PRO_ASM_ALIGN);
err = ProAsmcompconstraintAsmreferenceSet(constraintAxis, asmAxisSel, PRO_DATUM_SIDE_YELLOW);
err = ProAsmcompconstraintCompreferenceSet(constraintAxis, compAxisSel, PRO_DATUM_SIDE_YELLOW);
err = ProArrayObjectAdd((ProArray*)&constraints, -1, 1, &constraintAxis);
ProAsmcompconstraint constraintDatum;
err = ProAsmcompconstraintAlloc(&constraintDatum);
err = ProAsmcompconstraintTypeSet(constraintDatum, PRO_ASM_ALIGN);
err = ProAsmcompconstraintAsmreferenceSet(constraintDatum, asmDatumSel, PRO_DATUM_SIDE_YELLOW);
err = ProAsmcompconstraintCompreferenceSet(constraintDatum, compDatumSel, PRO_DATUM_SIDE_YELLOW);
err = ProArrayObjectAdd((ProArray*)&constraints, -1, 1, &constraintDatum);
// 组装
ProAsmcomp asmcomp_b;
err = ProAsmcompAssemble(asm_model, comp_model_b, identity_matrix, &asmcomp_b);
err = ProAsmcompConstraintsSet(NULL, &asmcomp_b, constraints);
err = ProSolidRegenerate((ProSolid)asmcomp_b.owner, PRO_REGEN_NO_FLAGS);
// 释放数组空间
ProArrayFree((ProArray*)&constraints);
return PRO_TK_NO_ERROR;
}
// 测试组装
int TestAssembly002(uiCmdCmdId command,
uiCmdValue *p_value,
void *p_push_command_data)
{
ProError err;
// 获取组立档案:TEST.ASM
ProMdl mdlAsm;
err = ProMdlInit(L"TEST", PRO_MDL_ASSEMBLY, &mdlAsm);
if (PRO_TK_E_NOT_FOUND == err)
{
MessageBoxW(NULL, L"未打开TEST.ASM", L"www.icaxdev.com", MB_OK);
return -1;
}
// 获取零件档案:A.PRT
ProMdl mdlCompA;
err = ProMdlInit(L"A", PRO_MDL_PART, &mdlCompA);
if (PRO_TK_E_NOT_FOUND == err)
{
MessageBoxW(NULL, L"未打开A.PRT", L"www.icaxdev.com", MB_OK);
return -1;
}
// 获取零件档案:B.PRT
ProMdl mdlCompB;
err = ProMdlInit(L"B", PRO_MDL_PART, &mdlCompB);
if (PRO_TK_E_NOT_FOUND == err)
{
MessageBoxW(NULL, L"未打开B.PRT", L"www.icaxdev.com", MB_OK);
return -1;
}
// 组装
UserAssembly002((ProAssembly)mdlAsm, (ProSolid)mdlCompA, (ProSolid)mdlCompB);
return 0;
}