一、使用"acdbDisplayPreviewFromDwg"函数
1. 引用说明
ACDB_PORT bool acdbDisplayPreviewFromDwg (
const ACHAR * pszDwgfilename,
void * pPreviewWnd,
const Adesk:: UInt32* pBgColor = nullptr
) ;
2. 使用案例
HWND pWnd;
CFrameWnd * pFrame = ( CFrameWnd* ) GetDlgItem ( IDC_PICTURE) ;
bool bResult = acdbDisplayPreviewFromDwg ( _T ( "填入文件路径" ) , pFrame- > m_hWnd) ;
二、结合AcGs库,重载静态文本,用以预览图形
1. 引用说明
AcGsManager ::AcGsManager界面提供的服务有助于隐藏将AutoCAD和ARX应用程序连接到图形系统(GS)的详 细信息。每个AutoCAD AcDisplay对象都有一个AcGsManager。因此,AcGsManager对象与MDI客户端窗口以及AcDbDatabase对象具有一对一的关系。函数acgsGetGSManager()提供对AcGsManager对象的访问。 AcGsDevice :AcGsDevice是绘图表面的抽象,并封装了向其渲染图形的基础设备。 目前,AcGsDevice对应于GUI显示窗口。 最终,将支持其他类型的设备(例如,硬拷贝,文件[.bmp,.wmf,.dwf]和剪贴板)。一个AcGsDevice拥有一个或多个AcGsView,并且必须管理损坏的区域并将更新委派给这些视图。 此外,它还响应操作系统通知,例如图形系统(GS)客户端从基础设备转发的窗口大小更改。 AcGsView :AcGsView对象对应于GUI窗口的一部分,定义视图的一组相机参数以及要查看的对象的集合。更具体地说,AcGsView可用于表示AutoCAD视口。将AcGsView添加到AcGsDevice。一个视图一次只能属于一个设备。需要告知视图正在查看的内容。客户端添加和删除对应于指定模型的已查看部分的(AcGiDrawable,AcGsModel)对。每个AcGsView都有一个RenderMode,它对应于在该视图中绘制几何图形时采用的算法… AcGsView具有两种状态:交互式和非交互式。默认情况下,AcGsView是非交互式的。这意味着所有方法在调用时立即执行,仅在完成时返回。所有返回值都是可信赖的,并且可以调用所有方法。AcGsModel :AcGsModel类是数据库的抽象。 该数据库可以是AcDbDatabase,它可以是瞬时图形的容器(例如Icon和Grid),或者可以表示另一种持久数据库格式,例如DWF。 最重要的是,AcGsModel为数据库提供了一种机制,可将更改(添加,删除和修改)通知图形系统(GS)。每个AcGsModel都有一个RenderType,它向GS提供有关如何渲染此模型的几何图形的提示。
2. 使用案例
#pragma once
class IMechGsPreviewCtrl : public CStatic
{
public :
IMechGsPreviewCtrl ( ) ;
~ IMechGsPreviewCtrl ( ) ;
private :
AcDbExtents m_Extents;
AcGsView * m_pView;
AcGsDevice * m_pDevice;
AcGsModel * m_pModel;
AcDbDatabase * m_pDb;
protected :
afx_msg void OnPaint ( ) ;
afx_msg void OnSize ( UINT nType, int cx, int cy) ;
DECLARE_MESSAGE_MAP ( )
public :
bool Init ( const CString& strFilePath) ;
bool Init ( AcDbDatabase * pDb) ;
protected :
void ZoomWindow ( ) ;
void Clear ( ) ;
void InitGS ( ) ;
bool InitInner ( AcDbDatabase * pDb) ;
bool GetActiveViewPortInfo ( double & dHeight, double & dWidth, AcGePoint3d& ptViewCenter,
AcGeVector3d& vecViewDir, double & dTwist, bool bViewCenter) ;
void GetAllEnt ( AcDbBlockTableRecord * pBlockTableRecord, AcDbObjectIdArray& EntIdArr) ;
Acad:: ErrorStatus GetEntExtents ( const AcDbObjectId& EntId, AcDbExtents& Extents) ;
void GetEntExtents ( const AcDbObjectIdArray& EntIdArr, AcDbExtents& Extents) ;
void GetMidPoint ( const AcGePoint3d& pt1, const AcGePoint3d& pt2, AcGePoint3d& ptMid) ;
} ;
#include "stdafx.h"
#include "resource.h"
#include "IMechGsPreviewCtrl.h"
IMechGsPreviewCtrl:: IMechGsPreviewCtrl ( )
{
m_pView = nullptr ;
m_pDevice = nullptr ;
m_pModel = nullptr ;
m_pDb = nullptr ;
}
IMechGsPreviewCtrl:: ~ IMechGsPreviewCtrl ( )
{
Clear ( ) ;
}
BEGIN_MESSAGE_MAP ( IMechGsPreviewCtrl, CStatic)
ON_WM_PAINT ( )
ON_WM_SIZE ( )
END_MESSAGE_MAP ( )
void IMechGsPreviewCtrl:: OnPaint ( )
{
CPaintDC dc ( this ) ;
if ( m_pView)
{
m_pView- > invalidate ( ) ;
m_pView- > update ( ) ;
}
}
void IMechGsPreviewCtrl:: OnSize ( UINT nType, int cx, int cy)
{
CRect rect;
if ( m_pDevice)
{
GetClientRect ( & rect) ;
m_pDevice- > onSize ( rect. Width ( ) , rect. Height ( ) ) ;
}
}
void IMechGsPreviewCtrl:: Clear ( )
{
AcGsManager * pGsManager = acgsGetGsManager ( ) ;
ASSERT ( pGsManager) ;
if ( m_pView)
{
m_pView- > eraseAll ( ) ;
if ( m_pDevice)
{
bool b = m_pDevice- > erase ( m_pView) ;
ASSERT ( b) ;
}
AcGsClassFactory * pFactory = pGsManager- > getGSClassFactory ( ) ;
ASSERT ( pFactory) ;
pFactory- > deleteView ( m_pView) ;
m_pView = nullptr ;
}
if ( m_pModel)
{
pGsManager- > destroyAutoCADModel ( m_pModel) ;
m_pModel = nullptr ;
}
if ( m_pDevice)
{
pGsManager- > destroyAutoCADDevice ( m_pDevice) ;
m_pDevice = nullptr ;
}
if ( m_pDb)
{
delete m_pDb;
m_pDb = nullptr ;
}
}
bool IMechGsPreviewCtrl:: Init ( const CString& strFilePath)
{
Clear ( ) ;
m_pDb = new AcDbDatabase ( false , true ) ;
Acad:: ErrorStatus es = m_pDb- > readDwgFile ( strFilePath) ;
if ( es != Acad:: eOk)
{
delete m_pDb;
m_pDb = NULL ;
}
return InitInner ( m_pDb) ;
}
bool IMechGsPreviewCtrl:: Init ( AcDbDatabase * pDb)
{
Clear ( ) ;
return InitInner ( pDb) ;
}
bool IMechGsPreviewCtrl:: GetActiveViewPortInfo ( double & dHeight, double & dWidth, AcGePoint3d& ptViewCenter,
AcGeVector3d& vecViewDir, double & dTwist, bool bViewCenter)
{
AcDbDatabase * pDb = acdbHostApplicationServices ( ) - > workingDatabase ( ) ;
if ( pDb == NULL )
{
return false ;
}
AcDbViewportTable * pVTable = NULL ;
Acad:: ErrorStatus es = pDb- > getViewportTable ( pVTable, AcDb:: kForRead) ;
if ( es == Acad:: eOk)
{
AcDbViewportTableRecord * pViewPortRec = NULL ;
es = pVTable- > getAt ( _T ( "*Active" ) , pViewPortRec, AcDb:: kForRead) ;
if ( es == Acad:: eOk)
{
dHeight = pViewPortRec- > height ( ) ;
dWidth = pViewPortRec- > width ( ) ;
if ( bViewCenter == true )
{
struct resbuf rb;
memset ( & rb, 0 , sizeof ( struct resbuf) ) ;
acedGetVar ( _T ( "VIEWCTR" ) , & rb) ;
ptViewCenter = AcGePoint3d ( rb. resval. rpoint[ X] , rb. resval. rpoint[ Y] , rb. resval. rpoint[ Z] ) ;
}
else
{
ptViewCenter = pViewPortRec- > target ( ) ;
}
vecViewDir = pViewPortRec- > viewDirection ( ) ;
dTwist = pViewPortRec- > viewTwist ( ) ;
}
pVTable- > close ( ) ;
pViewPortRec- > close ( ) ;
}
return ( true ) ;
}
void IMechGsPreviewCtrl:: InitGS ( )
{
AcGsManager * pGsManager = acgsGetGsManager ( ) ;
ASSERT ( pGsManager) ;
AcGsClassFactory * pFactory = pGsManager- > getGSClassFactory ( ) ;
ASSERT ( pFactory) ;
m_pDevice = pGsManager- > createAutoCADDevice ( m_hWnd) ;
ASSERT ( m_pDevice) ;
CRect rect;
GetClientRect ( & rect) ;
m_pDevice- > onSize ( rect. Width ( ) , rect. Height ( ) ) ;
m_pView = pFactory- > createView ( ) ;
ASSERT ( m_pView) ;
m_pModel = pGsManager- > createAutoCADModel ( ) ;
ASSERT ( m_pModel) ;
m_pDevice- > add ( m_pView) ;
double dHeight = 0.0 , dWidth = 0.0 , dTwist = 0.0 ;
AcGePoint3d ptViewCenter;
AcGeVector3d vecViewDir;
GetActiveViewPortInfo ( dHeight, dWidth, ptViewCenter, vecViewDir, dTwist, true ) ;
m_pView- > setView ( ptViewCenter + vecViewDir, ptViewCenter, AcGeVector3d ( 0.0 , 1.0 , 0.0 ) , 1.0 , 1.0 ) ;
}
bool IMechGsPreviewCtrl:: InitInner ( AcDbDatabase * pDb)
{
if ( pDb == nullptr )
{
m_pDb = new AcDbDatabase ( true , true ) ;
}
else
{
m_pDb = pDb;
}
if ( m_pDb == nullptr )
{
return false ;
}
Acad:: ErrorStatus es = Acad:: eOk;
AcDbBlockTable * pBlockTable = nullptr ;
AcDbBlockTableRecord * pBlockTableRecord = NULL ;
if ( ( es = m_pDb- > getBlockTable ( pBlockTable, AcDb:: kForRead) ) != Acad:: eOk)
{
return false ;
}
if ( ( es = pBlockTable- > getAt ( ACDB_MODEL_SPACE, pBlockTableRecord, AcDb:: kForRead) ) != Acad:: eOk)
{
pBlockTableRecord- > close ( ) ;
return false ;
}
pBlockTable- > close ( ) ;
AcDbObjectIdArray EntIdArr;
GetAllEnt ( pBlockTableRecord, EntIdArr) ;
GetEntExtents ( EntIdArr, m_Extents) ;
InitGS ( ) ;
m_pView- > add ( pBlockTableRecord, m_pModel) ;
pBlockTableRecord- > close ( ) ;
ZoomWindow ( ) ;
return TRUE;
}
void IMechGsPreviewCtrl:: ZoomWindow ( )
{
AcGePoint3d ptViewCenter;
GetMidPoint ( m_Extents. maxPoint ( ) , m_Extents. minPoint ( ) , ptViewCenter) ;
double dLenght = m_Extents. maxPoint ( ) . x - m_Extents. minPoint ( ) . x;
double dWidth = m_Extents. maxPoint ( ) . y - m_Extents. minPoint ( ) . y;
m_pView- > setView ( ptViewCenter + AcGeVector3d:: kZAxis, ptViewCenter, AcGeVector3d:: kYAxis, dLenght * 1.05 , dWidth * 1.05 ) ;
OnPaint ( ) ;
}
void IMechGsPreviewCtrl:: GetMidPoint ( const AcGePoint3d& pt1, const AcGePoint3d& pt2, AcGePoint3d& ptMid)
{
ptMid. x = 0.5 * ( pt1. x + pt2. x) ;
ptMid. y = 0.5 * ( pt1. y + pt2. y) ;
ptMid. z = 0.5 * ( pt1. z + pt2. z) ;
}
void IMechGsPreviewCtrl:: GetAllEnt ( AcDbBlockTableRecord * pBlockTableRecord, AcDbObjectIdArray& EntIdArr)
{
EntIdArr. setPhysicalLength ( 0 ) ;
AcDbBlockTableRecordIterator * pIter = nullptr ;
if ( pBlockTableRecord- > newIterator ( pIter) == Acad:: eOk)
{
for ( pIter- > start ( ) ; ! pIter- > done ( ) ; pIter- > step ( ) )
{
AcDbObjectId EntId;
if ( Acad:: eOk == pIter- > getEntityId ( EntId) )
{
EntIdArr. append ( EntId) ;
}
}
delete pIter;
pIter = nullptr ;
}
return ;
}
Acad:: ErrorStatus IMechGsPreviewCtrl:: GetEntExtents ( const AcDbObjectId& EntId, AcDbExtents& Extents)
{
Acad:: ErrorStatus es;
AcDbEntityPointer pEntity ( EntId, AcDb:: kForRead) ;
if ( pEntity. openStatus ( ) == Acad:: eOk)
{
AcDbBlockReference * pBlkRef = AcDbBlockReference:: cast ( pEntity) ;
if ( pBlkRef)
{
es = pBlkRef- > geomExtentsBestFit ( Extents) ;
}
else
{
es = pEntity- > getGeomExtents ( Extents) ;
}
}
return ( es) ;
}
void IMechGsPreviewCtrl:: GetEntExtents ( const AcDbObjectIdArray& EntIdArr, AcDbExtents& Extents)
{
for ( int i = 0 ; i < EntIdArr. length ( ) ; i++ )
{
AcDbExtents ExtentsTemp;
if ( GetEntExtents ( EntIdArr[ i] , ExtentsTemp) == Acad:: eOk)
{
Extents. addExt ( ExtentsTemp) ;
}
}
}