Retrieving entity properties using AccoreConsole

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

The “dumpallproperties” is an easy way to list all the properties of an entity using Lisp. Unfortunately, this does not work when used in the script file along with AccoreConsole. A simple way to workaround this and display the entity properties in AccoreConsole, is to create a CRX plugin that uses the Non-COM property system to display the entity properties. 

If you are new to the Non-COM property system, please refer to this blog post : DevTV : Non-COM Property System

The code in this blog post uses portions of the code from that DevTV in a CRX application. Here is the relevant code and the complete sample project can be downloaded here :

Download EntProps_CRX

 <span>static</span><span>  <span>void</span><span>  ListEntityProperties(<span>void</span><span> )</span></span></span>
 <span>{</span>
 	TCHAR entityHandle[133];
 	<span>if</span><span>  (acedGetString(</span>
 		Adesk::kFalse, 
 		_T(<span>"\nEnter entity handle : "</span><span> ), </span>
 		entityHandle) != RTNORM)
 	<span>{</span>
 		acutPrintf(ACRX_T(<span>"\nInvalid entity handle."</span><span> ));</span>
 		<span>return</span><span> ;</span>
 	<span>}</span>
 	CUtils::DisplayProperties(entityHandle);
 <span>}</span>
 
 <span>void</span><span>  initApp() </span>
 <span>{</span> 
     acedRegCmds->addCommand(ACRX_T(<span>"MY_COMMANDS"</span><span> ), </span>
                             ACRX_T(<span>"EntProps"</span><span> ), </span>
                             ACRX_T(<span>"EntProps"</span><span> ), </span>
                             ACRX_CMD_TRANSPARENT, 
                             ListEntityProperties); 
 <span>}</span> 
 
 <span>#include</span><span>  <span>"rxmember.h"</span><span> </span></span>
 <span>#include</span><span>  <span>"rxvaluetype.h"</span><span> </span></span>
 <span>#include</span><span>  <span>"rxattrib.h"</span><span> </span></span>
 <span>#include</span><span>  <span>"rxprop.h"</span><span> </span></span>
 <span>#include</span><span>  <span>"dbobjptr.h"</span><span> </span></span>
 
 <span>// Utils.cpp</span><span> </span>
 <span>void</span><span>  CUtils::DisplayProperties(LPCTSTR handleStr)</span>
 <span>{</span>
 	Acad::ErrorStatus es;
 	AcApDocument *pActiveDoc 
 		= acDocManager->mdiActiveDocument();
 	AcDbDatabase *pDB = pActiveDoc->database();
 
 	AcDbObjectId id = AcDbObjectId::kNull;
 	AcDbHandle objHandle(handleStr);
     es = pDB->getAcDbObjectId(id, <span>false</span><span> , objHandle);</span>
     <span>if</span><span>  (es != Acad::eOk) </span>
 	<span>{</span>
         acutPrintf(
 		_T(<span>"\nCould not translate handle to objectId"</span><span> ));</span>
     <span>}</span>
 
 	AcDbObjectPointer<AcDbEntity> entity(id, AcDb::kForRead);
 
 	AcRxMemberIterator * iter = 
 		AcRxMemberQueryEngine::theEngine()->newMemberIterator
 		(entity);   
 
 	<span>for</span><span>  (; !iter->done(); iter->next())</span>
 	<span>{</span>
 		printValues(entity, iter->current());
 	<span>}</span>
 <span>}</span>
 
 <span>void</span><span>  CUtils::getAttInfo(</span>
 	<span>const</span><span>  AcRxAttribute * att, </span>
 	<span>const</span><span>  AcRxObject * member, </span>
 	AcString & attInfo)
 <span>{</span>
 	<span>if</span><span>  (att->isA() == AcRxCOMAttribute::desc())</span>
 	<span>{</span>
 		AcRxCOMAttribute * a = AcRxCOMAttribute::cast(att);
 		attInfo.format(_T(<span>"\n%s - %s"</span><span> ), </span>
 			att->isA()->name(), a->name()); 
 	<span>}</span>
 	<span>else</span><span>  <span>if</span><span>  (att->isA() == AcRxUiPlacementAttribute::desc())</span></span>
 	<span>{</span>
 		AcRxUiPlacementAttribute * a 
 			= AcRxUiPlacementAttribute::cast(att);
 		attInfo.format(
 		_T(<span>"\n%s - %s - %f"</span><span> ), </span>
 		att->isA()->name(), 
 		a->getCategory(member),
 		a->getWeight(member)); 
 	<span>}</span>
 	<span>else</span><span> </span>
 	<span>{</span>
 		attInfo.format(_T(<span>"\n%s"</span><span> ), att->isA()->name()); </span>
 	<span>}</span>
 <span>}</span>
 
 <span>void</span><span>  CUtils::printValues(</span>
 	AcRxObject * entity, 
 	<span>const</span><span>  AcRxMember * member)</span>
 <span>{</span>
 	Acad::ErrorStatus err = Acad::eOk;
 
 	AcString strValue;
 	AcRxProperty * prop = AcRxProperty::cast(member);
 	<span>if</span><span>  (prop != NULL)</span>
 	<span>{</span>
 		AcRxValue value;
 
 		<span>if</span><span>  ((err = prop->getValue(entity, value)) </span>
 			== Acad::eOk) 
 		<span>{</span>
 			ACHAR * szValue = NULL;
 
 			<span>int</span><span>  buffSize = value.toString(NULL, 0);</span>
 			<span>if</span><span>  (buffSize > 0)</span>
 			<span>{</span>
 				buffSize++;
 				szValue = <span>new</span><span>  ACHAR[buffSize];</span>
 				value.toString(szValue, buffSize);
 			<span>}</span>
 
 			strValue.format(
 				_T(<span>"%s = %s"</span><span> ), </span>
 				value.type().name(), 
 				(szValue == NULL) ? _T(<span>"none"</span><span> ) : szValue);  </span>
 
 			<span>if</span><span>  (szValue)</span>
 				<span>delete</span><span>  szValue;</span>
 		<span>}</span>
 		<span>else</span><span> </span>
 		<span>{</span>
 			strValue.format(_T(<span>"Error Code = %d"</span><span> ), err);</span>
 		<span>}</span>
 	<span>}</span>
 
 	AcString str;
 	str.format(_T(<span>"\n%s - %s [%s]"</span><span> ), member->isA()->name(), </span>
 		member->name(), strValue.kACharPtr());
 	acutPrintf(str);
 
 	<span>const</span><span>  AcRxAttributeCollection & atts </span>
 		= member->attributes();
 		
 	<span>for</span><span>  (<span>int</span><span>  i = 0; i < atts.count(); i++)</span></span>
 	<span>{</span>
 		<span>const</span><span>  AcRxAttribute * att = atts.getAt(i); </span>
 
 		AcString attInfo;
 		getAttInfo(att, member, attInfo);
 
 		acutPrintf(attInfo);
 	<span>}</span>
 
 	<span>if</span><span>  (member->children() != NULL)</span>
 	<span>{</span>
 		<span>for</span><span>  (<span>int</span><span>  i = 0; </span></span>
 			i < member->children()->length(); i++)
 		<span>{</span>
 			<span>const</span><span>  AcRxMember * subMember </span>
 				= member->children()->at(i);
 			printValues(entity, subMember);  
 		<span>}</span>
 	<span>}</span>
 <span>}</span>
 

The CRX plugin exposes the “EntProps” command which requires handle of the entity for which the properties are to be retrieved as its input.

Here is a sample script file to load the crx and invoke “EntProps” command.

(arxload “D:\Temp\CrxTest1.crx”)
EntProps
4C4   ;; Handle of the entity for which properties are to be displayed


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading