找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 510|回复: 0

[每日一码] 使用COM API插入图块

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2017-7-14 12:19:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Here is a sample ObjectARX code to demonstrate the usage of InsertBlock method of the AutoCAD COM API.
  1. #pragma warning( disable : 4278 )
  2. // Makes change to the tlb name based on the AutoCAD version.
  3. // For ex : acax18enu.tlb for AutoCAD 2010/2011 and 2012
  4. //          acax19enu.tlb for AutoCAD 2013
  5. #import "acax19ENU.tlb" no_implementation raw_interfaces_only named_guids
  6. #pragma warning( default : 4278 )

  7. #include <acadi.h>
  8. static void ADSProjectInsertBlock(void)
  9. {
  10.     int ret = RTNORM;

  11.     TCHAR drawingFilePath[500];
  12.     drawingFilePath[0] = _T('\0');
  13.     ret = acedGetString (
  14.                             NULL,
  15.                             _T("Enter file path : "),
  16.                             drawingFilePath
  17.                         );
  18.     if(ret != RTNORM)
  19.         return;

  20.     ads_point insertionPoint;
  21.     ret = acedGetPoint(
  22.                         NULL,
  23.                         _T("\nEnter insertion point: "),
  24.                         insertionPoint
  25.                        );
  26.     if(ret != RTNORM)
  27.         return;

  28.     CWinApp *pApp = acedGetAcadWinApp();
  29.     HRESULT hRes;
  30.     LPDISPATCH pDisp=NULL;

  31.     if(!pApp)
  32.         return;

  33.     pDisp=pApp->GetIDispatch(TRUE);
  34.     if    (!pDisp)
  35.           return;

  36.     CComPtr<AutoCAD::IAcadApplication>  pComApp;
  37.     hRes=pDisp->QueryInterface(
  38.                                 IID_IAcadApplication,
  39.                                 (void**)&pComApp
  40.                               );
  41.     if (FAILED(hRes))
  42.           return;

  43.     CComPtr<AutoCAD::IAcadDocument> pComDoc;
  44.     hRes=pComApp->get_ActiveDocument(&pComDoc);
  45.     if(FAILED(hRes))
  46.         return;

  47.     CComPtr<AutoCAD::IAcadModelSpace> pMSpace = NULL;
  48.     pComDoc->get_ModelSpace(&pMSpace);

  49.     _bstr_t block(drawingFilePath);
  50.     CComPtr<AutoCAD::IAcadBlockReference> pBlkRef = NULL;

  51.     SAFEARRAYBOUND rgsaBound;
  52.     rgsaBound.lLbound = 0L;
  53.     rgsaBound.cElements = 3;
  54.     SAFEARRAY* pInsertionPoint = NULL;
  55.     pInsertionPoint = SafeArrayCreate(VT_R8, 1, &rgsaBound);

  56.     for(long i = 0; i < 3; i++)
  57.     {
  58.         double value = insertionPoint;
  59.         SafeArrayPutElement(
  60.                                 pInsertionPoint,
  61.                                 &i,
  62.                                 &value
  63.                             );
  64.     }

  65.     VARIANT vInsertionPoint;
  66.     VariantInit(&vInsertionPoint);
  67.     V_VT(&vInsertionPoint) = VT_ARRAY | VT_R8;
  68.     V_ARRAY(&vInsertionPoint) = pInsertionPoint;

  69.     double scaleX = 1.0;
  70.     double scaleY = 1.0;
  71.     double scaleZ = 1.0;
  72.     double rotation = 0.0;

  73.     pMSpace->InsertBlock(
  74.                             vInsertionPoint,
  75.                             block,
  76.                             scaleX,
  77.                             scaleY,
  78.                             scaleZ,
  79.                             rotation,
  80.                             vtMissing,
  81.                             &pBlkRef
  82.                         );

  83.     VariantClear(&vInsertionPoint);
  84. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-19 18:38 , Processed in 0.255172 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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