最近有个项目需要使用arx开发一个插件,把dwg打印为pdf。由于之前使用zrx开发了一个类似的功能,于是就把工程配置改了下,使得工程可以生成arx。但经过测试发现,在zwcad上可以把dwg的图块批量打印为pdf,但在autocad上却不行,只能打印出第一个,后面的都不行。
经过多天的排查与资料查找,都没有找到原因。最后请教了下老大,才发现autocad的打印需要把环境变量BACKGROUNDPLOT设置为0,即前台打印,这才解决。
最后附下打印的代码,希望可以帮助到更多的人。
void PlotDwgToPdf(PlotSettingInfo& plotSettingInfo)
{
// 设置系统变量BACKGROUNDPLOT
SystemVar backgroudplot(_T("BACKGROUNDPLOT"), 0);
// 获取原视图
BOOL bNeedRestoreView = FALSE;
AcDbViewTableRecord viewOld;
GetCurrentView(viewOld);
AcPlPlotEngine* pEngine = NULL;
if (Acad::eOk == AcPlPlotFactory::createPublishEngine(pEngine))
{
std::set<CString> setPlotFile;
// Here is the progress dialog for the current plot process...
AcPlPlotProgressDialog* pPlotProgDlg = acplCreatePlotProgressDialog(acedGetAcadFrame()->m_hWnd, false, 1);
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kDialogTitle, L"Plot API Progress");
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kCancelJobBtnMsg, L"Cancel Job");
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kCancelSheetBtnMsg, L"Cancel Sheet");
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kSheetSetProgressCaption, L"Job Progress");
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kSheetProgressCaption, L"Sheet Progress");
pPlotProgDlg->setPlotProgressRange(0, 100);
pPlotProgDlg->onBeginPlot();
pPlotProgDlg->setIsVisible(true);
int nDwgCount = plotSettingInfo.m_vecBlockInfo4Plot.size();
for (int num = 0; num < plotSettingInfo.m_vecBlockInfo4Plot.size(); num++)
{
if (pPlotProgDlg->isPlotCancelled() || pPlotProgDlg->isSheetCancelled())
{
break;
}
CString csFilePathWithoutExt = plotSettingInfo.m_csSaveLocation + _T("\\") + CString(plotSettingInfo.m_vecBlockInfo4Plot[num].m_sPlotFileName.data());
CString csDstPdf = GetNextFilePath(csFilePathWithoutExt, _T(".pdf"), 0, plotSettingInfo.m_bOverride);
CString csStatus;
csStatus.Format(_T("正在打印%d/%d..."), num + 1, nDwgCount);
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kSheetName, csDstPdf);
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::PlotMSGIndex::kStatus, csStatus);
AcPlPlotInfo plotInfo;
SetPlotInfo(plotSettingInfo.m_vecBlockInfo4Plot[num], plotSettingInfo.m_idLayout, plotInfo);
DoPlot(pPlotProgDlg, pEngine, plotInfo, true, csDstPdf);
setPlotFile.insert(csDstPdf);
//int nProgressPercent = (num + 1) * 100 / nDwgCount;
pPlotProgDlg->setPlotProgressPos(100);
}
// Destroy the engine
pEngine->destroy();
pEngine = NULL;
// and the progress dialog.
pPlotProgDlg->destroy();
pPlotProgDlg = NULL;
}
else
{
// Ensure the engine is not already busy...
AfxMessageBox(L"Plot Engine is Busy...");
}
// 恢复视图
if (bNeedRestoreView)
{
// 将视图对象设置为当前视图
acedSetCurrentView(&viewOld, NULL);
}
}
demo代码:
https://github.com/ChiuqiuA/ArxSample.git
参考文档:
BACKGROUNDPLOT (System Variable) | AutoCAD 2020 | Autodesk Knowledge Network