找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1405|回复: 2

[分享] [转帖]How to obtain AcDb3dSolid OPM Properties in AutoCAD using ObjectARX/C++

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2015-11-12 08:34:36 | 显示全部楼层 |阅读模式

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

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

×
How to obtain AcDb3dSolid OPM Properties in AutoCAD using ObjectARX/C++
by Fenton Webb
Someone just asked me how to obtain the Position, Height, Length, etc. from a 3D Box in AutoCAD. Actually, from a programming point of view, it’s kind of involved. The reason is because what we know as a Box, under the hood, is actually a generalized AcDb3dSolid object, generalized meaning that an AcDb3dSolid can also represent a Torus, Sphere, Cone etc., and, because an AcDb3dSolid can be all of these object types (and more, because you can Push, Pull, Union, Boolean and also Subtract these objects into whatever you want) the object properties that are displayed in the Property window of AutoCAD can vary a lot. We achieve this varying property set by means of an AutoCAD COM API known as Dynamic properties. We also have a slightly different version known as Static properties.
Dynamic Properties can be created on the fly, whereby Static properties are “hard coded” (easiest way to describe) into the object. I should also mention that we have two types of Dynamic properties, Per-Class registered and Per-Instance registered – AcDb3dSolid uses Per-Instance because it is the instance (in the drawing) defines the types of properties that it needs to expose… If you want to see these in action, along with some Static properties, you should check out my QuickProperties ObjectARX sample in the 2009 version of the SDK.
One point to note in the code is, you’ll see me obtaining the Length, Width, Height etc. properties one way, then the Position property in another - that’s because the Length, Width, Height are all Dynamic properties, and the Position is a Static property.
Now for the code…


  1. // by Fenton Webb, DevTech, Autodesk 28/May/2010

  2.                 ads_point pnt;
  3.                 ads_name ename;

  4.                 // pick a solid

  5.                 int res = acedEntSel(_T("\nPick a Solid : "), ename, pnt);

  6.                 // if ok

  7.                 if (res == RTNORM)
  8.                 {
  9.                         AcDbObjectId id;

  10.                         // convert from an ADS ename to ObjectARX ObjectId

  11.                         acdbGetObjectId(id, ename);

  12.                         // get the IUknown of the entity, we’ll need it to get the properties for the instance and also so we can set them

  13.                         CComPtr<IUnknown> entIUnknown = NULL;
  14.                         HRESULT hr = AcAxGetIUnknownOfObject(&entIUnknown, id, acedGetAcadWinApp()->GetIDispatch(TRUE));
  15.                         double length=0.001, height=0.001, width=0.001;

  16.                         // get the dynamic properties to obtain Box info

  17.                         AcRxClass* pAcrxClass = AcDb3dSolid::desc();

  18.                         // Per-instance dynamic property managers

  19.                         OPMPerInstancePropertyExtension* pPerInstanceExtension = GET_OPM_PERINSTANCE_EXTENSION_PROTOCOL(pAcrxClass);

  20.                         if (NULL != pPerInstanceExtension)

  21.                         {

  22.                                 COleSafeArray sourceNames;
  23.                                 CComBSTR sourceName;

  24.                                 // get the property source names stored on the extension object

  25.                                 pPerInstanceExtension->GetObjectPropertySourceNames((LPVARIANT)sourceNames);

  26.                                 // find out how many we have

  27.                                 long start = 0;
  28.                                 long end = 0;
  29.                                 sourceNames.GetLBound(1, &start);
  30.                                 sourceNames.GetUBound(1, &end);

  31.                                 // loop the property sources

  32.                                 for (long i=start; i<=end; ++i)

  33.                                 {

  34.                                         // extract each one of the property sources out

  35.                                         CComPtr<IPropertySource> propertySource;
  36.                                         sourceNames.GetElement(&i, (void*)&sourceName);

  37.                                         // GetPropertySourceAt does AddRef, so we directly assign pointer here.

  38.                                         propertySource.p = GET_OPM_PERINSTANCE_PROPERTY_SOURCES()->GetPropertySourceAt(&sourceName);

  39.                                         // if we have one

  40.                                         if (propertySource.p)
  41.                                         {

  42.                                                 // if ok

  43.                                                 if (SUCCEEDED(hr))
  44.                                                 {

  45.                                                         // extract the Dynamic properties from it

  46.                                                         COleSafeArray props;

  47.                                                         hr = propertySource->GetProperties(entIUnknown, props);

  48.                                                         // if ok

  49.                                                         if (SUCCEEDED(hr))
  50.                                                         {
  51.                                                                 // loop all the dynamic properties that we have

  52.                                                                 long start = 0;
  53.                                                                 long end = 0;
  54.                                                                 props.GetLBound(1, &start);
  55.                                                                 props.GetUBound(1, &end);
  56.                                                                 for (long i=start; i<=end; ++i)

  57.                                                                 {

  58.                                                                         // first get the iUknown to make sure we have something

  59.                                                                         CComPtr<IUnknown> unknown = NULL;
  60.                                                                         props.GetElement(&i, (void*)&unknown);

  61.                                                                         // if we have

  62.                                                                         if (unknown.p)
  63.                                                                         {

  64.                                                                                 // try and cast it to an updated IDynamicProperty2

  65.                                                                                 CComQIPtr<IDynamicProperty2> dynProp = unknown;

  66.                                                                                 // if ok

  67.                                                                                 if (dynProp.p)
  68.                                                                                 {
  69.                                                                                         CComBSTR propName;

  70.                                                                                         // extract out the property name

  71.                                                                                         dynProp->GetDisplayName(&(propName.m_str));

  72.                                                                                         // get the value

  73.                                                                                         COleVariant getter;
  74.                                                                                         dynProp->GetCurrentValueData(entIUnknown, getter);

  75.                                                                                         // find out which property we are looking at
  76.                                                                                         // extend as you see fit

  77.                                                                                         if (CString(propName) == _T("Length"))
  78.                                                                                                 length = ((*(tagVARIANT*)(&getter))).dblVal;

  79.                                                                                         if (CString(propName) == _T("Width"))
  80.                                                                                                 width = ((*(tagVARIANT*)(&getter))).dblVal;

  81.                                                                                         if (CString(propName) == _T("Height"))
  82.                                                                                                 height = ((*(tagVARIANT*)(&getter))).dblVal;
  83.                                                                                 }
  84.                                                                         }
  85.                                                                 }
  86.                                                         }
  87.                                                 }
  88.                                         }
  89.                                 }

  90.                                 // now get the position from the solid

  91.                                 CComQIPtr<IAcad3DSolid> solidCom(entIUnknown);
  92.                                 if(solidCom != NULL)
  93.                                 {
  94.                                         COleVariant olePosition;
  95.                                         hr = solidCom->get_Position(&olePosition);
  96.                                         AcAxPoint3d position = olePosition;

  97.                                         minPnt.x = position.x - length/2.0;
  98.                                         minPnt.y = position.y - width/2.0;
  99.                                         minPnt.z = position.z - height/2.0;
  100.                                 }

  101.                         }
复制代码
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2015-11-13 08:31:43 | 显示全部楼层
艾玛~~~有点难理解啊....:(
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 5060个

财富等级: 富甲天下

发表于 2018-2-23 15:25:51 | 显示全部楼层
如果对象类形为任意呢?比如选中的是circle,甚至天正单行文字。可以通过OPM来获得对象属性吗?我改了一下,发现sourceNames.GetUBound(1, &end);这一句end为-1,运行不下去了。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 15:53 , Processed in 0.363531 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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