Dwg,png,jpg,Dxf格式转换

柔情痞子 提交于 2020-03-13 19:51:28

(1)dxf转dwg:打开dxf,另存为dwg

//打开dxf
ShellExecute(NULL, _T("open"), strDxfPath, NULL, NULL, SW_SHOW);
//另存为dwg
AcDbDatabase * pDB = acdbCurDwg();
Acad::ErrorStatus es = pDB->saveAs(strDwgPath);

(2)dwg转png,jpg

方法1:调用CAD的命令pngout,jpgout

方法2:调用CAD的打印

dwg转jpg:

bool DwgToJpg( CString strDwgPath, CString& strJpgPath,LPCTSTR strPixel, AcGePoint3d ptS,AcGePoint3d ptE)
{
	if ( _taccess(strDwgPath ,0) == -1 )
		return false;

       //dwg文件路径
	CComVar strTmpDwg;
	CopyFile(strDwgPath, strTmpDwg, FALSE);

      //jpg文件路径
	CString strTmpJpg = strTmpDwg.GetFilePathOnly()+strTmpDwg.GetFileNameOnly()+_T(".jpg");

	COpDWG on(strTmpDwg);
	if ( !on.open(true,psd::glb_dwgPsd) && !on.open(true/*, psd::glb_dwgPsd*/))
	{
		DeleteFile(strTmpDwg);
		return false;
	}

	//驱动配置文件路径
	CString strPrinterDir = CAppUtility::GetPathSupport()+_T("Drivers\\Printer\\");
	CPlotSetting set;
	set.SetCopyPath(strPrinterDir);
	set.setCentered(true);

	if (ptS != ptE)
	{
		set.setPlotTypeWindow(AcGePoint3d(((ptS.x > ptE.x) ? ptE.x : ptS.x), ((ptS.y > ptE.y) ? ptE.y : ptS.y), 0.0), 
			AcGePoint3d(((ptS.x < ptE.x) ? ptE.x : ptS.x), ((ptS.y < ptE.y) ? ptE.y : ptS.y), 0.0));
	}
	set.SetConfigFileJPG(strPixel);
	set.setFill(true);
	CPlotDWG plot(&set);
	plot.SetNeedProgress(false);
	bool bRet = false;
	if ( bRet = plot.ToJPG(strTmpDwg, strTmpJpg) )
	{
		strJpgPath = strTmpJpg;
	}
	on.close();
	DeleteFile(strTmpDwg);
	return bRet;
}

dwg转png:

bool CDBQueryToolsSevr::DwgToPng( CString strDwgPath, CString& strPngPath, AcGePoint3d ptS , AcGePoint3d ptE )
{
	if ( _taccess(strDwgPath ,0) == -1 )
		return false;

	//dwg文件路径
	CComVar strTmpDwg;
	CopyFile(strDwgPath, strTmpDwg, FALSE);

        //png文件路径
	CString strTmpPng = strTmpDwg.GetFilePathOnly()+strTmpDwg.GetFileNameOnly()+_T(".png");

	COpDWG on(strTmpDwg);
	if ( !on.open(true,psd::glb_dwgPsd) && !on.open(true/*, psd::glb_dwgPsd*/))
	{
		DeleteFile(strTmpDwg);
		return false;
	}

	//驱动配置文件路径
	CString strPrinterDir = CAppUtility::GetPathSupport()+_T("Drivers\\Printer\\");
 	CPlotSetting set;
	set.SetCopyPath(strPrinterDir);
	set.setCentered(true);

	if (ptS != ptE)
	{
		set.setPlotTypeWindow(AcGePoint3d(((ptS.x > ptE.x) ? ptE.x : ptS.x), ((ptS.y > ptE.y) ? ptE.y : ptS.y), 0.0), 
			AcGePoint3d(((ptS.x < ptE.x) ? ptE.x : ptS.x), ((ptS.y < ptE.y) ? ptE.y : ptS.y), 0.0));
	}
	set.SetConfigFilePNG();
	set.setFill(true);
	CPlotDWG plot(&set);
	plot.SetNeedProgress(false);
	bool bRet = false;
	if ( bRet = plot.ToPNG(strTmpDwg, strTmpPng) )
	{
		strPngPath = strTmpPng;
	}
	on.close();
	DeleteFile(strTmpDwg);
	return bRet;
}

DWG文件操作类:

//! DWG文件操作类
struct COpDWGInfo;
class COMSDK_EX COpDWG
{
public:
#ifdef _ACRXAPP
	COpDWG(LPCTSTR lpDWGPath,LPDISPATCH pApp=acedGetAcadWinApp()->GetIDispatch(TRUE));
#else
	COpDWG(LPCTSTR lpDWGPath,LPDISPATCH pApp);
#endif
	~COpDWG(void);
private:
	COpDWG(const COpDWG&);
	COpDWG& operator=(const COpDWG&);
	COpDWGInfo* m_pInfo;
public:
	//! 设置当前要操作的DWG路径
	void setDWGPath(LPCTSTR lpDWGPath);

	//! 判断DWG文件是否已打开
	bool isOpen()const;

	//! 打开DWG[打开后是否需要全图]
	bool open(bool IsZoom=true, LPCTSTR lpPsd=NULL);

	//! 关闭文件
	bool close();

	//! 保存DWG
	bool save();
	bool saveAs(LPCTSTR lpDWGNewPath);
};
#include "AcAdGetComPtr.h"
struct COpDWGInfo 
{
	std::wstring m_strDWGPath;
	AcAdGetComPtr m_Acad;
	COpDWGInfo(LPDISPATCH pApp):m_Acad(pApp){}
};

COpDWG::COpDWG(LPCTSTR lpDWGPath, LPDISPATCH pApp):
m_pInfo( new COpDWGInfo(pApp))
{
	m_pInfo->m_strDWGPath = lpDWGPath;
}

COpDWG::~COpDWG(void)
{
}

void COpDWG::setDWGPath( LPCTSTR lpDWGPath )
{
	m_pInfo->m_strDWGPath = lpDWGPath;
}
bool COpDWG::isOpen() const
{
	return m_pInfo->m_Acad.getDoc(m_pInfo->m_strDWGPath.c_str()) != NULL;
}

bool COpDWG::open(bool IsZoom/*=true*/, LPCTSTR lpPsd)
{
	AutoCAD::IAcadDocumentPtr pDoc = m_pInfo->m_Acad.getDoc(m_pInfo->m_strDWGPath.c_str());
	if ( pDoc != NULL )
	{
		return SUCCEEDED(pDoc->Activate());
	}

	// 取得所有文档
	AutoCAD::IAcadDocumentsPtr pDocs = m_pInfo->m_Acad.getDocs();
	if ( NULL == pDocs )
		return false;

	_bstr_t path(m_pInfo->m_strDWGPath.c_str());

	if ( lpPsd == NULL || lpPsd[0] == NULL )
	{
		if ( FAILED(pDocs->Open(path, COleVariant((short)FALSE), vtMissing, &pDoc)) )
			return false;
	}
	else
	{
		if ( FAILED(pDocs->Open(path, COleVariant((short)FALSE), COleVariant(lpPsd), &pDoc)) )
			return false;
	}

	if ( FAILED(pDoc->Activate()) )
		return false;

	// 需要全图显示
	HRESULT hr;
	if ( IsZoom )
	{
		hr = pDoc->SendCommand(_T("z e "));
	}

	return true;
}

bool COpDWG::close()
{
	AutoCAD::IAcadDocumentPtr pDoc = m_pInfo->m_Acad.getDoc(m_pInfo->m_strDWGPath.c_str());
	if ( pDoc != NULL )
	{
		pDoc->Close();
	}

	return true;
}

bool COpDWG::save()
{
	AutoCAD::IAcadDocumentPtr pDoc = m_pInfo->m_Acad.getDoc(m_pInfo->m_strDWGPath.c_str());
	if ( NULL == pDoc)
		return false;

	return SUCCEEDED(pDoc->Save());
}

bool COpDWG::saveAs( LPCTSTR lpDWGNewPath )
{
	AutoCAD::IAcadDocumentPtr pDoc = m_pInfo->m_Acad.getDoc(m_pInfo->m_strDWGPath.c_str());
	if ( NULL == pDoc)
		return false;

	return SUCCEEDED(pDoc->SaveAs(_bstr_t(lpDWGNewPath)));
}

打印设置类:

//! 配置类 
struct CPlotSettingInfo;
class COMSDK_EX CPlotSetting
{
	friend struct CPlotSettingInfo;
	friend struct CPlotDWGInfo;
	friend class CPlotDWG;
public:
	CPlotSetting(void);
	~CPlotSetting(void);
private:
	CPlotSetting(const CPlotSetting& p1);
	CPlotSetting& operator=(const CPlotSetting&p1);
private:
	// 打印类型
	enum emPlotToType
	{
		PDF = 0,
		DWF,
		JPG,
		PNG
	};
	CPlotSettingInfo* m_pInfo;
public:
	//! 设置打印比例
	void setScale(double dScale/*=1.0*/);
	//! 设置是否填充
	void setFill(bool bIsFill);

	//! 设置是否居中打印
	void setCentered(bool IsCentered/*=true*/);

	//! 设置视角
	void setPlotType(AcDbPlotSettings::PlotType emType/*=AcDbPlotSettings::kExtents*/);

	//! 设置视角为窗口
	void setPlotTypeWindow(const AcGePoint3d& pLB/*左下*/, const AcGePoint3d& pRT/*右上*/);

public:
	//! 设置拷贝路径[如果PC3文件不存在,则去该路径下寻找]
	bool SetCopyPath(LPCTSTR lpPath);

	//! 设置PDF配置文件
	bool SetConfigFilePDF(LPCTSTR lpPagerName=_T("ISO_A4_(210.00_x_297.00_MM)"), LPCTSTR lpPC3=_T("DWG To PDF.pc3"));

	//! 设置DWF配置文件
	bool SetConfigFileDWF(LPCTSTR lpPagerName=_T("UserDefinedImperial (12590.00 x 12590.00英寸)"), LPCTSTR lpPC3=_T("DWF6 ePlot.pc3"));

	//! 设置JPG配置文件
	bool SetConfigFileJPG(LPCTSTR lpPagerName=_T("XGA_(2100.00_x_1485.00_Pixels)"), LPCTSTR lpPC3=_T("PublishToWeb JPG.pc3"));

	//! 设置PNG配置文件
	bool SetConfigFilePNG(LPCTSTR lpPagerName=_T("XGA_(1485.00_x_2100.00_Pixels)"), LPCTSTR lpPC3=_T("PublishToWeb PNG.pc3"));

protected:
	//! 初始化
	bool Init(emPlotToType emType);

	//! 得到模型ID
	AcDbObjectId getLayout()const;

	//! 得到配置
	const AcDbPlotSettings* getSetting()const;
};
#include "MD5Checksum.h"
#include <axlock.h>
struct PaperInfos
{
	std::wstring m_strName;// 图纸名称
	double m_dWidth       ;// 图纸宽
	double m_dHeight      ;// 图纸高
};
struct ConfigAndPaper 
{
	std::wstring m_strPc3Name         ;// 文件名称
	std::wstring m_strPaperName       ;// 默认图纸
	std::vector<PaperInfos>m_arrPapers;// 所有图纸
};
struct CPlotSettingInfo
{
public:
	CPlotSettingInfo();
public:
	typedef std::map<CPlotSetting::emPlotToType,ConfigAndPaper> _arrConfigs;
	AcDbPlotSettings* m_pSetting           ;// CAD配置
	AcDbObjectId      m_LayoutId           ;// 当前模型ID

	_arrConfigs       m_arrConfigs         ;// 文件配置
	std::wstring      m_strCopyPC3Path     ;// 拷贝路径[如果PC3文件不存在,则去该路径下寻找]

	bool			  m_bIsbBackgroundPlot ;// 是否后台打印
	bool              m_bIsCentered        ;// 是否居中打印
	bool              m_bIsFit             ;// 是否布满图纸
	double            m_dScale             ;// 图纸比例尺

	AcDbPlotSettings::PlotType m_emPlotType;// 打印视窗
	AcGePoint3d       m_LeftBottom         ;// 左下角点(当m_emPlotType==AcDbPlotSettings::kWindow时的窗口范围)
	AcGePoint3d       m_RightTop           ;// 右上角点(当m_emPlotType==AcDbPlotSettings::kWindow时的窗口范围)

public:
	// 设置配置
	bool SetConfig(ConfigAndPaper& mItem, LPCTSTR lpPagerName, LPCTSTR lpPC3);

	// 取得当前合适的图纸名称
	std::wstring GetFitPaper()const;

private:
	//得到cad的安装目录
	CString GetCADPlotterPath();
};

CPlotSettingInfo::CPlotSettingInfo():
m_pSetting(NULL),
m_dScale(1.0),
m_bIsCentered(true),
m_bIsFit(true),
m_bIsbBackgroundPlot(true),
m_emPlotType(AcDbPlotSettings::kExtents)
{
}

CString CPlotSettingInfo::GetCADPlotterPath()
{
	TCHAR szpath[MAX_PATH]={0};  
	::GetModuleFileName(NULL,szpath,MAX_PATH);  

	CString strCadPlotterPath(szpath);
	strCadPlotterPath.TrimRight(_T("acad.exe"));
	strCadPlotterPath += _T("UserDataCache\\Plotters\\");

	return strCadPlotterPath;
}

bool CPlotSettingInfo::SetConfig( ConfigAndPaper& mItem, LPCTSTR lpPagerName, LPCTSTR lpPC3 )
{
	if ( !m_strCopyPC3Path.empty() )
	{
		const ACHAR* pszPath = NULL;
		if (Acad::eOk != acdbHostApplicationServices()->getRoamableRootFolder(pszPath))
			return false;

		std::wstring strPath = std::wstring(pszPath) + _T("Plotters\\");

		// 判断文件是否存在,如果不存在,则拷过来PMP文件
		CString  strFile(lpPC3);
		strFile.Replace(_T("pc3"),_T("pmp"));
		std::wstring szFile = strFile.GetString();

		//cad安装目录下的打印机目录
		CString strCADPlotterPath = GetCADPlotterPath();
		if (_taccess((strPath +_T("PMP Files\\")+szFile).c_str(),0) == -1 )
		{
			if ( !CopyFile((m_strCopyPC3Path+_T("PMP Files\\")+szFile).c_str(),(strPath+_T("PMP Files\\")+szFile).c_str(), FALSE) )
				return false;
		}
		else
		{
			CString strLocalMD5 = CMD5Checksum::GetMD5((strPath +_T("PMP Files\\")+szFile).c_str());
			CString strDirMD5 = CMD5Checksum::GetMD5((m_strCopyPC3Path+_T("PMP Files\\")+szFile).c_str());
			//if ( 0 != strLocalMD5.CompareNoCase(strDirMD5))
			{
				if ( !CopyFile((m_strCopyPC3Path+_T("PMP Files\\")+szFile).c_str(),(strPath+_T("PMP Files\\")+szFile).c_str(), FALSE) )
					return false;

				if (!CopyFile((m_strCopyPC3Path+_T("PMP Files\\")+szFile).c_str(),strCADPlotterPath+_T("PMP Files\\")+(szFile).c_str(), FALSE) )
					return false;
			}
		}

		// 判断文件是否存在,如果不存在,则拷过来PC3文件

		//if (_taccess((strPath + lpPC3).c_str(),0) == -1 )
		{
			if ( !CopyFile((m_strCopyPC3Path+lpPC3).c_str(),(strPath+lpPC3).c_str(), FALSE) )
				return false;
			
			if ( !CopyFile((m_strCopyPC3Path+lpPC3).c_str(),strCADPlotterPath+lpPC3, FALSE) )
				return false;
		}	
	}

	mItem.m_strPaperName = lpPagerName;
	mItem.m_strPc3Name   = lpPC3      ;

	// 取得所有图纸
	return true;
}

std::wstring CPlotSettingInfo::GetFitPaper() const
{
// 	_arrConfigs::const_iterator mIter=m_arrConfigs.find(m_emPlotType);
// 	if ( mIter == m_arrConfigs.end() )
 		return _T("");
// 
// 	// 遍历所有图纸,并解析其大小
// 	return ;
}

CPlotSetting::CPlotSetting(void):
m_pInfo( new CPlotSettingInfo)
{
	// 设置PDF配置文件
	SetConfigFilePDF();

	// 设置DWF配置文件
	SetConfigFileDWF();

	// 设置JPG配置文件
	SetConfigFileJPG();

	// 设置PNG配置文件
	SetConfigFilePNG();
}

CPlotSetting::~CPlotSetting(void)
{
}

AcDbObjectId CPlotSetting::getLayout() const
{
	return m_pInfo->m_LayoutId;
}

const AcDbPlotSettings* CPlotSetting::getSetting() const
{
	return m_pInfo->m_pSetting;
}

bool CPlotSetting::Init(emPlotToType emType)
{
	AcDbPlotSettingsValidator* pPSV = (AcDbPlotSettingsValidator*)acdbHostApplicationServices()->plotSettingsValidator();
	if ( pPSV==NULL )
		return false;

	AcApLayoutManager *pLayMan = (AcApLayoutManager *) acdbHostApplicationServices()->layoutManager();
	if ( NULL == pLayMan )
		return false;

	//get the active layout
	LPCTSTR lpName = pLayMan->findActiveLayout(TRUE);
	if ( lpName == NULL )
		return false;
	AcDbLayout *pLayout = pLayMan->findLayoutNamed(lpName,TRUE);	
	if ( pLayout == NULL )
		return false;

	m_pInfo->m_LayoutId = pLayout->id();

	delete m_pInfo->m_pSetting;
	m_pInfo->m_pSetting = new AcDbPlotSettings(pLayout->modelType());
	m_pInfo->m_pSetting->copyFrom(pLayout);

	Acad::ErrorStatus es =pPSV->setPlotRotation(m_pInfo->m_pSetting,AcDbPlotSettings::k0degrees);
	if ( m_pInfo->m_emPlotType == AcDbPlotSettings::kWindow )
	{
		es = pPSV->setPlotWindowArea(m_pInfo->m_pSetting,m_pInfo->m_LeftBottom.x, m_pInfo->m_LeftBottom.y, m_pInfo->m_RightTop.x, m_pInfo->m_RightTop.y);
	}

	es = pPSV->setPlotType(m_pInfo->m_pSetting,m_pInfo->m_emPlotType);
	es = pPSV->setPlotCfgName(m_pInfo->m_pSetting,m_pInfo->m_arrConfigs[emType].m_strPc3Name.c_str(),m_pInfo->m_arrConfigs[emType].m_strPaperName.c_str());	

	//所有配置名称 
	AcArray<const ACHAR *> mMediaList;
	std::vector<CString> arrMediaNames;
	pPSV->canonicalMediaNameList(m_pInfo->m_pSetting,mMediaList);
	for (int nIndex = 0; nIndex < mMediaList.length(); ++nIndex)
		arrMediaNames.push_back(mMediaList[nIndex]);

	// Set the Scale...
	if (m_pInfo->m_bIsFit)
	{
		es = pPSV->setUseStandardScale(m_pInfo->m_pSetting,Adesk::kTrue);
		es = pPSV->setStdScaleType(m_pInfo->m_pSetting,AcDbPlotSettings::kScaleToFit);
	}
	else // Choose to use this custom scale...
	{
		es=pPSV->setUseStandardScale(m_pInfo->m_pSetting,Adesk::kFalse);
		es=pPSV->setCustomPrintScale(m_pInfo->m_pSetting,m_pInfo->m_dScale,1);
	}

	// Specify that we want our plot centered by AutoCAD...
	es=pPSV->setPlotCentered(m_pInfo->m_pSetting,m_pInfo->m_bIsCentered);

	resbuf res;
	res.rbnext = NULL;
	res.restype = RTSHORT;
	res.resval.rint = 0/*m_pInfo->m_bIsbBackgroundPlot?1:*/;
	acedSetVar(L"BACKGROUNDPLOT",&res);

	// 设置系统打印ole对象相关变量
	resbuf res1;
	res1.restype = RTSHORT;
	res1.resval.rint = 1;//显示并打印边框
	AcAxDocLock mLock;
	acedSetVar(_T("OLEFRAME"), &res1);

	resbuf res2;
	res2.restype = RTSHORT;
	res2.resval.rint = 0;//所有 OLE 对象均可见并可打印
	acedSetVar(_T("OLEHIDE"), &res2);

	resbuf res3;
	res3.restype = RTSHORT;
	res3.resval.rint = 3;//设置 OLE 对象的默认打印质量   自动选择
	acedSetVar(_T("OLEQUALITY"), &res3);

	return es == Acad::eOk;
}

bool CPlotSetting::SetConfigFilePDF( LPCTSTR lpPagerName/*=_T("ISO_A4_(210.00_x_297.00_MM)")*/, LPCTSTR lpPC3/*=_T("DWG To PDF.pc3")*/ )
{
	m_pInfo->m_bIsFit = true;
	return m_pInfo->SetConfig(m_pInfo->m_arrConfigs[PDF], lpPagerName, lpPC3);
}

bool CPlotSetting::SetCopyPath( LPCTSTR lpPath )
{
	m_pInfo->m_strCopyPC3Path = lpPath;

	return true;
}

bool CPlotSetting::SetConfigFileDWF( LPCTSTR lpPagerName/*=_T("ISO_A4_(210.00_x_297.00_MM)")*/, LPCTSTR lpPC3/*=_T("DWF6 ePlot.pc3")*/ )
{
	m_pInfo->m_bIsFit = false;
	return m_pInfo->SetConfig(m_pInfo->m_arrConfigs[DWF], lpPagerName, lpPC3);
}

bool CPlotSetting::SetConfigFileJPG( LPCTSTR lpPagerName/*=_T("XGA_(1024.00_x_768.00_Pixels)")*/, LPCTSTR lpPC3/*=_T("PublishToWeb JPG.pc3")*/ )
{
	return m_pInfo->SetConfig(m_pInfo->m_arrConfigs[JPG], lpPagerName, lpPC3);
}

bool CPlotSetting::SetConfigFilePNG( LPCTSTR lpPagerName/*=_T("XGA_(1024.00_x_768.00_Pixels)")*/, LPCTSTR lpPC3/*=_T("PublishToWeb PNG.pc3")*/ )
{
	return m_pInfo->SetConfig(m_pInfo->m_arrConfigs[PNG], lpPagerName, lpPC3);
}

void CPlotSetting::setScale( double dScale )
{
	m_pInfo->m_dScale = dScale;
	m_pInfo->m_bIsFit = false;
}

//! 设置是否填充
void CPlotSetting::setFill(bool bIsFill)
{
	m_pInfo->m_bIsFit = bIsFill;
}

void CPlotSetting::setPlotType( AcDbPlotSettings::PlotType emType )
{
	m_pInfo->m_emPlotType = emType;
}

void CPlotSetting::setCentered( bool IsCentered/*=true*/ )
{
	m_pInfo->m_bIsCentered = IsCentered;
}

void CPlotSetting::setPlotTypeWindow( const AcGePoint3d& pLB/*左下*/, const AcGePoint3d& pRT/*右上*/ )
{
	m_pInfo->m_emPlotType = AcDbPlotSettings::kWindow;
	m_pInfo->m_LeftBottom = pLB;
	m_pInfo->m_RightTop   = pRT;
}


打印类:

//! 打印类 
struct CPlotDWGInfo;
class CPlotSetting;
class COMSDK_EX CPlotDWG
{
public:
	CPlotDWG(CPlotSetting* pSetting=NULL);
	~CPlotDWG(void);
private:
	CPlotDWGInfo* m_pInfo;
	CPlotDWG(const CPlotDWG&);
	CPlotDWG&operator=(const CPlotDWG&);
public:

	//! 设置是否需要打印的进度条 
	void SetNeedProgress(bool bNeedProgress = true);

	//! DWG打印PDF格式
	bool ToPDF(LPCTSTR dwgFilePath,LPCTSTR outPutFilePath);

	//! DWG打印DWF格式
	bool ToDWF(LPCTSTR dwgFilePath,LPCTSTR outPutFilePath);

	//! DWG打印PNG格式
	bool ToPNG(LPCTSTR dwgFilePath,LPCTSTR outPutFilePath);

	//! DWG打印JPG格式
	bool ToJPG(LPCTSTR dwgFilePath,LPCTSTR outPutFilePath);
};
#include "PlotSetting.h"
#include <axlock.h>

#define hhverifyx2(a,b) if ( !(a) ){assert(false);b;}
#define hhverifyErr2(a,b) if ( Acad::eOk != (a) ) {assert(false);b;}
#define hhverifyErr(a) if ( Acad::eOk != (a) ) {assert(false);}
struct CPlotDWGInfo 
{
	CPlotSetting* m_pSetting;// 配置
	bool          m_bIsNew  ;// 是否内部new出的对像
	bool		  m_bNeedProgress;//!< 是否需要打印的进度条 
public:
	//! 打印
	bool Plot(LPCTSTR dwgFilePath,LPCTSTR outPutFilePath, CPlotSetting::emPlotToType emType);
private:
	bool InnerPlot(LPCTSTR outPutFilePath);
};

bool CPlotDWGInfo::Plot( LPCTSTR dwgFilePath,LPCTSTR outPutFilePath, CPlotSetting::emPlotToType emType )
{
	// 检测必须项
	AcApDocManager* pMG = acDocManager;
	hhverifyx2(NULL != pMG && NULL != dwgFilePath && NULL != outPutFilePath && NULL != m_pSetting,return false;);

	// 保存当前资源
	AcApDocument* pDoc = curDoc();
	hhverifyx2(pDoc, return false;);
	
	Acad::ErrorStatus es = 	acDocManager->sendStringToExecute(pDoc,_T("zoom e\n"),true,false,false);


	pMG->pushAcadResourceHandle();
	pDoc->pushDbmod();

	bool bSuc = false;
	// 初始化配置
	if ( m_pSetting->Init(emType) )
	{
		// 打印
		bSuc = InnerPlot(outPutFilePath);
	}

	pDoc->popDbmod();

	pMG->popResourceHandle();

	return bSuc;
}

bool CPlotDWGInfo::InnerPlot(LPCTSTR outPutFilePath)
{
	Acad::ErrorStatus es;
	AcAxDocLock mLock(acdbCurDwg());
	AcPlPlotEngine* pEngine = NULL;
	hhverifyErr2(es=AcPlPlotFactory::createPublishEngine(pEngine), return false;);
	hhverifyErr2(es=pEngine->beginPlot(NULL), pEngine->destroy();return false;);

	AcPlPlotInfo plotInfo; // 用来描述如何制作打印

	//首先,将图层设置为指定的图层(一般当前图层)
	plotInfo.setLayout(m_pSetting->getLayout());//必须设置

	// 然后,用我们填充的plot设置覆盖布局设置
	plotInfo.setOverrideSettings(m_pSetting->getSetting());

	// 验证这些设置。
	AcPlPlotInfoValidator validator;
	validator.setMediaMatchingPolicy(AcPlPlotInfoValidator::kMatchEnabled);
	hhverifyErr2(es=validator.validate(plotInfo), pEngine->destroy();return false;);

	//开始文档.我们调用的版本依赖于plot -to- file状态。
	const TCHAR *szDocName=acDocManager->curDocument()->fileName();
	hhverifyErr2(es=pEngine->beginDocument(plotInfo, szDocName, NULL, 1, true,outPutFilePath), pEngine->destroy();return false;);

	AcPlPlotProgressDialog *pPlotProgDlg;
	if (m_bNeedProgress)
	{
		pPlotProgDlg = acplCreatePlotProgressDialog(acedGetAcadFrame()->m_hWnd,false,1,NULL,false);
		pPlotProgDlg->setPlotMsgString(AcPlPlotProgressDialog::kDialogTitle,_T("正在打印"));
		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("打印进度"));

		pPlotProgDlg->setPlotProgressRange(0,100);
		pPlotProgDlg->onBeginPlot();
		pPlotProgDlg->setIsVisible(true);

		es = pEngine->beginPlot(pPlotProgDlg);
	}

	// 通过向引擎发送命令和通知到进度对话框。
	AcPlPlotPageInfo pageInfo;
	hhverifyErr2(es=pEngine->beginPage(pageInfo, plotInfo, true),pEngine->destroy();return false;);

	hhverifyErr(es = pEngine->beginGenerateGraphics());
	hhverifyErr(es = pEngine->endGenerateGraphics());
	hhverifyErr(es = pEngine->endPage());

	if (m_bNeedProgress)
	{
		pPlotProgDlg->setSheetProgressPos(100);
		pPlotProgDlg->onEndSheet();
		pPlotProgDlg->setPlotProgressPos(100);
	}

	hhverifyErr(es = pEngine->endDocument());
	hhverifyErr(es = pEngine->endPlot());

	if (m_bNeedProgress)
	{
		pPlotProgDlg->onEndPlot();
	}
	//消除引擎 
	pEngine->destroy();
	pEngine = NULL;

	if (m_bNeedProgress)
	{
		pPlotProgDlg->destroy();
	}

	return true;
}


CPlotDWG::CPlotDWG(CPlotSetting* pSetting):
m_pInfo( new CPlotDWGInfo)
{
	m_pInfo->m_pSetting = pSetting;
	//! 默认需要打印的进度条 
	m_pInfo->m_bNeedProgress = true;
	if ( m_pInfo->m_pSetting == NULL )
	{
		m_pInfo->m_pSetting = new CPlotSetting;
		m_pInfo->m_bIsNew = true;
	}
	else
	{
		m_pInfo->m_bIsNew = false;
	}
}

CPlotDWG::~CPlotDWG(void)
{
	if ( m_pInfo->m_bIsNew )
		delete m_pInfo->m_pSetting;

	delete m_pInfo;
}

bool CPlotDWG::ToPNG( LPCTSTR dwgFilePath,LPCTSTR outPutFilePath )
{
	return m_pInfo->Plot(dwgFilePath, outPutFilePath, CPlotSetting::PNG);
}

bool CPlotDWG::ToPDF( LPCTSTR dwgFilePath,LPCTSTR outPutFilePath )
{
	return m_pInfo->Plot(dwgFilePath, outPutFilePath, CPlotSetting::PDF);
}

bool CPlotDWG::ToDWF( LPCTSTR dwgFilePath,LPCTSTR outPutFilePath )
{
	return m_pInfo->Plot(dwgFilePath, outPutFilePath, CPlotSetting::DWF);
}

bool CPlotDWG::ToJPG( LPCTSTR dwgFilePath,LPCTSTR outPutFilePath )
{
	return m_pInfo->Plot(dwgFilePath, outPutFilePath, CPlotSetting::JPG);
}

void CPlotDWG::SetNeedProgress( bool bNeedProgress /*= true*/ )
{
	m_pInfo->m_bNeedProgress = bNeedProgress;
}

方法3:自己写一个类似于qq截图的小功能

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!