CAD使用控件把DWG文件转成位图
说明
CAD控件提供的接口,可以把dwg文件转换成Bmp,Jpg文件,也可以把DWG文件中某个区域的图形绘制到CDC上或保存为Bmp文件。这些接口即能在VC中使用,也能在VB,C#,Delphi,网页中调用。
DwgToJpg
不需要使用CAD控件打开dwg文件,直接把dwg文件转成jpg文件。
VC接口:
BOOL MxDraw::DwgToJpg(IN LPCTSTR pszDwgFilePath,
OUT LPCTSTR pszJpgFilePath,
int iWidth = -1,
int iHeight = -1);
COM接口:
bool DwgToJpg(string pszDwgFileName, string pszJpgFileName, int lWidth, int lHeight)
MxDrawXLib.IMxDrawApplication 的成员
| 参数 | 说明 |
|---|---|
|
pszDwgFilePath |
转入的DWG文件路径 |
|
pszJpgFilePath |
另存为的JPG文件路径 |
|
iWidth |
保存后的JPG文件的像素宽度 |
|
iHeight |
保存后的JPG文件的像素高度 |
VC调用参考例程:
void CTestDlg::OnBnClickedDwgtojpgButton()
{
// TODO:
CPreviewFileDialog openDlg(TRUE,_T("dwg"),NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
//_T("dwg(*.dwg) | *.dwg||"),
_T("DWG files (*.dwg)|*.dwg|DXF files (*.dxf)|*.dxf|Jpg files (*.jpg)|*.jpg|BMP files (*.bmp)|*.bmp||"),
this);
CString sDwgFileName;
if(openDlg.DoModal() == IDOK)
{
sDwgFileName = openDlg.GetPathName();
}
else
{
return;
}
//
CString sJpgFilePath;
CFileDialog openJpgDlg(FALSE,_T("jpg"),NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("jpg(*.jpg) | *.jpg||"),
this);
if(openJpgDlg.DoModal() == IDOK)
{
sJpgFilePath = openJpgDlg.GetPathName();
}
else
{
return;
}
// 1000,1000是转成jpg的像素宽度和高度。
if(MxDraw::DwgToJpg(sDwgFileName,sJpgFilePath,1000,1000) )
{
AfxMessageBox(_T("转换成功"));
}
else
{
AfxMessageBox(_T("转换失败"));
}
}
C#调用参考例程:
private void DwgToJpg_Click(object sender, EventArgs e)
{
// 创建一个应用对象
MxDrawApplication app = new MxDrawApplication();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Dwg 文件(*.Dwg)|*.Dwg|Dxf files (*.Dxf)|*.dxf";
if (ofd.ShowDialog() != DialogResult.OK)
{
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "jpg 文件(*.jpg)|*.jpg";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
// 1000,1000是转成jpg的像素宽度和高度。
if(app.DwgToJpg(ofd.FileName, sfd.FileName, 1000, 1000) )
{
MessageBox.Show("转换成功");
}
else
{
MessageBox.Show("转换失败");
}
}
}
WriteJpg
使用CAD控件打开dwg文件或经过了编辑后,调用该函数把它另存为Jpg文件。
VC接口:
BOOL MxDraw::WriteJpg(MXDRAWOCXHANDLE hOcx,LPCTSTR pszJpgFilePath,
int iWidth = -1,int iHeight = -1);
COM接口:
public virtual bool SaveJpgFile(string pszJpgFilePath, int lWidth, int lHeight)
AxMxDrawXLib.AxMxDrawX 的成员
| 参数 | 说明 |
|---|---|
|
hOcx |
控件的标识句柄 |
|
pszJpgFilePath |
保存的Jpg文件路径 |
|
lWidth |
保存后的Jpg文件像素宽度,取默认值-1,程序就自动取4000 |
|
lHeight |
保存后的Jpg文件像素高度,取默认值-1,程序就自动取4000 |
WriteBmp
使用CAD控件打开dwg文件或经过了编辑后,调用该函数把它另存为Bmp文件。
VC接口:
BOOL MxDraw::WriteBmp(MXDRAWOCXHANDLE hOcx,LPCTSTR pszBmpFilePath,
int iWidth = -1,int iHeight = -1);
COM接口:
public virtual bool SaveBmpFile(string pszBmpFilePath, int lWidth, int lHeight)
AxMxDrawXLib.AxMxDrawX 的成员
| 参数 | 说明 |
|---|---|
|
hOcx |
控件的标识句柄 |
|
pszJpgFilePath |
保存的Jpg文件路径 |
|
lWidth |
保存后的Jpg文件像素宽度,取默认值-1,程序就自动取4000 |
|
lHeight |
保存后的Jpg文件像素高度,取默认值-1,程序就自动取4000 |
VC调用参考例程:
void CTestDlg::OnBnClickedSavebmpButton()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog openDlg(FALSE,_T("bmp"),NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("bmp(*.bmp) | *.bmp||"),this);
CString sFileName;
if(openDlg.DoModal() == IDOK)
{
sFileName = openDlg.GetPathName();
}
else
{
return;
}
if(!MxDraw::WriteBmp(m_hDrawOcx,sFileName) )
{
CString sError;
if(MxDraw::GetLastError().IsEmpty() )
sError = _T("保存位图文件失败!");
else
sError.Format(_T("保存位图文件失败!原因为:%s"),MxDraw::GetLastError());
AfxMessageBox(sError);
}
else
{
AfxMessageBox(_T("保存成功"));
}
}
DrawToDc
绘制CAD控件当前图形中指定区域到CDC对象中。
VC接口:
BOOL MxDraw::DrawToDc(MXDRAWOCXHANDLE hOcx,
CDC* pDC,
int iDCx,int iDCy,int iDCWidth,int iDCHeight,
double dLbx,double dLby,double dRtx,double dRty );
AxMxDrawXLib.AxMxDrawX 的成员
| 参数 | 说明 |
|---|---|
|
hOcx |
标识控件的句柄 |
|
pDC |
把指定区域的图形中的内容绘制到pDC上 |
|
iDCx, iDCy |
绘制pDC的左上角位置 |
|
iDCWidth,iDCHeight |
绘制到pDC上的宽度和高度 |
|
dLbx, dLby |
指定图形区域的左下角坐标 |
|
dRtx, dRty |
指定图形区域的右下角坐标 |
VC调用参考例程:
void CTestCommands::DrawToBmp()
{
// 选择让用从图上选择个存位图的区域。
acutPrintf(_T("\n 请点取存位图的区域:"));
// 动态拖放输入,让用户确定要保存的区域
CRectSelJig getRect;
// pt1,pt2是矩形框的两点
AcGePoint3d pt1,pt2;
if(!getRect.DoIt(pt1,pt2) )
return;
// 让用户选择保存的位图文件.
CTestDlg* pDlg = (CTestDlg*)AfxGetApp()->GetMainWnd();
CFileDialog openDlg(FALSE,_T("bmp"),NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("bmp(*.bmp) | *.bmp||"),
pDlg);
CString sFileName;
if(openDlg.DoModal() == IDOK)
{
sFileName = openDlg.GetPathName();
}
else
{
return;
}
int iBmpWidth = 1000;
int iBmpHeight = 500;
// 开始保存文件.
CDC dc;
dc.Attach(::GetDC(NULL));
CBitmap bm;
bm.CreateCompatibleBitmap(&dc,iBmpWidth,iBmpHeight);
CDC tmpDc;
tmpDc.CreateCompatibleDC(&dc);
CBitmap*pOld = tmpDc.SelectObject(&bm);
if(MxDraw::DrawToDc(MxDraw::GetCurOcxHandle(),
&tmpDc,0,0,iBmpWidth,iBmpHeight,
pt1.x,pt1.y,pt2.x,pt2.y
)
)
{
tmpDc.SelectObject(pOld);
if(SaveBmp(&bm,&dc,sFileName) )
{
AfxMessageBox(_T("保存成功"));
}
else
{
AfxMessageBox(_T("保存失败"));
}
}
else
{
AfxMessageBox(_T("未知原因,保存失败"));
tmpDc.SelectObject(pOld);
}
}
bool CTestCommands::SaveBmp(CBitmap* pBmp,CDC* pDc,const CString& sBmpFilePath)
{
if(sBmpFilePath.IsEmpty() )
{
AfxMessageBox(_T("文件路径为空"));
return false;
}
BITMAP btm;
pBmp->GetBitmap(&btm);
DWORD size = btm.bmWidthBytes * btm.bmHeight;
LPSTR lpData = (LPSTR)GlobalAlloc(GPTR,size);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
///////////////////////////////////
GetDIBits(pDc->GetSafeHdc(),*pBmp,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=54+size;
bfh.bfOffBits=54;
bool isSuc = false;
CFile bf;
if(bf.Open(sBmpFilePath,CFile::modeCreate|CFile::modeWrite))
{
bf.Write(&bfh,sizeof(BITMAPFILEHEADER));
bf.Write(&bih,sizeof(BITMAPINFOHEADER));
bf.Write(lpData,size);
bf.Close();
isSuc = true;
}
else
{
AfxMessageBox(_T("创建文件失败"));
}
GlobalFree(lpData);
return isSuc;
}
浙公网安备 33010602011771号