明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1682|回复: 1

请教:为什么新建的表格(table)没有在图上显示出来

[复制链接]
发表于 2008-3-4 20:31:00 | 显示全部楼层 |阅读模式

void createAndSetDataLink()
{
    Acad::ErrorStatus es;
 AcDbTable *pTbl; 
 pTbl = new AcDbTable();

 const TCHAR *pTblStyle = ACRX_T("MY_Style");
   
    // Set the Table Style
    AcDbDictionary *pDict = NULL;
    AcDbObjectId idTblStyle;
    acdbHostApplicationServices()->workingDatabase()->getTableStyleDictionary(pDict,AcDb::kForWrite);
    es = pDict->getAt(pTblStyle,idTblStyle);
    pDict->close();
   
 
 if(Acad::eOk == es) pTbl->setTableStyle(idTblStyle);
    setTableColAndRows(pTbl, 5, 5);

    pTbl->setTextString(3,3,L"my love");

    pTbl->generateLayout(); // Very very important, else expect crashes later on

  if(NULL == pTbl)
 {
  acutPrintf(ACRX_T("\nSelected entity was not a table!"));
  return;
 }

 // Get an Excel file
 //
 struct resbuf *result;
 int rc;

 if ( (result = acutNewRb(RTSTR)) == NULL)
 {
  pTbl->close();
  acutPrintf(ACRX_T("\nUnable to allocate buffer!"));
  return;
 }

 result->resval.rstring=NULL;

 rc = acedGetFileD( ACRX_T("Excel File"), // Title
  ACRX_T("c:\\"),  // Default pathname 
  ACRX_T("xls"),   // Default extension
  16,      // Control flags
  result);    // The path selected by the user.
 if (rc != RTNORM)
 {
  pTbl->close();
  acutPrintf(ACRX_T("\nError in selecting an EXCEL file!"));
  return;
 }

 // Retrieve the file name from the ResBuf.
 ACHAR fileName[MAX_PATH];
 _tcscpy(fileName, result->resval.rstring);
 rc = acutRelRb(result);

 static ACHAR sMyDataLink[MAX_PATH] = ACRX_T("TLLink");
 // Get the Data Link Manager.
 AcDbDataLinkManager* pDlMan = acdbHostApplicationServices()->workingDatabase()->getDataLinkManager();
 assert(pDlMan);

 AcDbObjectId idDL;
 AcDbDataLink *pDL = NULL; 
 // Check if a Data Link with the name already exists. If so, remove it.
 if( pDlMan->getDataLink(sMyDataLink, pDL, AcDb::kForRead) == Acad::eOk && pDL)
 {
  pDL->close();
  es = pDlMan->removeDataLink(sMyDataLink, idDL);
  if( es != Acad::eOk )
  {
   pTbl->close();
   acutPrintf(ACRX_T("\nError in removing the Data Link!"));
   return;
  }
 }
 

 // Create a Data Link with the name.
 es = pDlMan->createDataLink(ACRX_T("AcExcel"), sMyDataLink, ACRX_T("This is a test for Excel type data link."), fileName, idDL);
 if( es != Acad::eOk )
 {
  pTbl->close();
  acutPrintf(ACRX_T("\nError in creating Data Link!\nPlease check if there is a sheet named 'Sheet1' in the XLS file."));
  return;
 }

 // Open the Data Link.
 es = acdbOpenObject<AcDbDataLink>(pDL, idDL, AcDb::kForWrite);
 if ( es != Acad::eOk || !pDL )
 {
  pTbl->close();
  acutPrintf(ACRX_T("\nError in opening the Data Link object!"));
  return;
 }

 //  Set options of the Data Link
 es = pDL->setOption(AcDb::kDataLinkOptionPersistCache);
 es = pDL->setUpdateOption(pDL->updateOption() | AcDb::kUpdateOptionAllowSourceUpdate);

 // Close the Data Link.
 pDL->close();

 // Set data link to the table object at cell(2,2).
 es = pTbl->setDataLink(0,0, idDL, true);
 if( es != Acad::eOk )
 {
  pTbl->close();
  acutPrintf(ACRX_T("\nError in setting Data Link to the selected table!\nPlease check if there is a sheet named 'Sheet1' in the XLS file."));
  return;
 }
 pTbl->setPosition(AcGePoint3d(0, 0, 0));
 // Don't forget to close the table object.
    pTbl->close();
}

 楼主| 发表于 2008-3-6 22:07:00 | 显示全部楼层

忘记了一条语句

不好意思,原因找到了,  在pTbl->close();语句前面加上addEntToDwg(acdbHostApplicationServices()->workingDatabase(),pTbl);

加上后一切就OK了.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:22 , Processed in 0.153232 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表