类似之前的注释的查找和替换,主要是对符号的遍历,使用函数ProDrawingDtlsyminstVisit,代码如下:
ProError UserSymbolVisitAct(ProDtlitem *symitem,ProError status,ProAppData data)
{
vector<ProDtlitem> *pf=(vector<ProDtlitem>*)data;
pf->push_back(*symitem);
return PRO_TK_NO_ERROR;
}
void CSearchAndReplace::OnBnClickedBtnSearchSymbol()
{
// TODO: 在此添加控件通知处理程序代码
//获取用户编辑框中输入的内容
UpdateData(TRUE);
ProError status;
ProName name;
ProStringToWstring(name,"D:\\demo");
ProPath *file_list,*dir_list;
status=ProArrayAlloc(0,sizeof(ProPath),1,(ProArray*)&file_list);
status=ProArrayAlloc(0,sizeof(ProPath),1,(ProArray*)&dir_list);
//将指定路径下的文件存入file_list中
status=ProFilesList(name,L"*.drw",PRO_FILE_LIST_LATEST,&file_list,&dir_list);
int size=0;
status=ProArraySizeGet((ProArray)file_list,&size);
int num=0;
//清空列表框
m_listBox.ResetContent();
ProMdl mdl;
for (int i=0;i<size;i++)
{
//加载模型
status=ProMdlLoad(file_list[i],PRO_MDL_DRAWING,PRO_B_FALSE,&mdl);
//显示
status=ProMdlDisplay(mdl);
vector<ProDtlitem> syms;
//遍历当前工程图中的所有符号
status=ProDrawingDtlsyminstVisit((ProDrawing)mdl,1,(ProDtlitemVisitAction)UserSymbolVisitAct,NULL,(ProAppData)&syms);
ProDtlsyminstdata sym_data;
ProDtlsymdef sym_def;
ProDtlsymdefdata def_data;
ProName inst_name,mdl_name;
for (unsigned int j=0;j<syms.size();j++)
{
//获取符号实例的数据
status=ProDtlsyminstDataGet((ProDtlsyminst*)&syms[j],PRODISPMODE_NUMERIC,&sym_data/*输出项*/);
//获得符号定义
status=ProDtlsyminstdataDefGet(sym_data,&sym_def/*输出项*/);
//获得符号定义的数据
status=ProDtlsymdefDataGet(&sym_def,&def_data/*输出项*/);
//获得符号定义的名称
status=ProDtlsymdefdataNameGet(def_data,inst_name/*输出项*/);
CString strName,strMdlName;
strName.Format(_T("%ws"),inst_name);
if (m_strSymbolName==strName)
{
//AfxMessageBox(_T("找到符号!"));
num++;
status=ProMdlNameGet(mdl,mdl_name);
strMdlName.Format(_T("%ws"),mdl_name);
m_listBox.AddString(strMdlName);
}
}
}
if (0==num)
{
AfxMessageBox(_T("找不到该符号!"));
}
status=ProArrayFree((ProArray*)&file_list);
status=ProArrayFree((ProArray*)&dir_list);
}
运行结果:
2、符号替换
//创建符号
int UserSyminstCreate(ProDrawing drawing,ProName sym_name,ProDtlattach attach,ProVector pos)
{
ProError status;
ProDtlsymdef symdef;
ProDtlsyminstdata sym_data;
ProDtlsyminst sym_inst;
ProDtlvartext vartext;
ProPath sym_def_path;
ProLine var_text_prompt;
ProLine var_text_replace;
//符号存储的路径
ProStringToWstring(sym_def_path,"C:\\Users\\Public\\Documents\\symbol");
ProStringToWstring(var_text_prompt,"CAP");
ProStringToWstring(var_text_replace,"DCG");
//加载需要创建的符号
status=ProDrawingDtlsymdefRetrieve(drawing,sym_def_path,sym_name,PRO_VALUE_UNUSED,PRO_B_TRUE,&symdef);
//分配data
status=ProDtlsyminstdataAlloc(drawing,&sym_data);
status=ProDtlsyminstdataDefSet(sym_data,&symdef);
//分配attach
status=ProDtlattachAlloc(PRO_DTLATTACHTYPE_FREE,NULL,pos,NULL,&attach);
status=ProDtlsyminstdataAttachtypeSet(sym_data,PROSYMDEFATTACHTYPE_FREE);
status=ProDtlsyminstdataAttachmentSet(sym_data,attach);
//分配text
status=ProDtlvartextAlloc(var_text_prompt,var_text_replace,&vartext);
status=ProDtlsyminstdataVartextAdd(sym_data,vartext);
//创建该符号
status=ProDtlsyminstCreate(drawing,sym_data,&sym_inst);
status=ProDtlsyminstShow(&sym_inst);
ProDtlattachFree(attach);
ProDtlvartextFree(vartext);
ProDtlsyminstdataFree(sym_data);
return PRO_TK_NO_ERROR;
}
void CSearchAndReplace::OnBnClickedBtnReplaceSymbol()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
ProError status;
ProName name;
ProStringToWstring(name,"D:\\demo");
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"*.drw",PRO_FILE_LIST_LATEST,&file_list,&dir_list);
int size=0;
status=ProArraySizeGet((ProArray)file_list,&size);
ProName string;
//将编辑框的值(替换符号名称)赋给string
wcscpy_s(string,m_strReplaceSymbol);
ProMdl mdl;
for (int i=0;i<size;i++)
{
status=ProMdlLoad(file_list[i],PRO_MDL_DRAWING,PRO_B_FALSE,&mdl);
status=ProMdlDisplay(mdl);
int window_id;
status=ProWindowCurrentGet(&window_id);
vector<ProDtlitem> syms;
status=ProDrawingDtlsyminstVisit((ProDrawing)mdl,1,(ProDtlitemVisitAction)UserSymbolVisitAct,NULL,(ProAppData)&syms);
ProDtlsyminstdata sym_data;
ProDtlsymdef sym_def;
ProDtlsymdefdata def_data;
ProName inst_name;
ProDtlattach attach;
ProDtlattachType type;
ProView view;
ProVector location;
ProSelection attach_point;
for (unsigned int j=0;j<syms.size();j++)
{
status=ProDtlsyminstDataGet((ProDtlsyminst*)&syms[j],PRODISPMODE_NUMERIC,&sym_data);
status=ProDtlsyminstdataDefGet(sym_data,&sym_def);
status=ProDtlsymdefDataGet(&sym_def,&def_data);
status=ProDtlsymdefdataNameGet(def_data,inst_name);
CString strName;
strName.Format(_T("%ws"),inst_name);
if (m_strSymbolName==strName)
{
//获得当前符号(即将被替换的符号)的依附项
status=ProDtlsyminstdataAttachmentGet(sym_data,&attach/*输出项*/);
//获得详细的依附项数据
status=ProDtlattachGet(attach,&type,&view,location,&attach_point/*后四个参数均为输出项*/);
//将替换符号的名称,被替换符号的依附和位置作为输入参数,完成在被替换符号处创建替换符号
UserSyminstCreate((ProDrawing)mdl,string,attach,location);
//删除被替换符号定义,完成替换
status=ProDtlsymdefDelete(&sym_def);
}
}
//status=ProWindowRefresh(window_id);
status=ProSolidRegenerate((ProSolid)mdl,PRO_REGEN_FORCE_REGEN);
}
status=ProArrayFree((ProArray*)&file_list);
status=ProArrayFree((ProArray*)&dir_list);
}
运行结果:
替换前
替换后: