找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1163|回复: 0

[分享] Non-COM property with list of possible values

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2015-7-22 18:58:05 | 显示全部楼层 |阅读模式

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

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

×
Non-COM property with list of possible valuesBy Adam Nagy
In the Property palette you can find properties which only accept values that are listed for the property. If you want to create such a property then you could do that by creating a new enum type and use that to create a new AcRxValueType that implements IAcRxEnumeration
Header file
/////////////////////////////////////////////////////////////////////
// MyEnumType ///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

typedef enum {} MyEnum;

template<typename ValueType>
class MyValueTypeTemplate : public AcRxValueType
{
public:
  MyValueTypeTemplate(const ACHAR* name,
    const IAcRxEnumeration& pEnum,
    AcRxMemberCollectionConstructorPtr memberConstruct,
    void* userData = NULL):
  AcRxValueType(name,pEnum, sizeof(ValueType),
    memberConstruct, userData) {}
  virtual int subToString(const void* instance, ACHAR* buffer,
    size_t sizeInACHARs, AcRxValueType::StringFormat format)
    const ADESK_OVERRIDE;
  virtual bool subEqualTo(const void* a, const void* b)
    const ADESK_OVERRIDE;
};

template<typename ValueType>
class MyEnumTypeTemplate :
  public MyValueTypeTemplate<ValueType>, public IAcRxEnumeration
{
  AcArray<const AcRxEnumTag*> m_tags;

public:
  MyEnumTypeTemplate(const ACHAR* name,
    AcRxMemberCollectionConstructorPtr memberConstruct,
    void* userData = NULL):
  MyValueTypeTemplate<ValueType>(
    name,*this, memberConstruct, userData) {}

  ~MyEnumTypeTemplate()
  {
    for (int i=m_tags.length()-1;i>=0;i--)
      AcRxMember::deleteMember(m_tags);
  }

  virtual int count() const ADESK_OVERRIDE
  {
    return m_tags.length();
  }

  virtual const AcRxEnumTag& getAt(int i) const ADESK_OVERRIDE
  {
    return *m_tags;
  }

  void append(AcRxEnumTag& tag)
  {
    m_tags.append(&tag);
    void acdbImpSetOwnerForEnumTag(
      const AcRxClass*, AcRxEnumTag*);
    acdbImpSetOwnerForEnumTag(this, &tag);
  }
};

/////////////////////////////////////////////////////////////////////
// MyListProperty ///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

class MyListProperty : public AcRxProperty  
{
public:
  static AcRxObject * makeMyListProperty();

  static const ACHAR * kCategoryName;
  static AcRxCategory * category;

  MyListProperty();
  virtual ~MyListProperty();

  virtual Acad::ErrorStatus subGetValue(
    const AcRxObject* pO, AcRxValue& value) const;

  virtual Acad::ErrorStatus subSetValue(
    AcRxObject* pO, const AcRxValue& value) const;
};


C++ file
/////////////////////////////////////////////////////////////////////
// MyEnumType ///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

void makeMyEnumTypeTemplateProperties(
  class AcRxMemberCollectionBuilder & collectionBuilder, void*);

template<>
int MyEnumTypeTemplate<MyEnum>::subToString(
  const void *instance, ACHAR *buffer, size_t sizeInACHARs,
  AcRxValueType::StringFormat format) const
{
  const ACHAR* formatString = L"%d";
  MyEnum & value = *(MyEnum*)instance;
  if (buffer==NULL)
    return _scwprintf(formatString,value);
  return swprintf_s(buffer,sizeInACHARs,formatString,value);
}

template<>
bool MyEnumTypeTemplate<MyEnum>::subEqualTo(
  const void *a, const void* b) const
{
  MyEnum & v1 = *(MyEnum*)a;
  MyEnum & v2 = *(MyEnum*)b;
  return v1==v2;
}

template<>
struct AcRxValueType::Desc< MyEnum >
{
  __declspec(dllexport) static const AcRxValueType& value() throw();
  static void del();
};

MyEnumTypeTemplate<MyEnum>* s_pMyEnumTypeTemplate = NULL;

const AcRxValueType& AcRxValueType::Desc< MyEnum >::value() throw()
{
  if (s_pMyEnumTypeTemplate==NULL)
  {
    s_pMyEnumTypeTemplate = new MyEnumTypeTemplate<MyEnum>(
      L"MyEnumProperties",&makeMyEnumTypeTemplateProperties);
    AcRxEnumTag* pTag;
    pTag = new  AcRxEnumTag  (L"One", (int)0);
    s_pMyEnumTypeTemplate->append(*pTag);
    pTag = new  AcRxEnumTag  (L"Two", (int)1);
    s_pMyEnumTypeTemplate->append(*pTag);
    pTag = new  AcRxEnumTag  (L"Three", (int)2);
    s_pMyEnumTypeTemplate->append(*pTag);
    pTag = new  AcRxEnumTag  (L"Four", (int)3);
    s_pMyEnumTypeTemplate->append(*pTag);
  }
  return *s_pMyEnumTypeTemplate;
};

// This should be called when the value type is not needed anymore
// Best call it when the module gets unloaded (On_kUnloadAppMsg)
void AcRxValueType::Desc< MyEnum >::del()
{
  if (s_pMyEnumTypeTemplate)
  {
    const ACHAR * name = s_pMyEnumTypeTemplate->name();

    if (acrxSysRegistry())
      acrxClassDictionary->remove(s_pMyEnumTypeTemplate->name());

    s_pMyEnumTypeTemplate = NULL;
  }
};

void makeMyEnumTypeTemplateProperties(
  class AcRxMemberCollectionBuilder & collectionBuilder, void*)
{
}

/////////////////////////////////////////////////////////////////////
// MyListProperty ///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

const ACHAR * MyListProperty::kCategoryName = _T("My Category");

AcRxCategory * MyListProperty::category = NULL;

MyListProperty::MyListProperty() :
AcRxProperty(_T("My List Property"),
  AcRxValueType::Desc< MyEnum >::value())
{
  if (category == NULL)
  {
    AcRxCategory * parent =  AcRxCategory::rootCategory();
    category = parent->findDescendant(kCategoryName);
    if (category == NULL)
      category = new AcRxCategory(kCategoryName, parent);
  }

  // OPM = Object Property Manager / Property Palette

  // Add the Placement attribute to set under which category in
  // the OPM the property will be shown
  attributes().add(new AcRxUiPlacementAttribute(kCategoryName, 0));
  // Add this attribute so that AutoCAD will automatically create
  // the COM wrapper for this property, i.e. it will be visible
  // in OPM
  attributes().add(new AcRxGenerateDynamicPropertiesAttribute());

}     
MyListProperty::~MyListProperty()
{
}

/// <summary>
/// This is called by the system to get the property value for
/// a specific object
/// </summary>
Acad::ErrorStatus MyListProperty::subGetValue(
  const AcRxObject* pO, AcRxValue& value) const
{
  AcDbEntity * ent = AcDbEntity::cast(pO);
  if (ent == NULL)
    return Acad::eNotThatKindOfClass;

  // Get the value from the custom entity
  // or wherever we stored the value
  AEN1MyCircle * pMyCircle = AEN1MyCircle::cast(pO);
  value = AcRxValue(static_cast<MyEnum>(pMyCircle->m_myEnum));  

  return Acad::eOk;
}

/// <summary>
/// This is called by the system to retrieve the property value for
/// a specific object
/// </summary>
Acad::ErrorStatus MyListProperty::subSetValue(
  AcRxObject* pO, const AcRxValue& value) const
{
  AcDbEntity * ent = AcDbEntity::cast(pO);
  if (ent == NULL)
    return Acad::eNotThatKindOfClass;

  const MyEnum * val = rxvalue_cast<MyEnum>(&value);
  if (val == NULL)
    return Acad::eInvalidInput;

  // Set the property of the custom entity
  // or store it wherever we want to
  AEN1MyCircle * pMyCircle = AEN1MyCircle::cast(pO);
  pMyCircle->m_myEnum = *val;  

  return Acad::eOk;
}

/// <summary>
/// This is called to create an instance of our class
/// </summary>
AcRxObject * MyListProperty::makeMyListProperty()
{
  return new MyListProperty();
}



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

本版积分规则

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

GMT+8, 2024-11-23 08:05 , Processed in 0.325618 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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