Creo二次开发:坐标系自动装配

时间:2021-11-21 05:41:59

 在瓶底可以点选放置一个时间标签(标签可以是零件)

代码如下:

//创建坐标系UDF
void putcsys(ProMdl mdl,double x,double y,double z)
{
    ProError err=PRO_TK_NO_ERROR;
    ProUdfdata data;
    err=ProUdfdataAlloc(&data);
    ProPath path=L"udf_def_csys.gph";
    err=ProUdfdataPathSet(data,path);
    ProSelection ref_item_csy;
    ProModelitem mdl_itm_top;
    err=ProModelitemByNameInit(mdl,PRO_CSYS,_T("ASM_DEF_CSYS"),&mdl_itm_top);
    err=ProSelectionAlloc(NULL,&mdl_itm_top,&ref_item_csy);
    ProUdfreference reference_csy;
    err=ProUdfreferenceAlloc(_T("csys"),ref_item_csy,PRO_B_FALSE,&reference_csy);
    err=ProUdfdataReferenceAdd(data,reference_csy);

    ProUdfvardim vardim_x;
    err=ProUdfvardimAlloc(_T("d9"),x,PROUDFVARTYPE_DIM,&vardim_x);
    err=ProUdfdataUdfvardimAdd(data,vardim_x);

    ProUdfvardim vardim_y;
    err=ProUdfvardimAlloc(_T("d10"),y,PROUDFVARTYPE_DIM,&vardim_y);
    err=ProUdfdataUdfvardimAdd(data,vardim_y);

    ProUdfvardim vardim_z;
    err=ProUdfvardimAlloc(_T("d11"),z,PROUDFVARTYPE_DIM,&vardim_z);
    err=ProUdfdataUdfvardimAdd(data,vardim_z);

    err=ProUdfdataScaleSet(data,PROUDFSCALETYPE_USER,2.0);
    err=ProUdfdataDimdisplaySet(data,PROUDFDIMDISP_NORMAL);
    err=ProUdfdataDependencySet(data,PROUDFDEPENDENCY_INDEPENDENT);

    ProUdfCreateOption op={PROUDFOPT_FIX_MODEL_UI_OFF};
    ProGroup udf;
    err=ProUdfCreate((ProSolid)mdl,data,NULL,&op,1,&udf);
    if(err!=PRO_TK_NO_ERROR)
    {
        AfxMessageBox(_T("Udf创建失败"));
    }
    ProUdfdataFree(data);
    ProUdfvardimFree(vardim_x);
    ProUdfvardimFree(vardim_y);
    ProUdfvardimFree(vardim_z);

    ProUdfreferenceFree(reference_csy);
    ProSelectionFree(&ref_item_csy);

    int wid=0;
    err=ProWindowCurrentGet(&wid);
    err=ProWindowRefresh(wid);
}
//通过坐标系自动装配
ProError UserAssembleByCsys(ProAssembly asm_mdl,ProSolid comp_mdl)
{    
    ProErr  err;
    ProName asm_csys_name, comp_csys_name;
    ProModelitem asm_csys, comp_csys;
    ProSelection asm_sel, comp_sel;
    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;

    ProAsmcomppath comp_path;
    ProIdTable c_id_table;
    c_id_table[0] = -1;

    //装配体中的装配参照
    ProStringToWstring(asm_csys_name,"ACS0");
    //元件的装配参照
    ProStringToWstring(comp_csys_name, "PRT_CSYS_DEF");

    //将零件添加到组件的初始位置
    err=ProAsmcompAssemble(asm_mdl,comp_mdl, identity_matrix, &asmcomp);
    //初始化数组constraints
    err=ProArrayAlloc(0,sizeof(ProAsmcompconstraint), 1, (ProArray*)&constraints);

    //得到组件基准或者组件中用于定位的元件基准
    err = ProModelitemByNameInit(asm_mdl, PRO_CSYS,asm_csys_name,&asm_csys );
    
    //得到元件基准
    err = ProModelitemByNameInit(comp_mdl,PRO_CSYS,comp_csys_name,&comp_csys);

    //选中组件中的参照,注意此处的comp_path
    err=ProAsmcomppathInit(asm_mdl, c_id_table, 0, &comp_path);
    err=ProSelectionAlloc(&comp_path, &asm_csys, &asm_sel);

    //选中元件中的 参照
    err=ProSelectionAlloc(NULL, &comp_csys, &comp_sel);

    //设定参照组, 包含有参照类型以及各自的约束对象
    err=ProAsmcompconstraintAlloc(&constraint);
    err=ProAsmcompconstraintTypeSet(constraint, PRO_ASM_CSYS);
    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_NO_FLAGS);
    return PRO_TK_NO_ERROR;
}
void CPart3DDlg::OnBnClickedBtnTabAdd()
{
    // TODO: 在此添加控件通知处理程序代
    ProMdl asm_mdl=NULL;
    ProMdl comp_mdl=NULL;
    ProError err=ProMdlCurrentGet(&asm_mdl);
    ProPoint3d position;
    int pid;
    ProSelection* sels=NULL;
    int n_sels;

    err=ProWindowCurrentGet(&pid);
    err=ProSelect("prt_or_asm",1,NULL,NULL,NULL,NULL,&sels,&n_sels);
    if (err!=PRO_TK_NO_ERROR)
    {
        return;
    }
    ProSelectionPoint3dGet(sels[0],position);
    //放置坐标系udf
    putcsys((ProSolid)asm_mdl,position[0],position[1],position[2]);
    //读出要添加的零件模型
    ProMdlRetrieve(L"comp.prt",PRO_MDL_PART,&comp_mdl);
    //自动装配
    UserAssembleByCsys((ProAssembly)asm_mdl,(ProSolid)comp_mdl);
    
    ProWindowActivate(pid);
}

运行结果

Creo二次开发:坐标系自动装配

Creo二次开发:坐标系自动装配