具有如下功能:
1)在列表中列出装配体下所有的零件,选择多个需要生成工程图的零件
2)然后对选中的零件生成工程图3)在每一个工程图中添加表,列出工程图对应模型的参数信息(名称,类型,值等)
代码如下:
//动作函数
ProError UserFeatVisitAct(ProFeature* p_feature,ProError status,ProAppData data)
{
ProArrayObjectAdd((ProArray*)data,PRO_VALUE_UNUSED,1,p_feature);
return PRO_TK_NO_ERROR;
}
void CDrawingGenerate::UserLoadAllPartInAsm(ProMdl mdl)
{
ProError status;
ProArray array;
status=ProArrayAlloc(0,sizeof(ProFeature),1,&array);
if (PRO_TK_NO_ERROR!=status)
{
AfxMessageBox(_T("内存错误!"));
return;
}
//遍历特征
status=ProSolidFeatVisit((ProSolid)mdl,(ProFeatureVisitAction)UserFeatVisitAct,NULL,(ProAppData)&array);
if (PRO_TK_NO_ERROR!=status)
{
AfxMessageBox(_T("遍历失败!"));
return;
}
int array_size;
status=ProArraySizeGet(array,&array_size);
if (0==array_size)
{
AfxMessageBox(_T("no feature was found!"));
return;
}
//循环筛选特征
for (int i=0;i
ProFeature feature;
feature=((ProFeature*)array)[i];
int num=feature.id;
ProMdl mdl_prt;
ProName name;
ProFeattype p_type;
status=ProFeatureTypeGet(&feature,&p_type);
//1000--PRO_MDL_COMPONENT,筛选出PART即可
if (1000==p_type)
{
status=ProAsmcompMdlGet((ProAsmcomp*)&feature,&mdl_prt);
status=ProMdlNameGet(mdl_prt,name);
CString m_strID;
m_strID.Format(_T("%d"),num);
m_PartList.InsertItem(0,name);
m_PartList.SetItemText(0,1,m_strID);
UserLoadAllPartInAsm(mdl_prt);
}
}
ProArrayFree(&array);
}
void CDrawingGenerate::OnBnClickedBtnOpenAssembly()
{
// TODO: 在此添加控件通知处理程序代码
ProError err;
ProMdl mdl;
ProFamilyName name;
ProStringToWstring(name,"D:\\demo\\parts\\bottle.asm");
err=ProMdlRetrieve(name,PRO_MDL_ASSEMBLY,&mdl);
if (PRO_TK_NO_ERROR!=err)
{
return;
}
ProMdlDisplay(mdl);
UserLoadAllPartInAsm(mdl);
int pid;
ProWindowCurrentGet(&pid);
ProWindowActivate(pid);
}
//遍历参数的动作函数
ProError UserParamVisitAct(ProParameter *param,ProError status,ProAppData data)
{
vector
pf->push_back(*param);
return PRO_TK_NO_ERROR;
}
//向表格添加数据
ProError TableTextAdd(ProDwgtable* table,int col,int row,char* text)
{
ProError status;
ProWstring* lines;
ProLine first_line;
//准备要填写的内容
status=ProArrayAlloc(1,sizeof(ProWstring),1,(ProArray*)&lines);
ProStringToWstring(first_line,text);
lines[0]=(ProWstring)first_line;
//将内容填入表格
status=ProDwgtableTextEnter(table,col,row,lines);
status=ProArrayFree((ProArray*)&lines);
return status;
}
int CDrawingGenerate::UserDrawingAllCreate()
{
ProError status;
ProName name;
ProStringToWstring(name,"D:\\demo\\parts");
ProPath *file_list,*dir_list;
status=ProArrayAlloc(0,sizeof(ProPath),1,(ProArray*)&file_list);
status=ProArrayAlloc(0,sizeof(ProPath),1,(ProArray*)&dir_list);
status=ProFilesList(name,L"*.prt",PRO_FILE_LIST_LATEST,&file_list,&dir_list);
if (PRO_TK_NO_ERROR!=status)
{
return status;
}
ProMdl mdl;
int size=0;
ProArraySizeGet((ProArray)file_list,&size);
for (int i=0;i
status=ProMdlLoad(file_list[i],PRO_MDL_PART,PRO_B_FALSE,&mdl);
ProMdlDisplay(mdl);
//遍历参数
ProParamvalue proval;
ProModelitem item;
status=ProMdlToModelitem(mdl,&item);
vector
status=ProParameterVisit(&item,NULL,(ProParameterAction)UserParamVisitAct,&dims);
if (PRO_TK_NO_ERROR!=status)
{
return status;
}
//生成工程图
ProMdlType mdl_type;
ProMdldata data;
ProModel model;
ProFileName msgfile;
ProName predefined_template,name;
ProDrawing created_drawing=NULL;
ProDwgcreateOptions options=(ProDwgcreateOptions)0;
ProDwgcreateErrs errors;
ProStringToWstring(msgfile,"msg_drawing.txt");
status=ProMdlTypeGet(mdl,&mdl_type);
if (PRO_TK_NO_ERROR!=status||PRO_MDL_PART!=mdl_type)
{
return PRO_TK_INVALID_TYPE;
}
status=ProMdlDataGet(mdl,&data);
wcscpy_s(model.name,data.name);
wcscpy_s(model.type,data.type);
ProStringToWstring(predefined_template,"c_drawing");
status=ProMessageDisplay(msgfile,"User enter new drawing name:");
status=ProMessageStringRead(PRO_NAME_SIZE,name);
if (PRO_TK_NO_ERROR!=status)
{
return status;
}
status=ProDrawingFromTmpltCreate(name,predefined_template,&model,options,&created_drawing,&errors);
if (PRO_TK_NO_ERROR!=status/*||PRO_TK_DWGCREATE_ERRORS!=status*/)
{
return status;
}
//创建表格
ProDwgtabledata tdata;
ProDwgtable table;
double init_heights[]={1,1,0};
double widths[]={15,15,15};
ProHorzJust adjusts[]={PROHORZJUST_CENTER,PROHORZJUST_CENTER,PROHORZJUST_CENTER};
double pos[]={400.0,200.0,0.0};
int n_rows;
ProCharLine char_name,char_value,char_type;
status=ProDwgtabledataAlloc(&tdata);
//设置表格起点
status=ProDwgtabledataOriginSet(tdata,pos);
status=ProDwgtabledataSizetypeSet(tdata,PRODWGTABLESIZE_CHARACTERS);
//设置表格列属性
status=ProDwgtabledataColumnsSet(tdata,3,widths,adjusts);
//设置表格行属性
status=ProDwgtabledataRowsSet(tdata,1,init_heights);
//创建表格
status=ProDrawingTableCreate(created_drawing,tdata,PRO_B_FALSE,&table);
//向表格添加数据
status=TableTextAdd(&table,1,1,"Param Name");
status=TableTextAdd(&table,2,1,"Value");
status=TableTextAdd(&table,3,1,"Type");
for (unsigned int j=0;j
status=ProParameterValueGet(&dims[j],&proval);
status=ProDwgtableRowsCount(&table,&n_rows);
//在表格的末尾添加一行
status=ProDwgtableRowAdd(&table,n_rows,PRO_B_FALSE,1);
//在新行中添加参数名称、数值和类型
ProWstringToString(char_name,dims[j].id);
status=TableTextAdd(&table,1,n_rows+1,char_name);
switch(proval.type)
{
case PRO_PARAM_DOUBLE:
sprintf_s(char_value,"%f",proval.value.d_val);
break;
case PRO_PARAM_INTEGER:
sprintf_s(char_value,"%d",proval.value.i_val);
break;
case PRO_PARAM_STRING:
sprintf_s(char_value,"%ws",proval.value.s_val);
default:
break;
}
status=TableTextAdd(&table,2,n_rows+1,char_value);
sprintf_s(char_type,"%d",proval.type);
status=TableTextAdd(&table,3,n_rows+1,char_type);
}
//更新表格显示
status=ProDrawingTablesUpdate(created_drawing);
}
status=ProArrayFree((ProArray*)&file_list);
status=ProArrayFree((ProArray*)&dir_list);
return PRO_TK_NO_ERROR;
}
void CDrawingGenerate::OnBnClickedBtnDrawingCreate()
{
// TODO: 在此添加控件通知处理程序代码
UserDrawingAllCreate();
}
运行结果: