整个打印的过程是比较复杂的, 要全面搞懂, 就要了解AcDbLayout、 AcDbPlotSettings、
AcDbPlotSettingsValidator、 AcPlPlotEngine、 AcPlPlotInfo几个类(缺一不可),以及它们之间的相互关系,而打印进度对话框可以不要。
注意加上头文件:#include “AcPlPlotEngine.h”
void batPlot()
{
//批量打印
// 取得当前layout
AcDbLayoutManager *pLayoutManager =
acdbHostApplicationServices()->layoutManager(); //取得布局管理器对象
AcDbLayout *pLayout =
pLayoutManager->findLayoutNamed(pLayoutManager->findActiveLayout(TRUE),TRUE);//获得当前布局
AcDbObjectId m_layoutId = pLayout->objectId();//获得布局的Id
//获得打印机验证器对象
AcDbPlotSettingsValidator *pPSV =NULL;
pPSV = acdbHostApplicationServices()->plotSettingsValidator();
//更新打印设备列表
pPSV->refreshLists(pLayout);
//打印机设置
ACHAR* m_strDevice = _T(“DWF6 ePlot.pc3”);//打印机名字
pPSV->setPlotCfgName(pLayout,m_strDevice);//设置打印设备
ACHAR* m_mediaName = _T(“ISO A4”);//图纸名称
pPSV->setCanonicalMediaName(pLayout,m_mediaName);//设置图纸尺寸
pPSV->setPlotType(pLayout,AcDbPlotSettings::kWindow);//设置打印范围为窗口
pPSV->setPlotWindowArea(pLayout,100,100,200,200);//设置打印范围,超出给范围的将打不出来
pPSV->setCurrentStyleSheet(pLayout,_T(“JSTRI.ctb”));//设置打印样式表
pPSV->setPlotCentered(pLayout,true);//是否居中打印
pPSV->setUseStandardScale(pLayout,true);//设置是否采用标准比例
pPSV->setStdScaleType(pLayout,AcDbPlotSettings::kScaleToFit);//布满图纸
pPSV->setPlotRotation(pLayout,AcDbPlotSettings::k90degrees);//设置打印方向
//pPSV->setPlotViewName(pLayout,_T(“打印1”));
//准备打印/////////////////////////////////////////////////////////////////////////
AcPlPlotEngine* pEngine = NULL;//创建打印引擎
if(AcPlPlotFactory::createPublishEngine(pEngine)!=Acad::eOk)
{
acedAlert(_T(“打印失败!”));
return;
}
// 打印进度对话框
AcPlPlotProgressDialog *pPlotProgDlg = acplCreatePlotProgressDialog(acedGetAcadFrame()->m_hWnd,false,1);
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kDialogTitle,_T(“lot API Progress”));
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kCancelJobBtnMsg,_T(“Cancel Job”));
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kCancelSheetBtnMsg,_T(“Cancel Sheet”));
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kSheetSetProgressCaption,_T(“Job Progress”));
pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kSheetProgressCaption,_T(“Sheet Progress”));
pPlotProgDlg->setPlotProgressRange(0,100);
pPlotProgDlg->onBeginPlot();
pPlotProgDlg->setIsVisible(true);
//begin plot
Acad::ErrorStatus es = pEngine->beginPlot(pPlotProgDlg);
AcPlPlotPageInfo pageInfo;//打印页信息
AcPlPlotInfo plotInfo; //打印信息
// 设置布局
plotInfo.setLayout(m_layoutId);
// 重置参数
plotInfo.setOverrideSettings(pLayout);
AcPlPlotInfoValidator validator;//创建打印信息验证器
validator.setMediaMatchingPolicy(AcPlPlotInfoValidator::kMatchEnabled);
es = validator.validate(plotInfo);
// begin document
const TCHAR *szDocName = acDocManager->curDocument()->fileName();//获得当前的文件名
//最后一个参数需要传入文件名
es = pEngine->beginDocument(plotInfo, szDocName, NULL,1,true,_T(“D:\\test.dwf”));
//给打印机和进度对话框发送消息
pPlotProgDlg->onBeginSheet();
pPlotProgDlg->setSheetProgressRange(0, 100);
pPlotProgDlg->setSheetProgressPos(0);
//begin page
es = pEngine->beginPage(pageInfo, plotInfo, true);
es = pEngine->beginGenerateGraphics();
es = pEngine->endGenerateGraphics();
//end page
es = pEngine->endPage();
pPlotProgDlg->setSheetProgressPos(100);
pPlotProgDlg->onEndSheet();
pPlotProgDlg->setPlotProgressPos(100);
//end document
es = pEngine->endDocument();
//end plot
es = pEngine->endPlot();
//返回资源
pEngine->destroy();
pEngine = NULL;
pPlotProgDlg->destroy();
pLayout->close();
}