hulei_accp 发表于 2005-9-9 11:19:00

对象的关联

<P>exam10b.cpp</P>
<P>#include "string.h"<BR>#include "stdlib.h"<BR>#include "aced.h"<BR>#include "dbents.h"<BR>#include "dbsymtb.h"<BR>#include "dbapserv.h"<BR>#include "adslib.h"</P>
<P>void assocLines();<BR>void addToModelSpace(AcDbObjectId&amp;,AcDbEntity *);<BR>void initApp(void);<BR>void unloadApp(void);<BR>extern "C" <BR>AcRx::AppRetCode <BR>acrxEntryPoint(AcRx::AppMsgCode,void *);</P>
<P>class MyObjectNotify:public AcDbObject<BR>{<BR>public:<BR>&nbsp;ACRX_DECLARE_MEMBERS(MyObjectNotify);<BR>&nbsp;MyObjectNotify(){};<BR>&nbsp;void eLinkage(AcDbObjectId i,double f=1.0)<BR>&nbsp;{<BR>&nbsp;&nbsp;mId=i;<BR>&nbsp;&nbsp;mFactor=f;<BR>&nbsp;};<BR>&nbsp;void modified(const AcDbObject *);<BR>&nbsp;Acad::ErrorStatus dwgInFields(AcDbDwgFiler *);<BR>&nbsp;Acad::ErrorStatus dwgOutFields(AcDbDwgFiler *)const ;<BR>&nbsp;Acad::ErrorStatus dxfInFields(AcDbDxfFiler *);<BR>&nbsp;Acad::ErrorStatus dxfOutFields(AcDbDxfFiler *) const;<BR>private:<BR>&nbsp;AcDbObjectId mId;<BR>&nbsp;double mFactor;<BR>};</P>
<P>ACRX_DXF_DEFINE_MEMBERS(MyObjectNotify,AcDbObject,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AcDb::kDHL_CURRENT,AcDb::kMReleaseCurrent,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,MYOBJECTMODIFY,exam10b);</P>
<P>void MyObjectNotify::modified(const AcDbObject *pObj)<BR>{<BR>&nbsp;AcDbLine *pLine=AcDbLine::cast(pObj);<BR>&nbsp;if(!pLine)<BR>&nbsp;{<BR>&nbsp;&nbsp;const char * str=pObj-&gt;isA()-&gt;name();<BR>&nbsp;&nbsp;acutPrintf("This is a %s.\n",str);<BR>&nbsp;&nbsp;acutPrintf("i only work with lines,sorry.\n");<BR>&nbsp;&nbsp;return;<BR>&nbsp;}<BR>&nbsp;acutPrintf("\nReactor attached to %lx calling %lx.\n",<BR>&nbsp;&nbsp;pLine-&gt;objectId(),mId);<BR>&nbsp;AcDbLine *pLine2;<BR>&nbsp;if(acdbOpenObject((AcDbObject *&amp;)pLine2,mId,<BR>&nbsp;&nbsp;AcDb::kForWrite)==Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;AcGePoint3d p=pLine-&gt;startPoint();<BR>&nbsp;&nbsp;AcGePoint3d q=pLine-&gt;endPoint();<BR>&nbsp;&nbsp;AcGeVector3d v=q-p;<BR>&nbsp;&nbsp;double len=v.length();</P>
<P>&nbsp;&nbsp;p=pLine2-&gt;startPoint();<BR>&nbsp;&nbsp;q=pLine2-&gt;endPoint();<BR>&nbsp;&nbsp;v=q-p;<BR>&nbsp;&nbsp;v=len*mFactor *v.normal();<BR>&nbsp;&nbsp;pLine2-&gt;setEndPoint(p+v);<BR>&nbsp;&nbsp;pLine2-&gt;close();<BR>&nbsp;}<BR>}</P>
<P>Acad::ErrorStatus<BR>MyObjectNotify::dwgInFields(AcDbDwgFiler * filer)<BR>{<BR>&nbsp;assertWriteEnabled();<BR>&nbsp;AcDbObject::dwgInFields(filer);<BR>&nbsp;filer-&gt;readItem(&amp;mFactor);<BR>&nbsp;filer-&gt;readItem((AcDbSoftPointerId *) &amp;mId);<BR>&nbsp;return filer-&gt;filerStatus();<BR>}</P>
<P><BR>Acad::ErrorStatus<BR>MyObjectNotify::dwgOutFields(AcDbDwgFiler * filer) const<BR>{<BR>&nbsp;assertReadEnabled();<BR>&nbsp;AcDbObject::dwgOutFields(filer);<BR>&nbsp;filer-&gt;writeItem(mFactor);<BR>&nbsp;filer-&gt;writeItem((AcDbSoftPointerId *) &amp;mId);<BR>&nbsp;return filer-&gt;filerStatus();<BR>}</P>
<P><BR>Acad::ErrorStatus<BR>MyObjectNotify::dxfInFields(AcDbDxfFiler * filer) <BR>{<BR>&nbsp;assertReadEnabled();<BR>&nbsp;Acad::ErrorStatus es;<BR>&nbsp;if((es=AcDbObject::dxfInFields(filer))!=Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;return es;<BR>&nbsp;}</P>
<P>&nbsp;if(!filer-&gt;atSubclassData("MyObjectNotify"))<BR>&nbsp;{<BR>&nbsp;&nbsp;return Acad::eBadDxfSequence;<BR>&nbsp;}</P>
<P>&nbsp;struct resbuf rbIn;<BR>&nbsp;while(es==Acad::eOk)<BR>&nbsp;{<BR>&nbsp;&nbsp;if((es=filer-&gt;readItem(&amp;rbIn))==Acad::eOk)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(rbIn.restype==AcDb::kDxfReal)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;mFactor=rbIn.resval.rreal;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else if(rbIn.restype==AcDb::kDxfSoftPointerId)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;acdbGetObjectId(mId,rbIn.resval.rlname);<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;return (filer-&gt;pushBackItem());<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>&nbsp;return filer-&gt;filerStatus();<BR>}</P>
<P>Acad::ErrorStatus<BR>MyObjectNotify::dxfOutFields(AcDbDxfFiler * filer) const<BR>{<BR>&nbsp;assertReadEnabled();<BR>&nbsp;AcDbObject::dxfOutFields(filer);<BR>&nbsp;filer-&gt;writeItem(AcDb::kDxfSubclass,<BR>&nbsp;&nbsp;"MyObjectNotify");<BR>&nbsp;filer-&gt;writeItem(AcDb::kDxfReal,mFactor);<BR>&nbsp;filer-&gt;writeItem(AcDb::kDxfSoftPointerId,mId);<BR>&nbsp;return filer-&gt;filerStatus();<BR>}</P>
<P>void assocLines()<BR>{<BR>&nbsp;AcDbDatabase *pDb=acdbHostApplicationServices()-&gt;workingDatabase();<BR>&nbsp;AcDbObjectId aId,bId;<BR>&nbsp;AcDbLine *pLineA=new AcDbLine;<BR>&nbsp;pLineA-&gt;setDatabaseDefaults(pDb);<BR>&nbsp;pLineA-&gt;setStartPoint(AcGePoint3d(1,1,0));<BR>&nbsp;pLineA-&gt;setEndPoint(AcGePoint3d(2,1,0));<BR>&nbsp;addToModelSpace(aId,pLineA);<BR>&nbsp;ads_printf("Line A is %lx from 1,1 to 2,1j.\n",<BR>&nbsp;&nbsp;pLineA-&gt;objectId());<BR>&nbsp;AcDbLine *pLineB=new AcDbLine;<BR>&nbsp;pLineB-&gt;setDatabaseDefaults(pDb);<BR>&nbsp;pLineB-&gt;setStartPoint(AcGePoint3d(1,2,0));<BR>&nbsp;pLineB-&gt;setEndPoint(AcGePoint3d(2,2,0));<BR>&nbsp;addToModelSpace(bId,pLineB);<BR>&nbsp;ads_printf("Line B is %lx from 1,2 to 2,2.\n",<BR>&nbsp;&nbsp;pLineB-&gt;objectId());</P>
<P>&nbsp;AcDbDictionary *pNamedObj;<BR>&nbsp;AcDbDictionary *pNameList;<BR>&nbsp;pDb-&gt;getNamedObjectsDictionary(pNamedObj,<BR>&nbsp;&nbsp;AcDb::kForWrite);<BR>&nbsp;if(pNamedObj-&gt;getAt("ASDK_DICT",<BR>&nbsp;&nbsp;(AcDbObject * &amp;) pNameList,AcDb::kForWrite)<BR>&nbsp;&nbsp;==Acad::eKeyNotFound)<BR>&nbsp;{<BR>&nbsp;&nbsp;pNameList=new AcDbDictionary;<BR>&nbsp;&nbsp;AcDbObjectId DictId;<BR>&nbsp;&nbsp;pNamedObj-&gt;setAt("ASDK_DICT",pNameList,DictId);<BR>&nbsp;}<BR>&nbsp;pNamedObj-&gt;close();<BR>&nbsp;MyObjectNotify *pObj=new MyObjectNotify();<BR>&nbsp;pObj-&gt;eLinkage(bId);<BR>&nbsp;AcDbObjectId objId;<BR>&nbsp;if((pNameList-&gt;getAt("object_to_notify_A",objId))<BR>&nbsp;&nbsp;==Acad::eKeyNotFound)<BR>&nbsp;{<BR>&nbsp;&nbsp;pNameList-&gt;setAt("object_to_notify_A",pObj,objId);<BR>&nbsp;&nbsp;pObj-&gt;close();<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;delete pObj;<BR>&nbsp;&nbsp;ads_printf("object_to_notify_A already existes\n");<BR>&nbsp;}<BR>&nbsp;pLineA-&gt;addPersistentReactor(objId);<BR>&nbsp;pLineA-&gt;close();<BR>&nbsp;pObj=new MyObjectNotify();<BR>&nbsp;pObj-&gt;eLinkage(aId);<BR>&nbsp;if((pNameList-&gt;getAt("object_to_notify_B",objId))<BR>&nbsp;&nbsp;==Acad::eKeyNotFound)<BR>&nbsp;{<BR>&nbsp;&nbsp;pNameList-&gt;setAt("object_to_notify_B",pObj,objId);<BR>&nbsp;&nbsp;pObj-&gt;close();<BR>&nbsp;}<BR>&nbsp;else<BR>&nbsp;{<BR>&nbsp;&nbsp;delete pObj;<BR>&nbsp;&nbsp;ads_printf("object_to_notify_b already existes\n");<BR>&nbsp;}<BR>&nbsp;pNameList-&gt;close();</P>
<P>&nbsp;pLineB-&gt;addPersistentReactor(objId);<BR>&nbsp;pLineB-&gt;close();<BR>}</P>
<P>void addToModelSpace(AcDbObjectId &amp;objId,AcDbEntity * pEntity)<BR>{<BR>&nbsp;AcDbBlockTable *pBlockTable;<BR>&nbsp;AcDbBlockTableRecord *pSpaceRecord;<BR>&nbsp;acdbHostApplicationServices()-&gt;workingDatabase()<BR>&nbsp;&nbsp;-&gt;getSymbolTable(pBlockTable,AcDb::kForRead);<BR>&nbsp;pBlockTable-&gt;getAt(ACDB_MODEL_SPACE,pSpaceRecord,<BR>&nbsp;&nbsp;AcDb::kForWrite);<BR>&nbsp;pBlockTable-&gt;close();<BR>&nbsp;pSpaceRecord-&gt;appendAcDbEntity(objId,pEntity);<BR>&nbsp;pSpaceRecord-&gt;close();<BR>&nbsp;return;<BR>}</P>
<P><BR>void initApp()<BR>{<BR>&nbsp;acedRegCmds-&gt;addCommand("EXAM10b",<BR>&nbsp;&nbsp;"ALINES",<BR>&nbsp;&nbsp;"ALINES",<BR>&nbsp;&nbsp;ACRX_CMD_MODAL,<BR>&nbsp;&nbsp;assocLines);<BR>&nbsp;MyObjectNotify::rxInit();<BR>&nbsp;acrxBuildClassHierarchy();<BR>}</P>
<P>void unloadApp()<BR>{<BR>&nbsp;acedRegCmds-&gt;removeGroup("EXAM10B");<BR>&nbsp;deleteAcRxClass(MyObjectNotify::desc());<BR>}</P>
<P>AcRx::AppRetCode<BR>acrxEntryPoint(AcRx::AppMsgCode msg, void *appId)<BR>{<BR>&nbsp;switch(msg)<BR>&nbsp;{<BR>&nbsp;case AcRx::kInitAppMsg:<BR>&nbsp;&nbsp;acrxDynamicLinker-&gt;unlockApplication(appId);<BR>&nbsp;&nbsp;acrxDynamicLinker-&gt;registerAppMDIAware(appId);<BR>&nbsp;&nbsp;initApp();<BR>&nbsp;&nbsp;break;<BR>&nbsp;case AcRx::kUnloadAppMsg:<BR>&nbsp;&nbsp;unloadApp();<BR>&nbsp;}<BR>&nbsp;return AcRx::kRetOK;<BR>}</P>

hulei_accp 发表于 2005-9-9 11:20:00

<P>exam10b.def</P>
<P>LIBRARY&nbsp;&nbsp;exam10b<BR>DESCRIPTION&nbsp;&nbsp;"EXAM10B APPLICATION"<BR>EXPORTS<BR>acrxEntryPoint&nbsp;&nbsp;PRIVATE<BR>acrxGetApiVersion&nbsp;PRIVATE</P>

hulei_accp 发表于 2005-9-9 11:23:00

<P>用vc++6.0开发,autocad2002中使用正常。</P>
<P>command :ALINES</P>
<P>会增加两条直线,对任意一条直线操作,都会影响到第二个直线。</P>
页: [1]
查看完整版本: 对象的关联