luowy 发表于 2009-11-25 10:32:00

[原创]executeInApplicationContext及appContextNewDocument

昨天看了2个帖子,都是关于executeInApplicationContext的,初初觉得这个比较麻烦,但后来发现并不那么神秘。
以下是源码:static void RaLfAATest_sss(void)
{
Acad::ErrorStatus es;
AcDbDatabase *pNewDB = creatNewDB();//根据函数返回新建数据库指针

if ( pNewDB == NULL )
{
   acutPrintf( L"Failed To Create A New DataBase!");
   delete pNewDB;
   return;
}
AcApDocument *pNewDoc = acDocManager->document( pNewDB );//获取当前数据库的文档指针
es = acDocManager->lockDocument( pNewDoc );//重要,锁定新建文档

if ( es != Acad::eOk )
{
   acutPrintf(L"Failed to Lock the Document Associated with the New DataBase!" );
   delete pNewDB;
   return;
}//状态判断
AcDbBlockTable *pBT = NULL;
es = pNewDB->getBlockTable( pBT, AcDb::kForRead );
if ( es != Acad::eOk )
{
   acutPrintf(ACRX_T("Failed to Get the BlockTable in the New DataBase! \nError code: %s"), acadErrorStatusText( es ) );
   delete pNewDB;
   return;
}//状态判断
AcDbBlockTableRecord *pBTR = NULL;
es = pBT->getAt( ACDB_MODEL_SPACE, pBTR, AcDb::kForWrite );
if ( es != Acad::eOk )
{
   acutPrintf(ACRX_T("Failed to Get the ModalSpace in BlockTableRecord in the New DataBase! \nError code: %s"), acadErrorStatusText( es ) );
   pBT->close();
   delete pNewDB;
   return;
}//状态判断
pBT->close();
AcDbLine *pLine = new AcDbLine( AcGePoint3d(0,0,0), AcGePoint3d(100,100,100) );
es = pBTR->appendAcDbEntity( pLine );
if ( es != Acad::eOk )
{
   acutPrintf(ACRX_T("Failed to Get the ModalSpace in BlockTableRecord in the New DataBase! \nError code: %s"), acadErrorStatusText( es ) );
   pBT->close();
   pBTR->close();
   pLine->close();
   delete pNewDB;
   return;
}//状态判断
pLine->close();
pBTR->close();
   
acDocManager->unlockDocument( pNewDoc );//对新数据库操作完毕,及时释放锁定的文档
pNewDoc->transactionManager()->flushGraphics();//刷新文档,似乎没用
}

AcDbDatabase *creatNewDB()
{
Acad::ErrorStatus es;
AcDbDatabase* pNewDb=NULL;
static ACHAR pData[] =L"acad.dwt";
acDocManager->executeInApplicationContext(CreateDoc,(void *)pData);
int docCount = acDocManager->documentCount();
AcApDocument* npDoc;
AcApDocumentIterator *pDocItr = acDocManager->newAcApDocumentIterator();
//获取新建立文档的指针
for ( int i = 0 ; !pDocItr->done(); pDocItr->step(), i++ )
{
if ( i == docCount-1 )
{
   npDoc = pDocItr->document();
   break;
}
}
delete pDocItr;

pNewDb = npDoc->database();//获取新建立文档的数据库指针
if(!pNewDb) returnNULL; //如果创建失败,数据库为NULL,我认为这个可以省略
returnpNewDb;
}
void CreateDoc( void *pData)
{
Acad::ErrorStatus es;
AcApDocument* pDoc = acDocManager->mdiActiveDocument(); //记录当前文档

if (acDocManager->isApplicationContext())
{
es = acDocManager->appContextNewDocument((const ACHAR *)pData);
es = acDocManager->activateDocument( pDoc );//这里将文档切换回原来的文档,以便该文档继续执行代码
}
}

cad新学生 发表于 2009-11-25 16:12:00

谢谢!最近正研究不明白这呢!强顶!

cad新学生 发表于 2009-11-25 16:48:00

<p>可是Lz为甚麽创建的图形并不显示出来,保存后再打开才显示?能不能打开dwg文件读写数据库后会立即显示图形?</p><p></p>

luowy 发表于 2009-11-25 17:13:00

<p>哎,剩下的你去琢磨吧,我已经研究了1天了</p>

cad新学生 发表于 2009-11-26 08:59:00

哦,呵呵!还是谢谢你!

wyh881023 发表于 2010-10-11 17:20:00

很强啊 困扰了我好久啊

ws_ong123 发表于 2011-11-21 20:41:12

c:\Users\Administrator\Documents\Visual Studio Projects\zcapp50\acrxEntryPoint.cpp(210): error C2664: “AcApDocManager::executeInApplicationContext” : 不能将参数 1 从“void (void)”转换为“void (__cdecl *)(void *)”

ws_ong123 发表于 2011-11-21 20:41:47

acDocManager->executeInApplicationContext(CreateDoc,(void*)pData);

homoko 发表于 2012-6-27 21:07:50

ws_ong123 发表于 2011-11-21 20:41 static/image/common/back.gif
c:\Users\Administrator\Documents\Visual Studio Projects\zcapp50\acrxEntryPoint.cpp(210): error C2664 ...

查资料到此处,解决此问题的方法是把CreateDoc这个函数设置为静态函数即可
页: [1]
查看完整版本: [原创]executeInApplicationContext及appContextNewDocument