对象的关联
<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&,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> ACRX_DECLARE_MEMBERS(MyObjectNotify);<BR> MyObjectNotify(){};<BR> void eLinkage(AcDbObjectId i,double f=1.0)<BR> {<BR> mId=i;<BR> mFactor=f;<BR> };<BR> void modified(const AcDbObject *);<BR> Acad::ErrorStatus dwgInFields(AcDbDwgFiler *);<BR> Acad::ErrorStatus dwgOutFields(AcDbDwgFiler *)const ;<BR> Acad::ErrorStatus dxfInFields(AcDbDxfFiler *);<BR> Acad::ErrorStatus dxfOutFields(AcDbDxfFiler *) const;<BR>private:<BR> AcDbObjectId mId;<BR> double mFactor;<BR>};</P>
<P>ACRX_DXF_DEFINE_MEMBERS(MyObjectNotify,AcDbObject,<BR> AcDb::kDHL_CURRENT,AcDb::kMReleaseCurrent,<BR> 0,MYOBJECTMODIFY,exam10b);</P>
<P>void MyObjectNotify::modified(const AcDbObject *pObj)<BR>{<BR> AcDbLine *pLine=AcDbLine::cast(pObj);<BR> if(!pLine)<BR> {<BR> const char * str=pObj->isA()->name();<BR> acutPrintf("This is a %s.\n",str);<BR> acutPrintf("i only work with lines,sorry.\n");<BR> return;<BR> }<BR> acutPrintf("\nReactor attached to %lx calling %lx.\n",<BR> pLine->objectId(),mId);<BR> AcDbLine *pLine2;<BR> if(acdbOpenObject((AcDbObject *&)pLine2,mId,<BR> AcDb::kForWrite)==Acad::eOk)<BR> {<BR> AcGePoint3d p=pLine->startPoint();<BR> AcGePoint3d q=pLine->endPoint();<BR> AcGeVector3d v=q-p;<BR> double len=v.length();</P>
<P> p=pLine2->startPoint();<BR> q=pLine2->endPoint();<BR> v=q-p;<BR> v=len*mFactor *v.normal();<BR> pLine2->setEndPoint(p+v);<BR> pLine2->close();<BR> }<BR>}</P>
<P>Acad::ErrorStatus<BR>MyObjectNotify::dwgInFields(AcDbDwgFiler * filer)<BR>{<BR> assertWriteEnabled();<BR> AcDbObject::dwgInFields(filer);<BR> filer->readItem(&mFactor);<BR> filer->readItem((AcDbSoftPointerId *) &mId);<BR> return filer->filerStatus();<BR>}</P>
<P><BR>Acad::ErrorStatus<BR>MyObjectNotify::dwgOutFields(AcDbDwgFiler * filer) const<BR>{<BR> assertReadEnabled();<BR> AcDbObject::dwgOutFields(filer);<BR> filer->writeItem(mFactor);<BR> filer->writeItem((AcDbSoftPointerId *) &mId);<BR> return filer->filerStatus();<BR>}</P>
<P><BR>Acad::ErrorStatus<BR>MyObjectNotify::dxfInFields(AcDbDxfFiler * filer) <BR>{<BR> assertReadEnabled();<BR> Acad::ErrorStatus es;<BR> if((es=AcDbObject::dxfInFields(filer))!=Acad::eOk)<BR> {<BR> return es;<BR> }</P>
<P> if(!filer->atSubclassData("MyObjectNotify"))<BR> {<BR> return Acad::eBadDxfSequence;<BR> }</P>
<P> struct resbuf rbIn;<BR> while(es==Acad::eOk)<BR> {<BR> if((es=filer->readItem(&rbIn))==Acad::eOk)<BR> {<BR> if(rbIn.restype==AcDb::kDxfReal)<BR> {<BR> mFactor=rbIn.resval.rreal;<BR> }<BR> else if(rbIn.restype==AcDb::kDxfSoftPointerId)<BR> {<BR> acdbGetObjectId(mId,rbIn.resval.rlname);<BR> }<BR> else<BR> {<BR> return (filer->pushBackItem());<BR> }<BR> }<BR> }<BR> return filer->filerStatus();<BR>}</P>
<P>Acad::ErrorStatus<BR>MyObjectNotify::dxfOutFields(AcDbDxfFiler * filer) const<BR>{<BR> assertReadEnabled();<BR> AcDbObject::dxfOutFields(filer);<BR> filer->writeItem(AcDb::kDxfSubclass,<BR> "MyObjectNotify");<BR> filer->writeItem(AcDb::kDxfReal,mFactor);<BR> filer->writeItem(AcDb::kDxfSoftPointerId,mId);<BR> return filer->filerStatus();<BR>}</P>
<P>void assocLines()<BR>{<BR> AcDbDatabase *pDb=acdbHostApplicationServices()->workingDatabase();<BR> AcDbObjectId aId,bId;<BR> AcDbLine *pLineA=new AcDbLine;<BR> pLineA->setDatabaseDefaults(pDb);<BR> pLineA->setStartPoint(AcGePoint3d(1,1,0));<BR> pLineA->setEndPoint(AcGePoint3d(2,1,0));<BR> addToModelSpace(aId,pLineA);<BR> ads_printf("Line A is %lx from 1,1 to 2,1j.\n",<BR> pLineA->objectId());<BR> AcDbLine *pLineB=new AcDbLine;<BR> pLineB->setDatabaseDefaults(pDb);<BR> pLineB->setStartPoint(AcGePoint3d(1,2,0));<BR> pLineB->setEndPoint(AcGePoint3d(2,2,0));<BR> addToModelSpace(bId,pLineB);<BR> ads_printf("Line B is %lx from 1,2 to 2,2.\n",<BR> pLineB->objectId());</P>
<P> AcDbDictionary *pNamedObj;<BR> AcDbDictionary *pNameList;<BR> pDb->getNamedObjectsDictionary(pNamedObj,<BR> AcDb::kForWrite);<BR> if(pNamedObj->getAt("ASDK_DICT",<BR> (AcDbObject * &) pNameList,AcDb::kForWrite)<BR> ==Acad::eKeyNotFound)<BR> {<BR> pNameList=new AcDbDictionary;<BR> AcDbObjectId DictId;<BR> pNamedObj->setAt("ASDK_DICT",pNameList,DictId);<BR> }<BR> pNamedObj->close();<BR> MyObjectNotify *pObj=new MyObjectNotify();<BR> pObj->eLinkage(bId);<BR> AcDbObjectId objId;<BR> if((pNameList->getAt("object_to_notify_A",objId))<BR> ==Acad::eKeyNotFound)<BR> {<BR> pNameList->setAt("object_to_notify_A",pObj,objId);<BR> pObj->close();<BR> }<BR> else<BR> {<BR> delete pObj;<BR> ads_printf("object_to_notify_A already existes\n");<BR> }<BR> pLineA->addPersistentReactor(objId);<BR> pLineA->close();<BR> pObj=new MyObjectNotify();<BR> pObj->eLinkage(aId);<BR> if((pNameList->getAt("object_to_notify_B",objId))<BR> ==Acad::eKeyNotFound)<BR> {<BR> pNameList->setAt("object_to_notify_B",pObj,objId);<BR> pObj->close();<BR> }<BR> else<BR> {<BR> delete pObj;<BR> ads_printf("object_to_notify_b already existes\n");<BR> }<BR> pNameList->close();</P>
<P> pLineB->addPersistentReactor(objId);<BR> pLineB->close();<BR>}</P>
<P>void addToModelSpace(AcDbObjectId &objId,AcDbEntity * pEntity)<BR>{<BR> AcDbBlockTable *pBlockTable;<BR> AcDbBlockTableRecord *pSpaceRecord;<BR> acdbHostApplicationServices()->workingDatabase()<BR> ->getSymbolTable(pBlockTable,AcDb::kForRead);<BR> pBlockTable->getAt(ACDB_MODEL_SPACE,pSpaceRecord,<BR> AcDb::kForWrite);<BR> pBlockTable->close();<BR> pSpaceRecord->appendAcDbEntity(objId,pEntity);<BR> pSpaceRecord->close();<BR> return;<BR>}</P>
<P><BR>void initApp()<BR>{<BR> acedRegCmds->addCommand("EXAM10b",<BR> "ALINES",<BR> "ALINES",<BR> ACRX_CMD_MODAL,<BR> assocLines);<BR> MyObjectNotify::rxInit();<BR> acrxBuildClassHierarchy();<BR>}</P>
<P>void unloadApp()<BR>{<BR> acedRegCmds->removeGroup("EXAM10B");<BR> deleteAcRxClass(MyObjectNotify::desc());<BR>}</P>
<P>AcRx::AppRetCode<BR>acrxEntryPoint(AcRx::AppMsgCode msg, void *appId)<BR>{<BR> switch(msg)<BR> {<BR> case AcRx::kInitAppMsg:<BR> acrxDynamicLinker->unlockApplication(appId);<BR> acrxDynamicLinker->registerAppMDIAware(appId);<BR> initApp();<BR> break;<BR> case AcRx::kUnloadAppMsg:<BR> unloadApp();<BR> }<BR> return AcRx::kRetOK;<BR>}</P> <P>exam10b.def</P>
<P>LIBRARY exam10b<BR>DESCRIPTION "EXAM10B APPLICATION"<BR>EXPORTS<BR>acrxEntryPoint PRIVATE<BR>acrxGetApiVersion PRIVATE</P> <P>用vc++6.0开发,autocad2002中使用正常。</P>
<P>command :ALINES</P>
<P>会增加两条直线,对任意一条直线操作,都会影响到第二个直线。</P>
页:
[1]