|
发表于 2020-2-29 10:36:00
|
显示全部楼层
非常感谢提供解答!你的程序有几个值得参考的地方!非常值得学习!
昨天下午我自己改了几个地方后也能正常运行,完整代码如下:红色字就是改动的地方.还有一点程序里没有标成红色的内容是,CAD官方说明里,页面设置函数传入的是 AcDbPlotSettings* pPlotSet,如果传入pLayout程序也能运行,如果改成pPlotSet当然也没问题,只是在网页里不好标成红色字,所以以说明形式表达。
static void test(void)
{
//////////////////////////////////////////////////////////////////////////
//批量打印
// 取得当前布局layoutId
AcDbLayoutManager *pLayoutManager = acdbHostApplicationServices()->layoutManager(); //取得布局管理器对象
AcDbObjectId m_layoutId = pLayoutManager->findLayoutNamed(pLayoutManager->findActiveLayout(TRUE), NULL);////获得布局的Id
//打开布局
AcDbObjectPointer pLayout(m_layoutId, AcDb::kForRead);
//获得打印机验证器对象
AcDbPlotSettingsValidator *pPSV = acdbHostApplicationServices()->plotSettingsValidator();
//更新打印设备列表
AcDbPlotSettings* pPlotSet = (AcDbPlotSettings*)pLayout;
pPSV->refreshLists(pPlotSet);
//delete pPlotSet;
//打印机设置
ACHAR* m_strDevice = _T("pdfFactory Pro");//打印机名字
pPSV->setPlotCfgName(pLayout, m_strDevice);//设置打印设备
ACHAR* m_mediaName = _T("ISO A4");//图纸名称
pPSV->setCanonicalMediaName(pLayout, m_mediaName);//设置图纸尺寸
pPSV->setPlotWindowArea(pLayout, 0, 0, 210, 297);//顺序1,设置打印范围,超出给范围的将打不出来
pPSV->setPlotOrigin(pPlotSettings, 0, 0);//顺序2
pPSV->setPlotType(pLayout, AcDbPlotSettings::kWindow);//顺序3,设置打印范围为窗口
pPSV->setCurrentStyleSheet(pLayout, _T("acad.ctb"));//设置打印样式表
pPSV->setPlotCentered(pLayout, true);//是否居中打印
pPSV->setUseStandardScale(pLayout, true);//设置是否采用标准比例
pPSV->setStdScaleType(pLayout, AcDbPlotSettings::kScaleToFit);//布满图纸
pPSV->setPlotRotation(pLayout, AcDbPlotSettings::k90degrees);//设置打印方向
//pPSV->setPlotViewName(pLayout, _T("打印1"));
pLayout->close();//此句必须保留,否则程序一闪而过而且不报错
//准备打印/////////////////////////////////////////////////////////////////////////
AcPlPlotEngine* pEngine = NULL;//创建打印引擎
if (AcPlPlotFactory::createPublishEngine(pEngine) != Acad::eOk)
{
acedAlert(_T("打印失败!"));
return;
}
Acad::ErrorStatus es;
// 打印进度对话框
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
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_result.pdf"));
//给打印机和进度对话框发送消息
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();
//////////////////////////////////////////////////////////////////////////
} |
|