ObjectARX通过选定的实体获取所有组名示例

效果

方式一

通过实体所在对象的永久反应器获取组对象

		ads_name ent;
		ads_point pt;
		if (RTNORM != acedEntSel(_T("\n选择对象: "),ent,pt))
		{
			return;
		}
		AcDbObjectId objId;
		acdbGetObjectId(objId,ent);
		AcDbEntityPointer pEnt(objId,AcDb::kForRead);
		Acad::ErrorStatus es=pEnt.openStatus();
		if (Acad::eOk != es)
		{
			acutPrintf(_T("\n打开对象失败,错误码=%s"),acadErrorStatusText(es));
			return;
		}
		const AcDbVoidPtrArray *pReactors=pEnt->reactors();
		if (pReactors==NULL || pReactors->length()<1)
		{
			acutPrintf(_T("\n该对象没有组!"));
			return;
		}
		CStringArray strGroupNames;
		for (int i=0;i<pReactors->length();i++)
		{
			void* pSomething = pReactors->at(i);
			if (pSomething==NULL) continue;
			if (!acdbIsPersistentReactor(pSomething)) continue;
			AcDbObjectId persReactorId=acdbPersistentReactorObjectId(pSomething);
			AcDbObjectPointer<AcDbGroup> pGroup(persReactorId,AcDb::kForRead);
			if (Acad::eOk != es) continue;			
			strGroupNames.Add(pGroup->name());
		}
		int nCount=strGroupNames.GetCount();
		if (nCount<1)
		{
			acutPrintf(_T("\n该对象没有组!"));
			return;
		}
		acutPrintf(_T("\n该对象拥有 %d 个组"),nCount);
		for (int i=0;i<nCount;i++)
		{
			acutPrintf(_T("\n第 %d 个组名为 %s ."),i+1,strGroupNames.GetAt(i));
		}

方式二

通过遍历组字典判断组是否包含实体对象来获取组对象

		ads_name ent;
		ads_point pt;
		if (RTNORM != acedEntSel(_T("\n选择对象: "),ent,pt))
		{
			return;
		}
		AcDbObjectId objId;
		acdbGetObjectId(objId,ent);
		AcDbEntityPointer pEnt(objId,AcDb::kForRead);
		Acad::ErrorStatus es=pEnt.openStatus();
		if (Acad::eOk != es)
		{
			acutPrintf(_T("\n打开对象失败,错误码=%s"),acadErrorStatusText(es));
			return;
		}
		AcDbObjectId groupDicId=acdbHostApplicationServices()->workingDatabase()->groupDictionaryId();
		AcDbObjectPointer<AcDbDictionary> pGroupDic(groupDicId,AcDb::kForRead);
		es=pGroupDic.openStatus();
		if (Acad::eOk != es)
		{
			acutPrintf(_T("\n打开组字典失败,错误码=%s"),acadErrorStatusText(es));
			return;
		}
		CStringArray strGroupNames;
		AcDbDictionaryIterator *pIter= pGroupDic->newIterator();
		for (;!pIter->done();pIter->next())
		{
			AcDbObjectId groupId= pIter->objectId();
			AcDbObjectPointer<AcDbGroup> pGroup(groupId,AcDb::kForRead);
			es = pGroup.openStatus();
			if (Acad::eOk != es)
			{
				acutPrintf(_T("\n打开组失败,错误码=%s"),acadErrorStatusText(es));
				continue;
			}
			if (pGroup->has(pEnt))
			{
				strGroupNames.Add(pGroup->name());
			}			
		}
		if (pIter!=NULL)
		{
			delete pIter;
		}
		int nCount=strGroupNames.GetCount();
		if (nCount<1)
		{
			acutPrintf(_T("\n该对象没有组!"));
			return;
		}
		acutPrintf(_T("\n该对象拥有 %d 个组"),nCount);
		for (int i=0;i<nCount;i++)
		{
			acutPrintf(_T("\n第 %d 个组名为 %s ."),i+1,strGroupNames.GetAt(i));
		}
posted @ 2021-07-04 13:02  edata  阅读(123)  评论(0编辑  收藏  举报