ObjectARX打印当前图纸为PDF
点击前往淘宝下单 CAD二次开发定制ObjectARX安装配置AutoCAD插件ZWCAD插件C++
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "acdocman.h"
#include "rxregsvc.h"
#include "aced.h"
#include "dbents.h"
#include "dbapserv.h"
#include "acdocman.h"
#include "dbplotsettings.h"
#include "AcPlPlotEngine.h"
#include "AcPlPlotFactory.h"
#include "AcPlDSDData.h"
#include "acplmisc.h"
#include <axlock.h>
#include <afxwin.h>
//-----------------------------------------------------------------------------
#define szRDS _RXST("Finn")
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CToPDFApp : public AcRxArxApp {
public:
CToPDFApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
// TODO: Load dependencies here
// You *must* call On_kInitAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
// TODO: Add your initialization code here
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
// TODO: Add your code here
// You *must* call On_kUnloadAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
// TODO: Unload dependencies here
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
static AcDbExtents getMinimumBoundingRectangle()
{
AcDbExtents extents;
// 获取当前图形数据库
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
// 开始事务
AcDbBlockTable* pBlockTable;
if (pDb->getBlockTable(pBlockTable, AcDb::kForRead) != Acad::eOk)
return extents;
AcDbBlockTableRecord* pModelSpace;
if (pBlockTable->getAt(ACDB_MODEL_SPACE, pModelSpace, AcDb::kForRead) != Acad::eOk) {
pBlockTable->close();
return extents;
}
bool hasExtents = false;
// 遍历模型空间中的所有实体
AcDbBlockTableRecordIterator* pIterator;
if (pModelSpace->newIterator(pIterator) != Acad::eOk) {
pModelSpace->close();
pBlockTable->close();
return extents;
}
for (pIterator->start(); !pIterator->done(); pIterator->step()) {
AcDbEntity* pEntity;
if (pIterator->getEntity(pEntity, AcDb::kForRead) != Acad::eOk)
continue;
AcDbExtents entityExtents;
if (pEntity->getGeomExtents(entityExtents) == Acad::eOk) {
if (hasExtents) {
extents.addExt(entityExtents);
}
else {
extents = entityExtents;
hasExtents = true;
}
}
pEntity->close();
}
delete pIterator;
pModelSpace->close();
pBlockTable->close();
return extents;
}
static void FinnToPDF()
{
CAcModuleResourceOverride resOverride;
CString strOutPutPath;
// 创建文件保存对话框
CFileDialog fileDlg(FALSE, _T("pdf"), L"output.pdf", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("PDF Files (*.pdf)|*.pdf"), NULL);
// 显示对话框并获取结果
if (fileDlg.DoModal() == IDOK)
{
strOutPutPath = fileDlg.GetPathName();
PrintToPDF(strOutPutPath);
AfxMessageBox(L"转换结束!", MB_OK | MB_ICONINFORMATION);
}
}
static void PrintToPDF(const CString &strOutPutPath)
{
//////////////////////////////////////////////////////////////////////////
//批量打印
// 取得当前布局layoutId
AcDbLayoutManager *pLayoutManager = acdbHostApplicationServices()->layoutManager();
AcDbObjectId btrId = pLayoutManager->getActiveLayoutBTRId();
AcDbBlockTableRecord *pBTR;
auto es = acdbOpenObject(pBTR, btrId, AcDb::kForRead);
AcDbObjectId m_layoutId = pBTR->getLayoutId();
//打开布局
AcDbObjectPointer<AcDbLayout> pLayout(m_layoutId, AcDb::kForWrite);
//获得打印机验证器对象
AcDbPlotSettingsValidator *pPSV = acdbHostApplicationServices()->plotSettingsValidator();
//更新打印设备列表
AcDbPlotSettings* pPlotSet = (AcDbPlotSettings*)pLayout;
pPSV->refreshLists(pPlotSet);
//delete pPlotSet;
//打印机设置
ACHAR* m_strDevice = _T("DWG To PDF.pc3");//打印机名字
pPSV->setPlotCfgName(pLayout, m_strDevice);//设置打印设备
ACHAR* m_mediaName = _T("ISO_A4_(210.00_x_297.00_MM)");//图纸名称
pPSV->setCanonicalMediaName(pLayout, m_mediaName);//设置图纸尺寸
auto box = getMinimumBoundingRectangle();
pPSV->setPlotWindowArea(pLayout, box.minPoint().x, box.minPoint().y,
box.maxPoint().x, box.maxPoint().y);//顺序1,设置打印范围,超出给范围的将打不出来
pPSV->setPlotOrigin(pLayout, 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();//此句必须保留,否则程序一闪而过而且不报错
pBTR->close(); //准备打印/////////////////////////////////////////////////////////////////////////
AcPlPlotEngine* pEngine = NULL;//创建打印引擎
es = AcPlPlotFactory::createPublishEngine(pEngine);
if (es != Acad::eOk)
{
acedAlert(_T("Plot Engine is Busy..."));
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, strOutPutPath);
//给打印机和进度对话框发送消息
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();
//////////////////////////////////////////////////////////////////////////
}
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CToPDFApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CToPDFApp, Finn, ToPDF, 2pdf, ACRX_CMD_MODAL, NULL)
THE END
0
二维码
打赏
海报
ObjectARX打印当前图纸为PDF
点击前往淘宝下单 CAD二次开发定制ObjectARX安装配置AutoCAD插件ZWCAD插件C++
//-------------------------------------------------------------------------……